'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const assert = require('assert'); 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.User.Admin; this.util = this.ctx.service.util.util; this.inviteCode = this.ctx.model.User.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.System.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.System.Menu.find({ _id: object.menus.map(i => ObjectId(i)) }).sort({ sort: 1 }); // // object.menus = menus; // return object; // } /** * 创建用户 * @param {Object} params 用户信息 */ async create({ password, ...data }) { data.password = { secret: password }; 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; } async delete({ id }) { const data = await this.model.findOne({ _id: ObjectId(id) }); if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到数据'); const { code } = data; const res = await this.model.deleteOne({ _id: ObjectId(id) }); await this.inviteCode.deleteOne({ code }); return res; } /** * 修改密码 * @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 code_phone code或者是phone * @property password 密码 */ async login({ code_phone, password }) { const object = await this.model.findOne({ $or: [{ code: code_phone }, { phone: code_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 menus = await this.ctx.model.System.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;