diff --git a/api/loginApi.uts b/api/loginApi.uts index 8b10f2b..98e1423 100644 --- a/api/loginApi.uts +++ b/api/loginApi.uts @@ -18,6 +18,7 @@ export class LoginApi { clientId: Config.VITE_APP_CLIENT_ID, grantType: 'password' } + // 请求参数处理 const Requestconfig = new RequestConfig() Requestconfig.url = "/auth/login" Requestconfig.headers!.isToken = false @@ -25,19 +26,19 @@ export class LoginApi { Requestconfig.headers!.repeatSubmit = false Requestconfig.method = HttpMethod.POST Requestconfig.data = configData - + //发起请求 const res = await this.http.request(Requestconfig) const data = res.data console.log(data); if (data == null) { throw new Error("登陆失败") } - // 手动转换 UTSJSONObject 到 VerifyCodeResult - const result: LoginResult = { + // 格式化返回数据 手动转换 UTSJSONObject 到 VerifyCodeResult + const result : LoginResult = { access_token: data["access_token"] as string } console.log(result.access_token); return result } -} +} \ No newline at end of file diff --git a/config.uts b/config.uts index e3e329c..e28e6d1 100644 --- a/config.uts +++ b/config.uts @@ -9,10 +9,10 @@ export class Config { // 接口加密功能开关(如需关闭 后端也必须对应关闭) - static readonly VITE_APP_ENCRYPT = false - // 接口加密传输 RSA 公钥与后端解密私钥对应 如更换需前后端一同更换 + static readonly VITE_APP_ENCRYPT = true + // 接口加密传输 RSA 公钥与后端解密私钥对应 如更换需前后端一同更换 公钥 static readonly VITE_APP_RSA_PUBLIC_KEY = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvEDuRIOM3oZPWj9Ukoc5pQklR4PFH6/clnjeFqjDLIgDyQvjxhgqAZQA+E9eD6qu6FsXPmK8djcL+nh3cFHz4pX473jDvO3Sve+8yL3VRQ0n2pRgQ2a01MJsy+WwTZCBYWf0VnLRIvANUoWQgy9vz94q7Va44dg7A1/3ICf+xAwIDAQAB' - // 接口响应解密 RSA 私钥与后端加密公钥对应 如更换需前后端一同更换 + // 接口响应解密 RSA 私钥与后端加密公钥对应 如更换需前后端一同更换 私钥 static readonly VITE_APP_RSA_PRIVATE_KEY = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAObq7yrxfvyieZtTjAYyrdvi59tYTXxjO5ajmPCRSXBY9M9wQ1tli297JN6mnY53UJMNyOFNSZVi8WSFoIXjpR87FmvChJlzeN/dZdd3SEs48Ee66XKeSePYqxa8oO5GKDsnajgpsOHKXSeeVSIysiIPS2/WsEqk0In9P4w3RsRFAgMBAAECgYBiMEWwce24SPICnRzuScBpvmsudrbEDIH7BOd0a6LZlcnLJwZNJ7mJlshPsHNQb+WgEf135+BBGEhioPtn0yuTdEuKP4kB9UdYUKiayWCoWhJpesv7sAD4RDClV7dhuV+gcd1AXD+YzyRIPbGm0VC2U+4q8/+UPRpVjqskbLVTgQJBAPRpou7g3S8n4XB527kq0D8I3+ZYwMxZhszwhrCDpJU319+ucmpLVwYIzDmZVeID2QQdUaDfIEViFHu95xDrGiUCQQDx3YOKn3yaEctk/ERVn7hDAyAXUbd8/pv2b24/M/l1ZevlsFem8U4Jk5Mu64t3z3YGJoymEjQmbucwT01iKhehAkEAxlnccsRmfFh/KkqauKE4M4++NTAd9zlInpUsmZ+cN8UEGnF2RTEzRKBrLOt1uWCqBB7PGiE6DVTVjr7FAQPrSQJAT5yeY87DcONSk9cFlzmPqV8p/QME5rvYEnHzVBKDlkUKNPyqnWToTvaoh9U4fyNmsfeWbEOprszqhFhWHG3GgQJAK8+ynmyFhaw63+Hx2KU5zR4hVuQso2IzrEurGxCxybV6mR7VBerb4502+EPx3PmOgxQL+niUFhcMWxcvBFP9+A==' diff --git a/pages.json b/pages.json index 014d706..0ea3442 100644 --- a/pages.json +++ b/pages.json @@ -11,6 +11,12 @@ "style": { "navigationBarTitleText": "登录" } + }, + { + "path": "pages/test/index", + "style": { + "navigationBarTitleText": "加解密测试" + } } ], "globalStyle": { diff --git a/pages/auth/login.uvue b/pages/auth/login.uvue index 7480385..ca3a17e 100644 --- a/pages/auth/login.uvue +++ b/pages/auth/login.uvue @@ -79,7 +79,7 @@ }) } } catch (e) { - console.error('登录失败:', e) + console.error('登录失败:'+ e.message) loadCode() } } diff --git a/pages/index.uvue b/pages/index.uvue index 472dfb1..fc44dca 100644 --- a/pages/index.uvue +++ b/pages/index.uvue @@ -3,6 +3,7 @@ {{title}} + 需要一个导航栏 @@ -18,6 +19,14 @@ }) } + + function goLogin1() { + + uni.navigateTo({ + url: '/pages/test/index' + }) + + } diff --git a/utils/HttpClient.uts b/utils/HttpClient.uts index 259b337..9e9efa6 100644 --- a/utils/HttpClient.uts +++ b/utils/HttpClient.uts @@ -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(config : RequestConfig) : Promise> { - 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>((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() - 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: () => { } }) }) diff --git a/utils/crypto.uts b/utils/crypto.uts index aba8b45..5a171ad 100644 --- a/utils/crypto.uts +++ b/utils/crypto.uts @@ -1,55 +1,34 @@ -import { createRsaPair, base64Decode, base64Encode, } from "@/uni_modules/laoqianjunzi-crypto"; -// 在插件中,h5不会导出CryptoRsaPair,需要自定义,等待后续作者处理 -// #ifdef H5 -type CryptoRsaPair = { - publicKey : string; - privateKey : string; -} -// #endif -// #ifdef APP-ANDROID -import { CryptoRsaPair } from "@/uni_modules/laoqianjunzi-crypto"; -// #endif +import { createRsaPair, base64Decode, base64Encode, defaultTextCipherOptions, aesEncryptText, } from "@/uni_modules/laoqianjunzi-crypto"; export class Crypto { /** * 随机生成32位的字符串 * @returns {string} */ - static generateRandomString() : string { - const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - let result = "" - for (let i = 0; i < 32; i++) { - result += chars.charAt( - Math.floor( - Math.random() * chars.length - ) - ) - } - return result; - } + static generateRandomString(length = 16) : string { + const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + let result = '' - private static readonly rsaBitsText = ref('2048') - static currentRsaBits() : number { - const parsed = parseInt(Crypto.rsaBitsText.value) - if (parsed == 1024 || parsed == 2048 || parsed == 3072 || parsed == 4096) { - return parsed + for (let i = 0; i < length; i++) { + result += chars.charAt(Math.floor(Math.random() * chars.length)) } - return 2048 + + return result } /** * 随机生成aes 密钥 * @returns {string} */ - static generateAesKey() : CryptoRsaPair { - return createRsaPair(Crypto.currentRsaBits()); + static generateAesKey() : string { + return Crypto.generateRandomString(16); } /** * 加密base64 * @returns {string} */ - static encryptBase64(str : CryptoRsaPair) : string { - return base64Encode(str.privateKey + str.publicKey); + static encryptBase64(str : string) : string { + return base64Encode(str); } /** @@ -65,8 +44,15 @@ export class Crypto { * @param aesKey * @returns {string} */ - static encryptWithAes() : string { - return ''; + static encryptWithAes(data : any, aesKey : string) : string { + // AES配置 + const options = defaultTextCipherOptions() + options.mode = 'ECB' + options.padding = 'PKCS7' + options.keyLength = 128 + // 请求体JSON + const json = JSON.stringify(data) + return aesEncryptText(aesKey, json, options); } /** diff --git a/utils/jsencrypt.uts b/utils/jsencrypt.uts index 3c5953a..d434f98 100644 --- a/utils/jsencrypt.uts +++ b/utils/jsencrypt.uts @@ -21,7 +21,7 @@ export class JSEncrypt { * @returns Base64密文 */ static encrypt(text : string) : string { - if (!text) { + if (text == "") { return ""; } return rsaEncryptText(JSEncrypt.PUBLIC_KEY, text, "base64"); @@ -34,7 +34,7 @@ export class JSEncrypt { * @returns 明文 */ static decrypt(cipherText : string) : string { - if (!cipherText) { + if (cipherText=="") { return ""; } return rsaDecryptText(JSEncrypt.PRIVATE_KEY, cipherText, "base64");