From 25aefe2aaad919d74b8ac6ccb2d3909de5a970d5 Mon Sep 17 00:00:00 2001 From: fanxb Date: Tue, 29 Nov 2022 23:02:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=A7=84=E5=88=99=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openRenamerBackend/api/ApplicationRuleApi.ts | 11 +- openRenamerBackend/api/GlobalConfigApi.ts | 22 +++ openRenamerBackend/dao/ApplicationRuleDao.ts | 14 +- openRenamerBackend/dao/GlobalConfigDao.ts | 50 +++++++ openRenamerBackend/dao/SavePathDao.ts | 2 +- .../constants/GlobalConfigCodeConstant.ts | 4 + .../entity/{dto => po}/ApplicationRule.ts | 52 ++++--- openRenamerBackend/entity/po/GlobalConfig.ts | 21 +++ .../entity/{dto => po}/SavePath.ts | 0 .../service/ApplicationRuleService.ts | 41 +++++- openRenamerBackend/service/FileService.ts | 2 +- openRenamerBackend/service/GlobalService.ts | 18 +++ openRenamerBackend/sqls/v003_新增默认模板.sql | 6 + openRenamerBackend/tsconfig.json | 4 +- openRenamerBackend/util/SqliteHelper.ts | 2 +- openRenamerFront/package.json | 2 +- openRenamerFront/src/App.vue | 19 ++- openRenamerFront/src/views/home/Home.vue | 72 +++++++--- .../home/components/ApplicationRuleList.vue | 78 +++++++++-- .../src/views/home/components/RuleBlock.vue | 127 +++++++++--------- 20 files changed, 418 insertions(+), 129 deletions(-) create mode 100644 openRenamerBackend/api/GlobalConfigApi.ts create mode 100644 openRenamerBackend/dao/GlobalConfigDao.ts create mode 100644 openRenamerBackend/entity/constants/GlobalConfigCodeConstant.ts rename openRenamerBackend/entity/{dto => po}/ApplicationRule.ts (56%) create mode 100644 openRenamerBackend/entity/po/GlobalConfig.ts rename openRenamerBackend/entity/{dto => po}/SavePath.ts (100%) create mode 100644 openRenamerBackend/service/GlobalService.ts create mode 100644 openRenamerBackend/sqls/v003_新增默认模板.sql diff --git a/openRenamerBackend/api/ApplicationRuleApi.ts b/openRenamerBackend/api/ApplicationRuleApi.ts index 2299846..39169f8 100644 --- a/openRenamerBackend/api/ApplicationRuleApi.ts +++ b/openRenamerBackend/api/ApplicationRuleApi.ts @@ -1,6 +1,5 @@ import { Context } from "koa"; import ApplicationRuleService from "../service/ApplicationRuleService"; -import config from "../config"; const router = {}; @@ -11,6 +10,14 @@ router["GET /applicationRule"] = async function (ctx: Context) { ctx.body = await ApplicationRuleService.getAll(); }; +/** + * 获取默认模板 + */ +router["GET /applicationRule/default"] = async function (ctx: Context) { + ; + ctx.body = await ApplicationRuleService.getDefault(); +}; + /** * 更新或者插入 */ @@ -26,4 +33,6 @@ router["DELETE /applicationRule/:id"] = async function (ctx: Context) { ctx.body = ""; }; + + export default router; diff --git a/openRenamerBackend/api/GlobalConfigApi.ts b/openRenamerBackend/api/GlobalConfigApi.ts new file mode 100644 index 0000000..6e1e141 --- /dev/null +++ b/openRenamerBackend/api/GlobalConfigApi.ts @@ -0,0 +1,22 @@ +import { Context } from "koa"; +import service from "../service/GlobalService"; + +const router = {}; + +/** + * 预览文件修改后的状态 + */ +router["GET /config/code"] = async function (ctx: Context) { + ctx.body = await service.getVal(ctx.request.query.code as string); +}; + +/** + * 提交修改 + */ +router["POST /config/update"] = async function (ctx: Context) { + ctx.body = await service.updateVal(ctx.request.body.code, ctx.request.body.val); +}; + + + +export default router; diff --git a/openRenamerBackend/dao/ApplicationRuleDao.ts b/openRenamerBackend/dao/ApplicationRuleDao.ts index 5b223e0..5cf3089 100644 --- a/openRenamerBackend/dao/ApplicationRuleDao.ts +++ b/openRenamerBackend/dao/ApplicationRuleDao.ts @@ -1,5 +1,5 @@ import ErrorHelper from "../util/ErrorHelper"; -import ApplicationRule from "../entity/dto/ApplicationRule"; +import ApplicationRule from "../entity/po/ApplicationRule"; import SqliteHelper from "../util/SqliteHelper"; export default class ApplicationRuleDao { @@ -13,6 +13,18 @@ export default class ApplicationRuleDao { return res; } + /** + * 查询id + * @param id id + * @returns + */ + static async getById(id: number): Promise { + let res = await SqliteHelper.pool.get('select * from application_rule where id=?', id); + return res; + } + + + /** * 新增 diff --git a/openRenamerBackend/dao/GlobalConfigDao.ts b/openRenamerBackend/dao/GlobalConfigDao.ts new file mode 100644 index 0000000..4daf590 --- /dev/null +++ b/openRenamerBackend/dao/GlobalConfigDao.ts @@ -0,0 +1,50 @@ +import ErrorHelper from "../util/ErrorHelper"; +import SqliteHelper from "../util/SqliteHelper"; +import GlobalConfig from "../entity/po/GlobalConfig"; + +export default class GlobalConfigDao { + + + /** + * 新增 + * @param obj + * @returns + */ + static async addOne(obj: GlobalConfig): Promise { + await SqliteHelper.pool.run('insert into global_config(code,val,description) values(?,?,?)' + , obj.code, obj.val, obj.description); + } + + /** + * 更新 + * @param code code + * @param val val + */ + static async updateOne(code: string, val: string): Promise { + await SqliteHelper.pool.run('update global_config set val=? where code=?', val, code); + } + + + /** + * 删除 + * @param code + */ + static async deleteByCode(code: string): Promise { + let res = await SqliteHelper.pool.run('delete from global_config where code=?', code); + if (res.changes == 0) { + throw ErrorHelper.Error404("数据不存在"); + } + } + + /** + * 查询 + * @param code + */ + static async getByCode(code: string): Promise { + let res = await SqliteHelper.pool.get('select val from global_config where code=?', code); + return res ? res.val : null; + + } + + +} \ No newline at end of file diff --git a/openRenamerBackend/dao/SavePathDao.ts b/openRenamerBackend/dao/SavePathDao.ts index b4de79a..71e3ed2 100644 --- a/openRenamerBackend/dao/SavePathDao.ts +++ b/openRenamerBackend/dao/SavePathDao.ts @@ -1,5 +1,5 @@ import ErrorHelper from "../util/ErrorHelper"; -import SavePath from "../entity/dto/SavePath"; +import SavePath from "../entity/po/SavePath"; import SqliteHelper from "../util/SqliteHelper"; export default class SavePathDao { diff --git a/openRenamerBackend/entity/constants/GlobalConfigCodeConstant.ts b/openRenamerBackend/entity/constants/GlobalConfigCodeConstant.ts new file mode 100644 index 0000000..e39b8f2 --- /dev/null +++ b/openRenamerBackend/entity/constants/GlobalConfigCodeConstant.ts @@ -0,0 +1,4 @@ +/** + * 默认模板id + */ +export const DEFAULT_TEMPLETE_ID = "defaultTempleteId"; \ No newline at end of file diff --git a/openRenamerBackend/entity/dto/ApplicationRule.ts b/openRenamerBackend/entity/po/ApplicationRule.ts similarity index 56% rename from openRenamerBackend/entity/dto/ApplicationRule.ts rename to openRenamerBackend/entity/po/ApplicationRule.ts index 3565fcd..660cbf4 100644 --- a/openRenamerBackend/entity/dto/ApplicationRule.ts +++ b/openRenamerBackend/entity/po/ApplicationRule.ts @@ -1,23 +1,31 @@ -export default class ApplicationRule { - /** - 创建时间 - */ - createdDate: number; - /** - 更新时间 - */ - updatedDate: number; - id: number; - /** - 名称 - */ - name: string; - /** - 说明 - */ - comment: string; - /** - 规则内容,json序列化后 - */ - content: string; +export default class ApplicationRule { + /** + 创建时间 + */ + createdDate: number; + /** + 更新时间 + */ + updatedDate: number; + id: number; + /** + 名称 + */ + name: string; + /** + 说明 + */ + comment: string; + /** + 规则内容,json序列化后 + */ + content: string; + + constructor(name: string, comment: string, content: string) { + this.createdDate = Date.now(); + this.updatedDate = this.createdDate; + this.name = name; + this.comment = comment; + this.content = content; + } } \ No newline at end of file diff --git a/openRenamerBackend/entity/po/GlobalConfig.ts b/openRenamerBackend/entity/po/GlobalConfig.ts new file mode 100644 index 0000000..bbd4f6d --- /dev/null +++ b/openRenamerBackend/entity/po/GlobalConfig.ts @@ -0,0 +1,21 @@ +export default class GlobalConfig { + /** + code + */ + code: string; + + /** + 规则内容,json序列化后 + */ + val: string; + /** + 描述 + */ + description: string; + + constructor(code: string, val: string, desc: string) { + this.code = code; + this.val = val; + this.description = desc; + } +} \ No newline at end of file diff --git a/openRenamerBackend/entity/dto/SavePath.ts b/openRenamerBackend/entity/po/SavePath.ts similarity index 100% rename from openRenamerBackend/entity/dto/SavePath.ts rename to openRenamerBackend/entity/po/SavePath.ts diff --git a/openRenamerBackend/service/ApplicationRuleService.ts b/openRenamerBackend/service/ApplicationRuleService.ts index b4078cf..e1f46c9 100644 --- a/openRenamerBackend/service/ApplicationRuleService.ts +++ b/openRenamerBackend/service/ApplicationRuleService.ts @@ -1,9 +1,9 @@ -import config from '../config'; -import * as path from 'path'; -import * as fs from 'fs-extra'; -import ApplicationRule from '../entity/dto/ApplicationRule'; +import ApplicationRule from '../entity/po/ApplicationRule'; import ApplicationRuleDao from '../dao/ApplicationRuleDao'; +import GlobalConfigDao from '../dao/GlobalConfigDao'; +import { DEFAULT_TEMPLETE_ID } from '../entity/constants/GlobalConfigCodeConstant'; +import GlobalConfig from '../entity/po/GlobalConfig'; class ApplicationRuleService { @@ -28,6 +28,39 @@ class ApplicationRuleService { await ApplicationRuleDao.delete(id); } + /** + * 获取默认模板 + */ + static async getDefault(): Promise { + let res: ApplicationRule; + let idStr = await GlobalConfigDao.getByCode(DEFAULT_TEMPLETE_ID); + if (idStr == null) { + let templteList = await ApplicationRuleDao.getAll(); + if (templteList.length == 0) { + res = new ApplicationRule("默认模板", "此模板为系统创建", "[]"); + await ApplicationRuleService.saveOrAdd(res); + } else { + res = templteList[0]; + } + await GlobalConfigDao.addOne(new GlobalConfig(DEFAULT_TEMPLETE_ID, res.id.toString(), "默认模板id")); + } else { + let templteList = await ApplicationRuleDao.getAll(); + if (templteList.length == 0) { + res = new ApplicationRule("默认模板", "此模板为系统创建", "[]"); + await ApplicationRuleService.saveOrAdd(res); + await GlobalConfigDao.updateOne(DEFAULT_TEMPLETE_ID, res.id.toString()); + } else { + let temp = templteList.filter(item => item.id.toString() === idStr); + if (temp.length > 0) { + res = temp[0]; + } else { + res = templteList[0]; + await GlobalConfigDao.updateOne(DEFAULT_TEMPLETE_ID, res.id.toString()); + } + } + } + return res; + } } export default ApplicationRuleService; diff --git a/openRenamerBackend/service/FileService.ts b/openRenamerBackend/service/FileService.ts index ac7d1d5..7fa4e24 100644 --- a/openRenamerBackend/service/FileService.ts +++ b/openRenamerBackend/service/FileService.ts @@ -5,7 +5,7 @@ import * as fs from 'fs-extra'; import ProcessHelper from '../util/ProcesHelper'; import FileObj from '../entity/vo/FileObj'; import SavePathDao from '../dao/SavePathDao'; -import SavePath from '../entity/dto/SavePath'; +import SavePath from '../entity/po/SavePath'; let numberSet = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]); diff --git a/openRenamerBackend/service/GlobalService.ts b/openRenamerBackend/service/GlobalService.ts new file mode 100644 index 0000000..e247424 --- /dev/null +++ b/openRenamerBackend/service/GlobalService.ts @@ -0,0 +1,18 @@ +import GlobalConfigDao from '../dao/GlobalConfigDao'; + +import { DEFAULT_TEMPLETE_ID } from '../entity/constants/GlobalConfigCodeConstant'; +import GlobalConfig from '../entity/po/GlobalConfig'; + + +class GlobalConfigService { + + static async getVal(code: string): Promise { + return GlobalConfigDao.getByCode(code); + } + + static async updateVal(code: string, val: string): Promise { + return GlobalConfigDao.updateOne(code, val); + } +} + +export default GlobalConfigService; diff --git a/openRenamerBackend/sqls/v003_新增默认模板.sql b/openRenamerBackend/sqls/v003_新增默认模板.sql new file mode 100644 index 0000000..2059abf --- /dev/null +++ b/openRenamerBackend/sqls/v003_新增默认模板.sql @@ -0,0 +1,6 @@ +CREATE TABLE global_config ( + code TEXT(40), + val TEXT(200), + description TEXT(100) DEFAULT (''), + CONSTRAINT global_config_PK PRIMARY KEY (code) +); \ No newline at end of file diff --git a/openRenamerBackend/tsconfig.json b/openRenamerBackend/tsconfig.json index b9f31eb..b618663 100644 --- a/openRenamerBackend/tsconfig.json +++ b/openRenamerBackend/tsconfig.json @@ -5,11 +5,11 @@ "module": "commonjs", "sourceMap": true, "outDir": "./dist", - "baseUrl":".", + "baseUrl": ".", "rootDir": "./", "watch": false, "strict": true, "strictNullChecks": false, "esModuleInterop": true } -} \ No newline at end of file +} diff --git a/openRenamerBackend/util/SqliteHelper.ts b/openRenamerBackend/util/SqliteHelper.ts index d2e399d..4a5c098 100644 --- a/openRenamerBackend/util/SqliteHelper.ts +++ b/openRenamerBackend/util/SqliteHelper.ts @@ -40,7 +40,7 @@ class SqliteHelper { try { let sql = ""; for (let j = 0; j < sqlLines.length; j++) { - sql = sql + sqlLines[j]; + sql = sql + " " + sqlLines[j]; if (sqlLines[j].endsWith(";")) { await SqliteHelper.pool.run(sql); sql = ""; diff --git a/openRenamerFront/package.json b/openRenamerFront/package.json index cb1b59c..045fd56 100644 --- a/openRenamerFront/package.json +++ b/openRenamerFront/package.json @@ -12,7 +12,7 @@ "axios": "^0.21.1", "core-js": "^3.6.5", "dayjs": "^1.10.7", - "element-plus": "^2.2.5", + "element-plus": "^2.2.25", "vue": "^3.0.0", "vue-router": "^4.0.0-0" }, diff --git a/openRenamerFront/src/App.vue b/openRenamerFront/src/App.vue index b284d23..9e8605e 100644 --- a/openRenamerFront/src/App.vue +++ b/openRenamerFront/src/App.vue @@ -1,6 +1,8 @@