fix: 页面刷新时主动续期 access_token,避免过期 token 触发 401 竞态导致权限空白
This commit is contained in:
@ -16,11 +16,28 @@ const isLoginPage = computed(() => {
|
||||
})
|
||||
|
||||
// 页面加载时刷新权限
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
if (userStore.token) {
|
||||
// ★ 如果 access_token 可能已过期,先主动用 refresh_token 换新 token
|
||||
// 避免后续 API 调用触发 401 → 刷新 → 重试的竞态导致权限空白
|
||||
try {
|
||||
const refreshTokenValue = localStorage.getItem('refresh_token')
|
||||
if (refreshTokenValue) {
|
||||
const res = await fetch('/api/v1/auth/refresh', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ refresh_token: refreshTokenValue })
|
||||
})
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
if (data.access_token) {
|
||||
userStore.setToken(data.access_token)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) { /* 静默,失败时走正常 401 刷新流程 */ }
|
||||
userStore.refreshUserPermissions()
|
||||
}
|
||||
// 当 Vue 根组件挂载完毕,确保 Dify 图标一定会被加载
|
||||
if (typeof (window as any).initDifyChatbot === 'function') {
|
||||
(window as any).initDifyChatbot()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user