You've already forked rollingDraw
初始化提交
This commit is contained in:
145
utils/indexedDB.js
Normal file
145
utils/indexedDB.js
Normal file
@@ -0,0 +1,145 @@
|
||||
// IndexedDB 封装工具
|
||||
const DB_NAME = 'LotteryDB'
|
||||
const DB_VERSION = 1
|
||||
const STORE_NAME = 'lotteryData'
|
||||
|
||||
class IndexedDBHelper {
|
||||
constructor() {
|
||||
this.db = null
|
||||
}
|
||||
|
||||
async init() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = window.indexedDB.open(DB_NAME, DB_VERSION)
|
||||
|
||||
request.onerror = () => {
|
||||
reject(new Error('Failed to open IndexedDB'))
|
||||
}
|
||||
|
||||
request.onsuccess = () => {
|
||||
this.db = request.result
|
||||
resolve(this.db)
|
||||
}
|
||||
|
||||
request.onupgradeneeded = (event) => {
|
||||
const db = event.target.result
|
||||
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
||||
db.createObjectStore(STORE_NAME)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async set(key, value) {
|
||||
if (!this.db) await this.init()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = this.db.transaction([STORE_NAME], 'readwrite')
|
||||
const store = transaction.objectStore(STORE_NAME)
|
||||
const request = store.put(value, key)
|
||||
|
||||
request.onsuccess = () => {
|
||||
console.log(`IndexedDB: Successfully saved ${key}`)
|
||||
resolve()
|
||||
}
|
||||
request.onerror = () => {
|
||||
console.error(`IndexedDB: Failed to save ${key}`, request.error)
|
||||
reject(new Error('Failed to save to IndexedDB'))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async get(key) {
|
||||
if (!this.db) await this.init()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = this.db.transaction([STORE_NAME], 'readonly')
|
||||
const store = transaction.objectStore(STORE_NAME)
|
||||
const request = store.get(key)
|
||||
|
||||
request.onsuccess = () => {
|
||||
console.log(`IndexedDB: Retrieved ${key}:`, request.result ? 'Yes' : 'No')
|
||||
resolve(request.result)
|
||||
}
|
||||
request.onerror = () => {
|
||||
console.error(`IndexedDB: Failed to retrieve ${key}`)
|
||||
reject(new Error('Failed to read from IndexedDB'))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async delete(key) {
|
||||
if (!this.db) await this.init()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = this.db.transaction([STORE_NAME], 'readwrite')
|
||||
const store = transaction.objectStore(STORE_NAME)
|
||||
const request = store.delete(key)
|
||||
|
||||
request.onsuccess = () => resolve()
|
||||
request.onerror = () => reject(new Error('Failed to delete from IndexedDB'))
|
||||
})
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
if (!this.db) await this.init()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = this.db.transaction([STORE_NAME], 'readonly')
|
||||
const store = transaction.objectStore(STORE_NAME)
|
||||
const keysRequest = store.getAllKeys()
|
||||
|
||||
const result = {}
|
||||
let completed = 0
|
||||
let totalKeys = 0
|
||||
|
||||
keysRequest.onsuccess = () => {
|
||||
const keys = keysRequest.result
|
||||
totalKeys = keys.length
|
||||
console.log(`IndexedDB: Found ${totalKeys} keys:`, keys)
|
||||
|
||||
if (totalKeys === 0) {
|
||||
resolve(result)
|
||||
return
|
||||
}
|
||||
|
||||
keys.forEach(key => {
|
||||
const getRequest = store.get(key)
|
||||
getRequest.onsuccess = () => {
|
||||
result[key] = getRequest.result
|
||||
console.log(`IndexedDB: Retrieved ${key}:`, getRequest.result ? 'Yes' : 'No')
|
||||
completed++
|
||||
if (completed === totalKeys) {
|
||||
console.log('IndexedDB: All data retrieved:', Object.keys(result))
|
||||
resolve(result)
|
||||
}
|
||||
}
|
||||
getRequest.onerror = () => {
|
||||
console.error(`IndexedDB: Failed to retrieve ${key}`)
|
||||
completed++
|
||||
if (completed === totalKeys) {
|
||||
resolve(result)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
keysRequest.onerror = () => reject(new Error('Failed to read all from IndexedDB'))
|
||||
})
|
||||
}
|
||||
|
||||
async clear() {
|
||||
if (!this.db) await this.init()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = this.db.transaction([STORE_NAME], 'readwrite')
|
||||
const store = transaction.objectStore(STORE_NAME)
|
||||
const request = store.clear()
|
||||
|
||||
request.onsuccess = () => resolve()
|
||||
request.onerror = () => reject(new Error('Failed to clear IndexedDB'))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const indexedDB = new IndexedDBHelper()
|
||||
Reference in New Issue
Block a user