fix(阶段3): api2 补上鉴权失败的统一处理
utils/http.ts 的拦截器一直有两条全站行为:login-required 清登录态并弹登录框、 permission-denied 弹提示。api2 从建包起就漏了这两条,于是此前已迁移到 api2 的 所有端点在鉴权失败时都是「点了没反应」—— 调用方各自 catch 才能提示,漏一个 就静默。 新加的两个统计端点是教师专属,非教师点「统计」必然 403,会把这个洞放大到 用户能天天撞见,所以一并补上。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import axios, { type AxiosRequestConfig } from "axios"
|
||||
import { createDiscreteApi } from "naive-ui"
|
||||
import { useAuthModalStore } from "shared/store/authModal"
|
||||
import { STORAGE_KEY } from "./constants"
|
||||
import type { ApiResponse } from "./http"
|
||||
import storage from "./storage"
|
||||
|
||||
const { message: toast } = createDiscreteApi(["message"])
|
||||
|
||||
interface Api2Error {
|
||||
error?: {
|
||||
@@ -51,6 +57,16 @@ instance.interceptors.response.use(
|
||||
: code === "account-disabled"
|
||||
? "Your account has been disabled"
|
||||
: message
|
||||
|
||||
// 与 utils/http.ts 的拦截器保持一致:这两种错误全站都是同样的处理,
|
||||
// 不放在这里的话每个调用点都得自己 catch,漏一个就是「点了没反应」。
|
||||
if (code === "login-required") {
|
||||
storage.remove(STORAGE_KEY.AUTHED)
|
||||
useAuthModalStore().openLoginModal()
|
||||
} else if (code === "permission-denied") {
|
||||
toast.error(legacyMessage || "权限不足")
|
||||
}
|
||||
|
||||
return Promise.reject({ error: code, data: legacyMessage })
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user