'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 ReviewExpertService extends CrudService { constructor(ctx) { super(ctx, 'reviewexpert'); this.model = this.ctx.model.ReviewExpert; } /** * 登陆 * @param {Object} params 登陆信息 * @property phone 手机号 * @property password 密码 */ async login({ phone, password }) { const object = await this.model.findOne({ 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; } /** * 修改密码 * @param {Object} {id,password} 用户id和密码 */ async password({ id, password }) { const object = await this.model.findById(id); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); object.password = { secret: password }; await object.save(); } } module.exports = ReviewExpertService;