提交 e915b9b5 编写于 作者: View Design's avatar View Design

封装全局status检测

上级 9fdf2be4
...@@ -20,27 +20,7 @@ const request = async (url, options = {}) => { ...@@ -20,27 +20,7 @@ const request = async (url, options = {}) => {
}, },
onResponseError({ request, response, options }) { onResponseError({ request, response, options }) {
const status = response.status const status = response.status
if (process.client && (status === 401 || status === 403)) { useRequestError(status)
// 全局弹提示
let title
if (status === 401) title = '抱歉,您尚未登录'
else if (status === 403) title = '抱歉,您没有权限'
nextTick(() => {
const toast = useToast()
toast.add({
icon: 'i-heroicons-exclamation-triangle-20-solid',
timeout: 3000,
title: title,
color: 'red'
})
})
// 全局弹登录
const { $isSignIn } = storeToRefs(useUserStore())
const { $openSign } = useUserStore()
if (status === 401 && !$isSignIn.value) {
$openSign()
}
}
// 处理响应错误 // 处理响应错误
console.log('[ResponseError]', request) console.log('[ResponseError]', request)
} }
......
export default function (status) {
if (process.client && (status === 401 || status === 403)) {
// 全局弹提示
let title
if (status === 401) title = '抱歉,您尚未登录'
else if (status === 403) title = '抱歉,您没有权限'
nextTick(() => {
const toast = useToast()
toast.add({
icon: 'i-heroicons-exclamation-triangle-20-solid',
timeout: 3000,
title: title,
color: 'red'
})
})
// 全局弹登录
const { $isSignIn } = storeToRefs(useUserStore())
const { $openSign } = useUserStore()
if (status === 401 && !$isSignIn.value) {
$openSign()
}
}
}
\ No newline at end of file
...@@ -11,9 +11,8 @@ export default (url, { onmessage, onerror, controller, params }) => { ...@@ -11,9 +11,8 @@ export default (url, { onmessage, onerror, controller, params }) => {
}, },
body: JSON.stringify(params), body: JSON.stringify(params),
onopen (response) { onopen (response) {
if (response.status === 401 || response.status === 403) { const status = response.status
throw Error() useRequestError(status)
}
}, },
onmessage (info) { onmessage (info) {
onmessage(info) onmessage(info)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册