user.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. async query({ name, ...info } = {}, options) {
  17. const newoptions = await this.ctx.service.util.getQueryOptions(options);
  18. const query = { ...info };
  19. if (name) {
  20. query.name = { $regex: name };
  21. }
  22. let res = await this.model.find(query, '+passwd', newoptions).exec();
  23. res = JSON.parse(JSON.stringify(res));
  24. for (const user of res) {
  25. if (user) {
  26. const { passwd } = user;
  27. user.passwd = passwd.secret;
  28. }
  29. }
  30. return res;
  31. }
  32. async count({ name, ...info }) {
  33. const query = { ...info };
  34. if (name) {
  35. query.name = { $regex: name };
  36. }
  37. const res = await this.model.count(query);
  38. return res;
  39. }
  40. // 重写创建方法
  41. async create(data) {
  42. const { name, mobile, passwd, type, gender } = data;
  43. assert(name && mobile && passwd && type, '缺少部分信息项');
  44. if (type !== '0') {
  45. assert(/^\d{11}$/i.test(mobile), 'mobile无效');
  46. }
  47. const newdata = data;
  48. newdata.passwd = { secret: passwd };
  49. const res = await this.model.create(newdata);
  50. if (res) {
  51. // 如果是班主任的时候将用户信息填入班主任表
  52. if (type === '1') {
  53. const headt = { name, phone: mobile, gender };
  54. await this.hModel.create(headt);
  55. }
  56. }
  57. return res;
  58. }
  59. async update({ id }, body) {
  60. const { name, mobile, passwd, openid, status, type } = body;
  61. assert(id, '缺少部分信息项');
  62. const user = await this.model.findById(id);
  63. if (!user) {
  64. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
  65. }
  66. if (passwd) {
  67. body.passwd = { secret: passwd };
  68. }
  69. return await this.model.updateOne({ _id: id }, body);
  70. // if (name) {
  71. // user.name = name;
  72. // }
  73. // if (mobile) {
  74. // user.mobile = mobile;
  75. // }
  76. // if (openid) {
  77. // user.openid = openid;
  78. // }
  79. // if (status) {
  80. // user.status = status;
  81. // } else {
  82. // user.status = '1';
  83. // }
  84. // if (type) {
  85. // user.type = type;
  86. // }
  87. // await user.save();
  88. // return user;
  89. }
  90. // 学校注册
  91. async register(data) {
  92. const { code, mobile, passwd, name } = data;
  93. assert(code && mobile && name && passwd, '缺少部分信息项');
  94. const user = await this.model.findOne({ mobile });
  95. if (user) {
  96. throw new BusinessError(ErrorCode.DATA_EXISTED);
  97. }
  98. const sch = await this.schModel.findOne({ code });
  99. if (!sch) {
  100. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  101. }
  102. const newdata = { name, mobile, type: '2', uid: sch.id };
  103. newdata.passwd = { secret: passwd };
  104. const res = await this.model.create(newdata);
  105. return res;
  106. }
  107. // 学生绑定
  108. async bind(data) {
  109. const { openid, id_number, mobile, unionid } = data;
  110. assert(openid && id_number && mobile, '缺少部分信息项');
  111. let user = await this.model.findOne({ mobile, type: '4' });
  112. if (user) {
  113. // 2021-06-07修改:根据身份证/手机号,找到对应的学生信息,将_id变为uid重保存
  114. const stuInfo = await this.stuModel.findOne({ $or: [{ id_number }, { mobile }] });
  115. if (stuInfo) {
  116. user.uid = stuInfo._id;
  117. // 将学生信息的openid也重置
  118. stuInfo.openid = openid;
  119. await stuInfo.save();
  120. }
  121. user.openid = openid;
  122. user.unionid = unionid;
  123. await user.save();
  124. } else {
  125. const stud = await this.stuModel.findOne({
  126. id_number: { $regex: `^${id_number}$`, $options: 'i' },
  127. });
  128. if (!stud) {
  129. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  130. }
  131. stud.openid = openid;
  132. stud.isComming = '1';
  133. await stud.save();
  134. // 再次检查是否有该用户,需要用uid查
  135. const have_user = await this.model.findOne({ uid: stud.id });
  136. const newdata = {
  137. name: stud.name,
  138. mobile: stud.phone,
  139. type: '4',
  140. uid: stud.id,
  141. openid,
  142. unionid,
  143. };
  144. if (have_user) {
  145. if (stud.name) have_user.name = stud.name;
  146. if (stud.phone) have_user.mobile = stud.phone;
  147. if (stud.id) have_user.uid = stud.id;
  148. user = await have_user.save();
  149. } else {
  150. newdata.passwd = { secret: '12345678' };
  151. user = await this.model.create(newdata);
  152. delete user.passwd;
  153. }
  154. }
  155. return user;
  156. }
  157. // 其他用户绑定
  158. async userbind(data) {
  159. const { openid, uid, qrcode } = data;
  160. assert(openid && uid && qrcode, '缺少部分信息项');
  161. let user = await this.model.findById(uid);
  162. if (!user) {
  163. user = await this.model.findOne({ uid });
  164. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  165. }
  166. user.openid = openid;
  167. const res = await user.save();
  168. if (res) {
  169. const { mq } = this.ctx;
  170. if (mq) {
  171. const msg = user.name + '绑定微信成功';
  172. const parm = {
  173. durable: true,
  174. headers: {
  175. userid: uid,
  176. openid,
  177. },
  178. };
  179. await mq.topic('qrcode.bind', qrcode, msg, parm);
  180. } else {
  181. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  182. }
  183. }
  184. return user;
  185. }
  186. // 通过openid查询用户信息
  187. async findByOpenid(openid) {
  188. // 通过openid查询用户信息
  189. const user = await this.model.findOne({ openid });
  190. return user;
  191. }
  192. // 通过openid查询用户信息
  193. async findByAppOpenid(appopenid) {
  194. console.log(appopenid);
  195. // 通过openid查询用户信息
  196. if (!appopenid) {
  197. console.error('没有appopenid');
  198. return;
  199. }
  200. let user = await this.model.findOne({ appopenid }).populate({
  201. path: 'uid',
  202. model: 'Student',
  203. select: 'classid bedroomid',
  204. populate: [
  205. {
  206. path: 'classid',
  207. model: 'Class',
  208. select: 'jslocationid',
  209. populate: {
  210. path: 'jslocationid',
  211. model: 'Location',
  212. select: 'name ibeacon',
  213. },
  214. },
  215. {
  216. path: 'bedroomid',
  217. model: 'Bedroom',
  218. select: 'code ibeacon',
  219. },
  220. ],
  221. });
  222. if (user) {
  223. user = JSON.parse(JSON.stringify(user));
  224. // 整理数据
  225. const { uid } = user;
  226. if (uid) {
  227. const { _id, classid, bedroomid } = uid;
  228. // 先解析classid
  229. if (classid) {
  230. const { jslocationid } = classid;
  231. if (jslocationid) {
  232. const { name, ibeacon } = jslocationid;
  233. if (name && ibeacon) {
  234. user.jsname = name;
  235. user.jsibeacon = ibeacon;
  236. }
  237. }
  238. }
  239. // 在解析bedroomid
  240. if (bedroomid) {
  241. const { code, ibeacon } = bedroomid;
  242. if (code && ibeacon) {
  243. user.bedroomname = code;
  244. user.bedroomibeacon = ibeacon;
  245. }
  246. }
  247. user.uid = _id;
  248. }
  249. }
  250. return user;
  251. }
  252. // 通过unionid查询用户信息
  253. async findByunionid(unionid) {
  254. // 通过unionid查询用户信息
  255. const user = await this.model.findOne({ unionid });
  256. return user;
  257. }
  258. // 学校用户生成
  259. async schoolregister() {
  260. const schools = await this.schModel.find();
  261. for (const sch of schools) {
  262. const user = await this.model.findOne({ uid: sch.id, type: '2' });
  263. if (!user) {
  264. const newdata = {
  265. name: sch.name,
  266. mobile: sch.code,
  267. type: '2',
  268. uid: sch.id,
  269. };
  270. newdata.passwd = { secret: '12345678' };
  271. await this.model.create(newdata);
  272. }
  273. }
  274. this.ctx.ok({ data: {} });
  275. }
  276. // 学生小程序绑定
  277. async appbind(data) {
  278. const { name, mobile, appopenid } = data;
  279. console.error(`appBind: name=>${name} / mobile=>${mobile} / appopenid = ${appopenid}`);
  280. assert(name, '缺少姓名');
  281. assert(mobile, '缺少手机号');
  282. assert(appopenid, '缺少小程序openid');
  283. const user = await this.model.findOne({ name, mobile });
  284. if (!user) {
  285. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
  286. }
  287. user.appopenid = appopenid;
  288. const res = await user.save();
  289. // const res = await this.model.update(
  290. // { name, mobile },
  291. // { $set: { appopenid } }
  292. // );
  293. return res;
  294. }
  295. }
  296. module.exports = UserService;