'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const jwt = require('jsonwebtoken'); // 临时专家 class Achieve_expertService extends CrudService { constructor(ctx) { super(ctx, 'achieve_expert'); this.model = this.ctx.model.AchieveExpert; } async create(data) { const { password } = data; data.password = { secret: password }; const res = await this.model.create(data); return res; } /** * 专家临时账号修改(包括评分与意见) * @param {Object} id 专家临时账号数据id * @param {Object} data 要修改的内容 */ async update({ id }, data) { const { password } = data; const older = await this.model.findById(id); if (!older) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到专家信息'); // 此处是检验专家是否可以进行修改 const { status } = older; if (status === '1') throw new BusinessError(ErrorCode.SERVICE_FAULT, '您的工作已完成,若有问题请联系平台管理员!'); if (password) { data.password = { secret: password }; } await this.model.findByIdAndUpdate(id, data); return await this.model.findById(id); } /** * 临时专家账号登录 * @param {Object} params 手机号,密码 */ async login({ phone, password }) { const expert = await this.model.findOne({ phone, status: '0' }, '+password'); if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '您不存在未完成的工作,无需登录'); const { password: op } = expert; const { secret } = op; if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误'); const data = _.omit(JSON.parse(JSON.stringify(expert)), [ 'expert_name', 'meta', 'password', '__v', 'verify' ]); const { secret: secrets } = this.config.jwt; data.name = _.get(expert, 'expert_name'); data.role = '3'; const token = jwt.sign(data, secrets); return token; } } module.exports = Achieve_expertService;