wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
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
<script lang="ts" setup>
import { ref, reactive, computed, watch, onBeforeMount } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { useI18n } from '@/hooks/web/useI18n'
import type { Corn } from './Hour.vue'
const props = defineProps({
  modelValue: propTypes.string.def('?')
})
 
const { t } = useI18n()
 
const type = ref('1')
const work = ref<string | number>(0)
const last = ref<string | number>(0)
const state = reactive<Corn>({
  cycle: {
    // 周期
    start: 0,
    end: 0
  },
  loop: {
    // 循环
    start: 0,
    end: 0
  },
  week: {
    // 指定周
    start: 0,
    end: 0
  },
  appoint: []
})
 
const resultValue = computed(() => {
  const result = []
  switch (type.value) {
    case '1': // 每秒
      result.push('*')
      break
    case '2': // 年期
      result.push(`${state.cycle.start}-${state.cycle.end}`)
      break
    case '3': // 循环
      result.push(`${state.loop.start}/${state.loop.end}`)
      break
    case '4': // 指定
      result.push(state.appoint.join(','))
      break
    case '6': // 最后
      result.push(`${last.value === 0 ? '' : last.value}L`)
      break
    case '7': // 指定周
      result.push(`${state.week.start}#${state.week.end}`)
      break
    default: // 不指定
      result.push('?')
      break
  }
  return result.join('')
})
 
onBeforeMount(() => {
  updateVal()
})
 
watch(
  () => props.modelValue,
  () => {
    updateVal()
  }
)
 
watch(
  () => resultValue.value,
  () => {
    emits('update:modelValue', resultValue.value)
  }
)
 
const updateVal = () => {
  if (!props.modelValue) {
    return
  }
  if (props.modelValue === '?') {
    type.value = '5'
  } else if (props.modelValue.indexOf('-') !== -1) {
    // 2周期
    if (props.modelValue.split('-').length === 2) {
      type.value = '2'
      state.cycle.start = props.modelValue.split('-')[0] as unknown as number
      state.cycle.end = props.modelValue.split('-')[1] as unknown as number
    }
  } else if (props.modelValue.indexOf('/') !== -1) {
    // 3循环
    if (props.modelValue.split('/').length === 2) {
      type.value = '3'
      state.loop.start = props.modelValue.split('/')[0] as unknown as number
      state.loop.end = props.modelValue.split('/')[1] as unknown as number
    }
  } else if (props.modelValue.indexOf('*') !== -1) {
    // 1每
    type.value = '1'
  } else if (props.modelValue.indexOf('L') !== -1) {
    // 6最后
    type.value = '6'
    last.value = props.modelValue.replace('L', '')
  } else if (props.modelValue.indexOf('#') !== -1) {
    // 7指定周
    if (props.modelValue.split('#').length === 2) {
      type.value = '7'
      state.week.start = props.modelValue.split('#')[0]
      state.week.end = props.modelValue.split('#')[1]
    }
  } else if (props.modelValue.indexOf('W') !== -1) {
    // 8工作日
    type.value = '8'
    work.value = props.modelValue.replace('W', '')
  } else {
    // *
    type.value = '4'
    state.appoint = props.modelValue.split(',')
  }
}
 
const emits = defineEmits(['update:modelValue'])
</script>
 
<template>
  <div>
    <div>
      <el-radio v-model="type" label="1" size="small" border>{{ t('cron.every_week') }}</el-radio>
    </div>
    <div>
      <el-radio v-model="type" label="5" size="small" border>{{ t('cron.not_set') }}</el-radio>
    </div>
    <div>
      <el-radio v-model="type" label="2" size="small" border>{{ t('cron.cycle') }}</el-radio>
      <span style="margin-left: 10px; margin-right: 5px">{{ t('cron.week_start') }}</span>
      <el-input-number
        v-model="state.cycle.start"
        :min="1"
        :max="7"
        size="small"
        style="width: 100px"
        @change="type = '2'"
      />
      <span style="margin-left: 5px; margin-right: 5px">{{ t('cron.week_end') }}</span>
      <el-input-number
        v-model="state.cycle.end"
        :min="2"
        :max="7"
        size="small"
        style="width: 100px"
        @change="type = '2'"
      />
    </div>
    <div>
      <el-radio v-model="type" label="3" size="small" border>{{ t('cron.repeat') }}</el-radio>
      <span style="margin-left: 10px; margin-right: 5px">{{ t('cron.week_start') }}</span>
      <el-input-number
        v-model="state.loop.start"
        :min="1"
        :max="7"
        size="small"
        style="width: 100px"
        @change="type = '3'"
      />
      <span style="margin-left: 5px; margin-right: 5px">{{ t('cron.every_begin') }}</span>
      <el-input-number
        v-model="state.loop.end"
        :min="1"
        :max="7"
        size="small"
        style="width: 100px"
        @change="type = '3'"
      />
      {{ t('cron.day_exec') }}
    </div>
    <div>
      <el-radio v-model="type" label="4" size="small" border>{{ t('cron.set') }}</el-radio>
      <el-checkbox-group v-model="state.appoint" style="margin-left: 50px; line-height: 25px">
        <el-checkbox v-for="i in 7" :key="i" :label="i + ''" @change="type = '4'" />
      </el-checkbox-group>
    </div>
    <div>
      <span style="font-size: 12px">{{ t('cron.week_tips') }}</span>
    </div>
  </div>
</template>
 
<style lang="less" scoped></style>