Files
xiaozhi-esp32-server/main/manager-web/src/apis/httpRequest.js
T
fc3f982309 优化登录 token 校验 (#345)
* 获得获取所有模型名称等功能 (#343)

* 优化登录 token 校验 (#330)

* fix manager-api bug

* 优化登录注册流程,跑通注册登录、接口

* update package

* 优化登录 token 校验:

1、后端新增 /api/v1/user/info 接口,优化 Oauth2Filter.getRequestToken 逻辑;
2、前端 /api/v1/user/login 登录成功后,保存 token 至浏览器本地;
3、前端请求添加本地 token。

---------

Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>

* update:分离home页面的组件

---------

Co-authored-by: CGD <3030332422@qq.com>
Co-authored-by: zhisheng <pzs034@gmail.com>
Co-authored-by: hrz <1710360675@qq.com>
2025-03-14 23:48:59 +08:00

150 lines
4.0 KiB
JavaScript
Executable File

import {goToPage, showDanger, showWarning, isNotNull} from '../utils/index'
import Constant from '../utils/constant'
import Fly from 'flyio/dist/npm/fly';
import store from '../store/index'
const fly = new Fly()
// 设置超时
fly.config.timeout = 30000
/**
* Request服务封装
*/
export default {
sendRequest,
reAjaxFun,
clearRequestTime
}
function sendRequest() {
return {
_sucCallback: null,
_failCallback: null,
_method: 'GET',
_data: {},
_header: {'content-type': 'application/json; charset=utf-8'},
_url: '',
_responseType: undefined, // 新增响应类型字段
'send'() {
if(isNotNull(store.getters.getToken)){
this._header.Authorization = 'Bearer ' + (JSON.parse(store.getters.getToken)).token
}
// 打印请求信息
fly.request(this._url, this._data, {
method: this._method,
headers: this._header,
responseType: this._responseType
}).then((res) => {
const error = httpHandlerError(res, this._failCallback);
if (error) {
return
}
if (this._sucCallback) {
this._sucCallback(res)
}
}).catch((res) => {
// 打印失败响应
console.log('catch', res)
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
},
// 新增类型设置方法
'type'(responseType) {
this._responseType = responseType;
return this;
}
}
}
/**
* Info 请求完成后返回信息
* callBack 回调函数
* errTip 自定义错误信息
*/
// 在错误处理函数中添加日志
function httpHandlerError(info, callBack) {
console.log('httpHandlerError', info)
/** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */
let networkError = false
if (info.status === 200) {
if (info.data.code === 'success' || info.data.code === 0 || info.data.code === undefined) {
return networkError
}else if (info.data.code === 401) {
goToPage(Constant.PAGE.LOGIN, true)
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 + ')')
}
if (fn) {
setTimeout(() => {
fn()
}, reAjaxSec * 1000)
}
}
function clearRequestTime() {
requestTime = 0
}