'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 jwt = require('jsonwebtoken'); // 管理员 class AdminService extends CrudService { constructor(ctx) { super(ctx, 'admin'); this.model = this.ctx.model.Admin; this.util = this.ctx.service.util.util; this.inviteCode = this.ctx.model.InviteCode; } async query(query, { skip = 0, limit = 0, ...other } = {}) { query = this.util.turnDateRangeQuery(this.util.turnFilter(query)); let data = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit)); if (data.length > 0) data = JSON.parse(JSON.stringify(data)); const menus = await this.ctx.model.Menu.find(); data = data.map(i => { const um = menus.filter(f => i.menus.find(mf => ObjectId(mf).equals(f._id))); i.menus = um; return i; }); return data; } async fetch({ id }) { let object = await this.model.findById(id); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); object = JSON.parse(JSON.stringify(object)); const menus = await this.ctx.model.Menu.find({ _id: object.menus.map(i => ObjectId(i)) }).sort({ sort: 1 }); object.menus = menus; return object; } /** * 创建用户 * @param {Object} params 用户信息 */ async create({ passwd, ...data }) { data.passwd = { secret: passwd }; const { phone, pid, code } = data; // 检查手机号 let num = await this.model.count({ phone }); if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有管理员/机构管理员/业务管理员 使用该手机号'); // 检查上级id try { num = await this.model.count({ _id: ObjectId(pid) }); if (num <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到上级的信息'); } catch (error) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到上级的信息'); } const res = await this.model.create(data); const icdata = { user_id: res._id, code, user_name: res.name }; try { const res = await this.inviteCode.create(icdata); } catch (error) { await this.model.deleteOne({ _id: res._id }); throw new BusinessError(ErrorCode.DATABASE_FAULT, '该邀请码已被使用,请重新更换邀请码'); } return res; } /** * 修改密码 * @param {Object} {id,passwd} 用户id和密码 */ async password({ id, passwd }) { const object = await this.model.findById(id); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); object.passwd = { secret: passwd }; await object.save(); } /** * 管理员登陆 * @param {Object} params 登陆信息 * @property code_phone code或者是phone * @property passwd 密码 */ async login({ code_phone, passwd }) { const object = await this.model.findOne({ $or: [{ code: code_phone }, { phone: code_phone }] }, '+passwd'); if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息'); const { passwd: op } = object; const { secret } = op; if (secret !== passwd) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误'); const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'passwd', '__v' ]); const { secret: secrets } = this.config.jwt; const menus = await this.ctx.model.Menu.find({ _id: object.menus.map(i => ObjectId(i)) }).sort({ sort: 1 }); data.menus = menus; const token = jwt.sign(data, secrets); return token; } } module.exports = AdminService;