//@ts-nocheck import { Component } from 'react' import { DeviceEventEmitter } from 'react-native'; import { toast, BaseUrl, isIos, toastShort, clearToast, version } from '../utils'; import redux from '../redux/store'; /** * GET 请求时,拼接请求URL * @param url 请求URL * @param params 请求参数 * @returns {*} */ const handleUrl = url => params => { if (params) { let paramsArray = [] Object.keys(params).forEach(key => paramsArray.push(key + '=' + encodeURIComponent(params[key]))); if (url.search(/\?/) === -1) { typeof (params) === 'object' ? url += '?' + paramsArray.join('&') : url; } else { url += '&' + paramsArray.join('&'); } }; console.log('GetUrl', JSON.stringify(BaseUrl + url)) return BaseUrl + url; } /** * fetch 网络请求超时处理 * @param original_promise 原始的fetch * @param timeout 超时时间 30s * */ const timeoutFetch = (original_fetch, timeout = 10 * 1000) => { let timeoutBlock = () => { } let timeout_promise = new Promise((resolve, reject) => { timeoutBlock = () => { // 请求超时处理 reject('请求超时'); } }) // Promise.race(iterable)方法返回一个promise // 这个promise在iterable中的任意一个promise被解决或拒绝后,立刻以相同的解决值被解决或以相同的拒绝原因被拒绝。 let abortable_promise = Promise.race([ original_fetch, timeout_promise ]) setTimeout(() => { timeoutBlock() }, timeout) return abortable_promise } const isEmojiCharacter = (substring) => { console.log("isEmojiCharacter----->", substring) for ( var i = 0; i < substring.length; i++) { var hs = substring.charCodeAt(i); if (0xd800 <= hs && hs <= 0xdbff) { if (substring.length > 1) { var ls = substring.charCodeAt(i + 1); var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000; if (0x1d000 <= uc && uc <= 0x1f77f) { console.log("存在表情") return true; } } } else if (substring.length > 1) { var ls = substring.charCodeAt(i + 1); if (ls == 0x20e3) { console.log("存在表情") return true; } } else { if (0x2100 <= hs && hs <= 0x27ff) { return true; } else if (0x2B05 <= hs && hs <= 0x2b07) { return true; } else if (0x2934 <= hs && hs <= 0x2935) { return true; } else if (0x3297 <= hs && hs <= 0x3299) { return true; } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) { console.log("存在表情") return true; } } } } /** * 网络请求工具类 */ export default class HttpUtils extends Component { /** * 基于fetch 封装的GET 网络请求 * @param url 请求URL * @param params 请求参数 * @returns {Promise} */ public onNetWorkError = () => { // toastShort("您的网络不稳定,请稍后重试") }; static getRequest = (url, params?= {}) => { console.log("get参数", params) let headers = new Headers({ 'Accept': 'application/json', 'Content-Type': 'application/json', }); const state = redux.getState(); if (url !== '/font-api/auth/login') { if (state && state.userMsg && state.userMsg.userInfo.token) { headers.append("token", state.userMsg.userInfo.token); headers.append("sign", "yyk_api"); } } console.log("get-header-is----", headers) return timeoutFetch(fetch(handleUrl(url)(params), { method: 'GET', headers: headers, mode: 'cors' })).then(response => { return response.json(); }).then(response => { return response; }).catch(error => { let reqeustError = error + ''; console.log("reqeustError", reqeustError) if (reqeustError == 'TypeError: Network request failed') { // toastShort("Oops! 请检查您的网络连接") } else if (reqeustError === "请求超时") { // toastShort('服务器繁忙,请您稍后重试') } else { // alert("2") // toast("前方拥堵,请您稍后重试"); } }) } /** * 基于fetch 的 POST 请求 * @param url 请求的URL * @param params 请求参数 * @returns {Promise} */ static postRequrst = (url, params = {}) => { console.log("postRequrstParams", params) let stringParams = JSON.stringify(params); console.log("stringParams", stringParams) let headers = new Headers({ 'Accept': 'application/json', 'Content-Type': 'application/json', }); let fetchUrl = BaseUrl + url; // alert("fetchUrl" + fetchUrl) const state = redux.getState(); if (url !== '/font-api/auth/login') { if (state && state.userMsg && state.userMsg.userInfo && state.userMsg.userInfo.token) { headers.append("token", state.userMsg.userInfo.token); headers.append("sign", "yyk_api"); } } return timeoutFetch(fetch(fetchUrl, { method: 'POST', headers: headers, body: stringParams })).then(response => { console.log(response) if (response.ok) { return response.json(); } else { toast("网络出错了,请稍后重试") } }).then(response => { return response; }).catch(error => { let reqeustError = error + ''; console.log("reqeustError", reqeustError) if (reqeustError == 'TypeError: Network request failed') { // toastShort("Oops! 请检查您的网络连接") } else if (reqeustError === "请求超时") { // toastShort('服务器繁忙,请您稍后重试') } else { // toast("前方拥堵,请您稍后重试"); } }) } }