system-copyright-react/src/request/request.ts
2025-04-17 08:46:11 +08:00

212 lines
6.1 KiB
TypeScript

import axios from "axios";
// 正式与测试环境
import {
operatorPluginBaseUrl,// 开票功能baseUrl
aiShopBaseUrl,// 买卖商品baseUrl
operatorBaseUrl, //绑定手机号
} from '../util/AjaxUtils'
// // 开发环境
// const operatorPluginBaseUrl = 'http://192.168.0.115:8099' //开票
// const aiShopBaseUrl = 'http://192.168.0.115:8081'//买卖'
// const operatorBaseUrl = 'http://192.168.0.15:8091' //绑定手机号
// axios.defaults.headers['Content-Type'] = 'application/json'; // 设置默认请求头为 JSON 格式
// 开票功能----------------------------------------------------------------------------
const request = axios.create({
baseURL: operatorPluginBaseUrl,
timeout: 5000,
});
request.interceptors.request.use(
(config) => {
// 有token带token
const token = sessionStorage.getItem('token')
config.headers = config.headers || {}
config.headers['Auth'] = token ? `Bearer ${token}` : '';
config.headers['Content-Type'] = 'application/json';
// 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 }
}
}
return config;
},
(err) => Promise.reject(err)
);
request.interceptors.response.use(
(res) => {
return res.data;
},
(err) => Promise.reject(err)
);
//-----------------------------------------------------------------------------------------
// 买卖功能----------------------------------------------------------------------------
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}` : '';
config.headers['Content-Type'] = 'application/json';
// 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 }
}
}
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}` : '';
config.headers['Content-Type'] = 'application/json';
// 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 }
}
}
return config;
},
(err) => Promise.reject(err)
);
phoneRequest.interceptors.response.use(
(res) => {
return res.data;
},
(err) => Promise.reject(err)
);
//--------------------------------------------------------------------------------------------
// 绑定手机号功能(不要token)----------------------------------------------------------------------------
const newRequest = axios.create({
baseURL: operatorBaseUrl,
timeout: 5000,
});
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)
);
//--------------------------------------------------------------------------------------------
// 下载发票
export const downloadInvoice = (id: string) => {
return `${operatorPluginBaseUrl}/operator-plugin/route/file/download/false/${id}`
};
// 显示图片 下载 预览文件 买卖功能
export const showImage = (fileId: string, isDownload?: boolean) => {
return `${aiShopBaseUrl}/aishop/route/file/download/${isDownload == false}/${fileId}`
}
// 上传图片 买卖功能
export const uploadImageUrl = () => {
return `${aiShopBaseUrl}/aishop/api/file/v2/upload-image`
}
// // 下载 预览文件 买卖功能
// export const downloadUrl = (fileId: string, isDownload?: boolean) => {
// return `${aiShopBaseUrl}/aishop/route/file/v2/download/${isDownload == false}/${fileId}`
// }
// export default request
// 上传文件 买卖功能
export const uploadFileUrl = () => {
return `${aiShopBaseUrl}/aishop/api/file/v2/upload-file`
}
export { request,aiShopRequest,phoneRequest,newRequest};