46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
import baseRequest from "./baseRequest";
|
|
import {get} from "./storeInfo";
|
|
// import webviewBaseRequest from "@/utils/webviewBaseRequest";
|
|
const base = baseRequest();
|
|
// 创建一个封装 fetch 的函数
|
|
export default function customFetch(method, url, options = {},mid) {
|
|
// 默认选项
|
|
const defaultOptions = {
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Req-Source-TF': 'wittgenstein',
|
|
...options.headers
|
|
// 可以添加其他默认头部信息
|
|
}
|
|
// 可以添加其他默认选项
|
|
};
|
|
let newBody = {...options.body}
|
|
if(mid){
|
|
newBody.mid=get("account").mid
|
|
}
|
|
const body=JSON.stringify({...base,...newBody})
|
|
// 合并选项
|
|
const mergedOptions = { ...defaultOptions, body};
|
|
// console.log("mergedOptions",mergedOptions)
|
|
// 返回 Promise 对象
|
|
return new Promise((resolve, reject) => {
|
|
fetch(url, mergedOptions)
|
|
.then(response => {
|
|
// 检查响应状态码
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
// 解析 JSON 响应
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
// 解析成功,返回数据
|
|
resolve(data);
|
|
})
|
|
.catch(error => {
|
|
// 请求失败,拒绝 Promise
|
|
reject(error);
|
|
});
|
|
});
|
|
} |