agentMech.js 1.0 KB

1234567891011121314151617181920212223242526
  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 AgentMechService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'agent_mech');
  10. this.model = this.ctx.model.Patent.AgentMech;
  11. }
  12. async login({ phone, password }) {
  13. const object = await this.model.findOne({ phone }, '+password');
  14. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
  15. const { password: op } = object;
  16. const { secret } = op;
  17. if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  18. const data = _.omit(JSON.parse(JSON.stringify(object)), ['meta', 'password', '__v']);
  19. const { secret: secrets } = this.config.jwt;
  20. const jwt = require('jsonwebtoken');
  21. const token = jwt.sign(data, secrets);
  22. return token;
  23. }
  24. }
  25. module.exports = AgentMechService;