12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const { ObjectId } = require('mongoose').Types;
- const _ = require('lodash');
- const assert = require('assert');
- const jwt = require('jsonwebtoken');
- // 展会
- class IndexService extends CrudService {
- constructor(ctx) {
- super(ctx, 'index');
- this.model = this.ctx.model.Dock;
- this.dockUser = this.ctx.model.DockUser;
- }
- async create(body) {
- const last = await this.model.findOne().sort({ room_id: -1 });
- let room_id = 1001;
- if (last) room_id = last.room_id + 1;
- body.room_id = room_id;
- body.password = { secret: room_id };
- const { vipuser } = body;
- if (_.isArray(vipuser) && vipuser.length > 0) {
- body.vipuser = vipuser.map(i => {
- const { password, vipname } = i;
- if (password) {
- i.password = { secret: password };
- } else {
- i.password = vipname;
- }
- return i;
- });
- }
- return await this.model.create(body);
- }
- /**
- * 展会管理者登陆
- * @param {Object} { room_phone, password } 登陆参数
- * @property {String} room_phone 房间号/电话
- * @property {String} password 密码
- */
- async login({ room_phone, password }) {
- const object = await this.model.findOne({ room_id: room_phone }, '+password');
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未找到展会号为: ${room_phone} 的展会`);
- const op = _.get(object, 'password.secret');
- if (!_.isEqual(op, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
- const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'passwd', '__v' ]);
- const { secret: jwts } = this.config.jwt;
- const token = jwt.sign(data, jwts);
- return token;
- }
- /**
- * vip用户登陆
- * @param {Obejct} { id, room_phone, password } vip登陆信息
- * @property {String} id 展会id
- * @property {String} room_phone 房间号/电话
- * @property {String} password 密码
- */
- async vipLogin({ id, room_phone, password }) {
- const object = await this.model.findOne({ _id: ObjectId(id) }, '+vipuser.password');
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到展会');
- const { vipuser } = object;
- const user = vipuser.find(f => _.isEqual(f.vipphone, room_phone));
- if (!user) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, `未在${object.room_id}展会中找到手机号为 ${room_phone} 的vip`);
- const op = _.get(user, 'password.secret');
- if (!_.isEqual(op, password)) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
- const data = {
- name: _.get(user, 'vipname'),
- phone: _.get(user, 'vipphone'),
- remark: id,
- };
- const { secret: jwts } = this.config.jwt;
- const token = jwt.sign(data, jwts);
- return token;
- }
- async dockProduct({ dock_id, type, field, skip = 0, limit = 10 } = {}) {
- assert(dock_id, '缺少展会信息');
- assert(type, '要查询的类型');
- const query = { dockStatus: '1' };
- if (field) query.field = field;
- const res = await this.dockUser.aggregate([
- { $match: { dock_id: ObjectId(dock_id), 'goodsList.type': type, 'goodsList.dockStatus': '1' } },
- { $project: { goodsList: 1 } },
- { $unwind: '$goodsList' },
- ]);
- const list = res.map(i => i.goodsList);
- return { data: _.slice(list, parseInt(skip), parseInt(skip) + parseInt(limit)), total: list.length };
- }
- }
- module.exports = IndexService;
|