user.js 8.1 KB

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