2025-11-29 16:31:34 +08:00
|
|
|
|
import IndexedDBHelper from '@/common/IndexedDBHelper.js'
|
|
|
|
|
|
import useChatGroupDBStore from '@/stores/userChatGroupStore'
|
|
|
|
|
|
import config from '@/config'
|
|
|
|
|
|
|
|
|
|
|
|
class BaseStore {
|
|
|
|
|
|
db = null
|
|
|
|
|
|
isDBReady = false
|
2025-12-16 20:24:03 +08:00
|
|
|
|
dbName = 'AppMainDB'
|
2025-11-29 16:31:34 +08:00
|
|
|
|
initPromise = null
|
|
|
|
|
|
|
|
|
|
|
|
constructor() {
|
2025-12-16 20:24:03 +08:00
|
|
|
|
// 不再自动执行初始化
|
2025-11-29 16:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 20:24:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 手动初始化入口、建立连接、如果版本号不对付才会清理
|
|
|
|
|
|
* 建议在登录成功后调用:await baseDB.init()
|
|
|
|
|
|
*/
|
|
|
|
|
|
async init() {
|
|
|
|
|
|
// 如果已经有初始化任务在进行了,直接返回该任务
|
|
|
|
|
|
if (this.initPromise) {
|
|
|
|
|
|
return this.initPromise;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建初始化任务
|
|
|
|
|
|
this.initPromise = this.checkAndInitDB();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
await this.initPromise;
|
|
|
|
|
|
console.log('数据库初始化成功');
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
this.initPromise = null; // 初始化失败允许下次重试
|
|
|
|
|
|
console.error('数据库初始化失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this.db;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取数据库实例
|
|
|
|
|
|
* 如果没初始化,会强制触发一次 init
|
|
|
|
|
|
*/
|
2025-11-29 16:31:34 +08:00
|
|
|
|
async getDB() {
|
2025-12-16 20:24:03 +08:00
|
|
|
|
if (!this.isDBReady) {
|
|
|
|
|
|
return await this.init();
|
2025-11-29 16:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
return this.db;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async checkAndInitDB() {
|
|
|
|
|
|
if (config.OnlyUseCachedDB) {
|
|
|
|
|
|
return this.initDB()
|
|
|
|
|
|
}
|
|
|
|
|
|
const localVersion = uni.getStorageSync('indexedDBVersion') || 1
|
|
|
|
|
|
console.log('DBVersion: ', localVersion, config.DBversion)
|
2025-12-16 20:24:03 +08:00
|
|
|
|
|
|
|
|
|
|
if (Number(localVersion) === Number(config.DBversion)) {
|
|
|
|
|
|
return this.initDB()
|
2025-11-29 16:31:34 +08:00
|
|
|
|
} else {
|
2025-12-16 20:24:03 +08:00
|
|
|
|
console.log('检测到版本更新,清空重置旧数据库')
|
|
|
|
|
|
await this.clearDB()
|
2025-11-29 16:31:34 +08:00
|
|
|
|
uni.setStorageSync('indexedDBVersion', config.DBversion);
|
2025-12-16 20:24:03 +08:00
|
|
|
|
return this.initDB();
|
2025-11-29 16:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initDB() {
|
|
|
|
|
|
this.db = new IndexedDBHelper(this.dbName, config.DBversion);
|
|
|
|
|
|
return this.db.openDB([{
|
|
|
|
|
|
name: 'record',
|
|
|
|
|
|
keyPath: "id",
|
2025-12-16 20:24:03 +08:00
|
|
|
|
autoIncrement: true
|
2025-11-29 16:31:34 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'messageGroup',
|
|
|
|
|
|
keyPath: "id",
|
2025-12-16 20:24:03 +08:00
|
|
|
|
autoIncrement: true
|
2025-11-29 16:31:34 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'messages',
|
|
|
|
|
|
keyPath: "id",
|
|
|
|
|
|
autoIncrement: true,
|
|
|
|
|
|
indexes: [{
|
|
|
|
|
|
name: 'parentGroupId',
|
|
|
|
|
|
key: 'parentGroupId',
|
|
|
|
|
|
unique: false
|
|
|
|
|
|
}]
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'api_cache',
|
2025-12-16 20:24:03 +08:00
|
|
|
|
keyPath: "cacheKey",
|
2025-11-29 16:31:34 +08:00
|
|
|
|
indexes: []
|
|
|
|
|
|
}
|
|
|
|
|
|
]).then(async () => {
|
|
|
|
|
|
if (useChatGroupDBStore) {
|
2025-12-16 20:24:03 +08:00
|
|
|
|
// 确保 Pinia Store 已准备好后再初始化子项
|
2025-11-29 16:31:34 +08:00
|
|
|
|
useChatGroupDBStore().init()
|
|
|
|
|
|
}
|
|
|
|
|
|
this.isDBReady = true
|
|
|
|
|
|
return this.db;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async clearDB() {
|
2025-12-16 20:24:03 +08:00
|
|
|
|
// 修正拼写错误并优化 Promise 写法
|
|
|
|
|
|
return new IndexedDBHelper().deleteDB(this.dbName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 彻底清空数据库并重新初始化
|
|
|
|
|
|
* 适用于切换账号或手动清理缓存
|
|
|
|
|
|
* 非特殊情况不要重置!!!!!!!!!!!!!!!
|
|
|
|
|
|
*/
|
|
|
|
|
|
async resetAndReinit() {
|
|
|
|
|
|
console.warn('开始执行数据库重置...');
|
|
|
|
|
|
|
|
|
|
|
|
if (this.db && this.db.db) {
|
|
|
|
|
|
this.db.db.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
await this.clearDB();
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 重置内部状态
|
|
|
|
|
|
this.db = null;
|
|
|
|
|
|
this.isDBReady = false;
|
|
|
|
|
|
this.initPromise = null;
|
|
|
|
|
|
|
|
|
|
|
|
uni.setStorageSync('indexedDBVersion', config.DBversion);
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 重新调用初始化流程
|
|
|
|
|
|
await this.init();
|
|
|
|
|
|
|
|
|
|
|
|
console.log('数据库重置及初始化完成');
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('数据库重置失败:', error);
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
}
|
2025-11-29 16:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-16 20:24:03 +08:00
|
|
|
|
// 导出实例
|
2025-11-29 16:31:34 +08:00
|
|
|
|
const baseDB = new BaseStore()
|
|
|
|
|
|
export default baseDB
|