优化用户信息初始化和数据库管理,确保应用启动时不阻塞,并在用户登录后异步初始化数据库。
This commit is contained in:
@@ -21,7 +21,10 @@ app.use(pinia)
|
||||
|
||||
// ==================== 初始化用户信息 ====================
|
||||
const userStore = useUserStore()
|
||||
userStore.initUser()
|
||||
// 异步初始化用户信息和数据库(不阻塞应用启动)
|
||||
userStore.initUser().catch((error) => {
|
||||
console.error('初始化用户信息失败:', error)
|
||||
})
|
||||
|
||||
// ==================== 配置 Element Plus ====================
|
||||
app.use(ElementPlus, {
|
||||
|
||||
@@ -181,7 +181,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
/**
|
||||
* 初始化用户信息(从本地存储恢复)
|
||||
*/
|
||||
const initUser = () => {
|
||||
const initUser = async () => {
|
||||
const savedToken = localStorage.getItem('token')
|
||||
const savedToken2 = localStorage.getItem('token2')
|
||||
const savedUser = localStorage.getItem('user-store')
|
||||
@@ -199,6 +199,17 @@ export const useUserStore = defineStore('user', () => {
|
||||
const parsed = JSON.parse(savedUser)
|
||||
if (parsed.state?.user) {
|
||||
user.value = parsed.state.user
|
||||
|
||||
// ⭐ 如果用户已登录,初始化数据库
|
||||
if (user.value?.id) {
|
||||
try {
|
||||
await databaseManager.ensureDatabase(user.value.id)
|
||||
console.log('✅ 数据库已初始化(从本地存储恢复)')
|
||||
} catch (error) {
|
||||
console.error('初始化数据库失败:', error)
|
||||
// 不阻断用户使用,降级到纯API模式
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('恢复用户信息失败:', error)
|
||||
|
||||
@@ -15,7 +15,9 @@ import { defineStore } from 'pinia'
|
||||
import { ref, computed, onUnmounted } from 'vue'
|
||||
import type { ChatSession } from '@/utils/db'
|
||||
import { SessionManager } from '@/utils/dbManagers/SessionManager'
|
||||
import { databaseManager } from '@/utils/db'
|
||||
import { getSessionList, clearUnread as clearUnreadAPI } from '@/api'
|
||||
import { useUserStore } from '@/stores/modules/user'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
// ==================== 辅助类型 ====================
|
||||
@@ -78,9 +80,10 @@ export const useSessionStore = defineStore('wechat-session', () => {
|
||||
* @param accountId 账号ID(可选,0表示全部)
|
||||
*
|
||||
* 执行流程:
|
||||
* 1. 从 IndexedDB 读取缓存(立即显示)
|
||||
* 2. 订阅数据库变更(自动更新 UI)
|
||||
* 3. 后台同步服务器数据(分页加载)
|
||||
* 1. 确保数据库已初始化
|
||||
* 2. 从 IndexedDB 读取缓存(立即显示)
|
||||
* 3. 订阅数据库变更(自动更新 UI)
|
||||
* 4. 后台同步服务器数据(分页加载)
|
||||
*/
|
||||
const init = async (accountId: number = 0) => {
|
||||
try {
|
||||
@@ -88,6 +91,23 @@ export const useSessionStore = defineStore('wechat-session', () => {
|
||||
|
||||
currentAccountId.value = accountId
|
||||
|
||||
// ⭐ 步骤0:确保数据库已初始化
|
||||
const userStore = useUserStore()
|
||||
const userId = userStore.userId
|
||||
|
||||
if (!userId) {
|
||||
console.warn('⚠️ 用户未登录,无法初始化数据库')
|
||||
ElMessage.warning('请先登录')
|
||||
return
|
||||
}
|
||||
|
||||
// 检查数据库是否已初始化
|
||||
if (!databaseManager.isInitialized()) {
|
||||
console.log('📗 数据库未初始化,正在初始化...')
|
||||
await databaseManager.ensureDatabase(userId)
|
||||
console.log('✅ 数据库初始化完成')
|
||||
}
|
||||
|
||||
// ⭐ 步骤1:从 IndexedDB 读取缓存(立即显示)
|
||||
sessions.value = await SessionManager.getUserSessions(accountId)
|
||||
console.log(`✅ 缓存读取完成,共 ${sessions.value.length} 个会话`)
|
||||
|
||||
Reference in New Issue
Block a user