admin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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. const user = await this.model.findOne({ account }, '+password').populate(populate);
  21. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  22. const { password: upwd, status } = user;
  23. if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
  24. if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
  25. // // 使用redis存储,后续的任何操作进行token的校验
  26. // await this.setUserInRedis(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. }
  34. module.exports = AdminService;