Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
1661d7951e | 23 hours ago |
8 changed files with 166 additions and 17 deletions
@ -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 |
|||
@ -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 |
|||
} |
|||
@ -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 |
|||
} |
|||
Loading…
Reference in new issue