Commit 12431e64 authored by 王玉鑫's avatar 王玉鑫

feat: 部分接口响应加密

parent 4725c937
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<p class="mb-[16px] text-lg font-semibold text-[#1a1a1a]">{{ detail.title }}</p> <p class="mb-[16px] text-lg font-semibold text-[#1a1a1a]">{{ detail.title }}</p>
<div class="mb-[17px] flex h-[22px] flex-wrap overflow-hidden"> <div class="mb-[17px] flex h-[22px] flex-wrap overflow-hidden">
<span <span
v-for="item in detail.industryList.split(',')" v-for="item in detail.industryList?.split(',') || []"
:key="item" :key="item"
class="mb-2 mr-[13px] bg-[rgba(44,104,255,0.08)] px-3 text-sm leading-[22px] text-[#2C68FF]" class="mb-2 mr-[13px] bg-[rgba(44,104,255,0.08)] px-3 text-sm leading-[22px] text-[#2C68FF]"
> >
......
import { publicKey } from '@/utils/constant.ts';
import JSEncrypt from 'jsencrypt';
import CryptoJS from 'crypto-js';
import { computed, ref } from 'vue';
/**
* 生成uuid
*/
export function genUUID(strLength = 16) {
let code = '';
const chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
const charsArr = chars.split('');
for (let i = 0; i < strLength; i++) {
const num = Math.floor(Math.random() * charsArr.length);
code += charsArr[num];
}
return code;
}
/**
* 加解密
*/
export function useCrypto() {
const aesKey = ref(genUUID());
const parsedKey = CryptoJS.enc.Utf8.parse(aesKey.value);
const getAesOptions = computed(() => {
return {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: parsedKey,
};
});
/** 生成header传递的密文 */
const getHeaderKey = computed(() => {
const encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
return encrypt.encrypt(aesKey.value).toString();
});
/** 解密 */
const decrypt = (content: string) => {
const words = CryptoJS.AES.decrypt(content, parsedKey, getAesOptions.value);
const result = CryptoJS.enc.Utf8.stringify(words);
return result ? JSON.parse(result) : null;
};
return {
getHeaderKey,
decrypt,
};
}
...@@ -5,7 +5,10 @@ import type { AxiosRequestConfig } from 'axios'; ...@@ -5,7 +5,10 @@ import type { AxiosRequestConfig } from 'axios';
import CryptoJS from 'crypto-js'; import CryptoJS from 'crypto-js';
import JSEncrypt from 'jsencrypt'; import JSEncrypt from 'jsencrypt';
import { watch } from 'vue'; import { watch } from 'vue';
import { cryptoUrls, publicKey } from '@/utils/constant.ts';
import { cloneDeep } from 'lodash-es';
import { useLoading } from './useLoading.ts'; import { useLoading } from './useLoading.ts';
import { genUUID, useCrypto } from './useCrypto.ts';
type RequestInstance = ReturnType<typeof axios.create>; type RequestInstance = ReturnType<typeof axios.create>;
...@@ -48,18 +51,6 @@ function addInterceptors(instance: RequestInstance) { ...@@ -48,18 +51,6 @@ function addInterceptors(instance: RequestInstance) {
return instance; return instance;
} }
function generateRandomStr(strLength = 16) {
let code = '';
const chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
const charsArr = chars.split('');
for (let i = 0; i < strLength; i++) {
const num = Math.floor(Math.random() * charsArr.length);
code += charsArr[num];
}
return code;
}
function generateTimer(uuid: any) { function generateTimer(uuid: any) {
return CryptoJS.AES.encrypt( return CryptoJS.AES.encrypt(
CryptoJS.enc.Utf8.parse(JSON.stringify({ time: String(new Date().getTime()), uuid })), CryptoJS.enc.Utf8.parse(JSON.stringify({ time: String(new Date().getTime()), uuid })),
...@@ -99,6 +90,13 @@ function handleError(res: any, call: any) { ...@@ -99,6 +90,13 @@ function handleError(res: any, call: any) {
} }
} }
/**
* 是否需要加解密
*/
function isUrlCrypto(url: RequestUrl) {
return cryptoUrls.includes(url);
}
/** /**
* http请求 * http请求
*/ */
...@@ -111,11 +109,10 @@ export function useRequest<T = Record<any, any>, P = Record<any, any>>( ...@@ -111,11 +109,10 @@ export function useRequest<T = Record<any, any>, P = Record<any, any>>(
) { ) {
const appStore = useAppStore(); const appStore = useAppStore();
const { changeShowLoading } = useLoading(); const { changeShowLoading } = useLoading();
const { getHeaderKey, decrypt } = useCrypto();
const request = (params: P = {} as P, specialUrl = '') => { const request = (params: P = {} as P, specialUrl = '') => {
const publicKey = const uuid = genUUID(16);
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfGTt8qny6pipLYYhBVAIGZ6CgMdmYCVPy3bAi8AO1MvOD3Pba1OxLSEvtctOmcibO6NL5NZgQrsTjfu1RMSJkwv1vY/GsKH4c/fbmd3QPhuKUZNHQhTMicbeGFadMM2xRkBkZDjeakemqTdLQFnkRFSeE35GKRkBBimOm4YWBQwIDAQAB';
const uuid = generateRandomStr(16);
const enCrypto = new JSEncrypt(); const enCrypto = new JSEncrypt();
enCrypto.setPublicKey(publicKey); enCrypto.setPublicKey(publicKey);
const secretKey = enCrypto.encrypt(uuid).toString(); const secretKey = enCrypto.encrypt(uuid).toString();
...@@ -132,9 +129,22 @@ export function useRequest<T = Record<any, any>, P = Record<any, any>>( ...@@ -132,9 +129,22 @@ export function useRequest<T = Record<any, any>, P = Record<any, any>>(
headers: { headers: {
Authorization: isAuth ? appStore.jwtToken : '', Authorization: isAuth ? appStore.jwtToken : '',
sign: `secretKey=${secretKey};timer=${timer}`, sign: `secretKey=${secretKey};timer=${timer}`,
secure: getHeaderKey.value,
...config.headers, ...config.headers,
}, },
}) })
.then((res) => {
if (isUrlCrypto(url) && res.success && res.data) {
const decryptData = decrypt(res.data as string);
console.log('解密数据为:', cloneDeep(decryptData));
return {
...res,
data: decryptData,
};
} else {
return res;
}
})
.catch((res) => { .catch((res) => {
return handleError(res, call); return handleError(res, call);
}) })
......
import { RequestUrl } from '@/types/api.ts';
/** 产业园级别 */ /** 产业园级别 */
export const industrialLevels = ['国家级', '省级', '市级', '区县级', '其他']; export const industrialLevels = ['国家级', '省级', '市级', '区县级', '其他'];
...@@ -100,3 +102,22 @@ export const standardFloorWeights = [ ...@@ -100,3 +102,22 @@ export const standardFloorWeights = [
/** 柱距 */ /** 柱距 */
export const pillarDistances = ['10m以下', '10-30m', '30-50m', '50m以上']; export const pillarDistances = ['10m以下', '10-30m', '30-50m', '50m以上'];
/** 公钥 */
export const publicKey =
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfGTt8qny6pipLYYhBVAIGZ6CgMdmYCVPy3bAi8AO1MvOD3Pba1OxLSEvtctOmcibO6NL5NZgQrsTjfu1RMSJkwv1vY/GsKH4c/fbmd3QPhuKUZNHQhTMicbeGFadMM2xRkBkZDjeakemqTdLQFnkRFSeE35GKRkBBimOm4YWBQwIDAQAB';
/** 需要加密的接口 */
export const cryptoUrls = [
RequestUrl.carrierList,
RequestUrl.carrierDetail,
RequestUrl.landDetail,
RequestUrl.landList,
RequestUrl.industrialParkList,
RequestUrl.industrialParkDetal,
RequestUrl.developZoneDetail,
RequestUrl.developZoneList,
RequestUrl.policyList,
RequestUrl.policyDetail,
RequestUrl.homePage,
];
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
</div> </div>
<div class="mb-[17px] flex flex-wrap"> <div class="mb-[17px] flex flex-wrap">
<span <span
v-for="item in detail.industryList.split(',')" v-for="item in detail.industryList?.split(',') || []"
:key="item" :key="item"
class="mb-2 mr-[13px] flex-wrap bg-[rgba(44,104,255,0.08)] px-3 text-sm leading-[22px] text-[#2C68FF]" class="mb-2 mr-[13px] flex-wrap bg-[rgba(44,104,255,0.08)] px-3 text-sm leading-[22px] text-[#2C68FF]"
> >
......
...@@ -12,9 +12,12 @@ import legacy from '@vitejs/plugin-legacy'; ...@@ -12,9 +12,12 @@ import legacy from '@vitejs/plugin-legacy';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'; import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig((env) => {
const isProd = env.mode === 'production';
return {
esbuild: { esbuild: {
drop: ['console'], ...(isProd ? { drop: ['console'] } : {}),
}, },
plugins: [ plugins: [
vue(), vue(),
...@@ -41,7 +44,7 @@ export default defineConfig({ ...@@ -41,7 +44,7 @@ export default defineConfig({
proxy: { proxy: {
'/userAPI': { '/userAPI': {
// target: 'http://172.19.161.87:8087', // target: 'http://172.19.161.87:8087',
target: 'http://test.user.liyeyun.com', target: 'http://test.api.liyeyun.com',
changeOrigin: true, changeOrigin: true,
secure: false, secure: false,
rewrite: (path) => path.replace(/^\/userAPI/, ''), rewrite: (path) => path.replace(/^\/userAPI/, ''),
...@@ -55,4 +58,5 @@ export default defineConfig({ ...@@ -55,4 +58,5 @@ export default defineConfig({
}, },
}, },
}, },
};
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment