1234567891011121314151617181920212223242526 |
- 'use strict';
- const _ = require('lodash');
- const assert = require('assert');
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- // 登陆
- class LoginService extends CrudService {
- constructor(ctx) {
- super(ctx, 'login');
- this.model = this.ctx.model.Card;
- }
- async login({ mobile, password }) {
- assert(mobile, '请填写手机号');
- assert(password, '请填写密码');
- let user = await this.model.findOne({ mobile }, '+password');
- if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '为找到该手机号的用户');
- user = JSON.parse(JSON.stringify(user));
- const { password: up, meta, __v, ...userInfo } = user;
- const { secret } = up;
- if (!_.isEqual(secret, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误!');
- return userInfo;
- }
- }
- module.exports = LoginService;
|