123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- "use strict";
- const { CrudService } = require("naf-framework-mongoose-free/lib/service");
- const { BusinessError, ErrorCode } = require("naf-core").Error;
- const { ObjectId } = require("mongoose").Types;
- const assert = require("assert");
- const _ = require("lodash");
- const jwt = require("jsonwebtoken");
- // 培训问诊表
- class TrainLiveService extends CrudService {
- constructor(ctx) {
- super(ctx, "trainlive");
- this.model = this.ctx.model.Consultation.TrainLive;
- }
- /**
- * 重写创建,room_id自动生成
- * @param {Object} body 数据
- */
- async create(body) {
- const last = await this.model.findOne().sort({ room_id: -1 });
- let room_id = "3001";
- if (last) room_id = parseInt(last.room_id) + 1;
- body.room_id = room_id;
- body.password = { secret: room_id };
- return await this.model.create(body);
- }
- /**
- * 展会管理者登陆
- * @param {Object} { room_id, password } 登陆参数
- * @property {String} room_id 房间号/电话
- * @property {String} password 密码
- */
- async login({ room_id, password }) {
- const object = await this.model.findOne({ room_id }, '+password');
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到展会号为: ${room_id} 的展会`);
- const op = _.get(object, 'password.secret');
- if (!_.isEqual(op, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
- const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
- const { secret: jwts } = this.config.jwt;
- const token = jwt.sign(data, jwts);
- return token;
- }
- }
- module.exports = TrainLiveService;
|