admin.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 assert = require('assert');
  6. //
  7. class AdminService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'admin');
  10. this.model = this.ctx.model.User.Admin;
  11. }
  12. /**
  13. * 登陆
  14. * @param {Object} body 登陆参数
  15. * @param body.account 账户
  16. * @param body.password 密码
  17. */
  18. async login({ account, password }) {
  19. const { populate } = this.getRefMods();
  20. let user = await this.model.findOne({ account }, '+password').populate(populate);
  21. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  22. const { password: upwd } = user;
  23. if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
  24. // // 使用redis存储,后续的任何操作进行token的校验
  25. // await this.setUserInRedis(user);
  26. user = JSON.parse(JSON.stringify(user));
  27. delete user.password;
  28. delete user.meta;
  29. delete user.__v;
  30. const token = this.ctx.service.util.jwt.encrypt(user);
  31. return token;
  32. }
  33. async resetPwd({ id }, { password }) {
  34. const data = await this.model.findById(id);
  35. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  36. data.password = { secret: password };
  37. await data.save();
  38. }
  39. }
  40. module.exports = AdminService;