'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError } = require('naf-core').Error; const moment = require('moment'); const jwt = require('jsonwebtoken'); class ChatService extends CrudService { constructor(ctx) { super(ctx, 'dock'); this.model = this.ctx.model.Dock; } // 创建登录Token async createJwtPwd(password) { const { secret } = this.config.jwt; const token = await jwt.sign(password, secret); return token; } async create(body) { // roomid与密码每次加一 const findroom = await this.model.find().sort({ room_id: -1 }); if (findroom.length > 0) { const room = (parseInt(findroom[0].room_id) + 1) + ''; // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const pas = await this.createJwtPwd(room); body.room_id = room; body.password = { secret: pas }; } else { // 将用户输入的密码进行加密并与查询到的用户数据密码相比对 const room = '1001'; const pas = await this.createJwtPwd(room); body.room_id = room; body.password = { secret: pas }; } const res = await this.model.create(body); return { ...JSON.parse(JSON.stringify(res)), password: res.password.secret, room_id: res.room_id }; } // 产品 async goods({ id }, body) { const apply = await this.model.findOne({ _id: ObjectId(id) }); if (!apply) { throw new BusinessError('没有查询到该申请用户'); } apply.goods.push({ ...body, // apply_time: moment().format('YYYY-MM-DD HH:mm:ss'), }); const res = await apply.save(); const info = _.last(res.goods); return info; } async apply({ id }, body) { const dock = await this.model.findOne({ _id: ObjectId(id) }); if (!dock) { throw new BusinessError('没有查询到该对接会'); } dock.apply.push({ ...body, apply_time: moment().format('YYYY-MM-DD HH:mm:ss'), }); const res = await dock.save(); const info = _.last(res.apply); return info; } async check({ id, dock_id }, { status }) { assert(status, '请审核是否允许参加对接会'); const dock = await this.model.findOne({ _id: ObjectId(dock_id) }); if (!dock) { throw new BusinessError('没有查询到该对接会'); } const user = dock.apply.id(id); if (!user) { throw new BusinessError('没有查询到用户的申请'); } user.status = status; const res = dock.save(); return res; } // async dockCheck({ id }, { is_allowed, reason = '' }) { // const dock = await this.model.findOne({ _id: ObjectId(id) }); // if (!dock) { // throw new BusinessError('没有查询到该对接会'); // } // assert(is_allowed, '请选择审核结果'); // dock.is_allowed = is_allowed; // dock.reason = reason; // const res = await dock.save(); // return res; // } // 根据申请人id查询所有申请的对接会 async myapply({ user_id, status, skip, limit }) { const total = await this.model.count({ status, 'apply.user_id': user_id }); const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit)); const result = { total, data }; return result; } async updatevipuser({ id }, info) { const dock = await this.model.findById(id); if (!dock) { throw new BusinessError('没有查询到该对接会'); } const vipuser = await dock.vipuser.id(info.vipid); if (!vipuser) { throw new BusinessError('没有查询到vip用户'); } if (info.vipname) { vipuser.vipname = info.vipname; } if (info.vipphone) { vipuser.vipphone = info.vipphone; } if (info.role) { vipuser.role = info.role; } if (info.company) { vipuser.company = info.company; } if (info.email) { vipuser.email = info.email; } if (info.content) { vipuser.content = info.content; } return await dock.save(); } async createvipuser({ id }, info) { const dock = await this.model.findById(id); if (!dock) { throw new BusinessError('没有查询到该对接会'); } const vipuser = {}; vipuser.vipname = info.vipname; vipuser.vipphone = info.vipphone; vipuser.role = info.role; vipuser.company = info.company; vipuser.email = info.email; vipuser.content = info.content; dock.vipuser.push(vipuser); return await dock.save(); } } module.exports = ChatService;