| | |
| | | router.push("/login"); |
| | | } |
| | | |
| | | function redirectToForbiddenIfNeeded() { |
| | | if (router.currentRoute.value.path === "/403") { |
| | | return; |
| | | } |
| | | router.push("/403"); |
| | | } |
| | | |
| | | http.interceptors.request.use((config) => { |
| | | const auth = useAuthStore(); |
| | | if (auth.token) { |
| | |
| | | redirectToLoginIfNeeded(); |
| | | return Promise.reject(new Error(body.message || "未登录或已过期")); |
| | | } |
| | | if (body && body.code === 403) { |
| | | redirectToForbiddenIfNeeded(); |
| | | return Promise.reject(new Error(body.message || "无权限访问")); |
| | | } |
| | | if (body && body.success === false) { |
| | | return Promise.reject(new Error(body.message || "请求失败")); |
| | | } |
| | |
| | | if (status === 401 || code === 401) { |
| | | redirectToLoginIfNeeded(); |
| | | } |
| | | if (status === 403 || code === 403) { |
| | | redirectToForbiddenIfNeeded(); |
| | | } |
| | | return Promise.reject(err); |
| | | } |
| | | ); |