diff --git a/openRenamerBackend/api/GlobalConfigApi.ts b/openRenamerBackend/api/GlobalConfigApi.ts index 6e1e141..8691d29 100644 --- a/openRenamerBackend/api/GlobalConfigApi.ts +++ b/openRenamerBackend/api/GlobalConfigApi.ts @@ -1,15 +1,22 @@ import { Context } from "koa"; -import service from "../service/GlobalService"; +import service from "../service/GlobalConfigService"; const router = {}; /** - * 预览文件修改后的状态 + * 获取单个配置 */ router["GET /config/code"] = async function (ctx: Context) { ctx.body = await service.getVal(ctx.request.query.code as string); }; +/** + * 获取多个配置项 + */ +router["POST /config/multCode"] = async function (ctx: Context) { + ctx.body = await service.getMultVal(ctx.request.body); +}; + /** * 提交修改 */ diff --git a/openRenamerBackend/api/QbServiceApi.ts b/openRenamerBackend/api/QbServiceApi.ts new file mode 100644 index 0000000..6f2e69d --- /dev/null +++ b/openRenamerBackend/api/QbServiceApi.ts @@ -0,0 +1,14 @@ +import { Context } from "koa"; +import service from "../service/QbService"; + +const router = {}; + +/** + * 获取单个配置 + */ +router["POST /qb/saveQbInfo"] = async function (ctx: Context) { + ctx.body = await service.saveAddress(ctx.request.body); +}; + + +export default router; diff --git a/openRenamerBackend/dao/GlobalConfigDao.ts b/openRenamerBackend/dao/GlobalConfigDao.ts index 4daf590..bc4503d 100644 --- a/openRenamerBackend/dao/GlobalConfigDao.ts +++ b/openRenamerBackend/dao/GlobalConfigDao.ts @@ -43,7 +43,26 @@ export default class GlobalConfigDao { 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; + } + /** + * 查询多个code + * @param code + */ + static async getByMulCode(codes: Array): Promise> { + if (codes.length == 0) { + return new Array(); + } + let codeStr = codes.map(item => `'${item}'`).join(','); + return await SqliteHelper.pool.all(`select * from global_config where code in (${codeStr})`); + } + + /** + * 插入一条 + * @param body body + */ + static async insertOrReplace(body: GlobalConfig): Promise { + await SqliteHelper.pool.run(`insert or replace into global_config values (?,?,?)`, body.code, body.val, body.description); } diff --git a/openRenamerBackend/entity/bo/rules/AutoRule.ts b/openRenamerBackend/entity/bo/rules/AutoRule.ts index f2098c2..761dda5 100644 --- a/openRenamerBackend/entity/bo/rules/AutoRule.ts +++ b/openRenamerBackend/entity/bo/rules/AutoRule.ts @@ -4,7 +4,7 @@ import path from 'path'; let pattern = new RegExp(/s(eason)?(\d+)/); -let eNumPatternArr = [new RegExp(/e(\d+)/), new RegExp(/\((\d+)\)/), new RegExp(/((\d+))/), new RegExp(/\.(\d+)\./), new RegExp(/-(\d+)/), new RegExp(/(\d+)/)]; +let eNumPatternArr = [new RegExp(/e[p]?(\d+)/), new RegExp(/[\(\[(](\d+)[\)\])]/), new RegExp(/[\.-](\d+)/), new RegExp(/(\d+)/)]; let resolutionPattern = new RegExp(/(\d{3,}[pP])/); let resolutionArr = ['1k', '1K', '2k', '2K', '4k', '4K', '8k', '8K']; let charSet = new Set([' ', '[', '.', '(', '(']); diff --git a/openRenamerBackend/entity/dto/QbAddressDto.ts b/openRenamerBackend/entity/dto/QbAddressDto.ts new file mode 100644 index 0000000..0b8ccb0 --- /dev/null +++ b/openRenamerBackend/entity/dto/QbAddressDto.ts @@ -0,0 +1,5 @@ +export default interface QbAddressDto { + address: string; + username: string; + password: string; +} \ No newline at end of file diff --git a/openRenamerBackend/index.ts b/openRenamerBackend/index.ts index 8dff10c..81a621f 100644 --- a/openRenamerBackend/index.ts +++ b/openRenamerBackend/index.ts @@ -9,6 +9,7 @@ import handleError from "./middleware/handleError"; import init from "./middleware/init"; import SqliteUtil from './util/SqliteHelper'; import log from './util/LogUtil'; +import { updateQbInfo } from './util/QbApiUtil'; console.log(config); @@ -30,6 +31,7 @@ app.use(handleError); app.use(RouterMW(router, path.join(config.rootPath, "dist/api"))); (async () => { await SqliteUtil.createPool(); + await updateQbInfo(null, null); app.listen(config.port); log.info(`server listened `, config.port); })(); diff --git a/openRenamerBackend/package.json b/openRenamerBackend/package.json index 8c8208e..c5e66f8 100644 --- a/openRenamerBackend/package.json +++ b/openRenamerBackend/package.json @@ -12,7 +12,7 @@ "@types/fs-extra": "^5.0.4", "@types/koa": "^2.0.47", "@types/node": "^11.13.4", - "axios": "^0.21.1", + "axios": "^0.21.4", "fs-extra": "^7.0.0", "koa": "^2.5.3", "koa-body": "^4.0.4", diff --git a/openRenamerBackend/service/GlobalService.ts b/openRenamerBackend/service/GlobalConfigService.ts similarity index 56% rename from openRenamerBackend/service/GlobalService.ts rename to openRenamerBackend/service/GlobalConfigService.ts index e247424..d7730e2 100644 --- a/openRenamerBackend/service/GlobalService.ts +++ b/openRenamerBackend/service/GlobalConfigService.ts @@ -10,9 +10,24 @@ class GlobalConfigService { return GlobalConfigDao.getByCode(code); } + /** + * 获取多个配置 + * @param codes codes + * @returns + */ + static async getMultVal(codes: Array): Promise { + let re = {}; + (await GlobalConfigDao.getByMulCode(codes)).forEach(item => re[item.code] = item.val); + return re; + } + static async updateVal(code: string, val: string): Promise { return GlobalConfigDao.updateOne(code, val); } + + static async insertOrReplace(body: GlobalConfig): Promise { + return GlobalConfigDao.insertOrReplace(body); + } } export default GlobalConfigService; diff --git a/openRenamerBackend/service/QbService.ts b/openRenamerBackend/service/QbService.ts new file mode 100644 index 0000000..877536d --- /dev/null +++ b/openRenamerBackend/service/QbService.ts @@ -0,0 +1,17 @@ +import QbAddressDto from "../entity/dto/QbAddressDto"; +import { tryLogin, updateQbInfo } from '../util/QbApiUtil'; +import GlobalConfigService from "./GlobalConfigService"; +import GlobalConfig from "../entity/po/GlobalConfig"; + +class QbService { + + static async saveAddress(body: QbAddressDto) { + await tryLogin(body.address, body.username, body.password, false); + await GlobalConfigService.insertOrReplace(new GlobalConfig("qbAddress", body.address, "qbAdress")); + await GlobalConfigService.insertOrReplace(new GlobalConfig("qbUsername", body.username, "")); + await GlobalConfigService.insertOrReplace(new GlobalConfig("qbPassword", body.password, "")); + await updateQbInfo(body, true); + } +} + +export default QbService; diff --git a/openRenamerBackend/util/QbApiUtil.ts b/openRenamerBackend/util/QbApiUtil.ts new file mode 100644 index 0000000..2097e12 --- /dev/null +++ b/openRenamerBackend/util/QbApiUtil.ts @@ -0,0 +1,102 @@ +import { Method } from "axios"; +import axios from "axios"; +import QbAddressDto from "../entity/dto/QbAddressDto"; +import GlobalService from '../service/GlobalConfigService'; +import { setUncaughtExceptionCaptureCallback } from "process"; + +//qb状态,true正常,false:无法访问 +let qbStatus = true; +let qbInfo: QbAddressDto = null; +let cookie: string = null; + +export function getQbStatus() { + return qbStatus; +} + +export async function updateQbInfo(info: QbAddressDto, status: boolean) { + if (!info) { + let obj = await GlobalService.getMultVal(["qbAddress", "qbUsername", "qbPassword"]); + if (!obj.qbAddress) { + qbStatus = false; + return; + } + qbInfo.address = obj.qbAddress; + qbInfo.username = obj.qbUsername; + qbInfo.password = obj.qbPassword; + } else { + qbInfo = info; + } + if (status) { + qbStatus = status; + } + + axios.defaults.baseURL = qbInfo.address; +} + +export function get() { + +} + +export function post() { + +} + +async function request(method: Method, url: string, query: any, body: any, isForm = false) { + if (!qbStatus) { + throw new Error("qbittorrent无法连接,请检查配置"); + } + let isTryLogin = false; + while (true) { + let headers = { "Cookie": cookie }; + if (isForm) { + headers['content-type'] = "multipart/form-data"; + } else if (method == "post") { + headers['content-type'] = "application/json"; + } + let res = await axios.request({ + baseURL: qbInfo.address, + url: url, + method, + params: query, + data: body, + headers, + }); + if (res.status == 200) { + return res.data; + } if (res.status == 403) { + if (isTryLogin) { + throw new Error("qb用户名密码设置有误"); + } else { + await tryLogin(qbInfo.address, qbInfo.username, qbInfo.password, true); + isTryLogin = true; + } + } else { + throw new Error("请求报错:" + res.data); + } + } + +} + +export async function tryLogin(address: string, username: string, password: string, updateStatus: boolean): Promise { + let body = { username, password }; + try { + let res = await axios.post(address + "/api/v2/auth/login", body, { + headers: { "Content-Type": "multipart/form-data;boundary=--------------------------125002698093981740970152" } + }); + let success = res.data.toLocaleLowerCase().contains('ok'); + if (updateStatus) { + qbStatus = success; + } + if (!success) { + throw new Error("登录失败"); + } else { + cookie = res.headers['Cookie']; + } + } catch (error) { + console.error("登录报错:", error); + if (updateStatus) { + qbStatus = false; + } + throw new Error("登录出错"); + } +} diff --git a/openRenamerFront/jsconfig.json b/openRenamerFront/jsconfig.json new file mode 100644 index 0000000..b91c660 --- /dev/null +++ b/openRenamerFront/jsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "@/*": ["src/*"] + }, + "target": "ES6", + "allowSyntheticDefaultImports": true + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] +} diff --git a/openRenamerFront/package.json b/openRenamerFront/package.json index 045fd56..09f5918 100644 --- a/openRenamerFront/package.json +++ b/openRenamerFront/package.json @@ -13,7 +13,7 @@ "core-js": "^3.6.5", "dayjs": "^1.10.7", "element-plus": "^2.2.25", - "vue": "^3.0.0", + "vue": "^3.2.45", "vue-router": "^4.0.0-0" }, "devDependencies": { diff --git a/openRenamerFront/src/App.vue b/openRenamerFront/src/App.vue index 6dd916d..7935038 100644 --- a/openRenamerFront/src/App.vue +++ b/openRenamerFront/src/App.vue @@ -1,7 +1,13 @@