user.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const Excel = require('exceljs');
  6. // 用户
  7. class UserService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'user');
  10. this.model = this.ctx.model.User;
  11. }
  12. // async create(data) {
  13. // // TODO:保存数据
  14. // data = await this.beforeCreate(data);
  15. // console.log(data);
  16. // let res = await this.model.create(data);
  17. // res = await this.afterCreate(data, res);
  18. // return res;
  19. // }
  20. async resetPwd({ id }, { password }) {
  21. const data = await this.model.findById(id);
  22. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  23. data.password = { secret: password };
  24. await data.save();
  25. }
  26. /**
  27. * 登陆
  28. * @param {Object} body 登陆参数
  29. * @param body.account
  30. * @param body.password
  31. */
  32. async login({ account, password }) {
  33. let user = await this.model.findOne({ account }, '+password');
  34. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  35. const { password: upwd, status, role_id } = user;
  36. // if (status !== '1') { throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态'); }
  37. if (password !== upwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); }
  38. user = JSON.parse(JSON.stringify(user));
  39. delete user.meta;
  40. delete user.__v;
  41. delete user.password;
  42. const token = this.ctx.service.util.jwt.encrypt(user);
  43. return token;
  44. }
  45. }
  46. module.exports = UserService;