user.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: passwd };
  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. console.log(id);
  39. console.log(data);
  40. const user = await this.model.findById(id);
  41. if (data.name)user.name = data.name;
  42. if (data.phone) user.phone = data.phone;
  43. if (data.uid) user.uid = data.uid;
  44. if (data.role) user.role = data.role;
  45. if (data.menus) user.menus = data.menus;
  46. if (data.pid) user.pid = data.pid;
  47. if (data.deptname) user.deptname = data.deptname;
  48. if (data.remark) user.remark = data.remark;
  49. if (data.code) user.code = data.code;
  50. if (data.isdel) user.isdel = data.isdel;
  51. await user.save();
  52. }
  53. // 用户修改密码
  54. async uppasswd(data) {
  55. const { id, oldpasswd, newpasswd } = data;
  56. assert(id && oldpasswd && newpasswd, '缺少部分信息项');
  57. // 根据用户id查询其他用户表中是否存在相应数据
  58. const user = await this.model.findById(id, '+passwd');
  59. // 如果用户不存在抛出异常
  60. if (!user) {
  61. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  62. }
  63. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  64. const _oldpasswd = await this.createJwtPwd(oldpasswd);
  65. // 如果两个密码不一致抛出异常
  66. if (_oldpasswd !== user.passwd.secret) {
  67. throw new BusinessError(ErrorCode.BAD_PASSWORD);
  68. }
  69. const _newpasswd = await this.createJwtPwd(newpasswd);
  70. user.passwd = { secret: _newpasswd };
  71. await user.save();
  72. }
  73. async querymenus({ id }) {
  74. const user = await this.model.findById(id);
  75. if (!user) {
  76. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  77. }
  78. const _menus = [];
  79. for (const elm of user.menus) {
  80. const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
  81. if (_menu) {
  82. _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
  83. }
  84. }
  85. return { id, menus: _menus };
  86. }
  87. // 按条件更新方法
  88. async updatebyuid({ id }, data) {
  89. const user = await this.model.findOne({ uid: id });
  90. if (user) {
  91. user.name = data.name;
  92. user.deptname = data.deptname;
  93. }
  94. await user.save();
  95. }
  96. // 按条件更新方法
  97. async bind(data) {
  98. const user = await this.model.findById(data.uid);
  99. if (!user) {
  100. throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  101. }
  102. user.openid = data.openid;
  103. return await user.save();
  104. }
  105. async findByOpenid(openid) {
  106. const user = await this.model.findOne({ openid });
  107. return user;
  108. }
  109. async finduserlist({ skip, limit, ...info }) {
  110. const query = {
  111. ...info,
  112. $or: [{ role: '4' }, { role: '5' }, { role: '6' }],
  113. };
  114. const total = await this.model.count(query);
  115. const data = await this.model
  116. .find(query)
  117. .skip(Number(skip))
  118. .limit(Number(limit));
  119. return { data, total };
  120. }
  121. async businessuser({ pid, skip, limit }) {
  122. const query = { };
  123. pid ? (query.pid = pid) : '';
  124. const total = await this.model.count(query);
  125. const data = await this.model
  126. .find(query)
  127. .skip(Number(skip))
  128. .limit(Number(limit));
  129. console.log(data);
  130. return { data, total };
  131. }
  132. // 重写删除方法
  133. /**
  134. * 根据id删除=>修改isdel为1
  135. * @param {Object} {id} 只接收id,不过需要解构,因为是object形式过来的
  136. */
  137. async delete({ id }) {
  138. const res = await this.model.update({ _id: ObjectId(id) }, { isdel: '1' });
  139. if (res) {
  140. const { config } = this.app;
  141. if (!config) throw new BusinessError(ErrorCode.SERVICE_FAULT, '系统错误,需要检查系统项目设置');
  142. const { project } = config;
  143. if (!project) throw new BusinessError(ErrorCode.SERVICE_FAULT, '系统错误,需要检查系统中各项目的设置');
  144. const { market } = project;
  145. if (!market) throw new BusinessError(ErrorCode.SERVICE_FAULT, '系统错误,需要检查系统项目中项目设置的各项目配置');
  146. const url = `${market}/product/userdelete`;
  147. const r = await this.ctx.curl(url, {
  148. method: 'post',
  149. headers: {
  150. 'content-type': 'application/json',
  151. },
  152. dataType: 'json',
  153. data: { id },
  154. });
  155. console.log(r);
  156. }
  157. return res;
  158. }
  159. }
  160. module.exports = UserService;