mirror of
https://github.com/fugary/simple-element-plus-template.git
synced 2026-09-11 09:23:53 +00:00
opt: 优化部分loading等异常问题
This commit is contained in:
@@ -17,7 +17,7 @@ NProgress.configure({ showSpinner: false, trickleSpeed: 500 })
|
|||||||
*/
|
*/
|
||||||
const checkRouteLoading = route => route?.meta?.loading ?? GLOBAL_ROUTE_LOADING
|
const checkRouteLoading = route => route?.meta?.loading ?? GLOBAL_ROUTE_LOADING
|
||||||
|
|
||||||
const startRouteLoading = (route) => {
|
export const startRouteLoading = (route) => {
|
||||||
if (checkRouteLoading(route)) {
|
if (checkRouteLoading(route)) {
|
||||||
NProgress.start()
|
NProgress.start()
|
||||||
if (GLOBAL_ROUTE_NEW_LOADING) {
|
if (GLOBAL_ROUTE_NEW_LOADING) {
|
||||||
@@ -25,17 +25,16 @@ const startRouteLoading = (route) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const endRouteLoading = (route) => {
|
|
||||||
if (checkRouteLoading(route)) {
|
export const endRouteLoading = (route, force = false) => {
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
if (GLOBAL_ROUTE_NEW_LOADING) {
|
if (GLOBAL_ROUTE_NEW_LOADING) {
|
||||||
$coreHideLoading()
|
$coreHideLoading(force)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查路有权限
|
* 检查路由权限
|
||||||
* @param to {RouteRecordSingleViewWithChildren} 目的地路由
|
* @param to {RouteRecordSingleViewWithChildren} 目的地路由
|
||||||
* @param from 出事路由
|
* @param from 出事路由
|
||||||
* @returns {{name: string}|boolean}
|
* @returns {{name: string}|boolean}
|
||||||
@@ -50,7 +49,7 @@ export const checkRouteAuthority = async (to) => {
|
|||||||
// check权限
|
// check权限
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
endRouteLoading(to)
|
endRouteLoading(to, true)
|
||||||
return { name: 'Login' }
|
return { name: 'Login' }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +60,8 @@ const processRouteSavedParam = (to, from) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const processRouteLoading = (to, from) => {
|
export const processRouteLoading = (to, from) => {
|
||||||
endRouteLoading(to)
|
const isLoginRoute = to.name === 'Login' || to.path === '/login'
|
||||||
|
endRouteLoading(to, isLoginRoute)
|
||||||
if (to.query?.language && Object.values(GlobalLocales).includes(to.query.language)) {
|
if (to.query?.language && Object.values(GlobalLocales).includes(to.query.language)) {
|
||||||
$changeLocale(to.query?.language)
|
$changeLocale(to.query?.language)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ export const useReload = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const $logout = () => {
|
export const $logout = () => {
|
||||||
|
$coreHideLoading(true)
|
||||||
useLoginConfigStore().logout()
|
useLoginConfigStore().logout()
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
$goto('/login')
|
$goto('/login')
|
||||||
@@ -206,32 +207,56 @@ export const $logout = () => {
|
|||||||
const globalLoadingConfig = {
|
const globalLoadingConfig = {
|
||||||
delay: LOADING_DELAY,
|
delay: LOADING_DELAY,
|
||||||
globalLoading: null,
|
globalLoading: null,
|
||||||
delayLoadingId: null
|
delayLoadingId: null,
|
||||||
|
count: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
export const $coreShowLoading = (message) => {
|
export const $coreShowLoading = (messageOrConfig) => {
|
||||||
const globalLoading = globalLoadingConfig.globalLoading
|
globalLoadingConfig.count++
|
||||||
if (globalLoading) {
|
if (globalLoadingConfig.count > 1) {
|
||||||
globalLoading.close()
|
return
|
||||||
}
|
}
|
||||||
const openLoading = () => ElLoading.service(Object.assign({
|
if (globalLoadingConfig.delayLoadingId) {
|
||||||
lock: true,
|
clearTimeout(globalLoadingConfig.delayLoadingId)
|
||||||
background: 'rgba(0, 0, 0, 0.7)',
|
globalLoadingConfig.delayLoadingId = null
|
||||||
text: message
|
}
|
||||||
}))
|
const text = isString(messageOrConfig) ? messageOrConfig : (messageOrConfig?.text || undefined)
|
||||||
if (globalLoadingConfig.delay > 0) {
|
const delay = isNumber(messageOrConfig?.delay) ? messageOrConfig.delay : globalLoadingConfig.delay
|
||||||
|
const openLoading = () => {
|
||||||
|
if (globalLoadingConfig.count > 0 && !globalLoadingConfig.globalLoading) {
|
||||||
|
globalLoadingConfig.globalLoading = ElLoading.service(Object.assign({
|
||||||
|
lock: true,
|
||||||
|
background: 'rgba(0, 0, 0, 0.7)',
|
||||||
|
text
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (delay > 0) {
|
||||||
globalLoadingConfig.delayLoadingId = setTimeout(() => {
|
globalLoadingConfig.delayLoadingId = setTimeout(() => {
|
||||||
globalLoadingConfig.globalLoading = openLoading()
|
globalLoadingConfig.delayLoadingId = null
|
||||||
}, globalLoadingConfig.delay)
|
openLoading()
|
||||||
|
}, delay)
|
||||||
} else {
|
} else {
|
||||||
globalLoadingConfig.globalLoading = openLoading()
|
openLoading()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const $coreHideLoading = () => {
|
export const $coreHideLoading = (force = false) => {
|
||||||
globalLoadingConfig.delayLoadingId && clearTimeout(globalLoadingConfig.delayLoadingId)
|
if (force) {
|
||||||
globalLoadingConfig.delayLoadingId = null
|
globalLoadingConfig.count = 0
|
||||||
globalLoadingConfig.globalLoading?.close()
|
} else {
|
||||||
|
globalLoadingConfig.count = Math.max(0, globalLoadingConfig.count - 1)
|
||||||
|
}
|
||||||
|
if (globalLoadingConfig.count === 0) {
|
||||||
|
if (globalLoadingConfig.delayLoadingId) {
|
||||||
|
clearTimeout(globalLoadingConfig.delayLoadingId)
|
||||||
|
globalLoadingConfig.delayLoadingId = null
|
||||||
|
}
|
||||||
|
if (globalLoadingConfig.globalLoading) {
|
||||||
|
globalLoadingConfig.globalLoading.close()
|
||||||
|
globalLoadingConfig.globalLoading = null
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const $coreAlert = (message, title = $i18nBundle('common.label.reminder'), options = undefined) => {
|
export const $coreAlert = (message, title = $i18nBundle('common.label.reminder'), options = undefined) => {
|
||||||
|
|||||||
32
src/vendors/axios.js
vendored
32
src/vendors/axios.js
vendored
@@ -45,6 +45,16 @@ $http.interceptors.request.use(/** @param config {ServiceRequestConfig} */ confi
|
|||||||
const networkErrorFun = debounce(() => ElMessage.error($i18nBundle('common.msg.networkError')), 300)
|
const networkErrorFun = debounce(() => ElMessage.error($i18nBundle('common.msg.networkError')), 300)
|
||||||
const networkTimeoutFun = debounce(() => ElMessage.error($i18nBundle('common.msg.networkTimeout')), 300)
|
const networkTimeoutFun = debounce(() => ElMessage.error($i18nBundle('common.msg.networkTimeout')), 300)
|
||||||
|
|
||||||
|
const handle401Redirect = debounce(() => {
|
||||||
|
$coreHideLoading(true)
|
||||||
|
useLoginConfigStore().logout()
|
||||||
|
$goto('/login')
|
||||||
|
}, 200)
|
||||||
|
|
||||||
|
const auth401ErrorFun = debounce((message) => {
|
||||||
|
ElMessage.error(message || $i18nBundle('common.msg.sessionExpired') || '登录会话已过期,请重新登录')
|
||||||
|
}, 300)
|
||||||
|
|
||||||
$http.interceptors.response.use(response => {
|
$http.interceptors.response.use(response => {
|
||||||
if (hasLoading(response.config)) {
|
if (hasLoading(response.config)) {
|
||||||
$coreHideLoading()
|
$coreHideLoading()
|
||||||
@@ -65,11 +75,25 @@ $http.interceptors.response.use(response => {
|
|||||||
} else if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') > -1) {
|
} else if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') > -1) {
|
||||||
networkTimeoutFun()
|
networkTimeoutFun()
|
||||||
}
|
}
|
||||||
if (error.response?.status === 401 && !error.response?.config.isLogin) {
|
const is401 = error.response?.status === 401
|
||||||
// 跳转登录页面
|
const isLoginReq = !!error.config?.isLogin
|
||||||
$goto('/login')
|
const resData = error.response?.data
|
||||||
|
const message = resData?.message || error.message || '系统异常,请稍后再试'
|
||||||
|
|
||||||
|
if (is401 && !isLoginReq) {
|
||||||
|
auth401ErrorFun(resData?.message)
|
||||||
|
handle401Redirect()
|
||||||
|
} else if (showErrorMessage(error.config)) {
|
||||||
|
ElMessage.error(message)
|
||||||
}
|
}
|
||||||
return error.response
|
|
||||||
|
if (error.response) {
|
||||||
|
error.response.data = error.response.data || {}
|
||||||
|
error.response.data.success = false
|
||||||
|
error.response.data.message = message
|
||||||
|
return error.response
|
||||||
|
}
|
||||||
|
return Promise.reject(error)
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user