dock.js 1.4 KB

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