代码拉取完成,页面将自动刷新
/*
* @Author: iYiu
* @Date: 2021-09-10 14:50:17
* @LastEditors: iYiu
* @LastEditTime: 2021-09-10 15:22:18
* @FilePath: \IndexedDB\dbClass.js
*/
export default class DB {
constructor(dbName,formName){
this.dbName = dbName;
this.slqVersion = '1'; //websql的版本号,由于ios的问题,版本号的写法不一样
this.dbVersion = 1; //indexDB的版本号
this.storeName = formName; //store----即“表”的名字
this.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB, //监听IndexDB
this.db = {}; //缓存数据库,避免同一个页面重复创建和销毁
this.store = null;
this.errorCode = {
//错误码
success: 200, //成功
error: 401, //key不存在
open: 91001, //打开数据库失败的错误
save: 91002, //保存数据失败的错误
get: 91003, //获取数据失败的错误
delete: 91004, //删除数据失败的错误
deleteAll: 91005, //清空数据库失败的错误
}
}
//创建“表”
createStore(dbName) {
let txn, store;
if (this.indexedDB) {
//如果是支持IndexDB的
txn = this.db[dbName].transaction(this.storeName, 'readwrite'); //IndexDB的读写权限
store = txn.objectStore(this.storeName);
}
return store;
}
open(callback, dbName) {
let that = this;
//打开数据库
if (that.indexedDB) {
//如果支持IndexDB
if (!that.db[dbName]) {
//如果缓存中没有,则进行数据库的创建或打开,提高效率
let request = that.indexedDB.open(dbName, that.dbVersion);
request.onerror = function (e) {
callback({ code: that.errorCode.open, error: e });
};
request.onsuccess = function (e) {
if (!that.db[dbName]) {
that.db[dbName] = e.target.result;
}
let store = that.createStore(dbName);
callback(store);
};
request.onupgradeneeded = function (e) {
that.db[dbName] = e.target.result;
let store = that.db[dbName].createObjectStore(that.storeName, {
keyPath: 'key',
});
callback(store);
};
} else {
//如果缓存中已经打开了数据库,就直接使用
let store = that.createStore(dbName);
callback(store);
}
}
}
save(kay, value) {
//保存数据到数据库 kay---数据key value----数据值
let that = this;
if (that.indexedDB) {
return new Promise((resolve, reject) => {
let dbName = that.dbName;
let inData = {
key: kay,
value: value,
};
that.open(function (result) {
let error = result.hasOwnProperty('error');
if (error) {
resolve(result);
} else {
let request = result.put(inData);
request.onsuccess = function (e) {
resolve({ code: that.errorCode.success, success: true }); //保存成功有success 字段
};
request.onerror = function (e) {
resolve({ code: that.errorCode.save, error: e });
};
}
}, dbName);
});
}
}
get(kay) {
//根据key获取值
let that = this;
return new Promise((resolve, reject) => {
let dbName = that.dbName;
if (that.indexedDB) {
that.open(function (result) {
let error = result.hasOwnProperty('error'); //判断返回的数据中是否有error字段
if (error) {
reject(result);
} else {
let request = result.get(kay);
request.onsuccess = function (e) {
let result = e.target.result
let data = result? result.value : undefined;
resolve({ code: data?that.errorCode.success:that.errorCode.error, data: data||[], success: true });
};
request.onerror = function (e) {
reject({ code: that.errorCode.get, error: e });
};
}
}, dbName);
}
});
}
delete(key) {
let that = this;
return new Promise((resolve, reject)=>{
//根据key删除某条数据
let dbName = that.dbName;
if (that.indexedDB) {
that.open(function (result) {
let error = result.hasOwnProperty('error');
if (error) {
resolve(result);
} else {
let request = result.get(key);
request.onsuccess = function (e) {
let recode = e.target.result;
if (recode) {
request = result.delete(key);
}
resolve({ code: recode?that.errorCode.success:that.errorCode.error, success: true });
};
request.onerror = function (e) {
resolve({ code: that.errorCode.delete, error: e });
};
}
}, dbName);
}
})
}
deleteAll() {
let that = this;
return new Promise((resolve, reject)=>{
//清空数据库
let dbName = that.dbName;
if (that.indexedDB) {
that.open(function (result) {
let error = result.hasOwnProperty('error');
if (error) {
resolve(result);
} else {
result.clear();
resolve({ code: that.errorCode.success, success: true });
}
}, dbName);
}
})
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。