fix: 优化登出流程,确保在关闭 Modal 后再执行路由跳转;更新 401 未授权处理逻辑

This commit is contained in:
康猛 2026-01-27 17:31:02 +08:00
parent 7bb43f079d
commit 9c59229c09
3 changed files with 39 additions and 13 deletions

View File

@ -153,12 +153,25 @@ export default observer(function ProfilePage() {
Toast.show({ title: err.message || '修改失败,请重试' })
return
}
Toast.hideModal()
Toast.show({ title: '密码已修改,请重新登录' })
// 退出登录
await userStore.signOut()
// 跳转到登录页
router.replace('/auth')
Toast.hideModal()
// 2. 等待 Modal 关闭动画完成(关键:避免 View 空指针)
setTimeout(() => {
// 3. 清空导航栈
router.dismissAll()
// 4. 执行登出
userStore.signOut()
// 5. 跳转到登录页
router.replace('/auth')
// 6. 显示提示
setTimeout(() => {
Toast.show({ title: '已退出登录' })
}, 100)
}, 400)
} catch (e) {
Toast.show({ title: '修改失败,请重试' })
}

View File

@ -24,11 +24,25 @@ export default observer(function SettingsPage() {
content="确定要退出登录吗?"
onCancel={Toast.hideModal}
onConfirm={async () => {
await signOut()
Toast.show({ title: '已退出登录' })
router.replace('/auth')
// 关闭modal
// 1. 立即关闭 Modal避免在动画期间操作路由
Toast.hideModal()
// 2. 等待 Modal 关闭动画完成(关键:避免 View 空指针)
setTimeout(() => {
// 3. 清空导航栈
router.dismissAll()
// 4. 执行登出
signOut()
// 5. 跳转到登录页
router.replace('/auth')
// 6. 显示提示
setTimeout(() => {
Toast.show({ title: '已退出登录' })
}, 100)
}, 400)
}}
/>,
)

View File

@ -1,6 +1,5 @@
import { router } from 'expo-router'
import { storage } from './storage'
import { storage as storage2 } from '../utils/storage'
import { storage } from './storage.native'
interface FetchLoggerOptions {
enableLogging?: boolean
@ -42,9 +41,9 @@ export const createFetchWithLogger = (options: FetchLoggerOptions = {}) => {
const response = await originalFetch(input, init)
if (response.status === 401) {
console.warn('🔐 401 未授权,跳转到登录页')
console.warn('🔐 401 未授权,清空登录状态')
// router.replace('/auth')
router.replace('/auth')
storage2.set('isLogin', false)
}
return response