|
@@ -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;
|