system-copyright-react/src/request/request.ts

212 lines
6.1 KiB
TypeScript
Raw Normal View History

2025-03-28 09:44:48 +08:00
import axios from "axios";
2025-04-14 17:38:30 +08:00
2025-04-17 08:36:10 +08:00
2025-04-15 15:42:16 +08:00
// 正式与测试环境
2025-04-15 11:00:48 +08:00
import {
operatorPluginBaseUrl,// 开票功能baseUrl
aiShopBaseUrl,// 买卖商品baseUrl
operatorBaseUrl, //绑定手机号
} from '../util/AjaxUtils'
2025-04-11 16:20:46 +08:00
2025-04-17 08:46:11 +08:00
// // 开发环境
2025-04-15 11:00:48 +08:00
// const operatorPluginBaseUrl = 'http://192.168.0.115:8099' //开票
// const aiShopBaseUrl = 'http://192.168.0.115:8081'//买卖'
// const operatorBaseUrl = 'http://192.168.0.15:8091' //绑定手机号
2025-04-11 16:20:46 +08:00
2025-04-17 08:36:10 +08:00
// axios.defaults.headers['Content-Type'] = 'application/json'; // 设置默认请求头为 JSON 格式
2025-04-15 11:00:48 +08:00
// 开票功能----------------------------------------------------------------------------
2025-03-28 09:44:48 +08:00
const request = axios.create({
2025-04-15 11:00:48 +08:00
baseURL: operatorPluginBaseUrl,
2025-03-28 09:44:48 +08:00
timeout: 5000,
});
request.interceptors.request.use(
(config) => {
// 有token带token
const token = sessionStorage.getItem('token')
config.headers = config.headers || {}
config.headers['Auth'] = token ? `Bearer ${token}` : '';
2025-04-17 08:36:10 +08:00
config.headers['Content-Type'] = 'application/json';
2025-04-17 08:46:11 +08:00
// config.data = { unused: 0 }
if (config.method === 'get' ) {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'put') {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'post') {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'delete') {
if (!config.data) {
config.data = { unused: 0 }
}
}
2025-03-28 09:44:48 +08:00
return config;
},
(err) => Promise.reject(err)
);
request.interceptors.response.use(
(res) => {
return res.data;
},
(err) => Promise.reject(err)
);
2025-04-15 11:00:48 +08:00
//-----------------------------------------------------------------------------------------
2025-04-17 08:36:10 +08:00
2025-04-15 11:00:48 +08:00
// 买卖功能----------------------------------------------------------------------------
const aiShopRequest = axios.create({
baseURL: aiShopBaseUrl,
timeout: 5000,
});
aiShopRequest.interceptors.request.use(
(config) => {
// 有token带token
const token = sessionStorage.getItem('token')
config.headers = config.headers || {}
config.headers['Auth'] = token ? `Bearer ${token}` : '';
2025-04-17 08:36:10 +08:00
config.headers['Content-Type'] = 'application/json';
2025-04-17 08:46:11 +08:00
// config.data = { unused: 0 }
if (config.method === 'get' ) {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'put') {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'post') {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'delete') {
if (!config.data) {
config.data = { unused: 0 }
}
}
2025-04-17 08:36:10 +08:00
2025-04-15 11:00:48 +08:00
return config;
},
(err) => Promise.reject(err)
);
aiShopRequest.interceptors.response.use(
(res) => {
return res.data;
},
(err) => Promise.reject(err)
);
//--------------------------------------------------------------------------------------------
// 绑定手机号功能----------------------------------------------------------------------------
const phoneRequest = axios.create({
baseURL: operatorBaseUrl,
timeout: 5000,
});
phoneRequest.interceptors.request.use(
(config) => {
// 有token带token
const token = sessionStorage.getItem('token')
config.headers = config.headers || {}
config.headers['Auth'] = token ? `Bearer ${token}` : '';
2025-04-17 08:36:10 +08:00
config.headers['Content-Type'] = 'application/json';
2025-04-17 08:46:11 +08:00
// config.data = { unused: 0 }
if (config.method === 'get' ) {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'put') {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'post') {
if (!config.data) {
config.data = { unused: 0 }
}
}
if (config.method === 'delete') {
if (!config.data) {
config.data = { unused: 0 }
}
}
2025-04-15 11:00:48 +08:00
return config;
},
(err) => Promise.reject(err)
);
phoneRequest.interceptors.response.use(
(res) => {
return res.data;
},
(err) => Promise.reject(err)
);
//--------------------------------------------------------------------------------------------
// 绑定手机号功能(不要token)----------------------------------------------------------------------------
2025-04-11 16:20:46 +08:00
const newRequest = axios.create({
2025-04-15 11:00:48 +08:00
baseURL: operatorBaseUrl,
2025-04-11 16:20:46 +08:00
timeout: 5000,
});
2025-04-15 11:00:48 +08:00
2025-04-11 16:20:46 +08:00
newRequest.interceptors.request.use(
(config) => {
// 有token带token
return config;
},
(err) => Promise.reject(err)
);
newRequest.interceptors.response.use(
(res) => {
return res.data;
},
(err) => Promise.reject(err)
);
2025-04-15 11:00:48 +08:00
//--------------------------------------------------------------------------------------------
2025-03-28 09:44:48 +08:00
2025-04-02 15:30:55 +08:00
// 下载发票
export const downloadInvoice = (id: string) => {
2025-04-15 11:00:48 +08:00
return `${operatorPluginBaseUrl}/operator-plugin/route/file/download/false/${id}`
2025-04-02 15:30:55 +08:00
};
2025-04-15 11:00:48 +08:00
2025-04-17 08:36:10 +08:00
// 显示图片 下载 预览文件 买卖功能
2025-04-11 16:20:46 +08:00
export const showImage = (fileId: string, isDownload?: boolean) => {
2025-04-15 11:00:48 +08:00
return `${aiShopBaseUrl}/aishop/route/file/download/${isDownload == false}/${fileId}`
2025-04-11 16:20:46 +08:00
}
2025-04-15 11:00:48 +08:00
// 上传图片 买卖功能
2025-04-11 16:20:46 +08:00
export const uploadImageUrl = () => {
2025-04-15 11:00:48 +08:00
return `${aiShopBaseUrl}/aishop/api/file/v2/upload-image`
2025-04-11 16:20:46 +08:00
}
2025-04-17 08:36:10 +08:00
// // 下载 预览文件 买卖功能
// export const downloadUrl = (fileId: string, isDownload?: boolean) => {
// return `${aiShopBaseUrl}/aishop/route/file/v2/download/${isDownload == false}/${fileId}`
// }
2025-04-11 16:20:46 +08:00
// export default request
2025-04-17 08:36:10 +08:00
// 上传文件 买卖功能
export const uploadFileUrl = () => {
return `${aiShopBaseUrl}/aishop/api/file/v2/upload-file`
}
2025-04-15 11:00:48 +08:00
export { request,aiShopRequest,phoneRequest,newRequest};