'use strict'; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const { ObjectId } = require('mongoose').Types; 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} id 数据id */ async delete({ id }) { await this.model.updateOne({ _id: id }, { status: '1' }); } /** * 解冻账号 * @param {Object} id 数据id */ async restore({ id }) { await this.model.updateOne({ _id: id }, { status: '0' }); } /** * 临时专家账号登录 * @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; // if (status !== '0') throw new BusinessError(ErrorCode.ACCESS_DENIED, '您的临时账号已被注销,如有疑问,请联系管理员'); 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; } async getExperts(ids) { const res = await this.model.find({ _id: ids }); return res; } /** * 获取可以生成临时账号的专家列表 */ async getExpertList() { // 1,去live项目中把所有专家整来 // 2,到achieve_expert表里,把可以使用的临时账号(status=0)的专家id都拿来 // 3.把1中2的结果去掉,返回 let expertList = []; const eres = await this.ctx.service.util.httpUtil.cpost('/spm', 'live', { service: 'users.expert', method: 'query' }, { method: 'useService' }); if (eres) expertList = eres.data; const have_accounts = await this.model.find({ status: 0 }, 'expert_user_id'); expertList = expertList.map(i => { const res = have_accounts.find(ae => ObjectId(ae.expert_user_id).equals(i.user_id)); if (res)i.cant_use = true; return i; }); return expertList; } } module.exports = Achieve_expertService;