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. async create(body) {
  16. const { passwd, ...data } = body;
  17. data.passwd = { secret: passwd };
  18. return await this.model.create(data);
  19. }
  20. // 重写创建方法
  21. async register(data) {
  22. assert(data.name && data.phone && data.passwd, '缺少部分信息项');
  23. assert(/^\d{11}$/i.test(data.phone), '无效的手机号');
  24. const user = await this.model.findOne({ phone: data.phone });
  25. if (user) {
  26. throw new BusinessError(ErrorCode.DATA_EXISTED);
  27. }
  28. const newdata = _.cloneDeep(data);
  29. const pas = data.passwd;
  30. newdata.passwd = { secret: pas };
  31. let result = {};
  32. if (data.type === '0' || data.type === '1' || data.type === '4') {
  33. result = await this.smodel.create(data);
  34. }
  35. if (data.type === '2') {
  36. result = await this.emodel.create(data);
  37. }
  38. newdata.userid = result.id;
  39. const res = await this.model.create(newdata);
  40. return res;
  41. }
  42. async update({ id }, data) {
  43. const user = await this.model.findById(id);
  44. const { userid, openid, role_id, name, phone, type } = data;
  45. if (userid) {
  46. user.userid = userid;
  47. }
  48. if (openid) {
  49. user.openid = openid;
  50. }
  51. if (role_id) {
  52. user.role_id = role_id;
  53. }
  54. if (name) {
  55. if (user.type === '0' || user.type === '1' || user.type === '4') {
  56. const staff = await this.smodel.findById(user.userid);
  57. staff.name = name;
  58. await staff.save();
  59. }
  60. if (user.type === '2') {
  61. const expert = await this.emodel.findById(user.userid);
  62. expert.name = name;
  63. await expert.save();
  64. }
  65. user.name = name;
  66. }
  67. if (phone) {
  68. const _user = await this.model.find({ phone });
  69. if (_.isEqual(_user.length, 0) || (_.isEqual(_user.length, 1) && _.isEqual(_user[0].id, user.id))) {
  70. if (user.type === '0' || user.type === '1' || user.type === '4') {
  71. const staff = await this.smodel.findById(user.userid);
  72. staff.phone = phone;
  73. await staff.save();
  74. }
  75. if (user.type === '2') {
  76. const expert = await this.emodel.findById(user.userid);
  77. expert.phone = phone;
  78. await expert.save();
  79. }
  80. user.phone = phone;
  81. } else {
  82. throw new BusinessError(ErrorCode.DATA_EXISTED);
  83. }
  84. }
  85. if (type) {
  86. user.type = type;
  87. }
  88. const res = await user.save();
  89. return res;
  90. }
  91. // 用户修改密码
  92. async uppasswd(data) {
  93. const { userid, oldpasswd, newpasswd } = data;
  94. assert(userid && oldpasswd && newpasswd, '缺少部分信息项');
  95. // 根据用户id查询用户表中是否存在相应数据
  96. const user = await this.model.findById(userid, '+passwd');
  97. // 如果用户不存在抛出异常
  98. if (!user) {
  99. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  100. }
  101. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  102. // 如果两个密码不一致抛出异常
  103. if (oldpasswd !== user.passwd) {
  104. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  105. }
  106. user.passwd = { secret: newpasswd };
  107. await user.save();
  108. }
  109. async login(data) {
  110. const { phone, passwd } = data;
  111. // 根据用户输入的手机号查询其他用户表中是否存在相应数据
  112. const user = await this.model.findOne({ phone }, '+passwd');
  113. // 如果用户不存在抛出异常
  114. if (!user) {
  115. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  116. }
  117. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  118. // 如果两个密码不一致抛出异常
  119. if (passwd !== user.passwd.secret) {
  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;