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); } } }