user.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.rmodel = this.ctx.model.Role;
  13. this.productmodel = this.ctx.app.config.axios.product;
  14. }
  15. // 重写创建方法
  16. async create(data) {
  17. const { name, phone, passwd, role } = data;
  18. console.log(data);
  19. assert(name && phone && passwd, '缺少部分信息项');
  20. // if (`${role}` !== '5') { assert(/^\d{11}$/i.test(phone), 'phone无效'); }
  21. // const user = await this.model.findOne({ phone });
  22. // if (user) {
  23. // throw new BusinessError(ErrorCode.DATA_EXISTED);
  24. // }
  25. const newdata = data;
  26. const pas = await this.createJwtPwd(passwd);
  27. newdata.passwd = { secret: pas };
  28. const res = await this.model.create(newdata);
  29. return res;
  30. }
  31. // 创建登录Token
  32. async createJwtPwd(password) {
  33. const { secret, expiresIn, issuer } = this.config.jwt;
  34. const token = await jwt.sign(password, secret);
  35. return token;
  36. }
  37. // 重写修改方法
  38. async update({ id }, data) {
  39. const {
  40. name,
  41. phone,
  42. passwd,
  43. openid,
  44. role,
  45. menus,
  46. remark,
  47. uid,
  48. deptid,
  49. deptname,
  50. pid,
  51. } = data;
  52. const user = await this.model.findById(id, '+passwd');
  53. if (name) {
  54. user.name = name;
  55. }
  56. if (phone) {
  57. user.phone = phone;
  58. }
  59. if (passwd) {
  60. const newpasswd = await this.createJwtPwd(passwd);
  61. user.passwd = { secret: newpasswd };
  62. }
  63. if (openid) {
  64. user.openid = openid;
  65. }
  66. if (role) {
  67. user.role = role;
  68. }
  69. if (menus) {
  70. user.menus = menus;
  71. }
  72. if (uid) {
  73. user.uid = uid;
  74. }
  75. if (deptid) {
  76. user.deptid = deptid;
  77. }
  78. if (deptname) {
  79. user.deptname = deptname;
  80. }
  81. if (pid) {
  82. user.pid = pid;
  83. }
  84. if (remark) {
  85. user.remark = remark;
  86. }
  87. await user.save();
  88. }
  89. // 用户修改密码
  90. async uppasswd(data) {
  91. const { id, oldpasswd, newpasswd } = data;
  92. assert(id && oldpasswd && newpasswd, '缺少部分信息项');
  93. // 根据用户id查询其他用户表中是否存在相应数据
  94. const user = await this.model.findById(id, '+passwd');
  95. // 如果用户不存在抛出异常
  96. if (!user) {
  97. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  98. }
  99. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  100. const _oldpasswd = await this.createJwtPwd(oldpasswd);
  101. // 如果两个密码不一致抛出异常
  102. if (_oldpasswd !== user.passwd.secret) {
  103. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  104. }
  105. const _newpasswd = await this.createJwtPwd(newpasswd);
  106. user.passwd = { secret: _newpasswd };
  107. await user.save();
  108. }
  109. async querymenus({ id }) {
  110. const user = await this.model.findById(id);
  111. if (!user) {
  112. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  113. }
  114. const _menus = [];
  115. for (const elm of user.menus) {
  116. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  117. if (_menu) {
  118. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  119. }
  120. }
  121. return { id, menus: _menus };
  122. }
  123. // 按条件更新方法
  124. async updatebyuid({ id }, data) {
  125. const user = await this.model.findOne({ uid: id });
  126. if (user) {
  127. user.name = data.name;
  128. user.deptname = data.deptname;
  129. }
  130. await user.save();
  131. }
  132. // 按条件更新方法
  133. async bind(data) {
  134. const user = await this.model.findById(data.uid);
  135. if (!user) {
  136. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  137. }
  138. user.openid = data.openid;
  139. return await user.save();
  140. }
  141. async findByOpenid(openid) {
  142. const user = await this.model.findOne({ openid });
  143. return user;
  144. }
  145. async finduserlist({ skip, limit, ...info }) {
  146. const query = {
  147. ...info,
  148. $or: [{ role: '4' }, { role: '5' }, { role: '6' }],
  149. };
  150. const total = await this.model.count(query);
  151. const data = await this.model
  152. .find(query)
  153. .skip(Number(skip))
  154. .limit(Number(limit));
  155. return { data, total };
  156. }
  157. async businessuser({ pid, skip, limit }) {
  158. const query = { code: { $regex: /^.{9}$/ } };
  159. pid ? (query.pid = pid) : '';
  160. const total = await this.model.count(query);
  161. const data = await this.model
  162. .find(query)
  163. .skip(Number(skip))
  164. .limit(Number(limit));
  165. return { data, total };
  166. }
  167. // 重写删除方法
  168. /**
  169. * 根据id删除=>修改isdel为1
  170. * @param {Object} {id} 只接收id,不过需要解构,因为是object形式过来的
  171. */
  172. async delete({ id }) {
  173. const res = await this.model.update({ _id: ObjectId(id) }, { isdel: '1' });
  174. if (res) {
  175. const arr = await this.productmodel.updateMany({ userid: ObjectId(id) }, { isdel: '1' });
  176. if (arr) {
  177. return arr;
  178. }
  179. }
  180. return res;
  181. }
  182. }
  183. module.exports = UserService;