Files
td_official/src/utils/request-go.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

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
};
requestGo.download = function (url: string, params: any, filename: string) {
return request({
url,
method: 'post',
baseURL: BASE_GO_URL,
data: params,
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;