user.js 3.7 KB

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