146 lines
4.1 KiB
JavaScript
146 lines
4.1 KiB
JavaScript
import IndexedDBHelper from '@/common/IndexedDBHelper.js'
|
||
import useChatGroupDBStore from '@/stores/userChatGroupStore'
|
||
import config from '@/config'
|
||
|
||
class BaseStore {
|
||
db = null
|
||
isDBReady = false
|
||
dbName = 'AppMainDB'
|
||
initPromise = null
|
||
|
||
constructor() {
|
||
// 不再自动执行初始化
|
||
}
|
||
|
||
/**
|
||
* 手动初始化入口、建立连接、如果版本号不对付才会清理
|
||
* 建议在登录成功后调用: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
|
||
*/
|
||
async getDB() {
|
||
if (!this.isDBReady) {
|
||
return await this.init();
|
||
}
|
||
return this.db;
|
||
}
|
||
|
||
async checkAndInitDB() {
|
||
if (config.OnlyUseCachedDB) {
|
||
return this.initDB()
|
||
}
|
||
const localVersion = uni.getStorageSync('indexedDBVersion') || 1
|
||
console.log('DBVersion: ', localVersion, config.DBversion)
|
||
|
||
if (Number(localVersion) === Number(config.DBversion)) {
|
||
return this.initDB()
|
||
} else {
|
||
console.log('检测到版本更新,清空重置旧数据库')
|
||
await this.clearDB()
|
||
uni.setStorageSync('indexedDBVersion', config.DBversion);
|
||
return this.initDB();
|
||
}
|
||
}
|
||
|
||
initDB() {
|
||
this.db = new IndexedDBHelper(this.dbName, config.DBversion);
|
||
return this.db.openDB([{
|
||
name: 'record',
|
||
keyPath: "id",
|
||
autoIncrement: true
|
||
},
|
||
{
|
||
name: 'messageGroup',
|
||
keyPath: "id",
|
||
autoIncrement: true
|
||
},
|
||
{
|
||
name: 'messages',
|
||
keyPath: "id",
|
||
autoIncrement: true,
|
||
indexes: [{
|
||
name: 'parentGroupId',
|
||
key: 'parentGroupId',
|
||
unique: false
|
||
}]
|
||
},
|
||
{
|
||
name: 'api_cache',
|
||
keyPath: "cacheKey",
|
||
indexes: []
|
||
}
|
||
]).then(async () => {
|
||
if (useChatGroupDBStore) {
|
||
// 确保 Pinia Store 已准备好后再初始化子项
|
||
useChatGroupDBStore().init()
|
||
}
|
||
this.isDBReady = true
|
||
return this.db;
|
||
});
|
||
}
|
||
|
||
async clearDB() {
|
||
// 修正拼写错误并优化 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;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 导出实例
|
||
const baseDB = new BaseStore()
|
||
export default baseDB |