user.js 8.1 KB

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