|
@@ -0,0 +1,70 @@
|
|
|
+'use strict';
|
|
|
+const _ = require('lodash');
|
|
|
+
|
|
|
+const findUserByCode = async (ctx, code) => {
|
|
|
+ const { code: qcode } = ctx.query;
|
|
|
+ const http = ctx.service.util.httpUtil;
|
|
|
+ const basePrefix = ctx.app.config.httpPrefix.base;
|
|
|
+ console.log('code ? code : qcode');
|
|
|
+ console.log(code);
|
|
|
+ console.log(qcode);
|
|
|
+ console.log(code ? code : qcode);
|
|
|
+ const res = await http.cget(`${basePrefix}/personal`, { code: code ? code : qcode });
|
|
|
+ if (res) return res;
|
|
|
+};
|
|
|
+
|
|
|
+const findOrgByCode = async ctx => {
|
|
|
+ const { code } = ctx.query;
|
|
|
+ const http = ctx.service.util.httpUtil;
|
|
|
+ const basePrefix = ctx.app.config.httpPrefix.base;
|
|
|
+ const adminRes = await http.cget(`${basePrefix}/admin`, { code });
|
|
|
+ if (!adminRes) return [];
|
|
|
+ const ids = [];
|
|
|
+ for (const r of adminRes) {
|
|
|
+ const pr = await http.cget(`${basePrefix}/admin`, { role: '2', pid: r._id });
|
|
|
+ const list = pr.map(i => i.code);
|
|
|
+ ids.push(...list);
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+};
|
|
|
+
|
|
|
+const role2Search = async (ctx, code) => {
|
|
|
+ // target:查询和 code有关的平台用户
|
|
|
+ const res = await findUserByCode(ctx, code);
|
|
|
+ const user_ids = res.map(i => i._id);
|
|
|
+ return user_ids;
|
|
|
+};
|
|
|
+
|
|
|
+const role1Search = async ctx => {
|
|
|
+ // target:查询和code有关的平台用户 和 与code有关的机构用户下的平台用户
|
|
|
+ let res = [];
|
|
|
+ // p1: 查询平台用户
|
|
|
+ const p1 = await role2Search(ctx);
|
|
|
+ res.push(...p1);
|
|
|
+ // p2:和code有关的机构用户下的平台用户
|
|
|
+ const orgCodes = await findOrgByCode(ctx);
|
|
|
+ for (const code of orgCodes) {
|
|
|
+ console.log(code);
|
|
|
+ const l = await role2Search(ctx, code);
|
|
|
+ res.push(...l);
|
|
|
+ }
|
|
|
+ res = _.uniq(res);
|
|
|
+ return res;
|
|
|
+};
|
|
|
+
|
|
|
+module.exports = options => {
|
|
|
+ return async function patentQuery(ctx, next) {
|
|
|
+ console.log('function in patentquery middleware');
|
|
|
+ const query = ctx.query;
|
|
|
+ const { code, role } = query;
|
|
|
+ if (code && role) {
|
|
|
+ let res;
|
|
|
+ if (role === '1') res = await role1Search(ctx);
|
|
|
+ else res = await role2Search(ctx);
|
|
|
+ delete ctx.query.code;
|
|
|
+ delete ctx.query.role;
|
|
|
+ ctx.query.user_id = { $elemMatch: { user_id: { $in: res } } };
|
|
|
+ }
|
|
|
+ await next();
|
|
|
+ };
|
|
|
+};
|