2024-08-27 13:46:19 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="cron-editor">
|
|
|
|
|
|
<div class="flex-o">
|
2025-06-06 18:20:30 +08:00
|
|
|
|
<cron-light :disabled="disabled" :readonly="readonly" :period="period" class="flex-o cron-ant" locale="zh-CN" format="quartz" :model-value="modelValue" @update:model-value="onUpdate" @error="onError" />
|
2024-08-27 13:46:19 +08:00
|
|
|
|
</div>
|
2024-11-25 00:53:36 +08:00
|
|
|
|
<div class="mt-5 flex">
|
2024-08-27 13:46:19 +08:00
|
|
|
|
<a-input :disabled="true" :readonly="readonly" :value="modelValue" @change="onChange"></a-input>
|
2024-11-25 00:53:36 +08:00
|
|
|
|
<fs-icon icon="ion:close-circle" class="pointer fs-16 ml-5 color-gray" title="清除选择" @click="onClear"></fs-icon>
|
2024-08-27 13:46:19 +08:00
|
|
|
|
</div>
|
2024-10-23 16:04:57 +08:00
|
|
|
|
<div class="helper">下次触发时间:{{ nextTime }}</div>
|
2024-08-27 13:46:19 +08:00
|
|
|
|
<div class="fs-helper">{{ errorMessage }}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2024-10-23 16:04:57 +08:00
|
|
|
|
import parser from "cron-parser";
|
|
|
|
|
|
import { computed, ref } from "vue";
|
|
|
|
|
|
import dayjs from "dayjs";
|
2025-06-07 01:19:37 +08:00
|
|
|
|
import { getCronNextTimes } from "/@/components/cron-editor/utils";
|
2024-10-07 03:21:16 +08:00
|
|
|
|
defineOptions({
|
2025-06-06 18:20:30 +08:00
|
|
|
|
name: "CronEditor",
|
2024-10-07 03:21:16 +08:00
|
|
|
|
});
|
2024-08-27 13:46:19 +08:00
|
|
|
|
const props = defineProps<{
|
|
|
|
|
|
modelValue?: string;
|
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
|
readonly?: boolean;
|
2025-06-06 18:20:30 +08:00
|
|
|
|
allowEveryMin?: boolean;
|
2024-08-27 13:46:19 +08:00
|
|
|
|
}>();
|
|
|
|
|
|
|
2024-10-23 16:04:57 +08:00
|
|
|
|
const period = ref<string>("");
|
2024-11-25 00:53:36 +08:00
|
|
|
|
if (props.modelValue == null || props.modelValue.endsWith("* * *")) {
|
2024-10-23 16:04:57 +08:00
|
|
|
|
period.value = "day";
|
2024-11-25 00:53:36 +08:00
|
|
|
|
} else if (props.modelValue.endsWith("* *")) {
|
|
|
|
|
|
period.value = "month";
|
|
|
|
|
|
} else if (props.modelValue.endsWith("*")) {
|
|
|
|
|
|
period.value = "year";
|
2024-10-23 16:04:57 +08:00
|
|
|
|
}
|
2024-08-27 13:46:19 +08:00
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
|
"update:modelValue": any;
|
2024-11-18 13:43:33 +08:00
|
|
|
|
change: any;
|
2024-08-27 13:46:19 +08:00
|
|
|
|
}>();
|
|
|
|
|
|
|
|
|
|
|
|
const errorMessage = ref<string | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
const onUpdate = (value: string) => {
|
|
|
|
|
|
if (value === props.modelValue) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-11-01 10:23:27 +08:00
|
|
|
|
const arr: string[] = value.split(" ");
|
|
|
|
|
|
if (arr[0] === "*") {
|
|
|
|
|
|
arr[0] = "0";
|
|
|
|
|
|
}
|
2025-06-06 18:20:30 +08:00
|
|
|
|
if (!props.allowEveryMin) {
|
|
|
|
|
|
if (arr[1] === "*") {
|
|
|
|
|
|
arr[1] = "0";
|
|
|
|
|
|
}
|
2024-11-01 10:23:27 +08:00
|
|
|
|
}
|
2025-06-06 18:20:30 +08:00
|
|
|
|
|
2024-11-01 10:23:27 +08:00
|
|
|
|
value = arr.join(" ");
|
|
|
|
|
|
|
2024-08-27 13:46:19 +08:00
|
|
|
|
emit("update:modelValue", value);
|
|
|
|
|
|
errorMessage.value = undefined;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const onPeriod = (value: string) => {
|
|
|
|
|
|
period.value = value;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const onChange = (e: any) => {
|
|
|
|
|
|
const value = e.target.value;
|
|
|
|
|
|
onUpdate(value);
|
|
|
|
|
|
};
|
|
|
|
|
|
const onError = (error: any) => {
|
|
|
|
|
|
errorMessage.value = error;
|
|
|
|
|
|
};
|
2024-10-23 16:04:57 +08:00
|
|
|
|
|
2024-11-25 00:53:36 +08:00
|
|
|
|
const onClear = () => {
|
|
|
|
|
|
if (props.disabled) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
onUpdate("");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-10-23 16:04:57 +08:00
|
|
|
|
const nextTime = computed(() => {
|
2024-11-01 00:59:09 +08:00
|
|
|
|
if (props.modelValue == null) {
|
|
|
|
|
|
return "请先设置正确的cron表达式";
|
|
|
|
|
|
}
|
2025-06-07 01:19:37 +08:00
|
|
|
|
|
2024-10-23 16:04:57 +08:00
|
|
|
|
try {
|
2025-06-07 01:19:37 +08:00
|
|
|
|
const nextTimes = getCronNextTimes(props.modelValue, 2);
|
|
|
|
|
|
return nextTimes.join(",");
|
2024-10-23 16:04:57 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.log(e);
|
|
|
|
|
|
return "请先设置正确的cron表达式";
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2024-08-27 13:46:19 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
<style lang="less">
|
|
|
|
|
|
.cron-editor {
|
|
|
|
|
|
.cron-ant {
|
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
|
&* > {
|
|
|
|
|
|
margin-bottom: 2px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
.vcron-select-list {
|
|
|
|
|
|
min-width: 56px;
|
|
|
|
|
|
}
|
|
|
|
|
|
.vcron-select-input {
|
|
|
|
|
|
min-height: 22px;
|
2024-09-04 15:49:00 +08:00
|
|
|
|
background-color: #fff;
|
2024-08-27 13:46:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
.vcron-select-container {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|