user.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 });
  112. if (user) {
  113. user.openid = openid;
  114. user.unionid = unionid;
  115. await user.save();
  116. } else {
  117. const stud = await this.stuModel.findOne({
  118. id_number: { $regex: `^${id_number}$`, $options: 'i' },
  119. });
  120. if (!stud) {
  121. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  122. }
  123. stud.openid = openid;
  124. stud.isComming = '1';
  125. await stud.save();
  126. // 再次检查是否有该用户,需要用uid查
  127. const have_user = await this.model.findOne({ uid: stud.id });
  128. const newdata = {
  129. name: stud.name,
  130. mobile: stud.phone,
  131. type: '4',
  132. uid: stud.id,
  133. openid,
  134. unionid,
  135. };
  136. if (have_user) {
  137. if (stud.name) have_user.name = stud.name;
  138. if (stud.phone) have_user.mobile = stud.phone;
  139. if (stud.id) have_user.uid = stud.id;
  140. user = await have_user.save();
  141. } else {
  142. newdata.passwd = { secret: '12345678' };
  143. user = await this.model.create(newdata);
  144. }
  145. }
  146. return user;
  147. }
  148. // 其他用户绑定
  149. async userbind(data) {
  150. const { openid, uid, qrcode } = data;
  151. assert(openid && uid && qrcode, '缺少部分信息项');
  152. const user = await this.model.findById(uid);
  153. if (!user) {
  154. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  155. }
  156. user.openid = openid;
  157. const res = await user.save();
  158. if (res) {
  159. const { mq } = this.ctx;
  160. if (mq) {
  161. const msg = user.name + '绑定微信成功';
  162. const parm = {
  163. durable: true,
  164. headers: {
  165. userid: uid,
  166. openid,
  167. },
  168. };
  169. await mq.topic('qrcode.bind', qrcode, msg, parm);
  170. } else {
  171. this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
  172. }
  173. }
  174. return user;
  175. }
  176. // 通过openid查询用户信息
  177. async findByOpenid(openid) {
  178. // 通过openid查询用户信息
  179. const user = await this.model.findOne({ openid });
  180. return user;
  181. }
  182. // 通过openid查询用户信息
  183. async findByAppOpenid(appopenid) {
  184. console.log(appopenid);
  185. // 通过openid查询用户信息
  186. if (!appopenid) {
  187. console.error('没有appopenid');
  188. return;
  189. }
  190. let user = await this.model.findOne({ appopenid }).populate({
  191. path: 'uid',
  192. model: 'Student',
  193. select: 'classid bedroomid',
  194. populate: [
  195. {
  196. path: 'classid',
  197. model: 'Class',
  198. select: 'jslocationid',
  199. populate: {
  200. path: 'jslocationid',
  201. model: 'Location',
  202. select: 'name ibeacon',
  203. },
  204. },
  205. {
  206. path: 'bedroomid',
  207. model: 'Bedroom',
  208. select: 'code ibeacon',
  209. },
  210. ],
  211. });
  212. if (user) {
  213. user = JSON.parse(JSON.stringify(user));
  214. // 整理数据
  215. const { uid } = user;
  216. if (uid) {
  217. const { _id, classid, bedroomid } = uid;
  218. // 先解析classid
  219. if (classid) {
  220. const { jslocationid } = classid;
  221. if (jslocationid) {
  222. const { name, ibeacon } = jslocationid;
  223. if (name && ibeacon) {
  224. user.jsname = name;
  225. user.jsibeacon = ibeacon;
  226. }
  227. }
  228. }
  229. // 在解析bedroomid
  230. if (bedroomid) {
  231. const { code, ibeacon } = bedroomid;
  232. if (code && ibeacon) {
  233. user.bedroomname = code;
  234. user.bedroomibeacon = ibeacon;
  235. }
  236. }
  237. user.uid = _id;
  238. }
  239. }
  240. return user;
  241. }
  242. // 通过unionid查询用户信息
  243. async findByunionid(unionid) {
  244. // 通过unionid查询用户信息
  245. const user = await this.model.findOne({ unionid });
  246. return user;
  247. }
  248. // 学校用户生成
  249. async schoolregister() {
  250. const schools = await this.schModel.find();
  251. for (const sch of schools) {
  252. const user = await this.model.findOne({ uid: sch.id, type: '2' });
  253. if (!user) {
  254. const newdata = {
  255. name: sch.name,
  256. mobile: sch.code,
  257. type: '2',
  258. uid: sch.id,
  259. };
  260. newdata.passwd = { secret: '12345678' };
  261. await this.model.create(newdata);
  262. }
  263. }
  264. this.ctx.ok({ data: {} });
  265. }
  266. // 学生小程序绑定
  267. async appbind(data) {
  268. const { name, mobile, appopenid } = data;
  269. console.error(
  270. `appBind: name=>${name} / mobile=>${mobile} / appopenid = ${appopenid}`
  271. );
  272. assert(name, '缺少姓名');
  273. assert(mobile, '缺少手机号');
  274. assert(appopenid, '缺少小程序openid');
  275. const user = await this.model.findOne({ name, mobile });
  276. if (!user) {
  277. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户信息不存在');
  278. }
  279. user.appopenid = appopenid;
  280. const res = await user.save();
  281. // const res = await this.model.update(
  282. // { name, mobile },
  283. // { $set: { appopenid } }
  284. // );
  285. return res;
  286. }
  287. }
  288. module.exports = UserService;