Files
certd/packages/ui/certd-client/src/components/cron-editor/index.vue
T

126 lines
2.8 KiB
Vue
Raw Normal View History

2024-08-27 13:46:19 +08:00
<template>
2025-06-29 14:09:09 +08:00
<div class="cron-editor">
<div class="flex-o">
<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" />
</div>
<div class="mt-5 flex">
<a-input :disabled="true" :readonly="readonly" :value="modelValue" @change="onChange"></a-input>
<fs-icon icon="ion:close-circle" class="pointer fs-16 ml-5 color-gray" :title="t('certd.cron.clearTip')" @click="onClear"></fs-icon>
</div>
<div class="helper">{{ t("certd.cron.nextTrigger") }}{{ nextTime }}</div>
<div class="fs-helper">{{ errorMessage }}</div>
</div>
2024-08-27 13:46:19 +08:00
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
2025-06-25 23:52:44 +02:00
import { useI18n } from "vue-i18n";
import { getCronNextTimes } from "/@/components/cron-editor/utils";
2025-06-25 23:52:44 +02:00
const { t } = useI18n();
defineOptions({
2025-06-29 14:09:09 +08:00
name: "CronEditor",
});
2024-08-27 13:46:19 +08:00
const props = defineProps<{
2025-06-29 14:09:09 +08:00
modelValue?: string;
disabled?: boolean;
readonly?: boolean;
allowEveryMin?: boolean;
2024-08-27 13:46:19 +08:00
}>();
const period = ref<string>("");
2024-11-25 00:53:36 +08:00
if (props.modelValue == null || props.modelValue.endsWith("* * *")) {
2025-06-29 14:09:09 +08:00
period.value = "day";
2024-11-25 00:53:36 +08:00
} else if (props.modelValue.endsWith("* *")) {
2025-06-29 14:09:09 +08:00
period.value = "month";
2024-11-25 00:53:36 +08:00
} else if (props.modelValue.endsWith("*")) {
2025-06-29 14:09:09 +08:00
period.value = "year";
}
2024-08-27 13:46:19 +08:00
const emit = defineEmits<{
2025-06-29 14:09:09 +08:00
"update:modelValue": any;
change: any;
2024-08-27 13:46:19 +08:00
}>();
const errorMessage = ref<string | null>(null);
const onUpdate = (value: string) => {
2025-06-29 14:09:09 +08:00
if (value === props.modelValue) {
return;
}
const arr: string[] = value.split(" ");
if (arr[0] === "*") {
arr[0] = "0";
}
if (!props.allowEveryMin) {
if (arr[1] === "*") {
arr[1] = "0";
}
}
value = arr.join(" ");
emit("update:modelValue", value);
errorMessage.value = undefined;
2024-08-27 13:46:19 +08:00
};
const onPeriod = (value: string) => {
2025-06-29 14:09:09 +08:00
period.value = value;
2024-08-27 13:46:19 +08:00
};
const onChange = (e: any) => {
2025-06-29 14:09:09 +08:00
const value = e.target.value;
onUpdate(value);
2024-08-27 13:46:19 +08:00
};
const onError = (error: any) => {
2025-06-29 14:09:09 +08:00
errorMessage.value = error;
2024-08-27 13:46:19 +08:00
};
2024-11-25 00:53:36 +08:00
const onClear = () => {
2025-06-29 14:09:09 +08:00
if (props.disabled) {
return;
}
onUpdate("");
2024-11-25 00:53:36 +08:00
};
const nextTime = computed(() => {
2025-06-29 14:09:09 +08:00
if (props.modelValue == null) {
return t("certd.cron.tip");
}
try {
const nextTimes = getCronNextTimes(props.modelValue, 2);
return nextTimes.join("");
} catch (e) {
console.log(e);
return t("certd.cron.tip");
}
});
2024-08-27 13:46:19 +08:00
</script>
<style lang="less">
.cron-editor {
2025-06-29 14:09:09 +08:00
.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;
background-color: #fff;
}
.vcron-select-container {
display: flex;
align-items: center;
}
}
2024-08-27 13:46:19 +08:00
}
</style>