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" class="time-input"
/> />
</div> </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>
</div> </div>
@ -137,7 +126,6 @@ const periodOptions = [
{ label: '每天', value: 'day' }, { label: '每天', value: 'day' },
{ label: '每小时', value: 'hour' }, { label: '每小时', value: 'hour' },
{ label: '每分钟', value: 'minute' }, { 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 selectedWeek = ref(1)
// //
const timeValues = ref({ const timeValues = ref({
second: 0,
minute: 0, minute: 0,
hour: 0, hour: 0,
day: 1, day: 1,
month: 1, month: 1,
}) })
// Cron // Cron5
const crontabValueObj = ref({ const crontabValueObj = ref({
second: '*',
min: '*', min: '*',
hour: '*', hour: '*',
day: '*', day: '*',
month: '*', month: '*',
week: '?', week: '?',
year: '',
}) })
// //
const executionDesc = computed(() => { const executionDesc = computed(() => {
const periodMap = { const periodMap = {
second: '每秒执行',
minute: '每分钟执行', minute: '每分钟执行',
hour: '每小时执行', hour: '每小时执行',
day: '每天执行', day: '每天执行',
@ -187,16 +171,12 @@ const executionDesc = computed(() => {
month: '每月执行', month: '每月执行',
year: '每年执行', year: '每年执行',
} }
return periodMap[selectedPeriod.value] || '每执行' return periodMap[selectedPeriod.value] || '每分钟执行'
}) })
// //
const showTimeInputs = computed(() => { const showTimeInputs = computed(() => {
return selectedPeriod.value !== 'second' return selectedPeriod.value !== 'minute'
})
const showSecond = computed(() => {
return ['minute', 'hour', 'day', 'week', 'month', 'year'].includes(selectedPeriod.value)
}) })
const showMinute = computed(() => { const showMinute = computed(() => {
@ -219,23 +199,10 @@ const showWeek = computed(() => {
return selectedPeriod.value === 'week' return selectedPeriod.value === 'week'
}) })
// Cron // Cron6 0
const crontabValueString = computed(() => { const crontabValueString = computed(() => {
const obj = crontabValueObj.value const obj = crontabValueObj.value
return ( return '0 ' + obj.min + ' ' + obj.hour + ' ' + obj.day + ' ' + obj.month + ' ' + obj.week
obj.second +
' ' +
obj.min +
' ' +
obj.hour +
' ' +
obj.day +
' ' +
obj.month +
' ' +
obj.week +
(obj.year === '' ? '' : ' ' + obj.year)
)
}) })
// Cron // Cron
@ -247,91 +214,67 @@ watch(
{ deep: true }, { deep: true },
) )
// Cron // Cron60
function updateCronExpression() { function updateCronExpression() {
switch (selectedPeriod.value) { switch (selectedPeriod.value) {
case 'second':
// * * * * * ? *
crontabValueObj.value = {
second: '*',
min: '*',
hour: '*',
day: '*',
month: '*',
week: '?',
year: '',
}
break
case 'minute': case 'minute':
// * * * * ? * // * * * * ?
crontabValueObj.value = { crontabValueObj.value = {
second: String(timeValues.value.second),
min: '*', min: '*',
hour: '*', hour: '*',
day: '*', day: '*',
month: '*', month: '*',
week: '?', week: '?',
year: '',
} }
break break
case 'hour': case 'hour':
// * * * ? * // * * * ?
crontabValueObj.value = { crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute), min: String(timeValues.value.minute),
hour: '*', hour: '*',
day: '*', day: '*',
month: '*', month: '*',
week: '?', week: '?',
year: '',
} }
break break
case 'day': case 'day':
// * * ? * // * * ?
crontabValueObj.value = { crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute), min: String(timeValues.value.minute),
hour: String(timeValues.value.hour), hour: String(timeValues.value.hour),
day: '*', day: '*',
month: '*', month: '*',
week: '?', week: '?',
year: '',
} }
break break
case 'week': case 'week':
// ? * * // ? *
crontabValueObj.value = { crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute), min: String(timeValues.value.minute),
hour: String(timeValues.value.hour), hour: String(timeValues.value.hour),
day: '?', day: '?',
month: '*', month: '*',
week: String(selectedWeek.value), week: String(selectedWeek.value),
year: '',
} }
break break
case 'month': case 'month':
// * ? * // * ?
crontabValueObj.value = { crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute), min: String(timeValues.value.minute),
hour: String(timeValues.value.hour), hour: String(timeValues.value.hour),
day: String(timeValues.value.day), day: String(timeValues.value.day),
month: '*', month: '*',
week: '?', week: '?',
year: '',
} }
break break
case 'year': case 'year':
// ? * // ?
crontabValueObj.value = { crontabValueObj.value = {
second: String(timeValues.value.second),
min: String(timeValues.value.minute), min: String(timeValues.value.minute),
hour: String(timeValues.value.hour), hour: String(timeValues.value.hour),
day: String(timeValues.value.day), day: String(timeValues.value.day),
month: String(timeValues.value.month), month: String(timeValues.value.month),
week: '?', week: '?',
year: '',
} }
break break
} }
@ -349,21 +292,37 @@ function handleWeekChange(week) {
updateCronExpression() updateCronExpression()
} }
// // 5660
function resolveExp() { function resolveExp() {
if (props.expression) { if (props.expression) {
const arr = props.expression.split(/\s+/) const arr = props.expression.split(/\s+/)
if (arr.length >= 6) { let obj = {}
// 6
const obj = { // 56
second: arr[0], 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], min: arr[1],
hour: arr[2], hour: arr[2],
day: arr[3], day: arr[3],
month: arr[4], month: arr[4],
week: arr[5], week: arr[5],
year: arr[6] ? arr[6] : '',
} }
} else {
clearCron()
return
}
crontabValueObj.value = { ...obj } crontabValueObj.value = { ...obj }
// //
@ -373,16 +332,6 @@ function resolveExp() {
} }
if ( if (
obj.second === '*' &&
obj.min === '*' &&
obj.hour === '*' &&
obj.day === '*' &&
obj.month === '*' &&
obj.week === '?'
) {
selectedPeriod.value = 'second'
} else if (
isNumber(obj.second) &&
obj.min === '*' && obj.min === '*' &&
obj.hour === '*' && obj.hour === '*' &&
obj.day === '*' && obj.day === '*' &&
@ -390,9 +339,7 @@ function resolveExp() {
obj.week === '?' obj.week === '?'
) { ) {
selectedPeriod.value = 'minute' selectedPeriod.value = 'minute'
timeValues.value.second = parseInt(obj.second) || 0
} else if ( } else if (
isNumber(obj.second) &&
isNumber(obj.min) && isNumber(obj.min) &&
obj.hour === '*' && obj.hour === '*' &&
obj.day === '*' && obj.day === '*' &&
@ -400,10 +347,8 @@ function resolveExp() {
obj.week === '?' obj.week === '?'
) { ) {
selectedPeriod.value = 'hour' selectedPeriod.value = 'hour'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0 timeValues.value.minute = parseInt(obj.min) || 0
} else if ( } else if (
isNumber(obj.second) &&
isNumber(obj.min) && isNumber(obj.min) &&
isNumber(obj.hour) && isNumber(obj.hour) &&
obj.day === '*' && obj.day === '*' &&
@ -411,11 +356,9 @@ function resolveExp() {
obj.week === '?' obj.week === '?'
) { ) {
selectedPeriod.value = 'day' selectedPeriod.value = 'day'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0 timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0 timeValues.value.hour = parseInt(obj.hour) || 0
} else if ( } else if (
isNumber(obj.second) &&
isNumber(obj.min) && isNumber(obj.min) &&
isNumber(obj.hour) && isNumber(obj.hour) &&
obj.day === '?' && obj.day === '?' &&
@ -423,12 +366,10 @@ function resolveExp() {
isNumber(obj.week) isNumber(obj.week)
) { ) {
selectedPeriod.value = 'week' selectedPeriod.value = 'week'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0 timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0 timeValues.value.hour = parseInt(obj.hour) || 0
selectedWeek.value = parseInt(obj.week) || 1 selectedWeek.value = parseInt(obj.week) || 1
} else if ( } else if (
isNumber(obj.second) &&
isNumber(obj.min) && isNumber(obj.min) &&
isNumber(obj.hour) && isNumber(obj.hour) &&
isNumber(obj.day) && isNumber(obj.day) &&
@ -436,12 +377,10 @@ function resolveExp() {
obj.week === '?' obj.week === '?'
) { ) {
selectedPeriod.value = 'month' selectedPeriod.value = 'month'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0 timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0 timeValues.value.hour = parseInt(obj.hour) || 0
timeValues.value.day = parseInt(obj.day) || 1 timeValues.value.day = parseInt(obj.day) || 1
} else if ( } else if (
isNumber(obj.second) &&
isNumber(obj.min) && isNumber(obj.min) &&
isNumber(obj.hour) && isNumber(obj.hour) &&
isNumber(obj.day) && isNumber(obj.day) &&
@ -449,15 +388,13 @@ function resolveExp() {
obj.week === '?' obj.week === '?'
) { ) {
selectedPeriod.value = 'year' selectedPeriod.value = 'year'
timeValues.value.second = parseInt(obj.second) || 0
timeValues.value.minute = parseInt(obj.min) || 0 timeValues.value.minute = parseInt(obj.min) || 0
timeValues.value.hour = parseInt(obj.hour) || 0 timeValues.value.hour = parseInt(obj.hour) || 0
timeValues.value.day = parseInt(obj.day) || 1 timeValues.value.day = parseInt(obj.day) || 1
timeValues.value.month = parseInt(obj.month) || 1 timeValues.value.month = parseInt(obj.month) || 1
} else { } else {
// 使 // 使
selectedPeriod.value = 'second' selectedPeriod.value = 'minute'
}
} }
} else { } else {
clearCron() clearCron()
@ -477,10 +414,9 @@ function submitFill() {
// //
function clearCron() { function clearCron() {
selectedPeriod.value = 'second' selectedPeriod.value = 'minute'
selectedWeek.value = 1 selectedWeek.value = 1
timeValues.value = { timeValues.value = {
second: 0,
minute: 0, minute: 0,
hour: 0, hour: 0,
day: 1, day: 1,

View File

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