Commit 0196ae2d07701f843a4f96e8dae8c98151dcefcc

Authored by ww
1 parent 96526939

fix: DEFECT-1169 任务中心自定义cron表达式拼接错误

... ... @@ -263,14 +263,20 @@ export const formSchemas: FormSchema[] = [
263 263 label: '周期',
264 264 required: true,
265 265 defaultValue: PeriodTypeEnum.MONTH,
266   - componentProps: {
267   - placeholder: '请选择周期',
268   - options: [
269   - { label: PeriodTypeNameEnum.DAY, value: PeriodTypeEnum.DAY },
270   - { label: PeriodTypeNameEnum.WEEK, value: PeriodTypeEnum.WEEK },
271   - { label: PeriodTypeNameEnum.MONTH, value: PeriodTypeEnum.MONTH },
272   - ],
273   - getPopupContainer: () => document.body,
  266 + componentProps: ({ formActionType }) => {
  267 + const { setFieldsValue } = formActionType;
  268 + return {
  269 + placeholder: '请选择周期',
  270 + options: [
  271 + { label: PeriodTypeNameEnum.DAY, value: PeriodTypeEnum.DAY },
  272 + { label: PeriodTypeNameEnum.WEEK, value: PeriodTypeEnum.WEEK },
  273 + { label: PeriodTypeNameEnum.MONTH, value: PeriodTypeEnum.MONTH },
  274 + ],
  275 + getPopupContainer: () => document.body,
  276 + onChange() {
  277 + setFieldsValue({ [FormFieldsEnum.EXECUTE_TIME_PERIOD]: null });
  278 + },
  279 + };
274 280 },
275 281 ifShow: ({ model }) => model[FormFieldsEnum.EXECUTE_TIME_TYPE] === ExecuteTimeTypeEnum.CUSTOM,
276 282 },
... ...
1   -import CronParser, { CronFields, HourRange, SixtyRange } from 'cron-parser';
  1 +import { HourRange, SixtyRange } from 'cron-parser';
2 2 import { dateUtil } from '/@/utils/dateUtil';
3   -import { ExecuteTimeTypeEnum, FormFieldsEnum, PushWayEnum } from './config';
  3 +import { ExecuteTimeTypeEnum, FormFieldsEnum, PeriodTypeEnum, PushWayEnum } from './config';
4 4 import { CreateTaskRecordType, TaskRecordType } from '/@/api/task/model';
5 5 import { DeviceCascadePickerValueType } from '../DevicePicker';
6 6 import { TaskTargetEnum } from '../../config';
... ... @@ -12,52 +12,31 @@ export interface FormValueType extends Partial<Record<FormFieldsEnum, any>> {
12 12 [FormFieldsEnum.DEVICE_PROFILE]: ProductCascadePickerValueType;
13 13 }
14 14
15   -type CanWrite<T> = {
16   - -readonly [K in keyof T]: T[K];
17   -};
18   -
19 15 interface GenCronExpressionResultType {
20 16 effective: boolean;
21 17 expression?: string;
22 18 }
23 19
24   -export const usePluginGenCronExpression = (
25   - time: string,
26   - expression = '* * * * * * *',
27   - includesYear = true
28   -): GenCronExpressionResultType => {
29   - try {
30   - const separator = ' ';
31   - const removeYear = expression.split(separator).slice(0, 6).join(separator);
32   -
33   - const date = dateUtil(time, 'HH:mm:ss');
34   -
35   - const second = date.get('second') as SixtyRange;
36   - const minute = date.get('minute') as SixtyRange;
37   - const hour = date.get('hour') as HourRange;
38   -
39   - const result = CronParser.parseExpression(removeYear, { utc: true, nthDayOfWeek: 4 });
40   - const fields = JSON.parse(JSON.stringify(result.fields)) as CanWrite<CronFields>;
41   - fields.second = [second];
42   - fields.minute = [minute];
43   - fields.hour = [hour];
44   -
45   - // console.log(CronParser.fieldsToExpression(CronParser.parseExpression('').fields).stringify());
46   -
47   - let newExpression = CronParser.fieldsToExpression(fields).stringify(true);
48   - newExpression = includesYear ? `${newExpression} *` : newExpression;
49   - return { effective: true, expression: newExpression };
50   - } catch (error) {
51   - // throw error;
52   - return { effective: false };
53   - }
  20 +export const genPollCronExpression = (value: number, type: TimeUnitEnum) => {
  21 + const placeholder = 'placeholder';
  22 + const expression = {
  23 + [TimeUnitEnum.SECOND]: `${placeholder} * * * * ? *`,
  24 + [TimeUnitEnum.MINUTE]: `* ${placeholder} * * * ? *`,
  25 + [TimeUnitEnum.HOUR]: `* * ${placeholder} * * ? *`,
  26 + };
  27 + const newExpression = expression[type];
  28 + return newExpression.replace(placeholder, `0/${value}`);
54 29 };
55 30
56 31 export const genCronExpression = (
57 32 time: string,
58   - expression = '* * * * * * *'
  33 + expression = '* * * * * * *',
  34 + periodType: PeriodTypeEnum
59 35 ): GenCronExpressionResultType => {
60 36 try {
  37 + if (periodType === PeriodTypeEnum.DAY) {
  38 + expression = '* * * * * ? *';
  39 + }
61 40 const separator = ' ';
62 41 const list: (string | number)[] = expression.split(separator);
63 42
... ... @@ -103,10 +82,12 @@ export const composeData = (result: Required<FormValueType>): CreateTaskRecordTy
103 82 deviceType: productDeviceType,
104 83 } = deviceProfile || {};
105 84
106   - const { expression } = genCronExpression(time, period);
  85 + const { expression } = genCronExpression(time, period, periodType);
107 86
108 87 const cron =
109   - executeTimeType === ExecuteTimeTypeEnum.POLL ? `0/${interval} * * * * ? *` : expression!;
  88 + executeTimeType === ExecuteTimeTypeEnum.POLL
  89 + ? genPollCronExpression(interval, pollUnit)
  90 + : expression!;
110 91
111 92 return {
112 93 name,
... ...