ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<script lang="ts" setup>
import { computed, provide, reactive, ref, unref, watch } from 'vue';
 
import { formatToDateTime } from '@jnpf/utils';
 
import { useDebounceFn } from '@vueuse/core';
import CronParser from 'cron-parser';
 
import { cronEmits, cronProps } from './easy.cron.data';
import DayUI from './tabs/DayUI.vue';
import HourUI from './tabs/HourUI.vue';
import MinuteUI from './tabs/MinuteUI.vue';
import MonthUI from './tabs/MonthUI.vue';
import SecondUI from './tabs/SecondUI.vue';
import WeekUI from './tabs/WeekUI.vue';
import YearUI from './tabs/YearUI.vue';
 
interface InputValuesState {
  cron: any;
  day: any;
  hour: any;
  minute: any;
  month: any;
  second: any;
  week: any;
  year: any;
}
 
const props = defineProps({ ...cronProps });
const emit = defineEmits([...cronEmits]);
const prefixCls = 'jnpf-easy-cron-inner';
provide('prefixCls', prefixCls);
const activeKey = ref(props.hideSecond ? 'minute' : 'second');
const second = ref<any>('*');
const minute = ref<any>('*');
const hour = ref<any>('*');
const day = ref<any>('*');
const month = ref<any>('*');
const week = ref<any>('?');
const year = ref<any>('*');
const inputValues = reactive<InputValuesState>({
  cron: '',
  day: '',
  hour: '',
  minute: '',
  month: '',
  second: '',
  week: '',
  year: '',
});
const preTimeList = ref('执行预览,会忽略年份参数。');
 
// cron表达式
const cronValueInner = computed(() => {
  const result: string[] = [];
  if (!props.hideSecond) {
    result.push(second.value ? second.value : '*');
  }
  result.push(
    minute.value ? minute.value : '*',
    hour.value ? hour.value : '*',
    day.value ? day.value : '*',
    month.value ? month.value : '*',
    week.value ? week.value : '?',
  );
  if (!props.hideYear && !props.hideSecond) result.push(year.value ? year.value : '*');
  return result.join(' ');
});
// 不含年
const cronValueNoYear = computed(() => {
  const v = cronValueInner.value;
  if (props.hideYear || props.hideSecond) return v;
  const vs = v.split(' ');
  if (vs.length >= 6) {
    // 转成 Quartz 的规则
    vs[5] = convertWeekToQuartz(vs[5] as string);
  }
  return vs.slice(0, -1).join(' ');
});
const calTriggerList = useDebounceFn(calTriggerListInner, 500);
 
watch(
  () => unref(props.value),
  (newVal) => {
    if (newVal === cronValueInner.value) {
      return;
    }
    formatValue();
  },
  { immediate: true },
);
 
watch(cronValueInner, (newValue) => {
  calTriggerList();
  emitValue(newValue);
  assignInput();
});
 
assignInput();
formatValue();
calTriggerListInner();
 
function assignInput() {
  inputValues.second = second.value;
  inputValues.minute = minute.value;
  inputValues.hour = hour.value;
  inputValues.day = day.value;
  inputValues.month = month.value;
  inputValues.week = week.value;
  inputValues.year = year.value;
  inputValues.cron = cronValueInner.value;
  if (!props.value) emitValue(inputValues.cron);
}
function formatValue() {
  if (!props.value) return;
  const values = props.value.split(' ').filter((item) => !!item);
  if (!values || values.length <= 0) return;
  let i = 0;
  if (!props.hideSecond) second.value = values[i++];
  if (values.length > i) minute.value = values[i++];
  if (values.length > i) hour.value = values[i++];
  if (values.length > i) day.value = values[i++];
  if (values.length > i) month.value = values[i++];
  if (values.length > i) week.value = values[i++];
  if (values.length > i) year.value = values[i];
  assignInput();
}
// Quartz 的规则:
// 1 = 周日,2 = 周一,3 = 周二,4 = 周三,5 = 周四,6 = 周五,7 = 周六
function convertWeekToQuartz(week: string) {
  const convert = (v: string) => {
    if (v === '0') {
      return '1';
    }
    if (v === '1') {
      return '0';
    }
    return (Number.parseInt(v) - 1).toString();
  };
  // 匹配示例 1-7 or 1/7
  const patten1 = /^([0-7])([-/])([0-7])$/;
  // 匹配示例 1,4,7
  // eslint-disable-next-line regexp/no-unused-capturing-group
  const patten2 = /^([0-7])(,[0-7])+$/;
  if (/^[0-7]$/.test(week)) {
    return convert(week);
  } else if (patten1.test(week)) {
    return week.replace(patten1, (_$0, before, separator, after) => {
      return separator === '/' ? convert(before) + separator + after : convert(before) + separator + convert(after);
    });
  } else if (patten2.test(week)) {
    return week
      .split(',')
      .map((v) => convert(v))
      .join(',');
  }
  return week;
}
function calTriggerListInner() {
  // 设置了回调函数
  if (props.remote) {
    props.remote(cronValueInner.value, Date.now(), (v) => {
      preTimeList.value = v;
    });
    return;
  }
  const options = {
    currentDate: formatToDateTime(new Date()),
  };
  const iter = CronParser.parseExpression(cronValueNoYear.value, options);
  const result: string[] = [];
  for (let i = 1; i <= 10; i++) {
    result.push(formatToDateTime(new Date(iter.next() as any)));
  }
  preTimeList.value = result.length > 0 ? result.join('\n') : '无执行时间';
}
function onInputBlur() {
  second.value = inputValues.second;
  minute.value = inputValues.minute;
  hour.value = inputValues.hour;
  day.value = inputValues.day;
  month.value = inputValues.month;
  week.value = inputValues.week;
  year.value = inputValues.year;
}
function onInputCronBlur(event) {
  emitValue(event.target.value);
}
function emitValue(value) {
  emit('change', value);
  emit('update:value', value);
}
</script>
 
<template>
  <div class="jnpf-easy-cron-inner">
    <div class="content">
      <a-tabs v-model:active-key="activeKey" class="cron-list" size="small">
        <a-tab-pane v-if="!hideSecond" key="second" tab="秒">
          <SecondUI v-model:value="second" :disabled="disabled" />
        </a-tab-pane>
        <a-tab-pane key="minute" tab="分">
          <MinuteUI v-model:value="minute" :disabled="disabled" />
        </a-tab-pane>
        <a-tab-pane key="hour" tab="时">
          <HourUI v-model:value="hour" :disabled="disabled" />
        </a-tab-pane>
        <a-tab-pane key="day" tab="日">
          <DayUI v-model:value="day" :disabled="disabled" :week="week" />
        </a-tab-pane>
        <a-tab-pane key="month" tab="月">
          <MonthUI v-model:value="month" :disabled="disabled" />
        </a-tab-pane>
        <a-tab-pane key="week" tab="周">
          <WeekUI v-model:value="week" :day="day" :disabled="disabled" />
        </a-tab-pane>
        <a-tab-pane v-if="!hideYear && !hideSecond" key="year" tab="年">
          <YearUI v-model:value="year" :disabled="disabled" />
        </a-tab-pane>
      </a-tabs>
      <a-divider />
      <!-- 执行时间预览 -->
      <a-row :gutter="8">
        <a-col :span="24">
          <a-row :gutter="8">
            <a-col :span="24" class="time-list">
              <a-input v-model:value="inputValues.second" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'second'">秒</span>
                </template>
              </a-input>
              <a-input v-model:value="inputValues.minute" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'minute'">分</span>
                </template>
              </a-input>
              <a-input v-model:value="inputValues.hour" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'hour'">时</span>
                </template>
              </a-input>
              <a-input v-model:value="inputValues.day" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'day'">日</span>
                </template>
              </a-input>
              <a-input v-model:value="inputValues.month" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'month'">月</span>
                </template>
              </a-input>
              <a-input v-model:value="inputValues.week" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'week'">周</span>
                </template>
              </a-input>
              <a-input v-model:value="inputValues.year" @blur="onInputBlur">
                <template #addonBefore>
                  <span class="allow-click" @click="activeKey = 'year'">年</span>
                </template>
              </a-input>
            </a-col>
            <a-col :span="24">
              <a-input v-model:value="inputValues.cron" @blur="onInputCronBlur">
                <template #addonBefore>
                  <a-tooltip title="Cron表达式">Cron表达式</a-tooltip>
                </template>
              </a-input>
            </a-col>
          </a-row>
        </a-col>
      </a-row>
      <a-row class="my-[10px]">
        <a-col :span="24">
          <div class="mb-[4px]">近十次执行时间(不含年)</div>
          <a-textarea :rows="5" :value="preTimeList" type="textarea" />
        </a-col>
      </a-row>
    </div>
  </div>
</template>
<style lang="scss">
@use './easy.cron.inner' as *;
</style>