Xxxu
2020-07-07 4f953ffc89fc95f83b152e914c5e65938b440f17
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="定时名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="定时名称"></el-input>
      </el-form-item>
      <!-- 定时控制 -->
      <el-form-item label="星期" prop="week">
        <el-radio-group v-model="week.type">
          <el-radio :label="0">每一天</el-radio>
          <el-radio :label="1">某几天</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="" prop="week" v-if="week.type == 1">
        <el-checkbox-group v-model="week.day">
          <el-checkbox
            v-for="item in weekList"
            :key="item.value"
            :label="item.value">
            {{item.label}}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="时间" prop="time">
        <el-time-picker
          v-model="time"
          format='HH:mm'
          value-format="HH:mm">
        </el-time-picker>
      </el-form-item>
      <el-form-item label="控灯动作" prop="value">
        <el-radio-group v-model="dataForm.value">
          <el-radio v-for="item in operateList" :label="item.value" :key="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <!-- 取消按钮 -->
      <button class="btn-cancel" @click="visible = false"></button>
      <!-- 确定按钮 -->
      <button class="btn-config" type="primary" @click="dataFormSubmit()"></button>
    </span>
  </el-dialog>
</template>
 
<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        timeDefaultId: 0,
        timeId: 0,
        parentId: '',
        name: '',
        type: '',
        beanName: '',
        value: '0',
        cron: '',
        createUserId: '',
        createTime: ''
      },
      week: {
        type: 0,
        day: []
      },
      time: '',
      operateList: [
        {
          value: '1',
          label: '正常开灯'
        },
        {
          value: '0',
          label: '关灯'
        },
        {
          value: '2',
          label: '20%亮度'
        },
        {
          value: '5',
          label: '50%亮度'
        },
        {
          value: '8',
          label: '80%亮度'
        },
        {
          value: '10',
          label: '100%亮度'
        }
      ],
      weekList: [
        {
          value: 1,
          label: '星期日'
        },
        {
          value: 2,
          label: '星期一'
        },
        {
          value: 3,
          label: '星期二'
        },
        {
          value: 4,
          label: '星期三'
        },
        {
          value: 5,
          label: '星期四'
        },
        {
          value: 6,
          label: '星期五'
        },
        {
          value: 7,
          label: '星期六'
        }
      ],
      dataRule: {
        name: [
          { required: true, message: '定时名称不能为空', trigger: 'blur' }
        ],
        beanName: [
          { required: true, message: '执行动作不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    timeTemName () {
      return this.changeName()
    }
  },
  watch: {
    timeTemName (curVal, oldVal) {
      this.dataForm.name = curVal
    }
  },
  methods: {
    init (data, parentId) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (data !== undefined && data !== null) {
          // this.dataForm = data
          this.dataForm.timeId = data.timeId
          this.dataForm.parentId = data.parentId
          this.dataForm.name = data.name
          this.dataForm.type = data.type
          this.dataForm.beanName = data.beanName
          this.dataForm.value = data.value
          this.dataForm.cron = data.cron
          this.dataForm.createUserId = data.createUserId
          this.dataForm.createTime = data.createTime
          this.dataForm.timeDefaultId = data.timeDefaultId
          this.cronToData(this.dataForm.cron)
        } else {
          this.dataForm.parentId = parentId
          this.dataForm.name = ''
          this.dataForm.type = 1
          this.dataForm.beanName = 'lightbatchTesk'
          this.dataForm.value = '0'
          this.dataForm.cron = ''
          this.dataForm.createUserId = ''
          this.dataForm.createTime = ''
          this.dataForm.timeDefaultId = 0
          this.week.type = 0
          this.week.day = []
          this.time = ''
        }
        this.changeName()
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (parseInt(this.week.type) !== 0) {
          // 星期不是每一天
          if (this.week.day.length === 0) {
            this.$message.error('星期请选择星期')
            return
          } else if (this.week.day.length === 7) {
            this.$message.error('星期请选择每一天')
            return
          }
        }
        if (this.time === undefined || this.time === null || this.time === '') {
          this.$message.error('时间不能为空')
          return
        }
        if (this.dataForm.value === undefined || this.dataForm.value === '') {
          this.$message.error('控灯操作不能为空')
          return
        }
        this.dataForm.cron = this.dataToCron(this.time, this.week)
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshData', this.dataForm)
          }
        })
      })
    },
    // 日期转cron表达式
    dataToCron (timeCode, weekType) {
      // 添加秒
      var cron = '0 '
      var time = timeCode.split(':')
      // 添加分钟,小时
      if (time && time.length === 2) {
        cron += time[1].replace(/\b0/gi, '') + ' '
        cron += time[0].replace(/\b0/gi, '') + ' '
      }
      // 添加天
      if (weekType.type === 0) {
        // 每一天
        cron += '* '
      } else if (weekType.type === 1) {
        // 某几天
        cron += '? '
      }
      // 添加月
      cron += '* '
      // 添加星期
      if (weekType.type === 0) {
        // 每一天
        cron += '?'
      } else if (weekType.type === 1) {
        // 某几天
        weekType.day.forEach(element => {
          cron += element + ','
        })
      }
      cron = (cron.substring(cron.length - 1) === ',') ? cron.substring(0, cron.length - 1) : cron
      return cron
    },
    // cron表达式转日期格式
    cronToData (cron) {
      if (cron === undefined || cron === '') {
        this.week.type = 0
        this.week.day = []
        return
      }
      var time = cron.split(' ')
      this.time = time[2] + ':' + time[1]
      if (time[3] === '*') {
        // 每一天
        this.week.type = 0
        this.week.day = []
      } else {
        this.week.type = 1
        // this.week.day = time[5].split(',')
        var list = []
        time[5].split(',').forEach(element => {
          list.push(parseInt(element))
        })
        this.week.day = list
      }
    },
    // 自动生成名称
    changeName () {
      var name = ''
      if (this.week.type === 0) {
        name += '每天 '
      } else if (this.week.type === 1) {
        this.week.day.forEach(day => {
          this.weekList.forEach(week => {
            if (day === week.value) {
              name += week.label + ','
            }
          })
        })
      }
      name = (name.substring(name.length - 1) === ',') ? name.substring(0, name.length - 1) : name
      name += this.time + ' '
      this.operateList.forEach(element => {
        if (element.value === this.dataForm.value) {
          name += element.label
        }
      })
      this.dataForm.name = name
    }
  }
}
</script>
 
<style lang="scss" scoped>
div {
  /deep/ .el-input__inner {
    background: transparent;
    color: #fff
  }
  /deep/ span {
    color: #fff
  }
  /deep/ .el-form-item__label {
    color: #fff
  }
}
</style>