user.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class UserService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'user');
  10. this.model = this.ctx.model.User;
  11. this.stuModel = this.ctx.model.Student;
  12. this.tModel = this.ctx.model.Teacher;
  13. this.schModel = this.ctx.model.School;
  14. this.hModel = this.ctx.model.Headteacher;
  15. }
  16. // 重写创建方法
  17. async create(data) {
  18. const { name, mobile, passwd, type, gender } = data;
  19. assert(name && mobile && passwd && type, '缺少部分信息项');
  20. assert(/^\d{11}$/i.test(mobile), 'mobile无效');
  21. const newdata = data;
  22. newdata.passwd = { secret: passwd };
  23. const res = await this.model.create(newdata);
  24. if (res) {
  25. // 如果是班主任的时候将用户信息填入班主任表
  26. if (type === '1') {
  27. const headt = { name, phone: mobile, gender };
  28. await this.hModel.create(headt);
  29. }
  30. }
  31. return res;
  32. }
  33. async update({ id }, { name, mobile, passwd, openid }) {
  34. assert(id, '缺少部分信息项');
  35. const user = await this.model.findById(id);
  36. if (!user) {
  37. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
  38. }
  39. if (passwd) {
  40. user.passwd = { secret: passwd };
  41. }
  42. if (name) {
  43. user.name = name;
  44. }
  45. if (mobile) {
  46. user.mobile = mobile;
  47. }
  48. if (openid) {
  49. user.openid = openid;
  50. }
  51. user.status = '1';
  52. await user.save();
  53. return user;
  54. }
  55. // 学校注册
  56. async register(data) {
  57. const { code, mobile, passwd, name } = data;
  58. assert(code && mobile && name && passwd, '缺少部分信息项');
  59. const user = await this.model.findOne({ mobile });
  60. if (user) {
  61. throw new BusinessError(ErrorCode.DATA_EXISTED);
  62. }
  63. const sch = await this.schModel.findOne({ code });
  64. if (!sch) {
  65. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  66. }
  67. const newdata = { name, mobile, type: '2', uid: sch.id };
  68. newdata.passwd = { secret: passwd };
  69. const res = await this.model.create(newdata);
  70. return res;
  71. }
  72. // 学生绑定
  73. async bind(data) {
  74. const { openid, id_number, mobile, unionid } = data;
  75. assert(openid && id_number && mobile, '缺少部分信息项');
  76. let user = await this.model.findOne({ mobile });
  77. if (user) {
  78. user.openid = openid;
  79. user.unionid = unionid;
  80. await user.save();
  81. } else {
  82. const stud = await this.stuModel.findOne({ id_number: { $regex: `^${id_number}$`, $options: 'i' } });
  83. if (!stud) {
  84. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  85. }
  86. stud.openid = openid;
  87. stud.isComming = '1';
  88. await stud.save();
  89. // 再次检查是否有该用户,需要用uid查
  90. const have_user = await this.model.findOne({ uid: stud.id });
  91. const newdata = { name: stud.name, mobile: stud.phone, type: '4', uid: stud.id, openid, unionid };
  92. if (have_user) {
  93. if (stud.name) have_user.name = stud.name;
  94. if (stud.phone) have_user.mobile = stud.phone;
  95. if (stud.id) have_user.uid = stud.id;
  96. user = await have_user.save();
  97. } else {
  98. newdata.passwd = { secret: '12345678' };
  99. user = await this.model.create(newdata);
  100. }
  101. }
  102. return user;
  103. }
  104. // 其他用户绑定
  105. async userbind(data) {
  106. const { openid, uid, qrcode } = data;
  107. assert(openid && uid && qrcode, '缺少部分信息项');
  108. const user = await this.model.findById(uid);
  109. if (!user) {
  110. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  111. }
  112. user.openid = openid;
  113. const res = await user.save();
  114. if (res) {
  115. const { mq } = this.ctx;
  116. if (mq) {
  117. const msg = user.name + '绑定微信成功';
  118. const parm = {
  119. durable: true,
  120. headers: {
  121. userid: uid,
  122. openid,
  123. } };
  124. await mq.topic('qrcode.bind', qrcode, msg, parm);
  125. } else {
  126. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  127. }
  128. }
  129. return user;
  130. }
  131. // 通过openid查询用户信息
  132. async findByOpenid(openid) {
  133. // 通过openid查询用户信息
  134. const user = await this.model.findOne({ openid });
  135. return user;
  136. }
  137. // 通过openid查询用户信息
  138. async findByAppOpenid(appopenid) {
  139. // 通过openid查询用户信息
  140. if (!appopenid) {
  141. console.error('没有appopenid');
  142. return;
  143. }
  144. const user = await this.model.findOne({ appopenid });
  145. return user;
  146. }
  147. // 通过unionid查询用户信息
  148. async findByunionid(unionid) {
  149. // 通过unionid查询用户信息
  150. const user = await this.model.findOne({ unionid });
  151. return user;
  152. }
  153. // 学校用户生成
  154. async schoolregister() {
  155. const schools = await this.schModel.find();
  156. for (const sch of schools) {
  157. const user = await this.model.findOne({ uid: sch.id, type: '2' });
  158. if (!user) {
  159. const newdata = { name: sch.name, mobile: sch.code, type: '2', uid: sch.id };
  160. newdata.passwd = { secret: '12345678' };
  161. await this.model.create(newdata);
  162. }
  163. }
  164. this.ctx.ok({ data: {} });
  165. }
  166. // 学生小程序绑定
  167. async appbind(data) {
  168. const { name, mobile, appopenid } = data;
  169. console.error(`appBind: name=>${name} / mobile=>${mobile} / appopenid = ${appopenid}`);
  170. assert(name, '缺少姓名');
  171. assert(mobile, '缺少手机号');
  172. assert(appopenid, '缺少小程序openid');
  173. const user = await this.model.findOne({ name, mobile });
  174. if (!user) {
  175. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
  176. }
  177. user.appopenid = appopenid;
  178. const res = await user.save();
  179. // const res = await this.model.update(
  180. // { name, mobile },
  181. // { $set: { appopenid } }
  182. // );
  183. return res;
  184. }
  185. }
  186. module.exports = UserService;