trainLive.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. const { CrudService } = require("naf-framework-mongoose-free/lib/service");
  3. const { BusinessError, ErrorCode } = require("naf-core").Error;
  4. const { ObjectId } = require("mongoose").Types;
  5. const assert = require("assert");
  6. const _ = require("lodash");
  7. const jwt = require("jsonwebtoken");
  8. // 培训问诊表
  9. class TrainLiveService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, "trainlive");
  12. this.model = this.ctx.model.Consultation.TrainLive;
  13. }
  14. /**
  15. * 重写创建,room_id自动生成
  16. * @param {Object} body 数据
  17. */
  18. async create(body) {
  19. const last = await this.model.findOne().sort({ room_id: -1 });
  20. let room_id = "3001";
  21. if (last) room_id = parseInt(last.room_id) + 1;
  22. body.room_id = room_id;
  23. body.password = { secret: room_id };
  24. return await this.model.create(body);
  25. }
  26. /**
  27. * 展会管理者登陆
  28. * @param {Object} { room_id, password } 登陆参数
  29. * @property {String} room_id 房间号/电话
  30. * @property {String} password 密码
  31. */
  32. async login({ room_id, password }) {
  33. const object = await this.model.findOne({ room_id }, '+password');
  34. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到展会号为: ${room_id} 的展会`);
  35. const op = _.get(object, 'password.secret');
  36. if (!_.isEqual(op, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  37. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
  38. const { secret: jwts } = this.config.jwt;
  39. const token = jwt.sign(data, jwts);
  40. return token;
  41. }
  42. }
  43. module.exports = TrainLiveService;