2025-10-30 19:30:06 +08:00
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
|
|
|
|
|
|
|
const env = import.meta.env;
|
2025-11-04 12:03:33 +08:00
|
|
|
|
const w: any = window;
|
|
|
|
|
|
const $wujie: any = w.$wujie;
|
2025-10-30 19:30:06 +08:00
|
|
|
|
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
|
baseURL: env.VITE_API_BASE_URL,
|
|
|
|
|
|
timeout: 60000,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json;application/xml;charset=utf-8;',
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
|
(config) => {
|
2025-11-04 16:28:34 +08:00
|
|
|
|
config.headers['company'] = 'carsafe';
|
2025-11-10 20:40:18 +08:00
|
|
|
|
config.headers['jobNumber'] = $wujie?.props?.USER_ID || '1980235559149838337';
|
2025-11-04 12:03:33 +08:00
|
|
|
|
config.headers['token'] = $wujie?.props?.TOKEN || '';
|
2025-11-10 20:40:18 +08:00
|
|
|
|
config.headers['userId'] = $wujie?.props?.USER_ID || '1980235559149838337';
|
|
|
|
|
|
config.headers['tenantId'] = $wujie?.props?.TENANT_ID || '1979091834410176514';
|
2025-10-30 19:30:06 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(res) => {
|
|
|
|
|
|
// 判断响应类型,如果是 blob,返回完整响应对象
|
|
|
|
|
|
if (res.config.responseType === 'blob') {
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 普通接口返回原来的 res.data
|
|
|
|
|
|
if (res.data.code !== 200) {
|
|
|
|
|
|
ElMessage.error(res.data.message);
|
|
|
|
|
|
}
|
|
|
|
|
|
return res.data;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
|
|
|
|
|
ElMessage.error('系统繁忙,请稍后再试');
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const get = (url: string, params = {}) => {
|
|
|
|
|
|
return service.get(url, { params });
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const post = (url: string, data = {}) => {
|
|
|
|
|
|
return service.post(url, data);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const upload = (url: string, formData = {}, onProgress?: any) => {
|
|
|
|
|
|
return service.post(url, formData, {
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
|
|
},
|
|
|
|
|
|
onUploadProgress: (event: any) => {
|
|
|
|
|
|
if (onProgress) {
|
|
|
|
|
|
onProgress({
|
|
|
|
|
|
loaded: event.loaded,
|
|
|
|
|
|
total: event.total,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const download = (url: string, params = {}, filename = 'download') => {
|
|
|
|
|
|
return service
|
|
|
|
|
|
.post(url, params, {
|
|
|
|
|
|
responseType: 'blob',
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((res: any) => {
|
2025-11-13 16:03:19 +08:00
|
|
|
|
const contentDisposition =
|
|
|
|
|
|
res.headers?.['content-disposition'] ||
|
|
|
|
|
|
res.headers?.['Content-Disposition'] ||
|
|
|
|
|
|
'';
|
2025-10-30 19:30:06 +08:00
|
|
|
|
let fileName = filename;
|
2025-11-13 16:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
if (contentDisposition) {
|
|
|
|
|
|
const regex = /(filename\*?)\s*=\s*(?:UTF-8'')?["']?([^;"']+)["']?/ig;
|
|
|
|
|
|
let match: RegExpExecArray | null = null;
|
|
|
|
|
|
while ((match = regex.exec(contentDisposition)) !== null) {
|
|
|
|
|
|
const key = match[1];
|
|
|
|
|
|
const val = match[2];
|
|
|
|
|
|
if (!val) continue;
|
|
|
|
|
|
if (key.endsWith('*')) {
|
|
|
|
|
|
if (val.toLowerCase() === 'utf-8') {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
fileName = decodeURIComponent(val);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
fileName = val;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fileName = val.replace(/^["']|["']$/g, '');
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-10-30 19:30:06 +08:00
|
|
|
|
}
|
2025-11-13 16:03:19 +08:00
|
|
|
|
|
2025-10-30 19:30:06 +08:00
|
|
|
|
const blob = new Blob([res.data]);
|
|
|
|
|
|
const link = document.createElement('a');
|
|
|
|
|
|
link.href = window.URL.createObjectURL(blob);
|
|
|
|
|
|
link.download = fileName;
|
|
|
|
|
|
document.body.appendChild(link);
|
|
|
|
|
|
link.click();
|
|
|
|
|
|
document.body.removeChild(link);
|
|
|
|
|
|
window.URL.revokeObjectURL(link.href);
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
ElMessage.error('下载失败');
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
get,
|
|
|
|
|
|
post,
|
|
|
|
|
|
upload,
|
|
|
|
|
|
download,
|
|
|
|
|
|
};
|