user.js 5.7 KB

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