1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- 'use strict';
- const Controller = require('egg').Controller;
- const { CrudController } = require('naf-framework-mongoose/lib/controller');
- const dealList = require('../util/deal-list');
- // 请求处理
- class RequestController extends Controller {
- constructor(ctx) {
- super(ctx);
- this.util = this.ctx.service.util.util;
- this.httpUtil = this.ctx.service.util.httpUtil;
- }
- async index() {
- const r = this.checkList();
- if (r) {
- // 纯人工处理
- const { service, method } = r;
- const res = await this.ctx.service[service][method]();
- this.ctx.body = res;
- } else {
- const { url, body, method } = this.util.analyzeUrl();
- // query和url已经合并,没有query了,所以直接body,get方法第二个位置不需要了,也不会传值来(传来就是找茬)
- // create,post,delete都有可能传,而且第二个位置都是body
- const res = await this.httpUtil[`$${method}`](url, body);
- // TODO,此处应该设置再处理名单,若在名单中,则继续处理数据,否则透传
- this.ctx.body = res;
- // this.ctx.ok();
- }
- }
- /**
- * 检查是否是纯人工处理路由
- */
- checkList() {
- const { url } = this.ctx.request;
- let obj;
- for (const i of dealList) {
- const { uri, service, method } = i;
- if (url.includes(uri)) {
- obj = { service, method };
- break;
- }
- }
- return obj;
- }
- }
- module.exports = CrudController(RequestController, {});
|