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