user.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  13. async create(data) {
  14. const { name, password, role } = data;
  15. assert(name, '用户名不能为空');
  16. assert(password, '密码不能为空');
  17. const { phone } = data;
  18. const has_phone = await this.model.findOne({ phone, role });
  19. if (has_phone) {
  20. throw new BusinessError('此身份手机号已被注册,请更换手机号');
  21. }
  22. const newdata = data;
  23. newdata.password = { secret: password };
  24. const res = await this.model.create(newdata);
  25. if (res) {
  26. const url = this.ctx.app.config.axios.auth.baseUrl;
  27. const newdata = {
  28. name,
  29. phone: data.phone,
  30. passwd: password,
  31. uid: res.id,
  32. role: data.role,
  33. pid: data.pid,
  34. deptname: data.deptname,
  35. code: data.code,
  36. openid: data.openid,
  37. };
  38. await this.ctx.curl(url, {
  39. method: 'post',
  40. headers: {
  41. 'content-type': 'application/json',
  42. },
  43. dataType: 'json',
  44. data: JSON.stringify(newdata),
  45. });
  46. }
  47. return res;
  48. }
  49. // 用户修改密码
  50. async uppasswd(data) {
  51. const { uid, newpasswd } = data;
  52. assert(uid && newpasswd, '缺少部分信息项');
  53. // 根据用户id查询其他用户表中是否存在相应数据
  54. const user = await this.model.findById(uid, '+password');
  55. // 如果用户不存在抛出异常
  56. if (!user) {
  57. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  58. }
  59. user.password = { secret: data.newpasswd };
  60. await user.save();
  61. }
  62. async update({ id }, data) {
  63. const user = await this.model.findById(id);
  64. // const { phone, role } = data;
  65. // const phoneList = await this.model.find({ phone, role });
  66. // const is_has = phoneList.find(f => f.id !== id);
  67. // if (is_has) throw new BusinessError('此身份手机号已被注册,请更换手机号');
  68. if (data.name) {
  69. user.name = data.name;
  70. }
  71. if (data.password) {
  72. user.password = { secret: data.password };
  73. }
  74. user.cardnumber = data.cardnumber;
  75. user.phone = data.phone;
  76. user.email = data.email;
  77. user.addr = data.addr;
  78. user.img_path = data.img_path;
  79. user.is_qy = data.is_qy;
  80. user.cardfile_a = data.cardfile_a;
  81. user.cardfile_b = data.cardfile_b;
  82. user.img_qy = data.img_qy;
  83. user.resume = data.resume;
  84. user.major = data.major;
  85. user.institution_type = data.institution_type;
  86. user.institution_name = data.institution_name;
  87. user.institution_code = data.institution_code;
  88. user.institution_nature = data.institution_nature;
  89. user.office_phone = data.office_phone;
  90. user.profession = data.profession;
  91. user.status = data.status;
  92. user.is_del = data.is_del;
  93. user.role = data.role;
  94. user.token = data.token;
  95. user.columnid = data.columnid;
  96. if (data.deptname) {
  97. user.deptname = data.deptname;
  98. }
  99. if (data.pid) {
  100. user.pid = data.pid;
  101. }
  102. const res = await user.save();
  103. if (res) {
  104. const url = this.ctx.app.config.axios.auth.baseUrl + '/updatebyuid/' + res.id;
  105. const newdata = { name: data.name, deptname: data.deptname };
  106. await this.ctx.curl(url, {
  107. method: 'post',
  108. headers: {
  109. 'content-type': 'application/json',
  110. },
  111. dataType: 'json',
  112. data: JSON.stringify(newdata),
  113. });
  114. }
  115. return res;
  116. }
  117. async login({ phone, password, role }) {
  118. assert(phone, '手机号不能为空');
  119. assert(password, '密码不能为空');
  120. assert(role, '需要选择用户类型');
  121. const user = await this.model.findOne({ phone, role }, '+password');
  122. if (!user) {
  123. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  124. }
  125. if (user.password.secret !== password) {
  126. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  127. }
  128. if (user.status === '0') {
  129. throw new BusinessError('用户未审核,请等待审核后再登陆');
  130. } else if (user.status === '2') {
  131. throw new BusinessError('用户申请被拒绝,请联系平台管理员');
  132. }
  133. return await this.createJwt(user);
  134. }
  135. // 创建登录Token
  136. async createJwt({
  137. id,
  138. name,
  139. phone,
  140. is_qy,
  141. img_path,
  142. email,
  143. resume,
  144. major,
  145. office_phone,
  146. profession,
  147. role,
  148. status,
  149. columnid,
  150. }) {
  151. const { secret, expiresIn = '1d', issuer } = this.config.jwt;
  152. const subject = phone;
  153. // const _userid = id;
  154. const res = {
  155. name,
  156. phone,
  157. is_qy,
  158. id,
  159. img_path,
  160. email,
  161. resume,
  162. major,
  163. office_phone,
  164. profession,
  165. role,
  166. status,
  167. columnid,
  168. };
  169. const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
  170. return token;
  171. }
  172. }
  173. module.exports = UserService;