12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class ChannelService extends CrudService {
- constructor(ctx) {
- super(ctx, 'channel');
- this.model = this.ctx.model.Channel;
- }
- async create(data) {
- const { user_id, title, orgin, type } = data;
- assert(user_id, '缺少创建人信息');
- assert(title, '缺少标题');
- assert(orgin, '缺少来源');
- assert(type, '缺少类别');
- const last = await this.model.findOne().sort({ room_id: -1 });
- if (last) {
- const { room_id } = last;
- if (parseInt(room_id)) data.room_id = `${parseInt(room_id) + 1}`;
- else data.room_id = `${room_id}1`;
- } else {
- data.room_id = '2001';
- }
- data.passwd = { secret: data.room_id };
- let res = await this.model.create(data);
- if (res) {
- res = JSON.parse(JSON.stringify(res));
- res.passwd = data.room_id;
- console.log(res);
- }
- return res;
- }
- async login(data) {
- const { room_id, passwd } = data;
- assert(room_id, '请填写房间号');
- assert(passwd, '请填写密码');
- let channel = await this.model.findOne({ room_id }, '+passwd');
- if (!channel) throw new BusinessError(ErrorCode.USER_NOT_EXIST, '房间号不存在');
- const secret = _.get(channel, 'passwd.secret');
- if (!secret) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '房间号密码缺失');
- if (passwd === secret) {
- channel = JSON.parse(JSON.stringify(channel));
- channel = _.omit(channel, [ 'passwd' ]);
- return channel;
- } throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
- }
- }
- module.exports = ChannelService;
|