index.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const { ObjectId } = require('mongoose').Types;
  5. const _ = require('lodash');
  6. const assert = require('assert');
  7. const jwt = require('jsonwebtoken');
  8. // 展会
  9. class IndexService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'index');
  12. this.model = this.ctx.model.Dock;
  13. this.dockUser = this.ctx.model.DockUser;
  14. }
  15. async create(body) {
  16. const last = await this.model.findOne().sort({ room_id: -1 });
  17. let room_id = 1001;
  18. if (last) room_id = last.room_id + 1;
  19. body.room_id = room_id;
  20. body.password = { secret: room_id };
  21. const { vipuser } = body;
  22. if (_.isArray(vipuser) && vipuser.length > 0) {
  23. body.vipuser = vipuser.map(i => {
  24. const { password, vipname } = i;
  25. if (password) {
  26. i.password = { secret: password };
  27. } else {
  28. i.password = vipname;
  29. }
  30. return i;
  31. });
  32. }
  33. return await this.model.create(body);
  34. }
  35. /**
  36. * 展会管理者登陆
  37. * @param {Object} { room_phone, password } 登陆参数
  38. * @property {String} room_phone 房间号/电话
  39. * @property {String} password 密码
  40. */
  41. async login({ room_phone, password }) {
  42. const object = await this.model.findOne({ room_id: room_phone }, '+password');
  43. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到展会号为: ${room_phone} 的展会`);
  44. const op = _.get(object, 'password.secret');
  45. if (!_.isEqual(op, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  46. const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'passwd', '__v' ]);
  47. const { secret: jwts } = this.config.jwt;
  48. const token = jwt.sign(data, jwts);
  49. return token;
  50. }
  51. /**
  52. * vip用户登陆
  53. * @param {Obejct} { id, room_phone, password } vip登陆信息
  54. * @property {String} id 展会id
  55. * @property {String} room_phone 房间号/电话
  56. * @property {String} password 密码
  57. */
  58. async vipLogin({ id, room_phone, password }) {
  59. const object = await this.model.findOne({ _id: ObjectId(id) }, '+vipuser.password');
  60. if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到展会');
  61. const { vipuser } = object;
  62. const user = vipuser.find(f => _.isEqual(f.vipphone, room_phone));
  63. if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未在${object.room_id}展会中找到手机号为 ${room_phone} 的vip`);
  64. const op = _.get(user, 'password.secret');
  65. if (!_.isEqual(op, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
  66. const data = {
  67. name: _.get(user, 'vipname'),
  68. phone: _.get(user, 'vipphone'),
  69. remark: id,
  70. };
  71. const { secret: jwts } = this.config.jwt;
  72. const token = jwt.sign(data, jwts);
  73. return token;
  74. }
  75. async dockProduct({ dock_id, type, field, skip = 0, limit = 10 } = {}) {
  76. assert(dock_id, '缺少展会信息');
  77. assert(type, '要查询的类型');
  78. const query = { dockStatus: '1' };
  79. if (field) query.field = field;
  80. const res = await this.dockUser.aggregate([
  81. { $match: { dock_id: ObjectId(dock_id), 'goodsList.type': type, 'goodsList.dockStatus': '1' } },
  82. { $project: { goodsList: 1 } },
  83. { $unwind: '$goodsList' },
  84. ]);
  85. const list = res.map(i => i.goodsList);
  86. return { data: _.slice(list, parseInt(skip), parseInt(skip) + parseInt(limit)), total: list.length };
  87. }
  88. }
  89. module.exports = IndexService;