user.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const jwt = require('jsonwebtoken');
  8. class UserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'user');
  11. this.model = this.ctx.model.User;
  12. this.smodel = this.ctx.model.Staff;
  13. this.emodel = this.ctx.model.Expert;
  14. }
  15. // 重写创建方法
  16. async register(data) {
  17. console.log(data);
  18. assert(data.name && data.phone && data.passwd, '缺少部分信息项');
  19. assert(/^\d{11}$/i.test(data.phone), '无效的手机号');
  20. const user = await this.model.findOne({ phone: data.phone });
  21. if (user) {
  22. throw new BusinessError(ErrorCode.DATA_EXISTED);
  23. }
  24. const newdata = _.cloneDeep(data);
  25. const pas = await this.createJwtPwd(data.passwd);
  26. newdata.passwd = pas;
  27. let result = {};
  28. if (data.type === '0' || data.type === '1') {
  29. result = await this.smodel.create(data);
  30. }
  31. if (data.type === '2') {
  32. result = await this.emodel.create(data);
  33. }
  34. newdata.userid = result.id;
  35. const res = await this.model.create(newdata);
  36. return res;
  37. }
  38. async update({ id }, data) {
  39. const user = await this.model.findById(id);
  40. const { userid, openid, role_id, name, phone, type } = data;
  41. if (userid) {
  42. user.userid = userid;
  43. }
  44. if (openid) {
  45. user.openid = openid;
  46. }
  47. if (role_id) {
  48. user.role_id = role_id;
  49. }
  50. if (name) {
  51. if (user.type === '0' || user.type === '1') {
  52. const staff = await this.smodel.findById(user.userid);
  53. staff.name = name;
  54. await staff.save();
  55. }
  56. if (user.type === '2') {
  57. const expert = await this.emodel.findById(user.userid);
  58. expert.name = name;
  59. await expert.save();
  60. }
  61. user.name = name;
  62. }
  63. if (phone) {
  64. const _user = await this.model.find({ phone });
  65. if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) {
  66. if (user.type === '0' || user.type === '1') {
  67. const staff = await this.smodel.findById(user.userid);
  68. staff.phone = phone;
  69. await staff.save();
  70. }
  71. if (user.type === '2') {
  72. const expert = await this.emodel.findById(user.userid);
  73. expert.phone = phone;
  74. await expert.save();
  75. }
  76. user.phone = phone;
  77. } else {
  78. throw new BusinessError(ErrorCode.DATA_EXISTED);
  79. }
  80. }
  81. if (type) {
  82. user.type = type;
  83. }
  84. const res = await user.save();
  85. return res;
  86. }
  87. // 用户修改密码
  88. async uppasswd(data) {
  89. const { userid, oldpasswd, newpasswd } = data;
  90. assert(userid && oldpasswd && newpasswd, '缺少部分信息项');
  91. // 根据用户id查询用户表中是否存在相应数据
  92. const user = await this.model.findById(userid, '+passwd');
  93. // 如果用户不存在抛出异常
  94. if (!user) {
  95. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  96. }
  97. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  98. const _oldpasswd = await this.createJwtPwd(oldpasswd);
  99. // 如果两个密码不一致抛出异常
  100. if (_oldpasswd !== user.passwd) {
  101. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  102. }
  103. const _newpasswd = await this.createJwtPwd(newpasswd);
  104. user.passwd = _newpasswd;
  105. await user.save();
  106. }
  107. async login(data) {
  108. const { phone, passwd } = data;
  109. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  110. const user = await this.model.findOne({ phone });
  111. // 如果用户不存在抛出异常
  112. if (!user) {
  113. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  114. }
  115. const _user = await this.model.findOne({ phone }, '+passwd');
  116. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  117. const pas = await this.createJwtPwd(passwd);
  118. // 如果两个密码不一致抛出异常
  119. if (pas !== _user.passwd) {
  120. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  121. }
  122. // 取出用户的类型,根据用户类型返回相应信息
  123. return await this.createJwt(user);
  124. }
  125. async createJwtPwd(password) {
  126. const { secret, expiresIn, issuer } = this.config.jwt;
  127. const token = await jwt.sign(password, secret);
  128. return token;
  129. }
  130. // 创建登录Token
  131. async createJwt({ id, userid, openid, role_id, name, phone, type }) {
  132. const { secret, expiresIn = '1d', issuer = type } = this.config.jwt;
  133. const subject = phone;
  134. const _uid = id;
  135. const res = { userid, uid: _uid, openid, role_id, name, phone, type };
  136. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  137. return token;
  138. }
  139. }
  140. module.exports = UserService;