100 lines
3.3 KiB
JavaScript
100 lines
3.3 KiB
JavaScript
var db;
|
||
|
||
function initDatabase() {
|
||
db = LocalStorage.openDatabaseSync("AntSEV", "1.0", "基本数据看", 100000);
|
||
try {
|
||
db.transaction(function (tx) {
|
||
tx.executeSql(`CREATE TABLE ProjectMenuTree (
|
||
id TEXT PRIMARY KEY,
|
||
pid TEXT, -- 父级ID,可以用于表示层级关系
|
||
project_id TEXT NOT NULL, -- 项目ID,非空
|
||
title TEXT NOT NULL, -- 标题,非空
|
||
ico TEXT -- 图标,可以存储图标路径或名称
|
||
);`);
|
||
|
||
tx.executeSql(`INSERT INTO ProjectMenuTree (id,pid,project_id,title,ico) VALUES
|
||
('1','-1','1','节点1','ico_tree_folder.png'),
|
||
('2','-1','1','节点2','ico_tree_folder.png'),
|
||
('3','-1','1','节点3','ico_tree_folder.png'),
|
||
('4','6','1','节点61','ico_tree_folder.png'),
|
||
('5','-1','1','节点5','ico_tree_folder.png'),
|
||
('6','7','1','节点6','ico_tree_line.png'),
|
||
('7','3','1','节点节点节点节点613','ico_tree_car.png');`)
|
||
})
|
||
} catch (err) {
|
||
// console.log("Error creating table in database: " + err)
|
||
};
|
||
}
|
||
|
||
function buildTree(data) {
|
||
// 创建一个映射表,用于快速查找每个节点
|
||
const map = new Map();
|
||
data.forEach(item => {
|
||
map.set(item.id, { id: item.id, title: item.title, ico: item.ico, items: [] }); // 初始化每个节点,并添加 children 属性
|
||
});
|
||
|
||
// 构建树结构
|
||
const tree = [];
|
||
data.forEach(item => {
|
||
if (item.pid !== "-1") { // 如果有父节点
|
||
const parent = map.get(item.pid);
|
||
if (parent) {
|
||
parent.items.push(map.get(item.id)); // 将当前节点添加到父节点的 children 中
|
||
}
|
||
} else {
|
||
tree.push(map.get(item.id)); // 如果没有父节点,则为根节点
|
||
}
|
||
});
|
||
|
||
return tree;
|
||
}
|
||
|
||
function readMenuTree(project_id) {
|
||
var res = []
|
||
var datas = []
|
||
if (!db) { return; }
|
||
db.transaction(function (tx) {
|
||
var result = tx.executeSql('select * from ProjectMenuTree where project_id=?', [project_id]);
|
||
if (result.rows.length > 0) {
|
||
res = result.rows
|
||
for (let i = 0; i < res.length; i++) {
|
||
let d = res.item(i)
|
||
datas.push(d)
|
||
}
|
||
res = buildTree(datas)
|
||
|
||
} else {
|
||
res = []
|
||
}
|
||
})
|
||
console.log(JSON.stringify(res))
|
||
return res
|
||
}
|
||
|
||
function readData(name) {
|
||
var res = "";
|
||
if (!db) { return; }
|
||
db.transaction(function (tx) {
|
||
var result = tx.executeSql('select value from data where name=?', [name]);
|
||
if (result.rows.length > 0) {
|
||
res = result.rows.item(0).value;
|
||
} else {
|
||
res = "Unknown";
|
||
}
|
||
})
|
||
return res
|
||
}
|
||
|
||
function insertData(name, value) {
|
||
var res = "";
|
||
if (!db) { return; }
|
||
db.transaction(function (tx) {
|
||
var result = tx.executeSql('INSERT OR REPLACE INTO data VALUES (?,?);', [name, value]);
|
||
if (result.rowsAffected > 0) {
|
||
res = "OK";
|
||
} else {
|
||
res = "Error";
|
||
}
|
||
})
|
||
return res
|
||
} |