|
@@ -3,10 +3,7 @@ import isNil from 'ramda/es/isNil';
|
|
|
import globalConfig from '@/config';
|
|
import globalConfig from '@/config';
|
|
|
import { CompressMethod } from '@/config/types';
|
|
import { CompressMethod } from '@/config/types';
|
|
|
// import { bytesBase64decode } from '@/utils/crypto';
|
|
// import { bytesBase64decode } from '@/utils/crypto';
|
|
|
-import {
|
|
|
|
|
- decryptResponsePayload,
|
|
|
|
|
- encryptRequestPayload,
|
|
|
|
|
-} from '@/utils/requestCrypto';
|
|
|
|
|
|
|
+import { decryptResponsePayload, encryptRequestPayload } from '@/utils/requestCrypto';
|
|
|
import { bytesToString, stringToBytes } from '@/utils/bytesUtils';
|
|
import { bytesToString, stringToBytes } from '@/utils/bytesUtils';
|
|
|
import { currentUnixTimestamp } from '@/utils/timeUtils';
|
|
import { currentUnixTimestamp } from '@/utils/timeUtils';
|
|
|
import {
|
|
import {
|
|
@@ -14,6 +11,7 @@ import {
|
|
|
IResponseInterceptor,
|
|
IResponseInterceptor,
|
|
|
RequestConfig,
|
|
RequestConfig,
|
|
|
} from '@/utils/request/types';
|
|
} from '@/utils/request/types';
|
|
|
|
|
+import { bytesBase64encode } from '@/utils/crypto';
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 请求数据加密拦截器
|
|
* 请求数据加密拦截器
|
|
@@ -54,8 +52,6 @@ export const requestEncryptionInterceptor: IRequestInterceptorAxios = async (con
|
|
|
|
|
|
|
|
config.data = config.data ?? {}; // 如果请求体为空,则设置为空对象(后端需要非空请求体)
|
|
config.data = config.data ?? {}; // 如果请求体为空,则设置为空对象(后端需要非空请求体)
|
|
|
if (['post', 'put'].includes(config.method?.toLowerCase() || '')) {
|
|
if (['post', 'put'].includes(config.method?.toLowerCase() || '')) {
|
|
|
- console.log('[requestEncryptionInterceptor] config:', config);
|
|
|
|
|
-
|
|
|
|
|
// 设置 Content-Type 为 application/octet-stream
|
|
// 设置 Content-Type 为 application/octet-stream
|
|
|
config.headers['Content-Type'] = 'application/octet-stream';
|
|
config.headers['Content-Type'] = 'application/octet-stream';
|
|
|
|
|
|
|
@@ -70,6 +66,12 @@ export const requestEncryptionInterceptor: IRequestInterceptorAxios = async (con
|
|
|
compressMethod as CompressMethod
|
|
compressMethod as CompressMethod
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
|
|
+ console.log(
|
|
|
|
|
+ `⬆️ 🔐 [requestEncryptionInterceptor] api (${config.url}) request data encrypted,`,
|
|
|
|
|
+ `timestamp: ${timestamp},`,
|
|
|
|
|
+ `data: ${bytesBase64encode(encrypted)}`
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
// 设置请求体
|
|
// 设置请求体
|
|
|
config.data = encrypted.buffer;
|
|
config.data = encrypted.buffer;
|
|
|
}
|
|
}
|
|
@@ -111,21 +113,22 @@ export const responseDecryptionInterceptor: IResponseInterceptor = async (respon
|
|
|
keyBytes,
|
|
keyBytes,
|
|
|
compressMethod as CompressMethod
|
|
compressMethod as CompressMethod
|
|
|
);
|
|
);
|
|
|
-
|
|
|
|
|
|
|
+ console.log(
|
|
|
|
|
+ `⬇️ 🔓 [responseDecryptionInterceptor] api (${response.config.url}) response data decrypted,`,
|
|
|
|
|
+ `timestamp: ${decrypted?.timestamp},`,
|
|
|
|
|
+ `data: ${bytesToString(decrypted?.data)}`
|
|
|
|
|
+ );
|
|
|
if (decrypted?.data?.length) {
|
|
if (decrypted?.data?.length) {
|
|
|
try {
|
|
try {
|
|
|
const dataStr = bytesToString(decrypted.data);
|
|
const dataStr = bytesToString(decrypted.data);
|
|
|
|
|
+ response.data = JSON.parse(dataStr);
|
|
|
console.log(
|
|
console.log(
|
|
|
- '[responseDecryptionInterceptor] Payload parsed. Decrypted timestamp:',
|
|
|
|
|
- decrypted.timestamp,
|
|
|
|
|
- '| Body string:',
|
|
|
|
|
- dataStr
|
|
|
|
|
|
|
+ `⬇️ [responseDecryptionInterceptor] api (${response.config.url}) response data parsed:`,
|
|
|
|
|
+ response.data
|
|
|
);
|
|
);
|
|
|
- response.data = JSON.parse(dataStr);
|
|
|
|
|
- console.log('[responseDecryptionInterceptor] response.data:', response.data);
|
|
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error(
|
|
console.error(
|
|
|
- '[responseDecryptionInterceptor] Failed to parse decrypted response data:',
|
|
|
|
|
|
|
+ `⬇️ [responseDecryptionInterceptor] api (${response.config.url}) response data parse failed:`,
|
|
|
error
|
|
error
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|