Browse Source

加密

qin_s1
herenshan112 23 hours ago
parent
commit
1661d7951e
  1. 102
      src/utils/axiosRequest/request.ts
  2. 5
      src/utils/encryptionAndDecryption/sm4Utils.ts
  3. 14
      src/utils/loadSvgFile.ts
  4. 19
      src/utils/request.ts
  5. 1
      src/utils/request123.ts
  6. 32
      src/utils/validate.ts
  7. 8
      src/views/hr/company/orgPeopleNew.vue

102
src/utils/axiosRequest/request.ts

@ -0,0 +1,102 @@
/**
@ 作者: 秦东
@ 时间: 2025-12-09 10:57:37
@ 功能: 封装axios请求
*/
import axios, { AxiosError, type InternalAxiosRequestConfig } from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { generateRandomString } from '@/utils/encryptionAndDecryption/randNumber'
import { sm4DecryptMethod, sm4EncryptMethod } from '@/utils/encryptionAndDecryption/sm4Utils'
import { useUserStore } from '@/stores/user'
/**
@ 作者: 秦东
@ 时间: 2025-12-09 10:58:34
@ 功能: 创建axios实例
*/
const service = axios.create({
baseURL: import.meta.env.VITE_APP_BASE_API,
timeout: 5000
})
/**
@ 作者: 秦东
@ 时间: 2025-12-09 11:00:05
@ 功能: 请求拦截
*/
service.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
const userStore = useUserStore()
// if (config.headers['content-type'] === 'application/json') {
let { data, headers } = config
console.log('请求拦截----data---1--->', data)
if (userStore.authToken) {
config.headers['Auth-token'] = userStore.authToken
}
//获取16位随机数
let randomString = generateRandomString(16)
config.headers['Auth-key'] = randomString
if (data) {
// 加密请求数据
config.data = {
encryptedFile: sm4EncryptMethod(JSON.stringify(data), randomString)
}
}
console.log('请求拦截---------->', randomString)
console.log('请求拦截----headers------>', headers)
console.log('请求拦截----data------>', config.data)
console.log('请求拦截----config------>', config)
// }
// console.log('请求拦截----content-type------>', config.headers['Content-Type'])
// console.log('请求拦截----config------>', config.headers)
return config
},
(error: AxiosError) => {
return Promise.reject(error)
}
)
/**
@ 作者: 秦东
@ 时间: 2025-12-09 21:00:20
@ 备注: 响应拦截
*/
service.interceptors.response.use(
response => {
console.log('行营结果---------->', response)
let { data, headers } = response
// console.log('行营结果----data------>', data.data)
let authKey = headers['auth-key']
console.log('行营结果----authKey------>', authKey)
// 解密响应数据
if (authKey) {
let jsonData = sm4DecryptMethod(data.data, authKey)
response.data.data = JSON.parse(jsonData)
}
console.log('行营结果----解密结构------>', headers['auth-key'], response)
return response.data
},
(error: AxiosError) => {
if (error.response) {
const status = error.response.status
switch (status) {
case 401:
// 处理401错误,例如跳转到登录页
break
case 403:
// 处理403错误,例如提示无权限
break
case 404:
// 处理404错误,例如提示资源不存在
break
case 500:
// 处理500错误,例如提示服务器错误
break
default:
// 处理其他错误
break
}
}
return Promise.reject(error)
}
)
export default service

5
src/utils/encryptionAndDecryption/sm4Utils.ts

@ -63,11 +63,6 @@ const sm4DecryptMethod = (data: string, customKey: string): string => {
console.log('SM4解密方法----解密结构--ivSetup---->', ivSetup)
console.log('SM4解密方法----解密结构---sm4TokenKey--->', sm4TokenKey)
console.log('SM4解密方法----解密结构---appSystemKey--->', appSystemKey)
console.log('SM4解密方法----解密结构---appSystemKey.length--->', appSystemKey.length)
console.log('SM4解密方法----解密结构---ivSetup.length--->', ivSetup.length)
return sm4.decrypt(data, appSystemKey, {
iv: ivSetup,
mode: sm4.constants.CBC,

14
src/utils/loadSvgFile.ts

@ -0,0 +1,14 @@
/**
@ 作者: 秦东
@ 时间: 2026-01-09 15:57:48
@ 功能: 加载svg文件
*/
export const loadSvgFiles = (): string[] => {
const icons = import.meta.glob('../assets/icons/*.svg')
const svgIcons: string[] = []
for (const icon in icons) {
const iconName = icon.split('assets/icons/')[1].split('.svg')[0]
svgIcons.push(iconName)
}
return svgIcons
}

19
src/utils/request.ts

@ -40,7 +40,10 @@ service.interceptors.request.use(
if (userStore.userToken) {
config.headers["user-token"] = userStore.userToken;
}
console.error('<---------------请求拦截---------->')
console.error('请求拦截----config------>', config.url)
console.error('请求拦截----data------>', config.data)
console.error('<---------------请求拦截---------->')
// if (config.headers['content-type'] === 'application/json') {
let { data, headers } = config
//获取16位随机数
@ -52,10 +55,10 @@ service.interceptors.request.use(
data: sm4EncryptMethod(JSON.stringify(data), randomString)
}
}
console.log('请求拦截---------->', randomString)
console.log('请求拦截----headers------>', headers)
console.log('请求拦截----data------>', config.data)
console.log('请求拦截----config------>', config)
// console.log('请求拦截---------->', randomString)
// console.log('请求拦截----headers------>', headers)
// console.log('请求拦截----data------>', config.data)
// console.log('请求拦截----config------>', config)
// }
// console.log('请求拦截----content-type------>', config.headers['Content-Type'])
@ -76,13 +79,15 @@ service.interceptors.response.use(
let { data, headers } = response
let authKey = headers['auth-key']
console.log('行营结果----authKey------>', authKey)
// console.log('行营结果----authKey------>', authKey)
// 解密响应数据
if (authKey) {
let jsonData = sm4DecryptMethod(data.data, authKey)
response.data.data = JSON.parse(jsonData)
}
console.log('行营结果----解密结构------>', headers['auth-key'], response)
console.error('行营结果----解密结构------>', response.config.url)
console.error('行营结果----解密结构------>', response.data)
// console.log('行营结果----解密结构------>', headers['auth-key'], response)
const { code, msg } = response.data;
if (code === 0 || code === 200 || code === 10001) {
return response.data;

1
src/utils/request123.ts

@ -4,6 +4,7 @@ import { ElMessage, ElMessageBox } from 'element-plus';
import router from '@/router';
import { useRouter } from 'vue-router'
import { generateRandomString } from '@/utils/encryptionAndDecryption/randNumber'
import { sm4DecryptMethod, sm4EncryptMethod } from './encryptionAndDecryption/sm4Utils';
const routerPinia = useRouter()

32
src/utils/validate.ts

@ -0,0 +1,32 @@
/**
@ 作者: 秦东
@ 时间: 2025-12-29 16:00:55
@ 功能: 路径匹配器
* @param {string} pattern
* @param {string} path
* @returns {Boolean}
*/
export const isPathMatch = (pattern: string, path: string): boolean => {
const regexPattern = pattern
.replace(/\//g, '\\/')
.replace(/\*\*/g, '.*')
.replace(/\*/g, '[^\\/]*')
const regex = new RegExp(`^${regexPattern}$`)
return regex.test(path)
}
/**
@ 作者: 秦东
@ 时间: 2025-12-29 16:02:21
@ 功能: 判断字符串是否为空
*/
export const isEmptyString = (str: string): boolean => {
return str.trim().length === 0
}
/**
@ 作者: 秦东
@ 时间: 2025-12-29 16:03:14
@ 功能: 判断url是否是http或https
*/
export const isHttpOrHttpsUrl = (url: string): boolean => {
return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
}

8
src/views/hr/company/orgPeopleNew.vue

@ -41,14 +41,14 @@ const props = defineProps({
},
});
//
const pickUserAry = ref(props.orgInfo ? props.orgInfo : []);
const pickUserKeyAry = ref(props.orgKey ? props.orgKey : []);
const pickUserAry = ref<any>(props.orgInfo ? props.orgInfo : []);
const pickUserKeyAry = ref<any>(props.orgKey ? props.orgKey : []);
const loading = ref(false);
const emits = defineEmits([
"update:isOpen",
"update:orgKey",
"update:orgInfo",
"subitRefresh",
"subitRefresh", 'update:isShow',
]);
const openPage = computed({
get() {
@ -63,7 +63,7 @@ const openPage = computed({
const currOrgManList = ref([]);
const loadTable = ref(false);
const totalCount = ref(0); //
const peopleAry = ref([]); //
const peopleAry = ref<any>([]); //
//
const getOrgTreeSub = () => {
currOrgManList.value = [];

Loading…
Cancel
Save