'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const jwt = require('jsonwebtoken'); // 培训问诊用户 class TrainUserService extends CrudService { constructor(ctx) { super(ctx, 'trainuser'); this.model = this.ctx.model.Consultation.TrainUser; } /** * 登陆 * @param {Object} params 登陆信息 * @property phone 电话 * @property password 密码 * @property train_id 培训问诊id */ async login({ phone, password, train_id }) { const object = await this.model.findOne({ train_id, phone }, '+password'); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); const { password: op, status } = object; const { secret } = op; if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!'); if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误'); const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]); const { secret: secrets } = this.config.jwt; const token = jwt.sign(data, secrets); return token; } } module.exports = TrainUserService;