trainLive.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 = 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)
  35. throw new BusinessError(
  36. ErrorCode.DATA_NOT_EXIST,
  37. `未找到展会号为: ${room_id} 的展会`
  38. );
  39. const op = _.get(object, "password.secret");
  40. if (!_.isEqual(op, password))
  41. throw new BusinessError(ErrorCode.BAD_PASSWORD, "密码错误");
  42. const data = _.omit(JSON.parse(JSON.stringify(object)), [
  43. "meta",
  44. "password",
  45. "__v",
  46. ]);
  47. const { secret: jwts } = this.config.jwt;
  48. const token = jwt.sign(data, jwts);
  49. return token;
  50. }
  51. }
  52. module.exports = TrainLiveService;