user.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const { ObjectId } = require('mongoose').Types;
  7. const moment = require('moment');
  8. // 用户
  9. class UserService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'user');
  12. this.model = this.ctx.model.User;
  13. }
  14. // async create(data) {
  15. // // TODO:保存数据
  16. // data = await this.beforeCreate(data);
  17. // console.log(data);
  18. // let res = await this.model.create(data);
  19. // res = await this.afterCreate(data, res);
  20. // return res;
  21. // }
  22. async resetPwd({ id }, { password }) {
  23. const data = await this.model.findById(id);
  24. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  25. data.password = { secret: password };
  26. await data.save();
  27. }
  28. /**
  29. * 登陆
  30. * @param {Object} body 登陆参数
  31. * @param body.account
  32. * @param body.password
  33. */
  34. async login({ account, password }) {
  35. let user = await this.model.findOne({ account }, '+password');
  36. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  37. const { password: upwd, status, role_id } = user;
  38. // if (status !== '1') { throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态'); }
  39. if (password !== upwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); }
  40. user = JSON.parse(JSON.stringify(user));
  41. delete user.meta;
  42. delete user.__v;
  43. delete user.password;
  44. const token = this.ctx.service.util.jwt.encrypt(user);
  45. return token;
  46. }
  47. // 监听用户vip信息
  48. // async watchvip() {
  49. // const list = [];
  50. // const data = await this.model.find();
  51. // for (const val of data) {
  52. // if (val.is_vip === '1') {
  53. // if (moment().isAfter(val.vip_end_time)) list.push(val);
  54. // }
  55. // }
  56. // if (list.length > 0) {
  57. // for (const val of list) {
  58. // await this.model.updateOne({ _id: ObjectId(val._id) }, { is_vip: '0' });
  59. // }
  60. // }
  61. // }
  62. }
  63. module.exports = UserService;