hanbaoshan
2020-10-27 da98d2a8a686cde09b20345e4a2b55a85410fde4
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
192
193
194
<template>
  <div>
    <b style="padding-right:30px;">定时重启:</b>
    <el-select v-model="every" placeholder="请选择" size="small" @change="changeEvery">
      <el-option label="关闭" value="never"></el-option>
      <el-option label="每天" value="day"></el-option>
      <el-option label="每周" value="week"></el-option>
      <el-option label="每月" value="month"></el-option>
    </el-select>
 
    <el-select
      v-show="every == 'month'"
      v-model="cronValueObj.day"
      placeholder="请选择"
      size="small"
      style="margin-left: 20px"
      @change="updateExpression"
    >
      <el-option v-for="item in days" :key="item.value" :label="item.label" :value="item.value"></el-option>
    </el-select>
 
    <el-select
      v-show="every == 'week'"
      v-model="cronValueObj.week"
      placeholder="请选择"
      size="small"
      style="margin-left: 20px"
      @change="updateExpression"
    >
      <el-option label="星期一" value="1"></el-option>
      <el-option label="星期二" value="2"></el-option>
      <el-option label="星期三" value="3"></el-option>
      <el-option label="星期四" value="4"></el-option>
      <el-option label="星期五" value="5"></el-option>
      <el-option label="星期六" value="6"></el-option>
      <el-option label="星期日" value="7"></el-option>
    </el-select>
 
    <el-time-picker
      v-show="every !== 'never'"
      v-model="time"
      :picker-options="{selectableRange: '00:00:00 - 23:59:59'}"
      value-format="HH:mm"
      format="HH:mm"
      placeholder="任意时间点"
      size="small"
      style="margin-left: 20px"
      @change="updateExpression"
    ></el-time-picker>
 
    <el-button
      v-show="saveBtn"
      type="primary"
      size="small"
      style="margin-left: 20px"
      @click="save"
    >保存</el-button>
  </div>
</template>
 
<script>
export default {
  name: "VueCrontab",
  props: ["expression"],
  computed: {
    days: () => {
      let arr = []
      for (let i = 1; i < 32; i++) {
        arr.push({
          label: i + "日",
          value: i + ""
        })
      }
 
      return arr
    }
  },
  watch: {
    expression: function () {
      this.resolveExp()
    }
  },
  data() {
    return {
      saveBtn: false,
      every: "never",
      time: "",
      cronValueObj: {
        min: "*",
        hour: "*",
        day: "*",
        month: "*",
        week: "*"
      },
      cronText: ""
    }
  },
  mounted() {
    this.resolveExp()
  },
  methods: {
    resolveExp() {
      //反解析 表达式
      "准备反解析", this.expression;
      if (this.expression.length) {
        let arr = this.expression.split(" ");
        if (arr.length >= 5) {
          //6 位以上是合法表达式
          this.cronValueObj.min = arr[0]
          this.cronValueObj.hour = arr[1]
          this.cronValueObj.day = arr[2]
          // this.cronValueObj.month = arr[3],
          this.cronValueObj.month = "*"
          this.cronValueObj.week = arr[4]
        }
 
        if (this.cronValueObj.week != "*") {
          this.every = "week"
        } else if (this.cronValueObj.day != "*") {
          this.every = "month"
        } else {
          this.every = "day"
        }
        this.time = this.cronValueObj.hour + ":" + this.cronValueObj.min
      } else {
        //没有传入的表达式 则还原
        this.clearCron();
      }
    },
    changeEvery() {
      this.saveBtn = true;
      if (this.every === "never") {
        this.cronText = ""
        return
      }
      if (this.every === "month") {
        this.cronValueObj.week = "*"
        this.cronValueObj.day = "1"
        if (!this.time.length) {
          this.time = "00:00"
        }
      }
      if (this.every === "week") {
        this.cronValueObj.day = "*"
        this.cronValueObj.week = "1"
        if (!this.time.length) {
          this.time = "00:00"
        }
      }
      if (this.every === "day") {
        this.cronValueObj.day = "*"
        this.cronValueObj.week = "*"
      }
      this.updateExpression()
    },
    updateExpression() {
      this.saveBtn = true;
      if (this.time.length) {
        let arr = this.time.split(":");
        this.cronValueObj.hour = arr[0]
        this.cronValueObj.min = arr[1]
      }
      this.crontabValueString()
    },
    clearCron() {
      this.cronValueObj.second = "*"
      this.cronValueObj.min = "*"
      this.cronValueObj.hour = "*"
      this.cronValueObj.day = "*"
      this.cronValueObj.month = "*"
      this.cronValueObj.week = "*"
    },
    crontabValueString: function () {
      let obj = this.cronValueObj;
      this.cronText =
        obj.min +
        " " +
        obj.hour +
        " " +
        obj.day +
        " " +
        obj.month +
        " " +
        obj.week
    },
    save() {
      this.$emit("update", this.cronText)
    }
  }
}
</script>
 
<style lang="scss">
</style>