113 lines
3.8 KiB
TypeScript
113 lines
3.8 KiB
TypeScript
// export default {
|
||
// // baseURL: 'http://103.39.218.177:8082',
|
||
// baseURL: `${process.env.NEXT_PUBLIC_CLIENT_BASE_URL}`,
|
||
// method: 'post',
|
||
// //`timeout`选项定义了请求发出的延迟毫秒数
|
||
// //如果请求花费的时间超过延迟的时间,那么请求会被终止
|
||
// timeout: 60 * 1000,
|
||
// //发送请求前允许修改数据
|
||
// // transformRequest: [function (data: any) {
|
||
// // return data;
|
||
// // }],
|
||
// // //数据发送到then/catch方法之前允许数据改动
|
||
// // transformResponse: [function (data: any) {
|
||
// // return data;
|
||
// // }],
|
||
// // headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||
// headers: { 'Content-Type': 'application/json; charset=UTF-8' },
|
||
// // withCredentials: false,//跨域请求时是否携带cookie
|
||
// responseType: 'json',//响应数据类型
|
||
// // xsrfCookieName: 'XSRF-TOKEN',
|
||
// // xsrfHeaderName: 'X-XSRF-TOKEN',
|
||
// // onUploadProgress: function (progressEvent: any) { },//上传进度事件
|
||
// // onDownloadProgress: function (progressEvent: any) { },//下载进度事件
|
||
// //`validateStatus`定义了是否根据http相应状态码,来resolve或者reject promise
|
||
// //如果`validateStatus`返回true(或者设置为`null`或者`undefined`),那么promise的状态将会是resolved,否则其状态就是rejected
|
||
// // validateStatus: function (status: number) {
|
||
// // return status >= 200 && status < 300 // 默认的
|
||
// // },
|
||
// //`maxRedirects`定义了在nodejs中重定向的最大数量
|
||
// // maxRedirects: 5
|
||
// } as any;
|
||
|
||
|
||
// import { getRuntimeEnv } from "../ipconfig";
|
||
|
||
// export async function getAxiosConfig() {
|
||
// let ip = await getRuntimeEnv("SUPABASE_URL"); // 直接用你 lib/ipconfig 里的方法
|
||
// if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 axios 配置");
|
||
|
||
// let protocol = "http";
|
||
// let port = 80;
|
||
|
||
// if (typeof window !== "undefined" && window.location && window.location.protocol) {
|
||
// protocol = window.location.protocol.replace(":", "");
|
||
// port = protocol === "https" ? 443 : 80;
|
||
|
||
// // ✅ HTTPS + 浏览器时,替换 IP,避免 TLS/CORS 报错
|
||
// if (protocol === "https") {
|
||
// ip = window.location.hostname;
|
||
// }
|
||
// }
|
||
|
||
// return {
|
||
// baseURL: `${protocol}://${ip}:${port}`, // 端口如需动态可再加参数
|
||
// method: 'post',
|
||
// timeout: 60 * 1000,
|
||
// headers: { 'Content-Type': 'application/json; charset=UTF-8' },
|
||
// responseType: 'json'
|
||
// } as const;
|
||
// }
|
||
|
||
|
||
import { getRuntimeEnv } from "../ipconfig";
|
||
//import { headers } from "next/headers";
|
||
|
||
export async function getAxiosConfig() {
|
||
let ip = await getRuntimeEnv("SUPABASE_URL");
|
||
if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 axios 配置");
|
||
|
||
let protocol = "http";
|
||
let port = 80;
|
||
|
||
if (typeof window !== "undefined" && window.location && window.location.protocol) {
|
||
// ✅ CSR 场景
|
||
protocol = window.location.protocol.replace(":", "");
|
||
port = protocol === "https" ? 443 : 80;
|
||
|
||
if (protocol === "https") {
|
||
ip = window.location.hostname;
|
||
}
|
||
} else {
|
||
// ✅ SSR 场景,延迟导入 headers
|
||
const { headers } = await import("next/headers");
|
||
const hdrs = headers();
|
||
|
||
const forwardedProto = hdrs.get("x-forwarded-proto");
|
||
const hostHeader = hdrs.get("host");
|
||
|
||
if (forwardedProto) {
|
||
protocol = forwardedProto;
|
||
}
|
||
|
||
if (hostHeader) {
|
||
ip = hostHeader;
|
||
if (hostHeader.includes(":")) {
|
||
const parts = hostHeader.split(":");
|
||
ip = parts[0];
|
||
port = parseInt(parts[1]);
|
||
} else {
|
||
port = (protocol === "https") ? 443 : 80;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
return {
|
||
baseURL: `${protocol}://${ip}:${port}`,
|
||
method: 'post',
|
||
timeout: 60 * 1000,
|
||
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
|
||
responseType: 'json'
|
||
} as const;
|
||
} |