|
|
|
@ -1,5 +1,22 @@
|
|
|
|
|
|
|
|
|
|
import { WEB_VERSION } from '@/config'
|
|
|
|
|
import { BUILD_VERSION } from '@/config'
|
|
|
|
|
|
|
|
|
|
const customHeaders = []
|
|
|
|
|
|
|
|
|
|
// 添加 HTTP Reuqest 自定义头部
|
|
|
|
|
export function appendRequestHeader(n, v) {
|
|
|
|
|
customHeaders.push({
|
|
|
|
|
name: n,
|
|
|
|
|
value: v
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRequestHeader() {
|
|
|
|
|
return customHeaders.reduce((acc, item) => {
|
|
|
|
|
acc[item.name] = item.value;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkStatus(response) {
|
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
@ -18,7 +35,7 @@ export function fetchText(url) {
|
|
|
|
|
return fetch(url, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Web-Version': WEB_VERSION
|
|
|
|
|
'X-Web-Version': BUILD_VERSION
|
|
|
|
|
}
|
|
|
|
|
}).then(checkStatus)
|
|
|
|
|
.then(response => response.text())
|
|
|
|
@ -30,10 +47,12 @@ export function fetchText(url) {
|
|
|
|
|
export function fetchJSON(url, data) {
|
|
|
|
|
const params = data ? new URLSearchParams(data).toString() : '';
|
|
|
|
|
const ifp = url.includes('?') ? '&' : '?';
|
|
|
|
|
const headerObj = getRequestHeader()
|
|
|
|
|
return fetch(`${url}${ifp}${params}`, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Web-Version': WEB_VERSION
|
|
|
|
|
'X-Web-Version': BUILD_VERSION,
|
|
|
|
|
...headerObj
|
|
|
|
|
}
|
|
|
|
|
}).then(checkStatus)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
@ -47,7 +66,7 @@ export function postForm(url, data) {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: data,
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Web-Version': WEB_VERSION
|
|
|
|
|
'X-Web-Version': BUILD_VERSION
|
|
|
|
|
}
|
|
|
|
|
}).then(checkStatus)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
@ -62,7 +81,7 @@ export function postJSON(url, obj) {
|
|
|
|
|
body: JSON.stringify(obj),
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-type': 'application/json; charset=UTF-8',
|
|
|
|
|
'X-Web-Version': WEB_VERSION
|
|
|
|
|
'X-Web-Version': BUILD_VERSION
|
|
|
|
|
}
|
|
|
|
|
}).then(checkStatus)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
@ -77,7 +96,7 @@ export function postStream(url, obj) {
|
|
|
|
|
body: JSON.stringify(obj),
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-type': 'application/octet-stream',
|
|
|
|
|
'X-Web-Version': WEB_VERSION
|
|
|
|
|
'X-Web-Version': BUILD_VERSION
|
|
|
|
|
}
|
|
|
|
|
}).then(checkStatus)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|