Files
td_official/src/utils/request-go.ts
2025-09-04 11:06:45 +08:00

52 lines
1.4 KiB
TypeScript

import $cache from '@/plugins/cache';
import request from '@/utils/request';
/**
* 包装 request 请求,统一使用 Go 服务地址作为 baseURL
* @param config 原始请求配置
*/
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) => {
return request({
baseURL: BASE_GO_URL,
...config,
headers: {
'Authorization': `Bearer ${$cache.local.get('goToken') || ''}`
}
});
};
requestGo.download = function (url: string, params: any, filename: string, method: 'post' | 'get' = 'post') {
return request({
url,
method: method,
baseURL: BASE_GO_URL,
data: method === 'post' ? params : undefined,
params: method === 'get' ? params : undefined,
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;