123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- '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, type: '4' });
- if (user) {
- // 2021-06-07修改:根据身份证/手机号,找到对应的学生信息,将_id变为uid重保存
- const stuInfo = await this.stuModel.findOne({ $or: [{ id_number }, { mobile }] });
- if (stuInfo) {
- user.uid = stuInfo._id;
- // 将学生信息的openid也重置
- stuInfo.openid = openid;
- await stuInfo.save();
- }
- 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);
- delete user.passwd;
- }
- }
- return user;
- }
- // 其他用户绑定
- async userbind(data) {
- const { openid, uid, qrcode } = data;
- assert(openid && uid && qrcode, '缺少部分信息项');
- let user = await this.model.findById(uid);
- if (!user) {
- user = await this.model.findOne({ 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;
|