2025-04-01 16:04:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* axios请求封装
|
|
|
|
|
|
* https://rudon.blog.csdn.net/
|
|
|
|
|
|
*/
|
|
|
|
|
|
import axios from 'axios'
|
2025-04-03 16:41:54 +08:00
|
|
|
|
import { getToken, getUser } from '@/utils/storage'
|
2025-06-24 13:35:33 +08:00
|
|
|
|
import router from '@/router'
|
|
|
|
|
|
|
2025-04-03 16:41:54 +08:00
|
|
|
|
import { ElMessage } from 'element-plus';
|
2025-07-03 19:14:34 +08:00
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
|
import { defineStore } from 'pinia'
|
2025-08-12 22:05:06 +08:00
|
|
|
|
import { setStorage , getStorage } from '@/utils/storage.js';
|
2025-04-03 16:41:54 +08:00
|
|
|
|
|
2025-04-01 16:04:54 +08:00
|
|
|
|
// 请求地址前缀
|
|
|
|
|
|
let baseURL = ''
|
2025-04-03 16:41:54 +08:00
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
2025-04-01 16:04:54 +08:00
|
|
|
|
// 生产环境
|
2025-08-21 22:07:46 +08:00
|
|
|
|
baseURL = "https://pk.zhukeping.com/"
|
2025-04-01 16:04:54 +08:00
|
|
|
|
} else {
|
2025-05-12 21:09:32 +08:00
|
|
|
|
// 测试环境
|
2025-08-21 22:07:46 +08:00
|
|
|
|
baseURL = "https://pk.zhukeping.com/"
|
2025-04-01 16:04:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
axios.interceptors.request.use((config) => {
|
2025-08-12 22:05:06 +08:00
|
|
|
|
const tokenCache = getStorage('token')
|
2025-06-24 13:35:33 +08:00
|
|
|
|
const url = sliceUrl(config.url)
|
2025-04-01 16:04:54 +08:00
|
|
|
|
// 请求超时时间 - 毫秒
|
2025-04-03 16:41:54 +08:00
|
|
|
|
config.timeout = 60000
|
2025-04-01 16:04:54 +08:00
|
|
|
|
config.baseURL = baseURL
|
|
|
|
|
|
// 自定义Content-type
|
2025-04-03 16:41:54 +08:00
|
|
|
|
config.headers['Content-type'] = 'application/json'
|
2025-08-12 22:05:06 +08:00
|
|
|
|
if (!(config.url == 'getVxQrcode' || config.url == 'getScanResult'||config.url == 'register')) {
|
|
|
|
|
|
config.headers['token'] = tokenCache;
|
|
|
|
|
|
}
|
2025-07-03 19:14:34 +08:00
|
|
|
|
|
2025-04-01 16:04:54 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
}, (error) => {
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
axios.interceptors.response.use((response) => {
|
2025-08-12 22:05:06 +08:00
|
|
|
|
return addPrefixToHeaderIcon(response.data)
|
2025-04-01 16:04:54 +08:00
|
|
|
|
}, (error) => {
|
|
|
|
|
|
// 可添加请求失败后的处理逻辑
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-08-29 13:31:08 +08:00
|
|
|
|
import { translateCountryName } from './countryUtil';
|
|
|
|
|
|
|
|
|
|
|
|
// 处理headerIcon的前缀和country的翻译
|
2025-08-12 22:05:06 +08:00
|
|
|
|
function addPrefixToHeaderIcon(data) {
|
|
|
|
|
|
// 处理数组:递归处理每个元素
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
|
|
data.forEach(item => addPrefixToHeaderIcon(item));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理对象:递归处理每个属性
|
|
|
|
|
|
if (typeof data === 'object' && data !== null) {
|
|
|
|
|
|
for (const key in data) {
|
2025-08-29 13:31:08 +08:00
|
|
|
|
if (key === 'headerIcon' || key === 'anchorIcon' && data.hasOwnProperty(key)) {
|
2025-08-12 22:05:06 +08:00
|
|
|
|
// 在headerIcon值前添加前缀(处理各种类型)anchorIconA anchorIconB anchorIcon
|
|
|
|
|
|
const value = data[key];
|
|
|
|
|
|
data[key] = "https://vv-1317974657.cos.ap-shanghai.myqcloud.com/headerIcon/" + (
|
|
|
|
|
|
typeof value === 'string' ? value
|
|
|
|
|
|
: value != null ? String(value)
|
|
|
|
|
|
: ""
|
|
|
|
|
|
);
|
2025-08-29 13:31:08 +08:00
|
|
|
|
} else if (key === 'country' && data.hasOwnProperty(key)) {
|
|
|
|
|
|
// 翻译country值
|
|
|
|
|
|
const value = data[key];
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
|
data[key] = translateCountryName(value);
|
|
|
|
|
|
}
|
2025-08-12 22:05:06 +08:00
|
|
|
|
} else if (typeof data[key] === 'object' && data[key] !== null) {
|
|
|
|
|
|
// 递归处理嵌套对象或数组
|
|
|
|
|
|
addPrefixToHeaderIcon(data[key]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
2025-04-01 16:04:54 +08:00
|
|
|
|
|
|
|
|
|
|
// axios的get请求
|
|
|
|
|
|
export function getAxios({ url, params }) {
|
|
|
|
|
|
// 使用axios发送GET请求
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
axios.get(url, {
|
|
|
|
|
|
params
|
|
|
|
|
|
// 请求成功,将返回的数据传递给resolve函数
|
|
|
|
|
|
}).then(res => {
|
2025-08-12 22:05:06 +08:00
|
|
|
|
if (res.code == 200) {
|
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
|
} else if (res.code == 40400) {
|
|
|
|
|
|
router.push('/')
|
|
|
|
|
|
ElMessage.error(res.code + '' + res.msg);
|
2025-08-21 21:36:08 +08:00
|
|
|
|
reject();
|
2025-08-12 22:05:06 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
ElMessage.error(res.code + '' + res.msg);
|
2025-08-21 21:36:08 +08:00
|
|
|
|
reject();
|
2025-08-12 22:05:06 +08:00
|
|
|
|
}
|
2025-04-01 16:04:54 +08:00
|
|
|
|
}).catch(err => {
|
|
|
|
|
|
reject(err)
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// axios的post请求
|
|
|
|
|
|
export function postAxios({ url, data }) {
|
2025-08-12 22:05:06 +08:00
|
|
|
|
console.log("postAxios", url, data);
|
2025-04-01 16:04:54 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
axios.post(
|
|
|
|
|
|
url,
|
|
|
|
|
|
data,
|
|
|
|
|
|
{
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
).then(res => {
|
2025-08-12 22:05:06 +08:00
|
|
|
|
if (res.code == 200) {
|
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
|
} else if (res.code == 40400) {
|
|
|
|
|
|
router.push('/')
|
|
|
|
|
|
console.log(res.msg);
|
|
|
|
|
|
ElMessage.error(res.code + '' + res.msg);
|
2025-08-21 21:36:08 +08:00
|
|
|
|
reject();
|
2025-08-12 22:05:06 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
console.log(res.msg);
|
|
|
|
|
|
ElMessage.error(res.code + '' + res.msg);
|
2025-08-21 21:36:08 +08:00
|
|
|
|
reject();
|
2025-08-12 22:05:06 +08:00
|
|
|
|
}
|
2025-04-01 16:04:54 +08:00
|
|
|
|
}).catch(err => {
|
2025-06-24 13:35:33 +08:00
|
|
|
|
reject(err)
|
2025-04-01 16:04:54 +08:00
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-04-10 18:11:03 +08:00
|
|
|
|
|
2025-08-12 22:05:06 +08:00
|
|
|
|
// export const downFile = async (urlstr, data) => {
|
|
|
|
|
|
|
|
|
|
|
|
// // 发送请求,获取文件流
|
|
|
|
|
|
// const response = await axios.post(urlstr, data, { responseType: 'blob' });
|
|
|
|
|
|
|
|
|
|
|
|
// // 获取文件名(如果后端设置了 Content-Disposition)
|
|
|
|
|
|
// const contentDisposition = response.headers['content-disposition'];
|
|
|
|
|
|
// let fileName = 'default-file-name'; // 默认文件名
|
|
|
|
|
|
// console.log(contentDisposition)
|
|
|
|
|
|
// console.log(response)
|
|
|
|
|
|
// if (contentDisposition) {
|
|
|
|
|
|
// // 从响应头中提取文件名
|
|
|
|
|
|
// const fileNameMatch = contentDisposition.match(/filename="(.+)"/);
|
|
|
|
|
|
// if (fileNameMatch && fileNameMatch.length > 1) {
|
|
|
|
|
|
// fileName = fileNameMatch[1];
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// // 创建一个临时的下载链接
|
|
|
|
|
|
// const blob = new Blob([response.data], { type: response.headers['content-type'] });
|
|
|
|
|
|
// const url = window.URL.createObjectURL(blob);
|
|
|
|
|
|
// const a = document.createElement('a');
|
|
|
|
|
|
// a.href = url;
|
|
|
|
|
|
// a.download = fileName; // 设置下载的文件名
|
|
|
|
|
|
// a.click();
|
|
|
|
|
|
|
|
|
|
|
|
// // 释放 URL 对象
|
|
|
|
|
|
// window.URL.revokeObjectURL(url);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
// //请求前验证
|
|
|
|
|
|
// function cheekalive() {
|
|
|
|
|
|
// const userCache = UserStore()
|
|
|
|
|
|
// const tokenCache = tokenStore()
|
|
|
|
|
|
// axios.post('api/account/cheekalive', {
|
|
|
|
|
|
// userId: userCache.user.id,
|
|
|
|
|
|
// currcode: tokenCache.token,
|
|
|
|
|
|
// },
|
|
|
|
|
|
// {
|
|
|
|
|
|
// headers: {
|
|
|
|
|
|
// 'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// ).then(res => {
|
|
|
|
|
|
// console.log(res.data)
|
|
|
|
|
|
// if (res.data) {
|
|
|
|
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// alert("账号在其他地方登录!")
|
|
|
|
|
|
// window.location.href = '/';
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
|
|
|
|
|
// //节流函数
|
|
|
|
|
|
// function throttle(func, limit) {
|
|
|
|
|
|
// let inThrottle;
|
|
|
|
|
|
// return function () {
|
|
|
|
|
|
// const args = arguments;
|
|
|
|
|
|
// const context = this;
|
|
|
|
|
|
// if (!inThrottle) {
|
|
|
|
|
|
// func.apply(context, args);
|
|
|
|
|
|
// inThrottle = true;
|
|
|
|
|
|
// setTimeout(() => inThrottle = false, limit);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2025-04-10 18:11:03 +08:00
|
|
|
|
|
2025-06-24 13:35:33 +08:00
|
|
|
|
function sliceUrl(url) {
|
|
|
|
|
|
const lastSlash = url.lastIndexOf('/');
|
|
|
|
|
|
const questionMark = url.indexOf('?');
|
|
|
|
|
|
if (questionMark == -1) {
|
|
|
|
|
|
const result = url.slice(lastSlash + 1, url.length);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const result = url.slice(lastSlash + 1, questionMark);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-01 16:04:54 +08:00
|
|
|
|
|
|
|
|
|
|
export default axios
|