user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // 学校注册
  34. async register(data) {
  35. const { code, mobile, passwd, name } = data;
  36. assert(code && mobile && name && passwd, '缺少部分信息项');
  37. const user = await this.model.findOne({ mobile });
  38. if (user) {
  39. throw new BusinessError(ErrorCode.DATA_EXISTED);
  40. }
  41. const sch = await this.schModel.findOne({ code });
  42. if (!sch) {
  43. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  44. }
  45. const newdata = { name, mobile, type: '2', uid: sch.id };
  46. newdata.passwd = { secret: passwd };
  47. const res = await this.model.create(newdata);
  48. return res;
  49. }
  50. // 学生绑定
  51. async bind(data) {
  52. const { openid, id_number, mobile, passwd } = data;
  53. assert(openid && id_number && mobile, '缺少部分信息项');
  54. let user = await this.model.findOne({ mobile });
  55. if (user) {
  56. if (user.passwd.secret !== passwd) {
  57. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  58. }
  59. user.openid = openid;
  60. await user.save();
  61. } else {
  62. const stud = await this.stuModel.findOne({ id_number });
  63. if (!stud) {
  64. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  65. }
  66. stud.openid = openid;
  67. await stud.save();
  68. const newdata = { name: stud.name, mobile: stud.phone, type: '4', uid: stud.id, openid };
  69. newdata.passwd = { secret: '12345678' };
  70. user = await this.model.create(newdata);
  71. }
  72. return user;
  73. }
  74. // 通过openid查询用户信息
  75. async findByOpenid(openid) {
  76. // 通过openid查询用户信息
  77. const user = await this.model.findOne({ openid });
  78. return user;
  79. }
  80. // 学校用户生成
  81. async schoolregister() {
  82. const schools = await this.schModel.find();
  83. for (const sch of schools) {
  84. const user = await this.model.findOne({ uid: sch.id, type: '2' });
  85. if (!user) {
  86. const newdata = { name: sch.name, mobile: sch.code, type: '2', uid: sch.id };
  87. newdata.passwd = { secret: '12345678' };
  88. await this.model.create(newdata);
  89. }
  90. }
  91. this.ctx.ok({ data: {} });
  92. }
  93. }
  94. module.exports = UserService;