lrf 11 miesięcy temu
rodzic
commit
229d4b31ee
1 zmienionych plików z 23 dodań i 7 usunięć
  1. 23 7
      src/service/proxy.service.ts

+ 23 - 7
src/service/proxy.service.ts

@@ -1,7 +1,15 @@
 import { App, Config, Inject, InjectClient, Provide } from '@midwayjs/core';
 import { Application, Context } from '@midwayjs/koa';
 import { RequestBase } from '../interface/proxy.interface';
-import { get, isObject, isString, lowerCase, omit, pick } from 'lodash';
+import {
+  get,
+  isEqual,
+  isObject,
+  isString,
+  lowerCase,
+  omit,
+  pick,
+} from 'lodash';
 import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
 import { PemService } from './pem.service';
 @Provide()
@@ -43,7 +51,13 @@ export class ProxyService {
       'ip',
       'originalUrl',
     ]) as RequestBase;
-
+    // 加密后的长度与解密后的长度不一致,会导致服务无法解析
+    delete result.header['content-length'];
+    if (this.useCrypto) {
+      if (result.body && result.rawBody) {
+        result.body = JSON.parse(result.rawBody);
+      }
+    }
     return result;
   }
   /**
@@ -70,7 +84,7 @@ export class ProxyService {
       break;
     }
     if (!url) return;
-    const reqConfig: any = {
+    let reqConfig: any = {
       url,
       method: rb.method,
       headers: rb.header,
@@ -78,15 +92,17 @@ export class ProxyService {
     if (lowerCase(rb.method) === 'post') {
       // 如果是post函数,且 不属于开发模式,则解密函数体.否则不用解密
       if (this.useCrypto) {
-        let preBody;
-        if (isString(rb.body)) preBody = rb.body;
-        else if (isObject(rb.body)) preBody = JSON.stringify(rb.body);
+        const preBody = get(rb.body, 'data');
         const deBody = this.pemService.decrypt(preBody);
-        reqConfig.data = deBody;
+        if (deBody) {
+          reqConfig.data = JSON.parse(deBody);
+          delete reqConfig.rawBody;
+        }
       } else {
         reqConfig.data = rb.body;
       }
     }
+    reqConfig = JSON.parse(JSON.stringify(reqConfig));
     const res = await this.serviceAxios.request(reqConfig);
     if (res.status !== 200) throw new Error('proxy service request error');
     const result = res.data;