mirror of
https://github.com/fanchenio/DawnLauncher.git
synced 2025-09-14 13:56:46 +08:00
optimize code.
This commit is contained in:
parent
90594e7f21
commit
c73a2b3709
@ -14,7 +14,7 @@ import { getDataSqlite3 } from "../../commons/betterSqlite3";
|
|||||||
let db = getDataSqlite3();
|
let db = getDataSqlite3();
|
||||||
|
|
||||||
// 分类表名
|
// 分类表名
|
||||||
let classificationTableName = "classification";
|
let tableName = "classification";
|
||||||
|
|
||||||
// 查询字段
|
// 查询字段
|
||||||
let selectColumn =
|
let selectColumn =
|
||||||
@ -41,7 +41,7 @@ function getClassification(row: any): Classification {
|
|||||||
*/
|
*/
|
||||||
function init() {
|
function init() {
|
||||||
// sql
|
// sql
|
||||||
let sql = `CREATE TABLE IF NOT EXISTS ${classificationTableName} (
|
let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
parent_id INTEGER,
|
parent_id INTEGER,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
@ -53,7 +53,7 @@ function init() {
|
|||||||
// 运行
|
// 运行
|
||||||
db.exec(sql);
|
db.exec(sql);
|
||||||
// 查询有多少条数据
|
// 查询有多少条数据
|
||||||
sql = `SELECT COUNT(id) count FROM ${classificationTableName}`;
|
sql = `SELECT COUNT(id) count FROM ${tableName}`;
|
||||||
let row: any = db.prepare(sql).get();
|
let row: any = db.prepare(sql).get();
|
||||||
let count = row.count as number;
|
let count = row.count as number;
|
||||||
if (count === 0) {
|
if (count === 0) {
|
||||||
@ -70,7 +70,7 @@ function list(parentId: number | null = null) {
|
|||||||
// 参数
|
// 参数
|
||||||
let params = [];
|
let params = [];
|
||||||
// sql
|
// sql
|
||||||
let sql = `SELECT ${selectColumn} FROM ${classificationTableName}`;
|
let sql = `SELECT ${selectColumn} FROM ${tableName}`;
|
||||||
if (parentId) {
|
if (parentId) {
|
||||||
sql += " WHERE parent_id = ?";
|
sql += " WHERE parent_id = ?";
|
||||||
params.push(parentId);
|
params.push(parentId);
|
||||||
@ -103,7 +103,7 @@ function add(
|
|||||||
// 获取序号
|
// 获取序号
|
||||||
let newOrder = getMaxOrder(parentId) + 1;
|
let newOrder = getMaxOrder(parentId) + 1;
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `INSERT INTO ${classificationTableName} (parent_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`) VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
let sql = `INSERT INTO ${tableName} (parent_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`) VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||||
// 运行
|
// 运行
|
||||||
let id = db
|
let id = db
|
||||||
.prepare(sql)
|
.prepare(sql)
|
||||||
@ -146,7 +146,7 @@ function add(
|
|||||||
*/
|
*/
|
||||||
function update(classification: Classification) {
|
function update(classification: Classification) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${classificationTableName} SET name = ?, type = ?, data = ?, shortcut_key = ?, global_shortcut_key = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET name = ?, type = ?, data = ?, shortcut_key = ?, global_shortcut_key = ? WHERE id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
return (
|
return (
|
||||||
db
|
db
|
||||||
@ -169,7 +169,7 @@ function update(classification: Classification) {
|
|||||||
*/
|
*/
|
||||||
function updateData(id: number, data: ClassificationData) {
|
function updateData(id: number, data: ClassificationData) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${classificationTableName} SET data = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET data = ? WHERE id = ?`;
|
||||||
return db.prepare(sql).run(JSON.stringify(data), id).changes > 0;
|
return db.prepare(sql).run(JSON.stringify(data), id).changes > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +179,7 @@ function updateData(id: number, data: ClassificationData) {
|
|||||||
*/
|
*/
|
||||||
function selectById(id: number): Classification | null {
|
function selectById(id: number): Classification | null {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `SELECT ${selectColumn} FROM ${classificationTableName} WHERE id = ?`;
|
let sql = `SELECT ${selectColumn} FROM ${tableName} WHERE id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
let row = db.prepare(sql).get(id);
|
let row = db.prepare(sql).get(id);
|
||||||
// 返回
|
// 返回
|
||||||
@ -201,7 +201,7 @@ function del(id: number) {
|
|||||||
// 查询有无子分类
|
// 查询有无子分类
|
||||||
let childList = list(classifictaion.id);
|
let childList = list(classifictaion.id);
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `DELETE FROM ${classificationTableName} WHERE id = ? or parent_id = ?`;
|
let sql = `DELETE FROM ${tableName} WHERE id = ? or parent_id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
let res = db.prepare(sql).run(id, id).changes > 0;
|
let res = db.prepare(sql).run(id, id).changes > 0;
|
||||||
if (res) {
|
if (res) {
|
||||||
@ -256,14 +256,14 @@ function updateOrder(
|
|||||||
newOrder = getMaxOrder(parentId) + 1;
|
newOrder = getMaxOrder(parentId) + 1;
|
||||||
}
|
}
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${classificationTableName} SET \`order\` = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET \`order\` = ? WHERE id = ?`;
|
||||||
// 更新排序
|
// 更新排序
|
||||||
db.prepare(sql).run(newOrder, fromClassification.id);
|
db.prepare(sql).run(newOrder, fromClassification.id);
|
||||||
// 判断新序号和老序号之间的数据是+1还是-1
|
// 判断新序号和老序号之间的数据是+1还是-1
|
||||||
if (newOrder > fromClassification.order) {
|
if (newOrder > fromClassification.order) {
|
||||||
// 新序号和老序号之间数据,序号-1
|
// 新序号和老序号之间数据,序号-1
|
||||||
let params = [fromClassification.order, newOrder, fromClassification.id];
|
let params = [fromClassification.order, newOrder, fromClassification.id];
|
||||||
sql = `UPDATE ${classificationTableName} SET \`order\` = \`order\` - 1 WHERE \`order\` > ? AND \`order\` <= ? AND id != ?`;
|
sql = `UPDATE ${tableName} SET \`order\` = \`order\` - 1 WHERE \`order\` > ? AND \`order\` <= ? AND id != ?`;
|
||||||
if (parentId) {
|
if (parentId) {
|
||||||
sql += " AND parent_id = ?";
|
sql += " AND parent_id = ?";
|
||||||
params.push(parentId);
|
params.push(parentId);
|
||||||
@ -274,7 +274,7 @@ function updateOrder(
|
|||||||
} else {
|
} else {
|
||||||
// 新序号和老序号之间数据,序号+1
|
// 新序号和老序号之间数据,序号+1
|
||||||
let params = [newOrder, fromClassification.order, fromClassification.id];
|
let params = [newOrder, fromClassification.order, fromClassification.id];
|
||||||
sql = `UPDATE ${classificationTableName} SET \`order\` = \`order\` + 1 WHERE \`order\` >= ? AND \`order\` < ? AND id != ?`;
|
sql = `UPDATE ${tableName} SET \`order\` = \`order\` + 1 WHERE \`order\` >= ? AND \`order\` < ? AND id != ?`;
|
||||||
if (parentId) {
|
if (parentId) {
|
||||||
sql += " AND parent_id = ?";
|
sql += " AND parent_id = ?";
|
||||||
params.push(parentId);
|
params.push(parentId);
|
||||||
@ -298,7 +298,7 @@ function reorder(parentId: number | null) {
|
|||||||
// 开启事务
|
// 开启事务
|
||||||
db.transaction(() => {
|
db.transaction(() => {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${classificationTableName} SET \`order\` = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET \`order\` = ? WHERE id = ?`;
|
||||||
// 更新序号
|
// 更新序号
|
||||||
for (let i = 0; i < classificationList.length; i++) {
|
for (let i = 0; i < classificationList.length; i++) {
|
||||||
db.prepare(sql).run(i + 1, classificationList[i].id);
|
db.prepare(sql).run(i + 1, classificationList[i].id);
|
||||||
@ -312,7 +312,7 @@ function reorder(parentId: number | null) {
|
|||||||
*/
|
*/
|
||||||
function getMaxOrder(parentId: number | null) {
|
function getMaxOrder(parentId: number | null) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${classificationTableName}`;
|
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${tableName}`;
|
||||||
if (parentId) {
|
if (parentId) {
|
||||||
sql += " WHERE parent_id = ?";
|
sql += " WHERE parent_id = ?";
|
||||||
} else {
|
} else {
|
||||||
@ -337,7 +337,7 @@ function updateIcon(id: number, icon: string | null) {
|
|||||||
let classification = selectById(id);
|
let classification = selectById(id);
|
||||||
if (classification) {
|
if (classification) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${classificationTableName} SET data = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET data = ? WHERE id = ?`;
|
||||||
// 更新图标
|
// 更新图标
|
||||||
classification.data.icon = icon;
|
classification.data.icon = icon;
|
||||||
return (
|
return (
|
||||||
|
@ -26,6 +26,7 @@ if (
|
|||||||
) {
|
) {
|
||||||
app.setPath("appData", join(dirname(process.execPath), "data"));
|
app.setPath("appData", join(dirname(process.execPath), "data"));
|
||||||
app.setPath("userData", join(dirname(process.execPath), "data"));
|
app.setPath("userData", join(dirname(process.execPath), "data"));
|
||||||
|
app.setPath("sessionData", join(dirname(process.execPath), "data"));
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.DIST_ELECTRON = join(__dirname, "..");
|
process.env.DIST_ELECTRON = join(__dirname, "..");
|
||||||
|
@ -7,7 +7,7 @@ import { getDataSqlite3 } from "../../commons/betterSqlite3";
|
|||||||
let db = getDataSqlite3();
|
let db = getDataSqlite3();
|
||||||
|
|
||||||
// 项目表名
|
// 项目表名
|
||||||
let itemTableName = "item";
|
let tableName = "item";
|
||||||
|
|
||||||
// 查询字段
|
// 查询字段
|
||||||
let selectColumn =
|
let selectColumn =
|
||||||
@ -36,7 +36,7 @@ function getItem(row: any): Item {
|
|||||||
*/
|
*/
|
||||||
function init() {
|
function init() {
|
||||||
// sql
|
// sql
|
||||||
let sql = `CREATE TABLE IF NOT EXISTS ${itemTableName} (
|
let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
classification_id INTEGER NOT NULL,
|
classification_id INTEGER NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
@ -58,7 +58,7 @@ function add(item: Item, reuseId: boolean = false) {
|
|||||||
// 获取序号
|
// 获取序号
|
||||||
let newOrder = getMaxOrder(item.classificationId) + 1;
|
let newOrder = getMaxOrder(item.classificationId) + 1;
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `INSERT INTO ${itemTableName}
|
let sql = `INSERT INTO ${tableName}
|
||||||
(classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
(classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||||
// 参数
|
// 参数
|
||||||
@ -73,7 +73,7 @@ function add(item: Item, reuseId: boolean = false) {
|
|||||||
];
|
];
|
||||||
// 重复使用ID
|
// 重复使用ID
|
||||||
if (reuseId && item.id) {
|
if (reuseId && item.id) {
|
||||||
sql = `INSERT INTO ${itemTableName}
|
sql = `INSERT INTO ${tableName}
|
||||||
(id, classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
(id, classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||||
params.unshift(item.id);
|
params.unshift(item.id);
|
||||||
@ -108,7 +108,7 @@ function batchAdd(
|
|||||||
// 循环添加
|
// 循环添加
|
||||||
for (let item of itemList) {
|
for (let item of itemList) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `INSERT INTO ${itemTableName}
|
let sql = `INSERT INTO ${tableName}
|
||||||
(classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
(classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||||
// 参数
|
// 参数
|
||||||
@ -123,7 +123,7 @@ function batchAdd(
|
|||||||
];
|
];
|
||||||
// 重复使用ID
|
// 重复使用ID
|
||||||
if (reuseId && item.id) {
|
if (reuseId && item.id) {
|
||||||
sql = `INSERT INTO ${itemTableName}
|
sql = `INSERT INTO ${tableName}
|
||||||
(id, classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
(id, classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||||
params.unshift(item.id);
|
params.unshift(item.id);
|
||||||
@ -147,7 +147,7 @@ function batchAdd(
|
|||||||
*/
|
*/
|
||||||
function update(item: Item) {
|
function update(item: Item) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${itemTableName}
|
let sql = `UPDATE ${tableName}
|
||||||
SET name = ?,
|
SET name = ?,
|
||||||
data = ?,
|
data = ?,
|
||||||
shortcut_key = ?,
|
shortcut_key = ?,
|
||||||
@ -177,7 +177,7 @@ function updateClassificationId(
|
|||||||
newClassificationId: number
|
newClassificationId: number
|
||||||
) {
|
) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${itemTableName}
|
let sql = `UPDATE ${tableName}
|
||||||
SET classification_id = ?
|
SET classification_id = ?
|
||||||
WHERE classification_id = ?`;
|
WHERE classification_id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
@ -193,7 +193,7 @@ function updateClassificationId(
|
|||||||
*/
|
*/
|
||||||
function updateData(id: number, itemData: ItemData) {
|
function updateData(id: number, itemData: ItemData) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${itemTableName}
|
let sql = `UPDATE ${tableName}
|
||||||
SET data = ?
|
SET data = ?
|
||||||
WHERE id = ?`;
|
WHERE id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
@ -206,7 +206,7 @@ function updateData(id: number, itemData: ItemData) {
|
|||||||
*/
|
*/
|
||||||
function getMaxOrder(classificationId: number) {
|
function getMaxOrder(classificationId: number) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${itemTableName} WHERE classification_id = ?`;
|
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${tableName} WHERE classification_id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
let row: any = db.prepare(sql).get(classificationId);
|
let row: any = db.prepare(sql).get(classificationId);
|
||||||
if (row && row.order) {
|
if (row && row.order) {
|
||||||
@ -222,7 +222,7 @@ function getMaxOrder(classificationId: number) {
|
|||||||
*/
|
*/
|
||||||
function selectById(id: number): Item | null {
|
function selectById(id: number): Item | null {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `SELECT ${selectColumn} FROM ${itemTableName} WHERE id = ?`;
|
let sql = `SELECT ${selectColumn} FROM ${tableName} WHERE id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
let row = db.prepare(sql).get(id);
|
let row = db.prepare(sql).get(id);
|
||||||
// 返回
|
// 返回
|
||||||
@ -244,7 +244,7 @@ function list(simple: boolean = false, classificationId: number | null = null) {
|
|||||||
// sql
|
// sql
|
||||||
let sql = `SELECT ${
|
let sql = `SELECT ${
|
||||||
simple ? simpleSelectColumn : selectColumn
|
simple ? simpleSelectColumn : selectColumn
|
||||||
} FROM ${itemTableName}`;
|
} FROM ${tableName}`;
|
||||||
if (classificationId) {
|
if (classificationId) {
|
||||||
sql += " WHERE classification_id = ?";
|
sql += " WHERE classification_id = ?";
|
||||||
params.push(classificationId);
|
params.push(classificationId);
|
||||||
@ -269,7 +269,7 @@ function selectByIdList(simple: boolean, idList: Array<number>) {
|
|||||||
// sql
|
// sql
|
||||||
let sql = `SELECT ${
|
let sql = `SELECT ${
|
||||||
simple ? simpleSelectColumn : selectColumn
|
simple ? simpleSelectColumn : selectColumn
|
||||||
} FROM ${itemTableName} WHERE id IN (`;
|
} FROM ${tableName} WHERE id IN (`;
|
||||||
for (let i = 0; i < idList.length; i++) {
|
for (let i = 0; i < idList.length; i++) {
|
||||||
sql += "?";
|
sql += "?";
|
||||||
if (i !== idList.length - 1) {
|
if (i !== idList.length - 1) {
|
||||||
@ -307,7 +307,7 @@ function del(id: number) {
|
|||||||
let item = selectById(id);
|
let item = selectById(id);
|
||||||
if (item) {
|
if (item) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `DELETE FROM ${itemTableName} WHERE id = ?`;
|
let sql = `DELETE FROM ${tableName} WHERE id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
let res = db.prepare(sql).run(id).changes > 0;
|
let res = db.prepare(sql).run(id).changes > 0;
|
||||||
if (res) {
|
if (res) {
|
||||||
@ -328,7 +328,7 @@ function del(id: number) {
|
|||||||
*/
|
*/
|
||||||
function deleteByClassificationId(classificationId: number) {
|
function deleteByClassificationId(classificationId: number) {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `DELETE FROM ${itemTableName} WHERE classification_id = ?`;
|
let sql = `DELETE FROM ${tableName} WHERE classification_id = ?`;
|
||||||
// 运行
|
// 运行
|
||||||
return db.prepare(sql).run(classificationId).changes > 0;
|
return db.prepare(sql).run(classificationId).changes > 0;
|
||||||
}
|
}
|
||||||
@ -343,7 +343,7 @@ function reorder(classification_id: number) {
|
|||||||
// 开启事务
|
// 开启事务
|
||||||
db.transaction(() => {
|
db.transaction(() => {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${itemTableName} SET \`order\` = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET \`order\` = ? WHERE id = ?`;
|
||||||
// 更新序号
|
// 更新序号
|
||||||
for (let i = 0; i < itemList.length; i++) {
|
for (let i = 0; i < itemList.length; i++) {
|
||||||
db.prepare(sql).run(i + 1, itemList[i].id);
|
db.prepare(sql).run(i + 1, itemList[i].id);
|
||||||
@ -396,7 +396,7 @@ function updateOrder(
|
|||||||
// 开启事务
|
// 开启事务
|
||||||
db.transaction(() => {
|
db.transaction(() => {
|
||||||
// SQL
|
// SQL
|
||||||
let sql = `UPDATE ${itemTableName} SET \`order\` = ?, classification_id = ? WHERE id = ?`;
|
let sql = `UPDATE ${tableName} SET \`order\` = ?, classification_id = ? WHERE id = ?`;
|
||||||
// 更新序号
|
// 更新序号
|
||||||
for (let i = 0; i < toItemList.length; i++) {
|
for (let i = 0; i < toItemList.length; i++) {
|
||||||
db.prepare(sql).run(i + 1, toClassificationId, toItemList[i].id);
|
db.prepare(sql).run(i + 1, toClassificationId, toItemList[i].id);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { BrowserWindow, shell } from "electron";
|
import { BrowserWindow, shell } from "electron";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { getMainBackgorunColor, sendToWebContent } from "../commons";
|
import { sendToWebContent } from "../commons";
|
||||||
import cacheData from "../commons/cacheData";
|
import cacheData from "../commons/cacheData";
|
||||||
|
|
||||||
// 窗口
|
// 窗口
|
||||||
|
Loading…
Reference in New Issue
Block a user