调整应用启动时的延迟时间和重试机制

This commit is contained in:
jxxghp
2025-06-14 13:16:51 +08:00
parent 74fc8bd131
commit 6cefdb5d37
2 changed files with 40 additions and 10 deletions

View File

@@ -22,12 +22,15 @@ const { t } = useI18n()
// 用户 Store
const userStore = useUserStore()
// 是否超级用户
let superUser = userStore.superUser
// 响应式的超级用户状态
const superUser = computed(() => userStore.superUser)
// ShortcutBar 引用
const shortcutBarRef = ref<InstanceType<typeof ShortcutBar> | null>(null)
// 未读消息检查状态
const hasCheckedUnread = ref(false)
// 获取用户权限信息
const userPermissions = computed(() => ({
is_superuser: userStore.superUser,
@@ -64,10 +67,12 @@ function goBack() {
// 检查未读消息并自动打开消息弹窗
async function checkUnreadMessages() {
if (!superUser) {
return // 只有超级用户才能看到消息
if (!superUser.value || hasCheckedUnread.value) {
return // 只有超级用户才能看到消息,且每次会话只检查一次
}
hasCheckedUnread.value = true
try {
const unreadCount = await checkUnreadOnStartup()
@@ -97,6 +102,20 @@ async function waitAndOpenMessageDialog() {
}
}
// 监听用户状态变化
watch(
superUser,
newValue => {
if (newValue && !hasCheckedUnread.value) {
// 用户状态变为超级用户且还没检查过未读消息时,延迟检查
setTimeout(() => {
checkUnreadMessages()
}, 1000)
}
},
{ immediate: true },
)
onMounted(() => {
// 获取菜单列表
startMenus.value = getMenuList(t('menu.start'))
@@ -109,7 +128,7 @@ onMounted(() => {
nextTick(() => {
setTimeout(() => {
checkUnreadMessages()
}, 500)
}, 2000) // 增加延迟时间到2秒
})
})
</script>

View File

@@ -50,23 +50,34 @@ export async function waitForServiceWorker(): Promise<ServiceWorker | null> {
// 应用启动时检查未读消息数量
export async function checkUnreadOnStartup(): Promise<number> {
try {
// 对于PWA应用冷启动时需要更长的等待时间
const isPWAColdStart = !navigator.serviceWorker.controller
const initialDelay = isPWAColdStart ? 3000 : 1000
// 初始延迟,等待应用完全启动
await new Promise(resolve => setTimeout(resolve, initialDelay))
// 等待Service Worker准备就绪
const sw = await waitForServiceWorker()
if (!sw) {
return 0
}
// 延迟1秒确保Service Worker完全准备好
// 额外延迟确保Service Worker完全准备好
await new Promise(resolve => setTimeout(resolve, 1000))
// 重试机制获取未读消息数量
for (let i = 0; i < 3; i++) {
for (let i = 0; i < 5; i++) {
// 增加重试次数
try {
const unreadCount = await getUnreadCount()
return unreadCount
if (unreadCount >= 0) {
// 确保返回有效数字
return unreadCount
}
} catch (error) {
if (i === 2) throw error // 最后一次重试失败则抛出错误
await new Promise(resolve => setTimeout(resolve, 500)) // 等待500ms后重试
if (i === 4) throw error // 最后一次重试失败则抛出错误
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))) // 递增等待时间
}
}