user.js 5.5 KB

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