Files
xiaozhi-esp32-server/main/manager-web/src/apis/httpRequest.js
T

149 lines
4.0 KiB
JavaScript
Raw Normal View History

2025-03-07 10:01:20 +08:00
import Fly from 'flyio/dist/npm/fly';
2025-04-05 20:19:28 +08:00
import store from '../store/index';
import Constant from '../utils/constant';
import { goToPage, isNotNull, showDanger, showWarning } from '../utils/index';
2025-03-07 10:01:20 +08:00
const fly = new Fly()
// 设置超时
fly.config.timeout = 30000
/**
* Request服务封装
*/
export default {
sendRequest,
reAjaxFun,
clearRequestTime
}
function sendRequest() {
return {
_sucCallback: null,
_failCallback: null,
_method: 'GET',
_data: {},
2025-04-05 20:19:28 +08:00
_header: { 'content-type': 'application/json; charset=utf-8' },
2025-03-07 10:01:20 +08:00
_url: '',
2025-03-12 13:38:44 +08:00
_responseType: undefined, // 新增响应类型字段
2025-03-07 10:01:20 +08:00
'send'() {
2025-04-05 20:19:28 +08:00
if (isNotNull(store.getters.getToken)) {
2025-03-14 23:48:59 +08:00
this._header.Authorization = 'Bearer ' + (JSON.parse(store.getters.getToken)).token
}
2025-03-12 13:38:44 +08:00
// 打印请求信息
2025-03-07 10:01:20 +08:00
fly.request(this._url, this._data, {
method: this._method,
2025-03-12 13:38:44 +08:00
headers: this._header,
responseType: this._responseType
2025-03-07 10:01:20 +08:00
}).then((res) => {
2025-03-12 13:38:44 +08:00
const error = httpHandlerError(res, this._failCallback);
2025-03-07 10:01:20 +08:00
if (error) {
return
}
2025-04-05 20:19:28 +08:00
2025-03-07 10:01:20 +08:00
if (this._sucCallback) {
this._sucCallback(res)
}
}).catch((res) => {
2025-03-12 13:38:44 +08:00
// 打印失败响应
2025-03-14 23:48:59 +08:00
console.log('catch', res)
2025-03-07 10:01:20 +08:00
httpHandlerError(res, this._failCallback)
})
return this
},
'success'(callback) {
this._sucCallback = callback
return this
},
'fail'(callback) {
this._failCallback = callback
return this
},
'url'(url) {
if (url) {
url = url.replaceAll('$', '/')
}
this._url = url
return this
},
'data'(data) {
this._data = data
return this
},
'method'(method) {
this._method = method
return this
},
'header'(header) {
this._header = header
return this
},
'showLoading'(showLoading) {
this._showLoading = showLoading
return this
},
'async'(flag) {
this.async = flag
2025-03-12 13:38:44 +08:00
},
// 新增类型设置方法
'type'(responseType) {
this._responseType = responseType;
return this;
2025-03-07 10:01:20 +08:00
}
}
}
/**
* Info 请求完成后返回信息
* callBack 回调函数
* errTip 自定义错误信息
*/
2025-03-12 13:38:44 +08:00
// 在错误处理函数中添加日志
2025-03-07 10:01:20 +08:00
function httpHandlerError(info, callBack) {
2025-04-05 20:19:28 +08:00
2025-03-07 10:01:20 +08:00
/** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */
let networkError = false
if (info.status === 200) {
2025-03-12 13:38:44 +08:00
if (info.data.code === 'success' || info.data.code === 0 || info.data.code === undefined) {
2025-03-07 10:01:20 +08:00
return networkError
2025-04-05 20:19:28 +08:00
} else if (info.data.code === 401) {
2025-04-01 10:59:35 +08:00
store.commit('clearAuth');
goToPage(Constant.PAGE.LOGIN, true);
2025-03-07 10:01:20 +08:00
return true
} else {
showDanger(info.data.msg)
return true
}
}
if (callBack) {
callBack(info)
} else {
showDanger(`网络请求出现了错误【${info.status}】`)
}
return true
}
let requestTime = 0
let reAjaxSec = 2
function reAjaxFun(fn) {
let nowTimeSec = new Date().getTime() / 1000
if (requestTime === 0) {
requestTime = nowTimeSec
}
let ajaxIndex = parseInt((nowTimeSec - requestTime) / reAjaxSec)
if (ajaxIndex > 10) {
showWarning('似乎无法连接服务器')
} else {
showWarning('正在连接服务器(' + ajaxIndex + ')')
}
2025-04-06 18:28:39 +08:00
if (ajaxIndex < 10 && fn) {
2025-03-07 10:01:20 +08:00
setTimeout(() => {
fn()
}, reAjaxSec * 1000)
}
}
function clearRequestTime() {
requestTime = 0
}