2025-07-29 16:24:08 +08:00
|
|
|
|
import $cache from '@/plugins/cache';
|
|
|
|
|
import request from '@/utils/request';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 包装 request 请求,统一使用 Go 服务地址作为 baseURL
|
|
|
|
|
* @param config 原始请求配置
|
|
|
|
|
*/
|
2025-07-30 16:25:45 +08:00
|
|
|
|
|
|
|
|
|
const BASE_GO_URL = import.meta.env.VITE_APP_BASE_API_GO;
|
|
|
|
|
|
|
|
|
|
interface RequestGo extends Function {
|
|
|
|
|
(config: any): Promise<any>;
|
|
|
|
|
download?: (url: string, params: any, filename: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requestGo: RequestGo = (config: any) => {
|
2025-07-29 16:24:08 +08:00
|
|
|
|
return request({
|
|
|
|
|
baseURL: BASE_GO_URL,
|
|
|
|
|
...config,
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${$cache.local.get('goToken') || ''}`
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-30 16:25:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
2025-09-04 11:06:45 +08:00
|
|
|
|
requestGo.download = function (url: string, params: any, filename: string, method: 'post' | 'get' = 'post') {
|
2025-07-30 16:25:45 +08:00
|
|
|
|
return request({
|
|
|
|
|
url,
|
2025-09-04 11:06:45 +08:00
|
|
|
|
method: method,
|
2025-07-30 16:25:45 +08:00
|
|
|
|
baseURL: BASE_GO_URL,
|
2025-09-04 11:06:45 +08:00
|
|
|
|
data: method === 'post' ? params : undefined,
|
|
|
|
|
params: method === 'get' ? params : undefined,
|
2025-07-30 16:25:45 +08:00
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${$cache.local.get('goToken') || ''}`
|
|
|
|
|
},
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
}).then((response) => {
|
|
|
|
|
// ✅ 只取 response.data
|
|
|
|
|
const blob = new Blob([response.data]);
|
|
|
|
|
const link = document.createElement('a');
|
|
|
|
|
link.style.display = 'none';
|
|
|
|
|
link.href = URL.createObjectURL(blob);
|
|
|
|
|
link.setAttribute('download', filename);
|
|
|
|
|
document.body.appendChild(link);
|
|
|
|
|
link.click();
|
|
|
|
|
document.body.removeChild(link);
|
|
|
|
|
URL.revokeObjectURL(link.href);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default requestGo;
|