This commit is contained in:
BianLzhaoMin 2026-02-10 14:59:14 +08:00
parent fad98a3bd0
commit 400a11fc7e
2 changed files with 143 additions and 196 deletions

View File

@ -64,17 +64,6 @@
class="time-input"
/>
</div>
<div class="time-input-item" v-if="showSecond">
<label></label>
<el-input-number
v-model="timeValues.second"
:min="0"
:max="59"
:controls="false"
size="default"
class="time-input"
/>
</div>
</div>
</div>
@ -137,7 +126,6 @@ const periodOptions = [
{ label: '每天', value: 'day' },
{ label: '每小时', value: 'hour' },
{ label: '每分钟', value: 'minute' },
{ label: '每秒', value: 'second' },
]
//
@ -152,34 +140,30 @@ const weekOptions = [
]
//
const selectedPeriod = ref('second')
const selectedPeriod = ref('minute')
//
const selectedWeek = ref(1)
//
const timeValues = ref({
second: 0,
minute: 0,
hour: 0,
day: 1,
month: 1,
})
// Cron
// Cron5
const crontabValueObj = ref({
second: '*',
min: '*',
hour: '*',
day: '*',
month: '*',
week: '?',
year: '',
})
//
const executionDesc = computed(() => {
const periodMap = {
second: '每秒执行',
minute: '每分钟执行',
hour: '每小时执行',
day: '每天执行',
@ -187,16 +171,12 @@ const executionDesc = computed(() => {
month: '每月执行',
year: '每年执行',
}
return periodMap[selectedPeriod.value] || '每执行'
return periodMap[selectedPeriod.value] || '每分钟执行'
})
//
const showTimeInputs = computed(() => {
return selectedPeriod.value !== 'second'
})
const showSecond = computed(() => {
return ['minute', 'hour', 'day', 'week', 'month', 'year'].includes(selectedPeriod.value)
return selectedPeriod.value !== 'minute'
})
const showMinute = computed(() => {
@ -219,23 +199,10 @@ const showWeek = computed(() => {
return selectedPeriod.value === 'week'
})
// Cron
// Cron6 0
const crontabValueString = computed(() => {
const obj = crontabValueObj.value
return (
obj.second +
' ' +
obj.min +
' ' +
obj.hour +
' ' +
obj.day +
' ' +
obj.month +
' ' +
obj.week +
(obj.year === '' ? '' : ' ' + obj.year)
)
return '0 ' + obj.min + ' ' + obj.hour + ' ' + obj.day + ' ' + obj.month + ' ' + obj.week
})
// Cron
@ -247,91 +214,67 @@ watch(
{ deep: true },
)
// Cron
// Cron60
function updateCronExpression() {
switch (selectedPeriod.value) {
case 'second':
// * * * * * ? *
crontabValueObj.value = {
second: '*',
min: '*',
hour: '*',
day: '*',
month: '*',
week: '?',
year: '',
}
break
case 'minute':
// * * * * ? *
// * * * * ?
crontabValueObj.value = {
second: String(timeValues.value.second),
min: '*',
hour: '*',
day: '*',
month: '*',
week: '?',
year: '',
}
break
case 'hour':
// * * * ? *
// * * * ?
crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute),
hour: '*',
day: '*',
month: '*',
week: '?',
year: '',
}
break
case 'day':
// * * ? *
// * * ?
crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute),
hour: String(timeValues.value.hour),
day: '*',
month: '*',
week: '?',
year: '',
}
break
case 'week':
// ? * *
// ? *
crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute),
hour: String(timeValues.value.hour),
day: '?',
month: '*',
week: String(selectedWeek.value),
year: '',
}
break
case 'month':
// * ? *
// * ?
crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute),
hour: String(timeValues.value.hour),
day: String(timeValues.value.day),
month: '*',
week: '?',
year: '',
}
break
case 'year':
// ? *
// ?
crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute),
hour: String(timeValues.value.hour),
day: String(timeValues.value.day),
month: String(timeValues.value.month),
week: '?',
year: '',
}
break
}
@ -349,115 +292,109 @@ function handleWeekChange(week) {
updateCronExpression()
}
//
// 5660
function resolveExp() {
if (props.expression) {
const arr = props.expression.split(/\s+/)
if (arr.length >= 6) {
// 6
const obj = {
second: arr[0],
let obj = {}
// 56
if (arr.length === 5) {
// 5
obj = {
min: arr[0],
hour: arr[1],
day: arr[2],
month: arr[3],
week: arr[4],
}
} else if (arr.length >= 6) {
// 67 []
// arr[0]0使
obj = {
min: arr[1],
hour: arr[2],
day: arr[3],
month: arr[4],
week: arr[5],
year: arr[6] ? arr[6] : '',
}
crontabValueObj.value = { ...obj }
} else {
clearCron()
return
}
//
//
const isNumber = (str) => {
return /^\d+$/.test(str)
}
crontabValueObj.value = { ...obj }
if (
obj.second === '*' &&
obj.min === '*' &&
obj.hour === '*' &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'second'
} else if (
isNumber(obj.second) &&
obj.min === '*' &&
obj.hour === '*' &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'minute'
timeValues.value.second = parseInt(obj.second) || 0
} else if (
isNumber(obj.second) &&
isNumber(obj.min) &&
obj.hour === '*' &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'hour'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0
} else if (
isNumber(obj.second) &&
isNumber(obj.min) &&
isNumber(obj.hour) &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'day'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
} else if (
isNumber(obj.second) &&
isNumber(obj.min) &&
isNumber(obj.hour) &&
obj.day === '?' &&
obj.month === '*' &&
isNumber(obj.week)
) {
selectedPeriod.value = 'week'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
selectedWeek.value = parseInt(obj.week) || 1
} else if (
isNumber(obj.second) &&
isNumber(obj.min) &&
isNumber(obj.hour) &&
isNumber(obj.day) &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'month'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
timeValues.value.day = parseInt(obj.day) || 1
} else if (
isNumber(obj.second) &&
isNumber(obj.min) &&
isNumber(obj.hour) &&
isNumber(obj.day) &&
isNumber(obj.month) &&
obj.week === '?'
) {
selectedPeriod.value = 'year'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
timeValues.value.day = parseInt(obj.day) || 1
timeValues.value.month = parseInt(obj.month) || 1
} else {
// 使
selectedPeriod.value = 'second'
}
//
//
const isNumber = (str) => {
return /^\d+$/.test(str)
}
if (
obj.min === '*' &&
obj.hour === '*' &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'minute'
} else if (
isNumber(obj.min) &&
obj.hour === '*' &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'hour'
timeValues.value.minute = parseInt(obj.min) || 0
} else if (
isNumber(obj.min) &&
isNumber(obj.hour) &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'day'
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
} else if (
isNumber(obj.min) &&
isNumber(obj.hour) &&
obj.day === '?' &&
obj.month === '*' &&
isNumber(obj.week)
) {
selectedPeriod.value = 'week'
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
selectedWeek.value = parseInt(obj.week) || 1
} else if (
isNumber(obj.min) &&
isNumber(obj.hour) &&
isNumber(obj.day) &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'month'
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
timeValues.value.day = parseInt(obj.day) || 1
} else if (
isNumber(obj.min) &&
isNumber(obj.hour) &&
isNumber(obj.day) &&
isNumber(obj.month) &&
obj.week === '?'
) {
selectedPeriod.value = 'year'
timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0
timeValues.value.day = parseInt(obj.day) || 1
timeValues.value.month = parseInt(obj.month) || 1
} else {
// 使
selectedPeriod.value = 'minute'
}
} else {
clearCron()
@ -477,10 +414,9 @@ function submitFill() {
//
function clearCron() {
selectedPeriod.value = 'second'
selectedPeriod.value = 'minute'
selectedWeek.value = 1
timeValues.value = {
second: 0,
minute: 0,
hour: 0,
day: 1,

View File

@ -452,9 +452,29 @@ const parseCronToChinese = (cronExpression) => {
if (!cronExpression) return ''
const parts = cronExpression.trim().split(/\s+/)
if (parts.length < 6) return ''
if (parts.length < 5) return ''
const [second, minute, hour, day, month, week, year] = parts
// 5 6/7 []
let second, minute, hour, day, month, week, year
if (parts.length === 5) {
// 5 0
second = '0'
minute = parts[0]
hour = parts[1]
day = parts[2]
month = parts[3]
week = parts[4]
year = ''
} else {
// 67 []
second = parts[0]
minute = parts[1]
hour = parts[2]
day = parts[3]
month = parts[4]
week = parts[5]
year = parts[6] || ''
}
//
const formatTime = (h, m, s) => {
@ -539,7 +559,6 @@ const parseCronToChinese = (cronExpression) => {
// 1
if (
second !== '?' &&
minute !== '?' &&
hour !== '?' &&
day === '*' &&
@ -547,11 +566,11 @@ const parseCronToChinese = (cronExpression) => {
week === '?' &&
(!year || year === '*')
) {
const s = getSingleValue(second)
const s = second === '?' ? '0' : getSingleValue(second)
const m = getSingleValue(minute)
const h = getSingleValue(hour)
if (!hasStep(second) && !hasStep(minute) && !hasStep(hour)) {
if (!hasStep(minute) && !hasStep(hour)) {
let result = `每天${formatTime(h, m, s)}执行`
// *
result = result.replace(/(\d)\*/g, '$10').replace(/\*/g, '0')
@ -561,7 +580,6 @@ const parseCronToChinese = (cronExpression) => {
// 2
if (
second !== '?' &&
minute !== '?' &&
hour !== '?' &&
day === '?' &&
@ -570,12 +588,12 @@ const parseCronToChinese = (cronExpression) => {
week !== '*' &&
(!year || year === '*')
) {
const s = getSingleValue(second)
const s = second === '?' ? '0' : getSingleValue(second)
const m = getSingleValue(minute)
const h = getSingleValue(hour)
const weekDesc = parseWeek(week)
if (!hasStep(second) && !hasStep(minute) && !hasStep(hour)) {
if (!hasStep(minute) && !hasStep(hour)) {
let result = `${weekDesc}${formatTime(h, m, s)}执行`
// *
result = result.replace(/(\d)\*/g, '$10').replace(/\*/g, '0')
@ -585,7 +603,6 @@ const parseCronToChinese = (cronExpression) => {
// 3
if (
second !== '?' &&
minute !== '?' &&
hour !== '?' &&
day !== '*' &&
@ -594,12 +611,12 @@ const parseCronToChinese = (cronExpression) => {
week === '?' &&
(!year || year === '*')
) {
const s = getSingleValue(second)
const s = second === '?' ? '0' : getSingleValue(second)
const m = getSingleValue(minute)
const h = getSingleValue(hour)
const dayDesc = parseDay(day)
if (!hasStep(second) && !hasStep(minute) && !hasStep(hour)) {
if (!hasStep(minute) && !hasStep(hour)) {
let result = `${dayDesc}${formatTime(h, m, s)}执行`
// *
result = result.replace(/(\d)\*/g, '$10').replace(/\*/g, '0')
@ -609,7 +626,6 @@ const parseCronToChinese = (cronExpression) => {
// 4
if (
second !== '?' &&
minute !== '?' &&
hour !== '?' &&
day !== '*' &&
@ -618,13 +634,13 @@ const parseCronToChinese = (cronExpression) => {
week === '?' &&
(!year || year === '*' || year === '')
) {
const s = getSingleValue(second)
const s = second === '?' ? '0' : getSingleValue(second)
const m = getSingleValue(minute)
const h = getSingleValue(hour)
const dayDesc = parseDay(day, true) // ""
const monthDesc = parseMonth(month)
if (!hasStep(second) && !hasStep(minute) && !hasStep(hour)) {
if (!hasStep(minute) && !hasStep(hour)) {
let result = `每年${monthDesc}${dayDesc}${formatTime(h, m, s)}执行`
// *
result = result.replace(/(\d)\*/g, '$10').replace(/\*/g, '0')
@ -679,11 +695,12 @@ const parseCronToChinese = (cronExpression) => {
timeParts.push('**')
}
if (second !== '*' && !hasStep(second)) {
// 5?00
if (second && second !== '*' && second !== '?' && !hasStep(second)) {
const s = getSingleValue(second)
timeParts.push(s.padStart(2, '0'))
} else {
timeParts.push('**')
timeParts.push('00') // 00
}
//
@ -704,22 +721,16 @@ const parseCronToChinese = (cronExpression) => {
const step = minute.split('/')[1]
partsDesc.push(`${step}分钟`)
}
if (second.includes('/') && second.split('/')[0] === '*') {
// 6/7
if (second && second.includes('/') && second.split('/')[0] === '*') {
const step = second.split('/')[1]
partsDesc.push(`${step}`)
}
//
if (partsDesc.length === 0) {
if (
second === '*' &&
minute === '*' &&
hour === '*' &&
day === '*' &&
month === '*' &&
week === '?'
) {
return '每秒执行'
if (minute === '*' && hour === '*' && day === '*' && month === '*' && week === '?') {
return '每分钟执行'
}
return `Cron表达式${cronExpression}`
}