'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 create(data) { const { name, mobile, passwd, type, gender } = data; assert(name && mobile && passwd && type, '缺少部分信息项'); 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 }, { name, mobile, passwd, openid }) { assert(id, '缺少部分信息项'); const user = await this.model.findById(id); if (!user) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在'); } if (passwd) { user.passwd = { secret: passwd }; } if (name) { user.name = name; } if (mobile) { user.mobile = mobile; } if (openid) { user.openid = openid; } user.status = '1'; 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, passwd, unionid } = data; assert(openid && id_number && mobile, '缺少部分信息项'); let user = await this.model.findOne({ mobile }); if (user) { if (user.passwd.secret !== passwd) { throw new BusinessError(ErrorCode.BAD_PASSWORD); } user.openid = openid; user.unionid = unionid; await user.save(); } else { const stud = await this.stuModel.findOne({ id_number }); if (!stud) { throw new BusinessError(ErrorCode.USER_NOT_EXIST); } stud.openid = openid; stud.isComming = '1'; await stud.save(); const newdata = { name: stud.name, mobile: stud.phone, type: '4', uid: stud.id, openid, unionid }; 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, } }; console.log(parm); console.log(msg); 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; } // 通过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: {} }); } } module.exports = UserService;