'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'); // 科教之旅机构表 class KjzlMediumService extends CrudService { constructor(ctx) { super(ctx, 'kjzl_medium'); this.model = this.ctx.model.Kjzl.KjzlMedium; } /** * 创建用户 * @param {Object} params 用户信息 */ async create({ password, ...data }) { data.password = { secret: password }; return await this.model.create(data); } /** * 修改密码 * @param {Object} {id,password} 用户id和密码 */ async password({ id, password }) { const object = await this.model.findById(id); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); object.password = { secret: password }; await object.save(); } /** * 登陆 * @param {Object} params 登陆信息 * @property phone 手机号 * @property password 密码 */ async login({ phone, password }) { const object = await this.model.findOne({ phone }, '+password'); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); const { password: op } = object; const { secret } = op; if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误'); const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]); const { secret: secrets } = this.config.jwt; const token = jwt.sign(data, secrets); return token; } } module.exports = KjzlMediumService;