'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const jwt = require('jsonwebtoken'); // 展会vip class DockVipService extends CrudService { constructor(ctx) { super(ctx, 'dock_vip'); this.model = this.ctx.model.Dock.DockVip; this.dock = this.ctx.model.Dock.Dock; } async create(data) { const { dock_id } = data; const dock = await this.dock.findById(dock_id); if (!dock) new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定展会!'); const { room_id } = dock; data.password = { secret: room_id }; return await this.model.create(data); } /** * 展会管理者登陆 * @param {Object} { dock_id } 展会id * @param {Object} { phone, password } 登陆参数 * @property {String} phone 电话 * @property {String} password 密码 */ async login({ dock_id }, { phone, password }) { const object = await this.model.findOne({ phone, dock_id }, '+password'); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到vip用户'); 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', 'password', '__v' ]); const { secret: jwts } = this.config.jwt; const token = jwt.sign(data, jwts); return token; } } module.exports = DockVipService;