levelSearch.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const _ = require('lodash');
  3. const mapping = [
  4. {
  5. table: 'company_base',
  6. keys: [{ key: 'tube_code', from: 'police_department', fromKey: 'num' }],
  7. },
  8. ];
  9. const checkQuery = async ctx => {
  10. const query = ctx.query;
  11. const { tube_code } = query;
  12. if (!tube_code) return;
  13. const table = ctx.params.table;
  14. const map = mapping.find(f => f.table === table);
  15. if (!table) return;
  16. const { keys } = map;
  17. const conf = keys.find(f => f.key === 'tube_code');
  18. if (!conf) return;
  19. const { from, fromKey } = conf;
  20. const deptRes = await ctx.service.fetch.index({ table: from }, { [fromKey]: tube_code });
  21. const { data: dept } = deptRes;
  22. // dept.type能区分是市局,分局,派出所,但是现在先不管
  23. const { id: branch_id } = dept;
  24. if (!branch_id) return;
  25. const deptsRes = await ctx.service.query.index({ table: from }, { branch_id });
  26. const { data: depts = [] } = deptsRes;
  27. const tube_company = depts.map(i => i.id);
  28. ctx.query.tube_company = tube_company;
  29. delete ctx.query.tube_code;
  30. };
  31. module.exports = options => {
  32. return async function levelSearch(ctx, next) {
  33. await checkQuery(ctx);
  34. await next();
  35. };
  36. };