rt-sim-training-client/src/utils/indexedDb.js
2023-05-20 17:50:32 +08:00

175 lines
6.8 KiB
JavaScript

import Vue from 'vue';
import store from '@/store/index';
// 创建或者打开数据库
export function openIndexedDB() {
const baseUrl = store.state.user.baseUrl;
const indexedDBName = baseUrl.replace(/http.?:\/\/(.*)[\/|:].*/, '$1');
const request = window.indexedDB.open(indexedDBName, 1);
request.onerror = function (event) {
console.error('数据库打开报错');
};
request.onsuccess = function (event) {
Vue.prototype.$db = request.result;
console.log('数据库打开成功');
};
request.onupgradeneeded = function (event) {
Vue.prototype.$db = event.target.result;
Vue.prototype.$db.createObjectStore('mapData', { keyPath: 'id' });
Vue.prototype.$db.createObjectStore('runPlan', { keyPath: 'templateId' });
};
}
// 新增数据
export function awaitDbAddData(tableName, data) {
return new Promise((resolve, reject) => {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName], 'readwrite').objectStore(tableName).add(data);
request.onsuccess = function(event) { resolve(request.result); };
request.onerror = function(event) { reject(event); };
} else {
openIndexedDB();
reject({message: '数据库未打开!'});
}
});
}
// 新增数据
export function dbAddData(tableName, data, params, successCallback, errorCallback) {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName], 'readwrite').objectStore(tableName).add(data);
request.onsuccess = function(event) {
if (successCallback && (typeof successCallback === 'function')) {
successCallback(request.result, params);
} else {
console.log('数据写入成功');
}
};
request.onerror = function(event) {
if (errorCallback && (typeof errorCallback === 'function')) {
errorCallback(event, params);
} else {
console.error('数据写入失败');
}
};
} else {
openIndexedDB();
setTimeout(() => {
dbAddData(tableName, data, params, successCallback, errorCallback);
}, 500);
}
}
// 读取数据
export function awaitDbReadData(tableName, key) {
return new Promise((resolve, reject) => {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName]).objectStore(tableName).get(key);
request.onsuccess = function(event) { console.log('数据读取成功'); resolve(request.result); };
request.onerror = function(event) { console.error('数据读取失败'); reject(event); };
} else {
openIndexedDB();
reject({message: '数据库未打开!'});
}
});
}
// 读取数据
export function dbReadData(tableName, key, params, successCallback, errorCallback) {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName]).objectStore(tableName).get(key);
request.onsuccess = function(event) {
if (successCallback && (typeof successCallback === 'function')) {
successCallback(request.result, params);
} else {
console.log('数据读取成功');
}
};
request.onerror = function(event) {
if (errorCallback && (typeof errorCallback === 'function')) {
errorCallback(event, params);
} else {
console.error('数据读取失败');
}
};
} else {
openIndexedDB();
setTimeout(() => {
dbReadData(tableName, key, params, successCallback, errorCallback);
}, 500);
}
}
// 修改数据
export function awaitDbUpdateData(tableName, data) {
return new Promise((resolve, reject) => {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName], 'readwrite').objectStore(tableName).put(data);
request.onsuccess = function(event) { console.log('数据更新成功'); resolve(request.result); };
request.onerror = function(event) { console.error('数据更新失败'); reject(event); };
} else {
openIndexedDB();
reject({message: '数据库未打开!'});
}
});
}
// 修改数据
export function dbUpdateData(tableName, data, params, successCallback, errorCallback) {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName], 'readwrite').objectStore(tableName).put(data);
request.onsuccess = function(event) {
if (successCallback && (typeof successCallback === 'function')) {
successCallback(request.result, params);
} else {
console.log('数据更新成功');
}
};
request.onerror = function(event) {
if (errorCallback && (typeof errorCallback === 'function')) {
errorCallback(event, params);
} else {
console.error('数据更新失败');
}
};
} else {
openIndexedDB();
setTimeout( ()=> {
dbUpdateData(tableName, data, params, successCallback, errorCallback);
}, 500);
}
}
// 删除数据
export function awaitDbDeleteData(tableName, key) {
return new Promise((resolve, reject) => {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName], 'readwrite').objectStore(tableName).delete(key);
request.onsuccess = function(event) { console.log('数据删除成功'); resolve(request.result); };
request.onerror = function(event) { console.error('数据删除失败'); reject(event); };
} else {
openIndexedDB();
reject({message: '数据库未打开!'});
}
});
}
// 删除数据
export function dbDeleteData(tableName, key, params, successCallback, errorCallback) {
if (Vue.prototype.$db) {
const request = Vue.prototype.$db.transaction([tableName], 'readwrite').objectStore(tableName).delete(key);
request.onsuccess = function (event) {
if (successCallback && (typeof successCallback === 'function')) {
request.onsuccess = successCallback(request.result, params);
} else {
request.onsuccess = function(event) { console.log('数据删除成功'); };
}
};
request.onerror = function (event) {
if (errorCallback && (typeof errorCallback === 'function')) {
errorCallback(event, params);
} else {
console.error('数据删除失败');
}
};
} else {
openIndexedDB();
setTimeout(()=> {
dbDeleteData(tableName, key, params, successCallback, errorCallback);
}, 500);
}
}