You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.3 KiB
JavaScript
149 lines
3.3 KiB
JavaScript
|
|
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;
|
|
}, {});
|
|
}
|
|
|
|
const initParams = [];
|
|
export function appendRequestParams(n, v) {
|
|
initParams.push({
|
|
name: n,
|
|
value: v
|
|
})
|
|
}
|
|
function getRequestInitParams() {
|
|
return initParams.reduce((acc, item) => {
|
|
acc[item.name] = item.value;
|
|
return acc;
|
|
}, {});
|
|
}
|
|
|
|
function checkStatus(response) {
|
|
if (response.status >= 200 && response.status < 300) {
|
|
return response
|
|
} else {
|
|
const message =
|
|
'Fetch error: ' + response.url + ' ' + response.status + ' (' +
|
|
response.statusText + ')'
|
|
const error = new Error(message)
|
|
error.response = response
|
|
throw error
|
|
}
|
|
}
|
|
|
|
function checkBizCode(responseJson) {
|
|
if (responseJson.errcode === 0) {
|
|
return responseJson;
|
|
} else {
|
|
throw new Error(responseJson.errmsg + ': ' + responseJson.errcode);
|
|
}
|
|
}
|
|
|
|
export function fetchText(url) {
|
|
const headerObj = getRequestHeader()
|
|
return fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'X-Web-Version': BUILD_VERSION,
|
|
...headerObj
|
|
}
|
|
}).then(checkStatus)
|
|
.then(response => response.text())
|
|
.catch(error => {
|
|
throw error
|
|
})
|
|
}
|
|
|
|
export function fetchJSON(url, data = {}) {
|
|
const initParams = getRequestInitParams();
|
|
const params4get = Object.assign({}, initParams, data);
|
|
const params = params4get ? new URLSearchParams(params4get).toString() : '';
|
|
const ifp = url.includes('?') ? '&' : '?';
|
|
const headerObj = getRequestHeader();
|
|
const fUrl = params !== '' ? `${url}${ifp}${params}` : url;
|
|
return fetch(fUrl, {
|
|
method: 'GET',
|
|
headers: {
|
|
'X-Web-Version': BUILD_VERSION,
|
|
...headerObj
|
|
}
|
|
}).then(checkStatus)
|
|
.then(response => response.json())
|
|
.then(checkBizCode)
|
|
.catch(error => {
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
export function postForm(url, data) {
|
|
const initParams = getRequestInitParams();
|
|
Object.keys(initParams).forEach(key => {
|
|
if (! data.has(key)) {
|
|
data.append(key, initParams[key]);
|
|
}
|
|
});
|
|
const headerObj = getRequestHeader()
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
body: data,
|
|
headers: {
|
|
'X-Web-Version': BUILD_VERSION,
|
|
...headerObj
|
|
}
|
|
}).then(checkStatus)
|
|
.then(response => response.json())
|
|
.then(checkBizCode)
|
|
.catch(error => {
|
|
throw error
|
|
})
|
|
}
|
|
|
|
export function postJSON(url, obj) {
|
|
const headerObj = getRequestHeader()
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
body: JSON.stringify(obj),
|
|
headers: {
|
|
'Content-type': 'application/json; charset=UTF-8',
|
|
'X-Web-Version': BUILD_VERSION,
|
|
...headerObj
|
|
}
|
|
}).then(checkStatus)
|
|
.then(response => response.json())
|
|
.then(checkBizCode)
|
|
.catch(error => {
|
|
throw error
|
|
})
|
|
}
|
|
|
|
export function postStream(url, obj) {
|
|
const headerObj = getRequestHeader()
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
body: JSON.stringify(obj),
|
|
headers: {
|
|
'Content-type': 'application/octet-stream',
|
|
'X-Web-Version': BUILD_VERSION,
|
|
...headerObj
|
|
}
|
|
}).then(checkStatus)
|
|
.then(response => response.json())
|
|
.catch(error => {
|
|
throw error
|
|
})
|
|
}
|