flat: 微调
This commit is contained in:
105
utils/db.js
105
utils/db.js
@@ -1,24 +1,50 @@
|
||||
// BaseDBStore.js
|
||||
import IndexedDBHelper from '@/common/IndexedDBHelper.js'
|
||||
// import UniStorageHelper from '../common/UniStorageHelper'
|
||||
import useChatGroupDBStore from '@/stores/userChatGroupStore'
|
||||
import config from '@/config'
|
||||
|
||||
class BaseStore {
|
||||
db = null
|
||||
isDBReady = false
|
||||
dbName = 'BrowsingHistory' // 'AppMainDB'
|
||||
dbName = 'AppMainDB'
|
||||
initPromise = null
|
||||
|
||||
constructor() {
|
||||
this.initPromise = this.checkAndInitDB()
|
||||
// 不再自动执行初始化
|
||||
}
|
||||
|
||||
async getDB() {
|
||||
if (!this.initPromise) {
|
||||
this.initPromise = this.checkAndInitDB();
|
||||
/**
|
||||
* 手动初始化入口、建立连接、如果版本号不对付才会清理
|
||||
* 建议在登录成功后调用: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();
|
||||
}
|
||||
await this.initPromise; // 等待初始化完成
|
||||
return this.db;
|
||||
}
|
||||
|
||||
@@ -28,30 +54,28 @@ class BaseStore {
|
||||
}
|
||||
const localVersion = uni.getStorageSync('indexedDBVersion') || 1
|
||||
console.log('DBVersion: ', localVersion, config.DBversion)
|
||||
if (localVersion === config.DBversion) {
|
||||
return this.initDB() // 🟢 记得加 return
|
||||
|
||||
if (Number(localVersion) === Number(config.DBversion)) {
|
||||
return this.initDB()
|
||||
} else {
|
||||
console.log('清空本地数据库')
|
||||
await this.clearDB() // 🟢 建议用 await
|
||||
console.log('检测到版本更新,清空重置旧数据库')
|
||||
await this.clearDB()
|
||||
uni.setStorageSync('indexedDBVersion', config.DBversion);
|
||||
return this.initDB(); // 🟢 记得加 return
|
||||
return this.initDB();
|
||||
}
|
||||
}
|
||||
|
||||
initDB() {
|
||||
// // #ifdef H5
|
||||
this.db = new IndexedDBHelper(this.dbName, config.DBversion);
|
||||
// // #endif
|
||||
|
||||
return this.db.openDB([{
|
||||
name: 'record',
|
||||
keyPath: "id",
|
||||
autoIncrement: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
{
|
||||
name: 'messageGroup',
|
||||
keyPath: "id",
|
||||
autoIncrement: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
{
|
||||
name: 'messages',
|
||||
@@ -65,12 +89,12 @@ class BaseStore {
|
||||
},
|
||||
{
|
||||
name: 'api_cache',
|
||||
keyPath: "cacheKey", // 使用 URL+参数 作为主键
|
||||
keyPath: "cacheKey",
|
||||
indexes: []
|
||||
}
|
||||
]).then(async () => {
|
||||
// 这里原来的逻辑保留
|
||||
if (useChatGroupDBStore) {
|
||||
// 确保 Pinia Store 已准备好后再初始化子项
|
||||
useChatGroupDBStore().init()
|
||||
}
|
||||
this.isDBReady = true
|
||||
@@ -79,13 +103,44 @@ class BaseStore {
|
||||
}
|
||||
|
||||
async clearDB() {
|
||||
return new Promise((resolve, rejetc) => {
|
||||
new IndexedDBHelper().deleteDB(this.dbName).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
// 修正拼写错误并优化 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
|
||||
Reference in New Issue
Block a user