重构数据库管理逻辑,简化用户数据库初始化流程。引入新的数据库管理类以支持动态数据库名称和用户状态管理。更新应用启动逻辑以确保在用户登录时正确初始化数据库。增强持久化数据恢复功能,确保用户数据的可靠性和一致性。
This commit is contained in:
@@ -7,97 +7,18 @@ import dayjs from "dayjs";
|
||||
import "dayjs/locale/zh-cn";
|
||||
import App from "./App";
|
||||
import "./styles/global.scss";
|
||||
import { db } from "./utils/db"; // 引入数据库实例
|
||||
import { initializeDatabaseFromPersistedUser } from "./utils/db";
|
||||
|
||||
// 设置dayjs为中文
|
||||
dayjs.locale("zh-cn");
|
||||
|
||||
// 清理旧数据库
|
||||
async function cleanupOldDatabase() {
|
||||
async function bootstrap() {
|
||||
try {
|
||||
// 获取所有数据库
|
||||
const databases = await indexedDB.databases();
|
||||
|
||||
for (const dbInfo of databases) {
|
||||
if (dbInfo.name === "CunkebaoDatabase") {
|
||||
console.log("检测到旧版数据库,开始清理...");
|
||||
|
||||
// 打开数据库检查版本
|
||||
const openRequest = indexedDB.open(dbInfo.name);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
openRequest.onsuccess = async event => {
|
||||
const database = (event.target as IDBOpenDBRequest).result;
|
||||
const objectStoreNames = Array.from(database.objectStoreNames);
|
||||
|
||||
// 检查是否存在旧表
|
||||
const hasOldTables = objectStoreNames.some(name =>
|
||||
[
|
||||
"kfUsers",
|
||||
"weChatGroup",
|
||||
"contracts",
|
||||
"newContactList",
|
||||
"messageList",
|
||||
].includes(name),
|
||||
);
|
||||
|
||||
if (hasOldTables) {
|
||||
console.log("发现旧表,删除整个数据库:", objectStoreNames);
|
||||
database.close();
|
||||
|
||||
// 删除整个数据库
|
||||
const deleteRequest = indexedDB.deleteDatabase(dbInfo.name);
|
||||
deleteRequest.onsuccess = () => {
|
||||
console.log("旧数据库已删除");
|
||||
resolve();
|
||||
};
|
||||
deleteRequest.onerror = () => {
|
||||
console.error("删除旧数据库失败");
|
||||
reject();
|
||||
};
|
||||
} else {
|
||||
console.log("数据库结构正确,无需清理");
|
||||
database.close();
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
openRequest.onerror = () => {
|
||||
console.error("无法打开数据库进行检查");
|
||||
reject();
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
await initializeDatabaseFromPersistedUser();
|
||||
} catch (error) {
|
||||
console.warn("清理旧数据库时出错(可忽略):", error);
|
||||
}
|
||||
}
|
||||
|
||||
// 数据库初始化
|
||||
async function initializeApp() {
|
||||
try {
|
||||
// 1. 清理旧数据库
|
||||
await cleanupOldDatabase();
|
||||
|
||||
// 2. 打开新数据库
|
||||
await db.open();
|
||||
console.log("数据库初始化成功");
|
||||
|
||||
// 3. 开发环境清空数据(可选)
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
console.log("开发环境:跳过数据清理");
|
||||
// 如需清空数据,取消下面的注释
|
||||
// await db.chatSessions.clear();
|
||||
// await db.contactsUnified.clear();
|
||||
// await db.contactLabelMap.clear();
|
||||
// await db.userLoginRecords.clear();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("数据库初始化失败:", error);
|
||||
console.warn("Failed to prepare database before app bootstrap:", error);
|
||||
}
|
||||
|
||||
// 渲染应用
|
||||
const root = createRoot(document.getElementById("root")!);
|
||||
root.render(
|
||||
<ConfigProvider locale={zhCN}>
|
||||
@@ -106,5 +27,4 @@ async function initializeApp() {
|
||||
);
|
||||
}
|
||||
|
||||
// 启动应用
|
||||
initializeApp();
|
||||
void bootstrap();
|
||||
|
||||
Reference in New Issue
Block a user