'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 ChannelService extends CrudService { constructor(ctx) { super(ctx, 'channel'); this.model = this.ctx.model.Channel; } async create(body) { const last = await this.model.findOne().sort({ room_id: -1 }); let room_id = 2001; if (last) room_id = last.room_id + 1; body.room_id = room_id; body.passwd = { secret: room_id }; return await this.model.create(body); } /** * 管理者登陆 * @param {Object} { room_id, passwd } 登陆参数 * @property {String} room_id 房间号 * @property {String} passwd 密码 */ async login({ room_id, passwd }) { const object = await this.model.findOne({ room_id }, '+passwd'); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到展会号为: ${room_id} 的展会`); const op = _.get(object, 'passwd.secret'); if (!_.isEqual(op, passwd)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误'); const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'passwd', '__v' ]); const { secret: jwts } = this.config.jwt; const token = jwt.sign(data, jwts); return token; } } module.exports = ChannelService;