lrf402788946 3 năm trước cách đây
mục cha
commit
aed4b819e9
3 tập tin đã thay đổi với 320 bổ sung312 xóa
  1. 5 1
      app/service/school.js
  2. 304 301
      app/service/user.js
  3. 11 10
      app/service/util.js

+ 5 - 1
app/service/school.js

@@ -88,6 +88,9 @@ class SchoolService extends CrudService {
       const is_change = dbStuList.find(f => f.classid || f.bedroomid);
       if (is_change) throw new BusinessError(ErrorCode.BUSINESS, '上报过的学生已经安排班级或寝室!若需要替换学生,让同性别的学生直接来和班主任说,修改信息即可.若还是有疑问,请和中心负责人联系(最好联系下)');
     }
+    // 2021-06-07 如果学生已经绑定,那也不允许修改名单了
+    const countOpenid = await this.ctx.model.Student.count({ termid, batchid, schid, openid: { $exists: true } });
+    if (countOpenid > 0) throw new BusinessError(ErrorCode.BUSINESS, '已有学生绑定账号,名单无法修改.若有问题请联系中心负责人!');
     // 获取学校名称
     let school_name;
     const sch = await this.ctx.model.School.findOne({ code: schid });
@@ -242,13 +245,14 @@ class SchoolService extends CrudService {
     // 删除掉第一行 表头行,这不是数据
     worksheet.spliceRows(0, 1);
     const stuList = [];
+    const noWhite = str => str.replace(/\s*/g, '');
     // 整理数据,根据检查合格的表头行,获取每个格子的数据,制成[object]格式
     worksheet.eachRow(row => {
       const stu = {};
       for (let i = 0; i < cols.length; i++) {
         const col = cols[i];
         if (!col) break;
-        let val = _.trim(row.getCell(col.colIndex));
+        let val = noWhite(row.getCell(col.colIndex));
         if (col.column === 'id_number') val = val.toUpperCase();
         stu[col.column] = val;
       }

+ 304 - 301
app/service/user.js

@@ -1,301 +1,304 @@
-'use strict';
-
-
-const assert = require('assert');
-const _ = require('lodash');
-const { ObjectId } = require('mongoose').Types;
-const { CrudService } = require('naf-framework-mongoose/lib/service');
-const { BusinessError, ErrorCode } = require('naf-core').Error;
-
-class UserService extends CrudService {
-  constructor(ctx) {
-    super(ctx, 'user');
-    this.model = this.ctx.model.User;
-    this.stuModel = this.ctx.model.Student;
-    this.tModel = this.ctx.model.Teacher;
-    this.schModel = this.ctx.model.School;
-    this.hModel = this.ctx.model.Headteacher;
-  }
-  async query({ name, ...info } = {}, options) {
-    const newoptions = await this.ctx.service.util.getQueryOptions(options);
-    const query = { ...info };
-    if (name) {
-      query.name = { $regex: name };
-    }
-    let res = await this.model.find(query, '+passwd', newoptions).exec();
-    res = JSON.parse(JSON.stringify(res));
-    for (const user of res) {
-      if (user) {
-        const { passwd } = user;
-        user.passwd = passwd.secret;
-      }
-    }
-    return res;
-  }
-  async count({ name, ...info }) {
-    const query = { ...info };
-    if (name) {
-      query.name = { $regex: name };
-    }
-    const res = await this.model.count(query);
-    return res;
-  }
-  // 重写创建方法
-  async create(data) {
-    const { name, mobile, passwd, type, gender } = data;
-    assert(name && mobile && passwd && type, '缺少部分信息项');
-    if (type !== '0') {
-      assert(/^\d{11}$/i.test(mobile), 'mobile无效');
-    }
-    const newdata = data;
-    newdata.passwd = { secret: passwd };
-    const res = await this.model.create(newdata);
-    if (res) {
-      // 如果是班主任的时候将用户信息填入班主任表
-      if (type === '1') {
-        const headt = { name, phone: mobile, gender };
-        await this.hModel.create(headt);
-      }
-    }
-    return res;
-  }
-
-  async update({ id }, body) {
-    const { name, mobile, passwd, openid, status, type } = body;
-    assert(id, '缺少部分信息项');
-    const user = await this.model.findById(id);
-    if (!user) {
-      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
-    }
-    if (passwd) {
-      body.passwd = { secret: passwd };
-    }
-    return await this.model.updateOne({ _id: id }, body);
-    // if (name) {
-    //   user.name = name;
-    // }
-    // if (mobile) {
-    //   user.mobile = mobile;
-    // }
-    // if (openid) {
-    //   user.openid = openid;
-    // }
-    // if (status) {
-    //   user.status = status;
-    // } else {
-    //   user.status = '1';
-    // }
-    // if (type) {
-    //   user.type = type;
-    // }
-    // await user.save();
-    // return user;
-  }
-
-  // 学校注册
-  async register(data) {
-    const { code, mobile, passwd, name } = data;
-    assert(code && mobile && name && passwd, '缺少部分信息项');
-    const user = await this.model.findOne({ mobile });
-    if (user) {
-      throw new BusinessError(ErrorCode.DATA_EXISTED);
-    }
-    const sch = await this.schModel.findOne({ code });
-    if (!sch) {
-      throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
-    }
-    const newdata = { name, mobile, type: '2', uid: sch.id };
-    newdata.passwd = { secret: passwd };
-    const res = await this.model.create(newdata);
-    return res;
-  }
-
-  // 学生绑定
-  async bind(data) {
-    const { openid, id_number, mobile, unionid } = data;
-    assert(openid && id_number && mobile, '缺少部分信息项');
-    let user = await this.model.findOne({ mobile });
-    if (user) {
-      user.openid = openid;
-      user.unionid = unionid;
-      await user.save();
-    } else {
-      const stud = await this.stuModel.findOne({
-        id_number: { $regex: `^${id_number}$`, $options: 'i' },
-      });
-      if (!stud) {
-        throw new BusinessError(ErrorCode.USER_NOT_EXIST);
-      }
-      stud.openid = openid;
-      stud.isComming = '1';
-      await stud.save();
-      // 再次检查是否有该用户,需要用uid查
-      const have_user = await this.model.findOne({ uid: stud.id });
-      const newdata = {
-        name: stud.name,
-        mobile: stud.phone,
-        type: '4',
-        uid: stud.id,
-        openid,
-        unionid,
-      };
-      if (have_user) {
-        if (stud.name) have_user.name = stud.name;
-        if (stud.phone) have_user.mobile = stud.phone;
-        if (stud.id) have_user.uid = stud.id;
-        user = await have_user.save();
-      } else {
-        newdata.passwd = { secret: '12345678' };
-        user = await this.model.create(newdata);
-      }
-    }
-    return user;
-  }
-
-  // 其他用户绑定
-  async userbind(data) {
-    const { openid, uid, qrcode } = data;
-    assert(openid && uid && qrcode, '缺少部分信息项');
-    const user = await this.model.findById(uid);
-    if (!user) {
-      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
-    }
-    user.openid = openid;
-    const res = await user.save();
-    if (res) {
-      const { mq } = this.ctx;
-      if (mq) {
-        const msg = user.name + '绑定微信成功';
-        const parm = {
-          durable: true,
-          headers: {
-            userid: uid,
-            openid,
-          },
-        };
-        await mq.topic('qrcode.bind', qrcode, msg, parm);
-      } else {
-        this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
-      }
-    }
-    return user;
-  }
-
-  // 通过openid查询用户信息
-  async findByOpenid(openid) {
-    // 通过openid查询用户信息
-    const user = await this.model.findOne({ openid });
-    return user;
-  }
-  // 通过openid查询用户信息
-  async findByAppOpenid(appopenid) {
-    console.log(appopenid);
-    // 通过openid查询用户信息
-    if (!appopenid) {
-      console.error('没有appopenid');
-      return;
-    }
-    let user = await this.model.findOne({ appopenid }).populate({
-      path: 'uid',
-      model: 'Student',
-      select: 'classid bedroomid',
-      populate: [
-        {
-          path: 'classid',
-          model: 'Class',
-          select: 'jslocationid',
-          populate: {
-            path: 'jslocationid',
-            model: 'Location',
-            select: 'name ibeacon',
-          },
-        },
-        {
-          path: 'bedroomid',
-          model: 'Bedroom',
-          select: 'code ibeacon',
-        },
-      ],
-    });
-
-    if (user) {
-      user = JSON.parse(JSON.stringify(user));
-      // 整理数据
-      const { uid } = user;
-      if (uid) {
-        const { _id, classid, bedroomid } = uid;
-        // 先解析classid
-        if (classid) {
-          const { jslocationid } = classid;
-          if (jslocationid) {
-            const { name, ibeacon } = jslocationid;
-            if (name && ibeacon) {
-              user.jsname = name;
-              user.jsibeacon = ibeacon;
-            }
-          }
-        }
-        // 在解析bedroomid
-        if (bedroomid) {
-          const { code, ibeacon } = bedroomid;
-          if (code && ibeacon) {
-            user.bedroomname = code;
-            user.bedroomibeacon = ibeacon;
-          }
-        }
-        user.uid = _id;
-      }
-    }
-    return user;
-  }
-
-  // 通过unionid查询用户信息
-  async findByunionid(unionid) {
-    // 通过unionid查询用户信息
-    const user = await this.model.findOne({ unionid });
-    return user;
-  }
-
-  // 学校用户生成
-  async schoolregister() {
-    const schools = await this.schModel.find();
-    for (const sch of schools) {
-      const user = await this.model.findOne({ uid: sch.id, type: '2' });
-      if (!user) {
-        const newdata = {
-          name: sch.name,
-          mobile: sch.code,
-          type: '2',
-          uid: sch.id,
-        };
-        newdata.passwd = { secret: '12345678' };
-        await this.model.create(newdata);
-      }
-    }
-    this.ctx.ok({ data: {} });
-  }
-
-  // 学生小程序绑定
-  async appbind(data) {
-    const { name, mobile, appopenid } = data;
-    console.error(
-      `appBind: name=>${name} / mobile=>${mobile} / appopenid = ${appopenid}`
-    );
-    assert(name, '缺少姓名');
-    assert(mobile, '缺少手机号');
-    assert(appopenid, '缺少小程序openid');
-    const user = await this.model.findOne({ name, mobile });
-    if (!user) {
-      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
-    }
-    user.appopenid = appopenid;
-    const res = await user.save();
-    // const res = await this.model.update(
-    //   { name, mobile },
-    //   { $set: { appopenid } }
-    // );
-    return res;
-  }
-}
-
-module.exports = UserService;
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.model = this.ctx.model.User;
+    this.stuModel = this.ctx.model.Student;
+    this.tModel = this.ctx.model.Teacher;
+    this.schModel = this.ctx.model.School;
+    this.hModel = this.ctx.model.Headteacher;
+  }
+  async query({ name, ...info } = {}, options) {
+    const newoptions = await this.ctx.service.util.getQueryOptions(options);
+    const query = { ...info };
+    if (name) {
+      query.name = { $regex: name };
+    }
+    let res = await this.model.find(query, '+passwd', newoptions).exec();
+    res = JSON.parse(JSON.stringify(res));
+    for (const user of res) {
+      if (user) {
+        const { passwd } = user;
+        user.passwd = passwd.secret;
+      }
+    }
+    return res;
+  }
+  async count({ name, ...info }) {
+    const query = { ...info };
+    if (name) {
+      query.name = { $regex: name };
+    }
+    const res = await this.model.count(query);
+    return res;
+  }
+  // 重写创建方法
+  async create(data) {
+    const { name, mobile, passwd, type, gender } = data;
+    assert(name && mobile && passwd && type, '缺少部分信息项');
+    if (type !== '0') {
+      assert(/^\d{11}$/i.test(mobile), 'mobile无效');
+    }
+    const newdata = data;
+    newdata.passwd = { secret: passwd };
+    const res = await this.model.create(newdata);
+    if (res) {
+      // 如果是班主任的时候将用户信息填入班主任表
+      if (type === '1') {
+        const headt = { name, phone: mobile, gender };
+        await this.hModel.create(headt);
+      }
+    }
+    return res;
+  }
+
+  async update({ id }, body) {
+    const { name, mobile, passwd, openid, status, type } = body;
+    assert(id, '缺少部分信息项');
+    const user = await this.model.findById(id);
+    if (!user) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
+    }
+    if (passwd) {
+      body.passwd = { secret: passwd };
+    }
+    return await this.model.updateOne({ _id: id }, body);
+    // if (name) {
+    //   user.name = name;
+    // }
+    // if (mobile) {
+    //   user.mobile = mobile;
+    // }
+    // if (openid) {
+    //   user.openid = openid;
+    // }
+    // if (status) {
+    //   user.status = status;
+    // } else {
+    //   user.status = '1';
+    // }
+    // if (type) {
+    //   user.type = type;
+    // }
+    // await user.save();
+    // return user;
+  }
+
+  // 学校注册
+  async register(data) {
+    const { code, mobile, passwd, name } = data;
+    assert(code && mobile && name && passwd, '缺少部分信息项');
+    const user = await this.model.findOne({ mobile });
+    if (user) {
+      throw new BusinessError(ErrorCode.DATA_EXISTED);
+    }
+    const sch = await this.schModel.findOne({ code });
+    if (!sch) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+    }
+    const newdata = { name, mobile, type: '2', uid: sch.id };
+    newdata.passwd = { secret: passwd };
+    const res = await this.model.create(newdata);
+    return res;
+  }
+
+  // 学生绑定
+  async bind(data) {
+    const { openid, id_number, mobile, unionid } = data;
+    assert(openid && id_number && mobile, '缺少部分信息项');
+    let user = await this.model.findOne({ mobile });
+    if (user) {
+      // 2021-06-07修改:根据身份证/手机号,找到对应的学生信息,将_id变为uid重保存
+      const stuInfo = await this.stuModel.findOne({ $or: [{ id_number }, { mobile }] });
+      if (stuInfo) user.uid = stuInfo._id;
+      user.openid = openid;
+      user.unionid = unionid;
+      await user.save();
+    } else {
+      const stud = await this.stuModel.findOne({
+        id_number: { $regex: `^${id_number}$`, $options: 'i' },
+      });
+      if (!stud) {
+        throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+      }
+      stud.openid = openid;
+      stud.isComming = '1';
+      await stud.save();
+      // 再次检查是否有该用户,需要用uid查
+      const have_user = await this.model.findOne({ uid: stud.id });
+      const newdata = {
+        name: stud.name,
+        mobile: stud.phone,
+        type: '4',
+        uid: stud.id,
+        openid,
+        unionid,
+      };
+      if (have_user) {
+        if (stud.name) have_user.name = stud.name;
+        if (stud.phone) have_user.mobile = stud.phone;
+        if (stud.id) have_user.uid = stud.id;
+        user = await have_user.save();
+      } else {
+        newdata.passwd = { secret: '12345678' };
+        user = await this.model.create(newdata);
+      }
+    }
+    return user;
+  }
+
+  // 其他用户绑定
+  async userbind(data) {
+    const { openid, uid, qrcode } = data;
+    assert(openid && uid && qrcode, '缺少部分信息项');
+    const user = await this.model.findById(uid);
+    if (!user) {
+      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    }
+    user.openid = openid;
+    const res = await user.save();
+    if (res) {
+      const { mq } = this.ctx;
+      if (mq) {
+        const msg = user.name + '绑定微信成功';
+        const parm = {
+          durable: true,
+          headers: {
+            userid: uid,
+            openid,
+          },
+        };
+        await mq.topic('qrcode.bind', qrcode, msg, parm);
+      } else {
+        this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
+      }
+    }
+    return user;
+  }
+
+  // 通过openid查询用户信息
+  async findByOpenid(openid) {
+    // 通过openid查询用户信息
+    const user = await this.model.findOne({ openid });
+    return user;
+  }
+  // 通过openid查询用户信息
+  async findByAppOpenid(appopenid) {
+    console.log(appopenid);
+    // 通过openid查询用户信息
+    if (!appopenid) {
+      console.error('没有appopenid');
+      return;
+    }
+    let user = await this.model.findOne({ appopenid }).populate({
+      path: 'uid',
+      model: 'Student',
+      select: 'classid bedroomid',
+      populate: [
+        {
+          path: 'classid',
+          model: 'Class',
+          select: 'jslocationid',
+          populate: {
+            path: 'jslocationid',
+            model: 'Location',
+            select: 'name ibeacon',
+          },
+        },
+        {
+          path: 'bedroomid',
+          model: 'Bedroom',
+          select: 'code ibeacon',
+        },
+      ],
+    });
+
+    if (user) {
+      user = JSON.parse(JSON.stringify(user));
+      // 整理数据
+      const { uid } = user;
+      if (uid) {
+        const { _id, classid, bedroomid } = uid;
+        // 先解析classid
+        if (classid) {
+          const { jslocationid } = classid;
+          if (jslocationid) {
+            const { name, ibeacon } = jslocationid;
+            if (name && ibeacon) {
+              user.jsname = name;
+              user.jsibeacon = ibeacon;
+            }
+          }
+        }
+        // 在解析bedroomid
+        if (bedroomid) {
+          const { code, ibeacon } = bedroomid;
+          if (code && ibeacon) {
+            user.bedroomname = code;
+            user.bedroomibeacon = ibeacon;
+          }
+        }
+        user.uid = _id;
+      }
+    }
+    return user;
+  }
+
+  // 通过unionid查询用户信息
+  async findByunionid(unionid) {
+    // 通过unionid查询用户信息
+    const user = await this.model.findOne({ unionid });
+    return user;
+  }
+
+  // 学校用户生成
+  async schoolregister() {
+    const schools = await this.schModel.find();
+    for (const sch of schools) {
+      const user = await this.model.findOne({ uid: sch.id, type: '2' });
+      if (!user) {
+        const newdata = {
+          name: sch.name,
+          mobile: sch.code,
+          type: '2',
+          uid: sch.id,
+        };
+        newdata.passwd = { secret: '12345678' };
+        await this.model.create(newdata);
+      }
+    }
+    this.ctx.ok({ data: {} });
+  }
+
+  // 学生小程序绑定
+  async appbind(data) {
+    const { name, mobile, appopenid } = data;
+    console.error(
+      `appBind: name=>${name} / mobile=>${mobile} / appopenid = ${appopenid}`
+    );
+    assert(name, '缺少姓名');
+    assert(mobile, '缺少手机号');
+    assert(appopenid, '缺少小程序openid');
+    const user = await this.model.findOne({ name, mobile });
+    if (!user) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
+    }
+    user.appopenid = appopenid;
+    const res = await user.save();
+    // const res = await this.model.update(
+    //   { name, mobile },
+    //   { $set: { appopenid } }
+    // );
+    return res;
+  }
+}
+
+module.exports = UserService;

+ 11 - 10
app/service/util.js

@@ -86,16 +86,17 @@ class UtilService extends CrudService {
     return res;
   }
   async utilMethod(query, body) {
-    // 重置班级
-    const { planid, termid, batchid, classid } = query;
-    const filters = {};
-    if (classid) filters.classid = classid;
-    else if (batchid)filters.batchid = batchid;
-    else if (termid)filters.termid = termid;
-    else if (planid)filters.planid = planid;
-    else throw new BusinessError(ErrorCode.BADPARAM, '需要重置范围');
-    await this.ctx.model.Student.updateMany(filters, { classid: undefined });
-    // 重置班级结束
+    // // 重置班级
+    console.log('in function:');
+    // const { planid, termid, batchid, classid } = query;
+    // const filters = {};
+    // if (classid) filters.classid = classid;
+    // else if (batchid)filters.batchid = batchid;
+    // else if (termid)filters.termid = termid;
+    // else if (planid)filters.planid = planid;
+    // else throw new BusinessError(ErrorCode.BADPARAM, '需要重置范围');
+    // await this.ctx.model.Student.updateMany(filters, { classid: undefined });
+    // // 重置班级结束
 
   }
   async getQueryOptions({ skip, limit, sort, desc } = {}) {