2024-07-03 19:59:39 +08:00
|
|
|
import baseRequest from "./baseRequest";
|
2024-07-12 22:52:48 +08:00
|
|
|
import { get } from "./storeInfo";
|
2024-11-14 15:15:39 +08:00
|
|
|
import { Toast } from "antd-mobile";
|
2024-07-12 22:52:48 +08:00
|
|
|
// import { useRouter } from "next/navigation";
|
2024-07-03 19:59:39 +08:00
|
|
|
// import webviewBaseRequest from "@/utils/webviewBaseRequest";
|
2024-07-17 16:58:27 +08:00
|
|
|
|
2024-07-03 19:59:39 +08:00
|
|
|
// 创建一个封装 fetch 的函数
|
2024-08-06 16:21:34 +08:00
|
|
|
export default function customFetch(method, url, options = {}, needMid) {
|
2024-11-13 17:46:45 +08:00
|
|
|
const controller = new AbortController();
|
|
|
|
const signal = controller.signal;
|
|
|
|
|
|
|
|
// 设置超时
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
controller.abort("Request timed out");
|
2024-11-14 15:15:39 +08:00
|
|
|
}, 15000);
|
2024-07-17 16:58:27 +08:00
|
|
|
const base = baseRequest();
|
2024-07-12 22:52:48 +08:00
|
|
|
// 默认选项
|
|
|
|
const defaultOptions = {
|
|
|
|
method: method,
|
2024-11-13 17:46:45 +08:00
|
|
|
signal,
|
2024-07-12 22:52:48 +08:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-Req-Source-TF": "wittgenstein",
|
|
|
|
...options?.headers,
|
|
|
|
// 可以添加其他默认头部信息
|
|
|
|
},
|
|
|
|
// 可以添加其他默认选项
|
|
|
|
};
|
|
|
|
let newBody = { ...options?.body };
|
2024-08-06 16:21:34 +08:00
|
|
|
if (needMid) {
|
|
|
|
let mid = get("account")?.mid;
|
2024-09-09 13:12:59 +08:00
|
|
|
if (!mid) return;
|
2024-07-22 14:38:59 +08:00
|
|
|
newBody.mid = mid;
|
2024-07-12 22:52:48 +08:00
|
|
|
}
|
|
|
|
const body = JSON.stringify({ ...base, ...newBody });
|
2024-07-08 20:07:36 +08:00
|
|
|
|
2024-07-12 22:52:48 +08:00
|
|
|
// 合并选项
|
|
|
|
const mergedOptions = { ...defaultOptions, body };
|
2024-10-25 19:19:41 +08:00
|
|
|
// console.log("body", body);
|
2024-07-12 22:52:48 +08:00
|
|
|
// 返回 Promise 对象
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fetch(url, mergedOptions)
|
|
|
|
.then((response) => {
|
2024-11-13 17:46:45 +08:00
|
|
|
clearTimeout(timeout);
|
2024-08-05 21:10:36 +08:00
|
|
|
// // 检查响应状态码
|
2024-11-13 17:46:45 +08:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error("Network response was not ok " + response.statusText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// const contentLength = response.headers.get("Content-Length");
|
|
|
|
// if (!contentLength) {
|
|
|
|
// throw new Error("Content-Length is null");
|
2024-08-05 21:10:36 +08:00
|
|
|
// }
|
2024-11-13 17:46:45 +08:00
|
|
|
|
|
|
|
// const total = parseInt(contentLength, 10);
|
|
|
|
// let loaded = 0;
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
function push() {
|
2024-11-14 15:15:39 +08:00
|
|
|
reader
|
|
|
|
.read()
|
|
|
|
.then(({ done, value }) => {
|
|
|
|
if (done) {
|
|
|
|
controller.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// loaded += value.byteLength;
|
|
|
|
// const progress = (loaded / total) * 100;
|
|
|
|
// console.log(`Progress: ${progress.toFixed(2)}%`);
|
|
|
|
controller.enqueue(value);
|
|
|
|
push();
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
controller.error(error);
|
|
|
|
});
|
|
|
|
}
|
2024-11-13 17:46:45 +08:00
|
|
|
push();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const newResponse = new Response(stream);
|
|
|
|
return newResponse.json();
|
2024-07-12 22:52:48 +08:00
|
|
|
// 解析 JSON 响应
|
2024-11-13 17:46:45 +08:00
|
|
|
// return response.json();
|
2024-07-12 22:52:48 +08:00
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
// 解析成功,返回数据
|
|
|
|
resolve(data);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2024-11-13 17:46:45 +08:00
|
|
|
if (error.name === "AbortError") {
|
|
|
|
// console.log("Fetch request timed out.");
|
|
|
|
Toast.show({
|
|
|
|
icon: "fail",
|
|
|
|
content: "请求超时,请检查网络后重试。",
|
|
|
|
position: "top",
|
|
|
|
});
|
|
|
|
// 这里可以实现重试逻辑
|
|
|
|
// fetchDataWithRetry();
|
|
|
|
} else {
|
|
|
|
console.error("Fetch error:", error);
|
|
|
|
}
|
2024-07-22 16:41:33 +08:00
|
|
|
// console.log("error", error);
|
2024-07-22 14:38:59 +08:00
|
|
|
// Toast.show({
|
|
|
|
// icon: "fail",
|
|
|
|
// content: error.toString(),
|
|
|
|
// position: "top",
|
|
|
|
// });
|
2024-07-12 22:52:48 +08:00
|
|
|
// 请求失败,拒绝 Promise
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|