request.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const { CrudController } = require('naf-framework-mongoose/lib/controller');
  4. const dealList = require('../util/deal-list');
  5. // 请求处理
  6. class RequestController extends Controller {
  7. constructor(ctx) {
  8. super(ctx);
  9. this.util = this.ctx.service.util.util;
  10. this.httpUtil = this.ctx.service.util.httpUtil;
  11. }
  12. async index() {
  13. const r = this.checkList();
  14. if (r) {
  15. // 纯人工处理
  16. const { service, method } = r;
  17. const res = await this.ctx.service[service][method]();
  18. this.ctx.body = res;
  19. } else {
  20. const { url, body, method } = this.util.analyzeUrl();
  21. // query和url已经合并,没有query了,所以直接body,get方法第二个位置不需要了,也不会传值来(传来就是找茬)
  22. // create,post,delete都有可能传,而且第二个位置都是body
  23. const res = await this.httpUtil[`$${method}`](url, body);
  24. // TODO,此处应该设置再处理名单,若在名单中,则继续处理数据,否则透传
  25. this.ctx.body = res;
  26. // this.ctx.ok();
  27. }
  28. }
  29. /**
  30. * 检查是否是纯人工处理路由
  31. */
  32. checkList() {
  33. const { url } = this.ctx.request;
  34. let obj;
  35. for (const i of dealList) {
  36. const { uri, service, method } = i;
  37. if (url.includes(uri)) {
  38. obj = { service, method };
  39. break;
  40. }
  41. }
  42. return obj;
  43. }
  44. }
  45. module.exports = CrudController(RequestController, {});