12345678910111213141516171819202122232425262728293031323334353637 |
- 'use strict';
- const _ = require('lodash');
- const mapping = [
- {
- table: 'company_base',
- keys: [{ key: 'tube_code', from: 'police_department', fromKey: 'num' }],
- },
- ];
- const checkQuery = async ctx => {
- const query = ctx.query;
- const { tube_code } = query;
- if (!tube_code) return;
- const table = ctx.params.table;
- const map = mapping.find(f => f.table === table);
- if (!table) return;
- const { keys } = map;
- const conf = keys.find(f => f.key === 'tube_code');
- if (!conf) return;
- const { from, fromKey } = conf;
- const deptRes = await ctx.service.fetch.index({ table: from }, { [fromKey]: tube_code });
- const { data: dept } = deptRes;
- // dept.type能区分是市局,分局,派出所,但是现在先不管
- const { id: branch_id } = dept;
- if (!branch_id) return;
- const deptsRes = await ctx.service.query.index({ table: from }, { branch_id });
- const { data: depts = [] } = deptsRes;
- const tube_company = depts.map(i => i.id);
- ctx.query.tube_company = tube_company;
- delete ctx.query.tube_code;
- };
- module.exports = options => {
- return async function levelSearch(ctx, next) {
- await checkQuery(ctx);
- await next();
- };
- };
|