login.js 933 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. const _ = require('lodash');
  3. const assert = require('assert');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. // 登陆
  7. class LoginService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'login');
  10. this.model = this.ctx.model.Card;
  11. }
  12. async login({ mobile, password }) {
  13. assert(mobile, '请填写手机号');
  14. assert(password, '请填写密码');
  15. let user = await this.model.findOne({ mobile }, '+password');
  16. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '为找到该手机号的用户');
  17. user = JSON.parse(JSON.stringify(user));
  18. const { password: up, meta, __v, ...userInfo } = user;
  19. const { secret } = up;
  20. if (!_.isEqual(secret, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误!');
  21. return userInfo;
  22. }
  23. }
  24. module.exports = LoginService;