From f0b4c7b7281ce0507094c2925d1997b1cc68df8c Mon Sep 17 00:00:00 2001 From: Admin <376654749@qq.com> Date: Thu, 6 Jan 2022 03:25:50 +0000 Subject: [PATCH] missing file --- rc-busness/config/async/env.js | 17 +++++++ rc-busness/config/async/fetch.js | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 rc-busness/config/async/env.js create mode 100644 rc-busness/config/async/fetch.js diff --git a/rc-busness/config/async/env.js b/rc-busness/config/async/env.js new file mode 100644 index 00000000..9242ccff --- /dev/null +++ b/rc-busness/config/async/env.js @@ -0,0 +1,17 @@ +/** + * 配置编译环境和线上环境之间的切换 + * baseUrl: 域名地址 + * routerMode: 路由模式 + * imgBaseUrl: 图片所在域名地址 + */ + +let baseUrl = 'http://47.96.75.242:10086/royalcanin/royalcanin/' +if (process.env.NODE_ENV === 'development') { + baseUrl = 'http://47.96.75.242:10086/royalcanin/royalcanin/'; +} else if (process.env.NODE_ENV === 'production') { + baseUrl = 'http://47.96.75.242:10086/royalcanin/royalcanin/'; +} +export { + baseUrl, + +} diff --git a/rc-busness/config/async/fetch.js b/rc-busness/config/async/fetch.js new file mode 100644 index 00000000..2eafb5a5 --- /dev/null +++ b/rc-busness/config/async/fetch.js @@ -0,0 +1,82 @@ +import { + baseUrl +} from './env' + +export default async(url = '', data = {}, type = 'GET', method = 'fetch') => { + type = type.toUpperCase() + url = baseUrl + url + + if (type === 'GET') { + // 数据拼接字符串 + let dataStr = '' + Object.keys(data).forEach(key => { + dataStr += key + '=' + data[key] + '&' + }) + + if (dataStr !== '') { + dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')) + url = url + '?' + dataStr + } + } + + if (window.fetch && method === 'fetch') { + let requestConfig = { + + method: type, + headers: { + // 'Accept': 'text/plain', + 'Content-Type': 'application/json' + }, + + mode: 'cors', + // channelId:'H5@2021', + cache: 'force-cache' + } + + if (type === 'POST') { + Object.defineProperty(requestConfig, 'body', { + value: JSON.stringify(data) + }) + } + try { + const response = await fetch(url, requestConfig) + const responseJson = await response.json() + return responseJson + } catch (error) { + throw new Error(error) + } + } else { + + return new Promise((resolve, reject) => { + let requestObj + + if (window.XMLHttpRequest) { + requestObj = new XMLHttpRequest() + } + + let sendData = '' + if (type === 'POST') { + sendData = JSON.stringify(data) + + } + requestObj.open(type, url, true) + requestObj.setRequestHeader('Content-Type', 'application/json') + requestObj.send(sendData) + + + requestObj.onreadystatechange = () => { + if (requestObj.readyState === 4) { + if (requestObj.status === 200) { + let obj = requestObj.response + if (typeof obj !== 'object') { + obj = JSON.parse(obj) + } + resolve(obj) + } else { + reject(requestObj) + } + } + } + }) + } +}