实现数据加密传输,成功调试登录接口
This commit is contained in:
+27
-11
@@ -3,6 +3,9 @@ import { Config } from "@/config.uts"
|
||||
import { HttpMethod } from "@/enum/HttpMethod"
|
||||
import { Auth } from "@/utils/Auth.uts"
|
||||
import { XiaoZhi } from "@/utils/XiaoZhi"
|
||||
import { Crypto } from "@/utils/crypto.uts"
|
||||
import { base64Encode, createRsaPair, rsaEncryptText } from "@/uni_modules/laoqianjunzi-crypto"
|
||||
|
||||
|
||||
/**
|
||||
* HTTP请求客户端
|
||||
@@ -19,6 +22,11 @@ export class HttpClient {
|
||||
|
||||
private static instance : HttpClient | null = null
|
||||
|
||||
private static encryptHeader = 'encrypt-key';
|
||||
private static publicKey = '-----BEGIN PUBLIC KEY-----\n' +
|
||||
Config.VITE_APP_RSA_PUBLIC_KEY +
|
||||
'\n-----END PUBLIC KEY-----';
|
||||
|
||||
/**
|
||||
* 获取单例实例
|
||||
*/
|
||||
@@ -38,7 +46,7 @@ export class HttpClient {
|
||||
* @returns 标准响应对象
|
||||
*/
|
||||
async request<T>(config : RequestConfig) : Promise<ApiResponse<T>> {
|
||||
console.log("请求拦截")
|
||||
console.log("请求拦截,请求地址:" + config.url)
|
||||
|
||||
let url : string = Config.BASE_URL + config.url
|
||||
let data = config.data ?? null
|
||||
@@ -96,14 +104,28 @@ export class HttpClient {
|
||||
*/
|
||||
if (Config.VITE_APP_ENCRYPT) {
|
||||
if (isEncrypt && (config.method === HttpMethod.POST || config.method === HttpMethod.PUT)) {
|
||||
console.log("加密")
|
||||
// AES密钥
|
||||
const aesKey = Crypto.generateAesKey();
|
||||
// Base64(AES_KEY)
|
||||
const aesKeyBase64 = base64Encode(aesKey)
|
||||
|
||||
// RSA加密后的Header值
|
||||
|
||||
|
||||
header[HttpClient.encryptHeader] = rsaEncryptText(HttpClient.publicKey, aesKeyBase64, 'base64')
|
||||
console.log(header[HttpClient.encryptHeader]);
|
||||
const requestData = config.data
|
||||
|
||||
if (requestData != null) {
|
||||
data = Crypto.encryptWithAes(requestData, aesKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (config.loading == true) {
|
||||
LoadingManager.show()
|
||||
}
|
||||
|
||||
console.log("请求拦截,请求地址:" + url)
|
||||
try {
|
||||
return await new Promise<ApiResponse<T>>((resolve, reject) => {
|
||||
uni.request({
|
||||
@@ -118,26 +140,20 @@ export class HttpClient {
|
||||
enableChunked,
|
||||
success: (res) => {
|
||||
const responseData = res["data"] as UTSJSONObject
|
||||
|
||||
console.log(
|
||||
"响应拦截-响应状态码:" +
|
||||
JSON.stringify(responseData["code"])
|
||||
)
|
||||
|
||||
const apiResponse = new ApiResponse<T>()
|
||||
|
||||
if (responseData != null) {
|
||||
apiResponse.code = (responseData["code"] as number) ?? 0
|
||||
apiResponse.msg = (responseData["msg"] as string) ?? ""
|
||||
apiResponse.data = responseData["data"] as T
|
||||
}
|
||||
console.log("响应拦截-响应状态码:" + apiResponse.code)
|
||||
|
||||
resolve(apiResponse)
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err)
|
||||
},
|
||||
complete: () => {}
|
||||
complete: () => { }
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user