123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const jwt = require('jsonwebtoken');
- const assert = require('assert');
- // 机构
- class OrganizationService extends CrudService {
- constructor(ctx) {
- super(ctx, 'organization');
- this.redis = this.app.redis;
- this.model = this.ctx.model.User.Organization;
- this.adminModel = this.ctx.model.User.Admin;
- this.util = this.ctx.service.util.util;
- }
- async query(condition, { skip = 0, limit = 0 }) {
- const query = await this.dealQueryCondition(_.cloneDeep(condition));
- const res = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit))
- .sort({ 'meta.createdAt': -1 });
- return res;
- }
- async count(condition) {
- const query = await this.dealQueryCondition(_.cloneDeep(condition));
- const res = await this.model.count(query);
- return res;
- }
- async dealQueryCondition({ role, code, ...condition } = {}) {
- condition = this.util.dealQuery(condition);
- // 查询业务管理
- const busFind = async query => await this.adminModel.find({ ...query, role: '3' }, { code: 1 });
- // 查询机构管理
- const orgFind = async query => await this.adminModel.find({ ...query, role: '2' }, { code: 1 });
- // 查询管理员
- const aFind = async query => await this.adminModel.find({ ...query, role: '1' }, { code: 1 });
- if (role === '1' && code) {
- // 管理员查询
- // =>获取该code下的机构管理员列表 => 用机构管理员id 获取业务管理员列表 => 将code都整理出来作为查询条件
- const a = await aFind({ code });
- if (a.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该管理员');
- const aid = _.get(_.head(a), '_id');
- const orgList = await orgFind({ pid: aid });
- const busList = await busFind({ pid: orgList.map(i => i._id) });
- const codes = [ ...orgList.map(i => i.code), ...busList.map(i => i.code), code ];
- condition.code = codes;
- } else if (role === '2' && code) {
- // 机构查询
- // =>获取该code下的业务管理员列表 => 将code都整理出来作为查询条件
- const o = await orgFind({ code });
- if (o.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该机构');
- const oid = _.get(_.head(o), '_id');
- const busList = await busFind({ pid: oid });
- const codes = [ ...busList.map(i => i.code), code ];
- condition.code = codes;
- } else if (code) {
- // 业务查询
- // code直接查询用户返回即可
- condition.code = code;
- }
- // 没有code,超级管理员,说明不限制
- return condition;
- }
- /**
- * 创建用户
- * @param {Object} params 用户信息
- */
- async create({ password, ...data }) {
- data.password = { secret: password };
- const { institution_code } = data;
- // 检查是否重复
- const num = await this.model.count({ institution_code, isdel: '0' });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已有个机构使用该 统一社会信用代码');
- 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, isdel: '0' }, '+password');
- if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
- const { password: op, status } = object;
- const { secret } = op;
- if (status !== '1') throw new BusinessError(ErrorCode.ACCESS_DENIED, '拒绝访问!');
- 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);
- // 记录登陆
- // let number = (await this.redis.get('login_number')) || 0;
- // number++;
- // await this.redis.set('login_number', number);
- return token;
- }
- // async delete({ id }) {
- // const object = await this.model.findById(id);
- // if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
- // object.isdel = '1';
- // await object.save();
- // }
- /**
- * 用手机号获取企业列表
- * @param {Object} query phone:电话号
- * @param {Object} options skip;limit
- */
- async getList({ name, phone }, { skip = 0, limit = 0 } = {}) {
- assert(phone, '缺少手机号');
- const query = { phone, status: '1' };
- if (name) query.name = name;
- const list = await this.model.find(query).skip(parseInt(skip)).limit(parseInt(limit));
- return list;
- }
- /**
- * 企业绑定微信
- * @param {Object} body
- * @property id 企业id
- * @property openid 微信openid
- */
- async bind({ id, openid }) {
- await this.bindRemove({ openid });
- const org = await this.model.findById(id);
- if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
- org.openid = openid;
- await org.save();
- return await org.save();
- }
- /**
- * 解除绑定
- * @param {Object} body
- * @property id 企业id
- * @property openid 微信id
- * 两种方式:id=>指定企业的openid解绑;openid=>删除所有企业的该openid
- */
- async bindRemove({ id, openid }) {
- if (id) {
- const org = await this.model.findById(id);
- if (!org) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定企业');
- org.openid = undefined;
- return await org.save();
- }
- const res = await this.model.updateMany({ openid }, { openid: undefined });
- return res;
- }
- /**
- * 企业微信登录
- * @param {Object} body post body
- * @property openid
- */
- async wxLogin({ openid }) {
- const org = await this.model.findOne({ openid });
- if (!org) return org;
- const data = _.omit(JSON.parse(JSON.stringify(org)), [ 'meta', 'password', '__v' ]);
- const { secret: secrets } = this.config.jwt;
- const token = jwt.sign(data, secrets);
- return token;
- }
- }
- module.exports = OrganizationService;
|