From 4ce452cb76b66eef096f7ade5e3a861fbfafaded Mon Sep 17 00:00:00 2001 From: COOL Date: Mon, 24 Jun 2024 14:09:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=B0=83=E7=94=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/task/db.json | 6 +++--- src/modules/task/service/demo.ts | 4 ++-- src/modules/task/service/info.ts | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/modules/task/db.json b/src/modules/task/db.json index 681ecbc..2e318c7 100644 --- a/src/modules/task/db.json +++ b/src/modules/task/db.json @@ -4,16 +4,16 @@ "id": 1, "jobId": null, "repeatConf": "{\"count\":1,\"type\":1,\"limit\":5,\"name\":\"每秒执行,总共5次\",\"taskType\":1,\"every\":1000,\"service\":\"taskDemoService.test()\",\"status\":1,\"id\":1,\"createTime\":\"2021-03-10 14:25:13\",\"updateTime\":\"2021-03-10 14:25:13\",\"jobId\":1}", - "name": "每秒执行,总共5次", + "name": "每秒执行一次", "cron": null, - "limit": 5, + "limit": null, "every": 1000, "remark": null, "status": 0, "startDate": null, "endDate": null, "data": null, - "service": "taskDemoService.test()", + "service": "taskDemoService.test(1,2)", "type": 1, "nextRunTime": "2021-3-10 14:25:18", "taskType": 1 diff --git a/src/modules/task/service/demo.ts b/src/modules/task/service/demo.ts index 9cc5bed..05ef055 100644 --- a/src/modules/task/service/demo.ts +++ b/src/modules/task/service/demo.ts @@ -12,8 +12,8 @@ export class TaskDemoService extends BaseService { /** * 描述 */ - async test() { - this.logger.info('我被调用了'); + async test(a, b) { + this.logger.info('我被调用了', a, b); return '任务执行成功'; } } diff --git a/src/modules/task/service/info.ts b/src/modules/task/service/info.ts index 3d46ba2..95504f4 100644 --- a/src/modules/task/service/info.ts +++ b/src/modules/task/service/info.ts @@ -332,14 +332,27 @@ export class TaskInfoService extends BaseService { const service = await this.app .getApplicationContext() .getAsync(_.lowerFirst(arr[0])); - for (const child of arr) { + + for (let i = 1; i < arr.length; i++) { + const child = arr[i]; if (child.includes('(')) { - const lastArr = child.split('('); - const param = lastArr[1].replace(')', ''); - if (!param) { - return service[lastArr[0]](); + const [methodName, paramsStr] = child.split('('); + const params = paramsStr + .replace(')', '') + .split(',') + .map(param => param.trim()); + + if (params.length === 1 && params[0] === '') { + return service[methodName](); } else { - return service[lastArr[0]](JSON.parse(param)); + const parsedParams = params.map(param => { + try { + return JSON.parse(param); + } catch (e) { + return param; // 如果不是有效的JSON,则返回原始字符串 + } + }); + return service[methodName](...parsedParams); } } }