1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- '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} 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 }, '+password');
- if (!expert) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到您的临时账号,如有疑问,请联系管理员');
- const { password: op, status } = 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;
- }
- }
- module.exports = Achieve_expertService;
|