Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let toastId
if (isGet) {
_config.method = 'GET'
_config.headers = {
...defaultHeaders,
..._config.headers
}
if (Object.keys(body).length) {
_url += `${_url.includes('?') ? '&' : '?'}${urlStringify(body)}`
}
} else {
_config.method = 'POST'
_config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
_config.body = urlStringify(body)
toastId = Toast.loading('Loading...', 8)
}
if (SHOW_LOG) {
log(`[fetchHTML] ${_url}`)
}
const isDev = require('../stores/system').default.state.dev
return fetch(_url, _config)
.then(res => {
// 开发模式
if (isDev) {
Alert.alert(
'dev',
`${JSON.stringify(_url)} ${JSON.stringify(_config)} ${res._bodyInit}`
)
}
if (!isGet) log(method, 'success', _url, _config, res)
export function xhr(
{ method = 'POST', url, data = {} } = {},
success = Function.prototype,
fail = Function.prototype
) {
// 避免userStore循环引用
const userStore = require('../stores/user').default
const { cookie: userCookie, userAgent } = userStore.userCookie
const toastId = Toast.loading('Loading...', 0)
const request = new XMLHttpRequest()
request.onreadystatechange = () => {
if (request.readyState !== 4) {
return
}
if (toastId) {
Portal.remove(toastId)
}
if (request.status === 200) {
success(request.responseText)
} else {
fail(request)
}
}
}
let _url = url
let toastId
if (isGet) {
_config.method = 'GET'
// 随机数防止接口CDN缓存
body.state = getTimestamp()
_url += `?${urlStringify(body)}`
} else {
_config.method = 'POST'
_config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
_config.body = urlStringify(body)
if (!noConsole) {
toastId = Toast.loading('Loading...', 0)
}
}
if (SHOW_LOG) {
log(`[fetchAPI] ${info || _url}`)
}
return fetch(_url, _config)
.then(response => {
if (toastId) Portal.remove(toastId)
return response.json()
})
.then(json => {
// 成功后清除失败计数
if (isGet) {
const key = `${url}|${urlStringify(data)}`
if (_retry[key]) _retry[key] = 0
...data
}
let _url = url
let toastKey
if (isGet) {
// 随机数防止接口CDN缓存
body.state = getTimestamp()
_url = `${url}?${urlStringify(body)}`
} else {
_config.method = 'POST'
_config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
_config.body = urlStringify(body)
if (!noConsole) {
toastKey = Toast.loading('Loading...', 0)
}
}
// log(info, _url, !isGet && _config)
return fetch(_url, _config)
.then(response => {
if (toastKey) {
Portal.remove(toastKey)
}
return response.json()
})
.then(json => {
// 成功后清除失败计数
if (isGet) {
const key = `${url}|${urlStringify(data)}`
if (retryCount[key]) {
export function xhr(
{ method = 'POST', url, data = {} } = {},
success = Function.prototype,
fail = Function.prototype
) {
// 避免userStore循环引用
const userStore = require('../stores/user').default
const { cookie: userCookie, userAgent } = userStore.userCookie
const toastKey = Toast.loading('Loading...', 0)
const request = new XMLHttpRequest()
request.onreadystatechange = () => {
if (request.readyState !== 4) {
return
}
if (toastKey) {
Portal.remove(toastKey)
}
if (request.status === 200) {
success(request.responseText)
} else {
fail()
}
}
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
Pragma: 'no-cache',
Referer: HOST
}
if (Object.keys(body).length) {
_url += `${_url.includes('?') ? '&' : '?'}${urlStringify(body)}`
}
} else {
_config.method = 'POST'
_config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
_config.body = urlStringify(body)
toastKey = Toast.loading('Loading...', 8)
}
log(`[fetchHTML] ${_url}`)
const systemStore = require('../stores/system').default
return fetch(_url, _config)
.then(res => {
// 开发模式
if (systemStore.state.dev) {
Alert.alert(
'dev',
`${JSON.stringify(_url)} ${JSON.stringify(_config)} ${res._bodyInit}`
)
}
// POST打印结果
if (!isGet) {
.then(res => {
// 开发模式
if (isDev) {
Alert.alert(
'dev',
`${JSON.stringify(_url)} ${JSON.stringify(_config)} ${res._bodyInit}`
)
}
if (!isGet) log(method, 'success', _url, _config, res)
if (toastId) Portal.remove(toastId)
return Promise.resolve(res.text())
})
.catch(err => {
handleGlobalError(event) {
// 报错时,取消加载动画
if(this.state.loadingCount > 0){
this.handleHideLoading(true)
}
if(event && event.type){
switch(event.type){
case 'SESSION_TIMEOUT':
Modal.alert('会话超时', '您的会话已超时,请重新登录')
break;
case 'SERVICE_ERROR':
if(event.message) {
Modal.alert('出错了', event.message)
}
break;
default:
if(event.message) {
Modal.alert('温馨提示', '系统未知异常')
}
break;
}
}
}
}
handleGlobalError(event) {
// 报错时,取消加载动画
if(this.state.loadingCount > 0){
this.handleHideLoading(true)
}
if(event && event.type){
switch(event.type){
case 'SESSION_TIMEOUT':
Modal.alert('会话超时', '您的会话已超时,请重新登录')
break;
case 'SERVICE_ERROR':
if(event.message) {
Modal.alert('出错了', event.message)
}
break;
default:
if(event.message) {
Modal.alert('温馨提示', '系统未知异常')
}
break;
}
}
}
}
request.onreadystatechange = () => {
if (request.readyState !== 4) {
return
}
if (toastKey) {
Portal.remove(toastKey)
}
if (request.status === 200) {
success(request.responseText)
} else {
fail()
}
}