调整request

master_basic
鲁誉程 1 year ago
parent e23bfccec6
commit b132be9544

@ -1,4 +1,7 @@
import { message } from "antd"; import { DEV } from './env/dev'
// 是否写死请求地址
const hardCode = true;
/** /**
* GET * GET
@ -13,14 +16,14 @@ export function getRequest(url: string, params: object) {
.join('&'); .join('&');
// 拼接查询字符串到 URL // 拼接查询字符串到 URL
let urlWithParams = url; let urlWithParams = ((hardCode ? DEV.PROXY_SERVER : '')) + url;
if (queryString) { if (queryString) {
urlWithParams += `?${queryString}`; urlWithParams += `?${queryString}`;
} }
return fetch(urlWithParams, { return fetch(urlWithParams, {
method: 'GET', method: 'GET',
mode:"cors", mode: "cors",
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
@ -40,7 +43,7 @@ export function getRequest(url: string, params: object) {
* @returns {Promise} - Promise response error * @returns {Promise} - Promise response error
*/ */
export function postRequest(url: string, data: object) { export function postRequest(url: string, data: object) {
return fetch('http://127.0.0.1:8088' + url, { return fetch(((hardCode ? DEV.PROXY_SERVER : '')) + url, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -48,7 +51,7 @@ export function postRequest(url: string, data: object) {
'User-Agent': '', 'User-Agent': '',
'sec-ch-ua': '' 'sec-ch-ua': ''
}, },
mode:"cors", mode: "cors",
body: JSON.stringify(data), body: JSON.stringify(data),
}) })
.then(response => { .then(response => {
@ -68,12 +71,12 @@ export function postRequest(url: string, data: object) {
export function postFormDataRequest(url: string, data: object) { export function postFormDataRequest(url: string, data: object) {
const formData = new FormData(); const formData = new FormData();
for (const key in data) { for (const key in data) {
formData.append(key, data[key]); formData.append(key, data[key]);
} }
return fetch(url, { return fetch(((hardCode ? DEV.PROXY_SERVER : '')) + url, {
method: 'POST', method: 'POST',
mode:"cors", mode: "cors",
body: formData body: formData
}) })
.then(response => { .then(response => {
@ -91,9 +94,9 @@ export function postFormDataRequest(url: string, data: object) {
*/ */
export function deleteRequest(url: string, params: object) { export function deleteRequest(url: string, params: object) {
const searchParams = new URLSearchParams(params); const searchParams = new URLSearchParams(params);
return fetch(`${url}?${searchParams.toString()}`, { return fetch(`${((hardCode ? DEV.PROXY_SERVER : '')) + url}?${searchParams.toString()}`, {
method: 'DELETE', method: 'DELETE',
mode:"cors", mode: "cors",
}) })
.then(response => { .then(response => {
if (!response.ok) { throw new Error('请求失败'); } if (!response.ok) { throw new Error('请求失败'); }
@ -110,12 +113,12 @@ export function deleteRequest(url: string, params: object) {
* @returns {Promise} - Promise * @returns {Promise} - Promise
*/ */
export function putRequest(url: string, data: object) { export function putRequest(url: string, data: object) {
return fetch(url, { return fetch(((hardCode ? DEV.PROXY_SERVER : '')) + url, {
method: 'PUT', method: 'PUT',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
mode:"cors", mode: "cors",
body: JSON.stringify(data) body: JSON.stringify(data)
}) })
.then(response => { .then(response => {
@ -133,9 +136,9 @@ export function putRequest(url: string, data: object) {
* @returns {Promise} - Promise blob error * @returns {Promise} - Promise blob error
*/ */
export function downloadFile(url: string, fileName?: string) { export function downloadFile(url: string, fileName?: string) {
return fetch(url, { return fetch(((hardCode ? DEV.PROXY_SERVER : '')) + url, {
method: 'GET', method: 'GET',
mode:"cors", mode: "cors",
}) })
.then(response => { .then(response => {
if (!response.ok) { throw new Error('下载失败'); } if (!response.ok) { throw new Error('下载失败'); }
@ -160,10 +163,10 @@ export function downloadFile(url: string, fileName?: string) {
* @returns {Promise} - Promise * @returns {Promise} - Promise
*/ */
export function uploadFile(url: string, formData: any) { export function uploadFile(url: string, formData: any) {
return fetch(url, { return fetch(((hardCode ? DEV.PROXY_SERVER : '')) + url, {
method: 'POST', method: 'POST',
body: formData, body: formData,
mode:"cors" mode: "cors"
}) })
.then(response => { .then(response => {
if (!response.ok) { throw new Error('上传失败'); } if (!response.ok) { throw new Error('上传失败'); }

Loading…
Cancel
Save