admin.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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.Admin;
  11. }
  12. async resetPwd({ id }, { password }) {
  13. const data = await this.model.findById(id);
  14. if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  15. data.password = { secret: password };
  16. await data.save();
  17. }
  18. /**
  19. * 登陆
  20. * @param {Object} body 登陆参数
  21. * @param body.account
  22. * @param body.password
  23. */
  24. async login({ account, password }) {
  25. let user = await this.model.findOne({ account }, '+password');
  26. if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
  27. const { password: upwd } = user;
  28. // if (status !== '1') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
  29. if (password !== upwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); }
  30. user = JSON.parse(JSON.stringify(user));
  31. delete user.meta;
  32. delete user.__v;
  33. delete user.password;
  34. const token = this.ctx.service.util.jwt.encrypt(user);
  35. return token;
  36. }
  37. }
  38. module.exports = AdminService;