fix: 应用启动即绑定用户数据库(修复刷新/登录后数据库未就绪的白屏)

This commit is contained in:
root
2026-10-16 14:32:38 +08:00
parent 8a0fde507f
commit 3f7b072ac6
8 changed files with 31 additions and 18 deletions
+1
View File
@@ -24,6 +24,7 @@ const toasts = useToasts()
const session = ref<Session | null>(getSession())
async function onLogin(s: Session) {
session.value = s
openUserDatabase(s.userId)
location.hash = '#/calendar'
route.value = 'calendar'
await refreshClock()
+7
View File
@@ -67,8 +67,13 @@ export function clearCloudCreds(): void {
localStorage.removeItem(CREDS_KEY)
}
let openedFor: string | null = null
export function openUserDatabase(userId: string): void {
if (openedFor === userId && db) return
if (db) unbindDatabase()
bindDatabase(new WorklogDB(LEGACY_DB_NAME + '_' + userId))
openedFor = userId
}
export async function createUser(username: string, password: string, migrateLegacy: boolean): Promise<Session> {
@@ -110,6 +115,7 @@ export async function login(username: string, password: string): Promise<Session
export async function logoutUser(): Promise<void> {
lockVault()
unbindDatabase()
openedFor = null
clearSession()
clearCloudCreds()
}
@@ -132,6 +138,7 @@ export async function deleteCurrentUser(): Promise<void> {
if (!s) throw new Error('未登录')
lockVault()
unbindDatabase()
openedFor = null
await Dexie.delete(LEGACY_DB_NAME + '_' + s.userId)
await registry.user.delete(s.userId)
clearSession()
+5
View File
@@ -2,6 +2,11 @@ import { createApp } from 'vue'
import App from './App.vue'
import './styles/tokens.css'
import { showFatalError } from './lib/errOverlay'
import { getSession, openUserDatabase } from './db/auth'
// 持久会话恢复:必须在任何视图挂载并读取数据之前绑定用户数据库
const restoredSession = getSession()
if (restoredSession) openUserDatabase(restoredSession.userId)
const app = createApp(App)