baotian
2024-06-04 b959135a1139fb66646523d92e5bd20c5910f283
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<template>
  <a-modal
    title="编辑定时任务"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="formLoading">
      <a-form :form="form">
        <a-row :gutter="24" style="display: none">
          <a-col :md="12" :sm="24">
            <a-form-item>
              <a-input v-decorator="['id']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item :label="jobNameLabel" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
              <a-select
                :placeholder="jobNamePlaceholder"
                :showArrow="true"
                :mode="jobNameMode"
                v-decorator="['jobName', { rules: [{ required: true, message: '请输入任务名称!' }] }]"
              >
                <a-select-option
                  v-for="(item, index) in JobNameData"
                  :key="index"
                  :value="item.jobName"
                  @click="onChangeJobName(item)"
                  >{{ item.jobName }}</a-select-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
              <span slot="label">
                <a-tooltip title="Run类型:类名/方法名,http类型:URL地址">
                  <a-icon type="question-circle-o" /> </a-tooltip
                >&nbsp; 请求地址
              </span>
              <a-input
                placeholder="请输入请地址"
                v-decorator="['requestUrl', { rules: [{ required: true, message: '请输入请求地址!' }] }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
 
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item :labelCol="labelCol_JG" :wrapperCol="wrapperCol_JG" label="请求类型">
              <a-radio-group
                v-decorator="['requestType', { rules: [{ required: true, message: '请选择请求类别!' }] }]"
              >
                <a-radio
                  v-for="(item, index) in requestTypeEnumDataDropDown"
                  :key="index"
                  :value="parseInt(item.code)"
                  @click="onChangeRequestType(item.code)"
                  >{{ item.value }}</a-radio
                >
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
 
        <a-row :gutter="24">
          <div v-show="showHeaders">
            <a-col :md="24" :sm="24">
              <a-form-item :labelCol="labelCol_JG" :wrapperCol="wrapperCol_JG" label="请求头" has-feedback>
                <a-input placeholder="请输入请求头" style="width: 100%" v-decorator="['headers']" />
              </a-form-item>
            </a-col>
          </div>
        </a-row>
 
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item
              :labelCol="labelCol_JG"
              :wrapperCol="wrapperCol_JG"
              :label="requestParametersLabel"
              has-feedback
            >
              <a-textarea :rows="1" :placeholder="requestParametersPlaceholder" v-decorator="['requestParameters']">
              </a-textarea>
            </a-form-item>
          </a-col>
        </a-row>
 
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="定时器类型">
              <a-select
                style="width: 100%"
                placeholder="请选择定时器类型"
                @change="onChangeTimerType"
                v-decorator="['timerType', { rules: [{ required: true, message: '请选择定时器类型!' }] }]"
              >
                <a-select-option v-for="(item, index) in spareTimeTypeDropDown" :key="index" :value="item.code">{{
                  item.value
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="执行间隔(秒)"
              has-feedback
              v-if="VisibleTimerType"
            >
              <a-input-number
                placeholder="请输入执行间隔"
                style="width: 100%"
                v-decorator="['interval', { rules: [{ required: true, message: '请输入执行间隔!' }] }]"
                :min="1"
              />
            </a-form-item>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="任务表达式" has-feedback v-else>
              <nobr>
                <a-input
                  placeholder="请输入任务表达式"
                  v-decorator="['cron', { rules: [{ required: true, message: '请输入任务表达式!' }] }]"
                />
                <a href="https://www.bejson.com/othertools/cron/" target="_Blank">参考</a>
              </nobr>
            </a-form-item>
          </a-col>
        </a-row>
 
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
              <span slot="label">
                <a-tooltip title="新增任务后立即执行,项目启动后立即执行">
                  <a-icon type="question-circle-o" /> </a-tooltip
                >&nbsp; 立即执行
              </span>
              <a-switch
                id="startNow"
                checkedChildren="是"
                unCheckedChildren="否"
                v-decorator="['startNow', { valuePropName: 'checked' }]"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="只执行一次">
              <a-switch
                id="doOnce"
                checkedChildren="是"
                unCheckedChildren="否"
                v-decorator="['doOnce', { valuePropName: 'checked' }]"
              />
            </a-form-item>
          </a-col>
        </a-row>
 
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
              <span slot="label">
                <a-tooltip
                  title="并行执行不会等待当前任务完成,发起执行后立即开始下次任务的倒计时。串行执行会等待当前任务完成才开始下次任务的倒计时"
                >
                  <a-icon type="question-circle-o" /> </a-tooltip
                >&nbsp; 执行类型
              </span>
              <a-select
                style="width: 100%"
                placeholder="请选择执行类型"
                v-decorator="['executeType', { rules: [{ required: true, message: '请选择执行类型!' }] }]"
              >
                <a-select-option v-for="(item, index) in executeTypeDropDown" :key="index" :value="item.code">{{
                  item.value
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item label="备注" :labelCol="labelCol_JG" :wrapperCol="wrapperCol_JG" has-feedback>
              <a-input placeholder="请输入备注" v-decorator="['remark']"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>
 
<script>
import { sysTimersEdit, sysTimersLocalJobList } from '@/api/modular/system/timersManage'
import { sysEnumDataList, sysEnumDataListByField } from '@/api/modular/system/enumManage'
 
export default {
  data() {
    return {
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 6,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 16,
        },
      },
      labelCol_JG: {
        xs: {
          span: 24,
        },
        sm: {
          span: 3,
        },
      },
      wrapperCol_JG: {
        xs: {
          span: 24,
        },
        sm: {
          span: 20,
        },
      },
      visible: false,
      confirmLoading: false,
      VisibleTimerType: true,
      requestTypeEnumDataDropDown: [],
      spareTimeTypeDropDown: [],
      executeTypeDropDown: [],
      LocalJobsDropDown: [],
      JobNameData: [],
      formLoading: false,
      jobNameLabel: '任务名称',
      jobNameMode: 'combobox',
      jobNamePlaceholder: '请输入任务名称',
      requestParametersLabel: '请求参数',
      requestParametersPlaceholder: '请输入请求参数',
      showHeaders: true,
      form: this.$form.createForm(this),
    }
  },
  methods: {
    // 初始化方法
    edit(record) {
      this.visible = true
      this.formLoading = true
      this.sysEnumDataList()
 
      // eslint-disable-next-line eqeqeq
      this.VisibleTimerType = record.timerType == 0
 
      if (record.requestType === 0) {
        this.onChangeRequestType(record.requestType)
      }
 
      setTimeout(() => {
        this.form.setFieldsValue({
          id: record.id,
          jobName: record.jobName,
          requestUrl: record.requestUrl,
          requestType: record.requestType,
          timerType: record.timerType,
          cron: record.cron,
          interval: record.interval,
          headers: record.headers,
          requestParameters: record.requestParameters,
          remark: record.remark,
          startNow: record.startNow,
          doOnce: record.doOnce,
          executeType: record.executeType,
        })
      }, 100)
    },
 
    /**
     * 获取枚举数据
     */
    sysEnumDataList() {
      sysEnumDataList({
        enumName: 'RequestTypeEnum',
      }).then((res) => {
        this.requestTypeEnumDataDropDown = res.data
      })
      sysEnumDataListByField({
        EntityName: 'SysTimer',
        FieldName: 'TimerType',
      }).then((res) => {
        this.spareTimeTypeDropDown = res.data
      })
      sysEnumDataListByField({
        EntityName: 'SysTimer',
        FieldName: 'ExecuteType',
      }).then((res) => {
        this.executeTypeDropDown = res.data
      })
      this.formLoading = false
    },
 
    onChangeTimerType(e) {
      this.VisibleTimerType = e === 0
    },
 
    onChangeRequestType(e) {
      this.showHeaders = e !== 0
      if (!this.showHeaders) {
        if (this.LocalJobsDropDown.length === 0) {
          sysTimersLocalJobList().then((res) => {
            this.LocalJobsDropDown = res.data
            this.JobNameData = res.data
          })
        } else {
          this.JobNameData = this.LocalJobsDropDown
        }
        this.jobNameLabel = '任务方法'
        this.jobNameMode = 'default'
        this.jobNamePlaceholder = '请选择任务方法'
        this.requestParametersLabel = '配置项参数'
        this.requestParametersPlaceholder = '请输入配置项参数'
      } else {
        this.JobNameData = []
        this.jobNameLabel = '任务名称'
        this.jobNameMode = 'combobox'
        this.jobNamePlaceholder = '请输入任务名称'
        this.requestParametersLabel = '请求参数'
        this.requestParametersPlaceholder = '请输入请求参数'
      }
    },
 
    onChangeJobName(e) {
      this.onChangeTimerType(e.timerType)
      setTimeout(() => {
        this.form.setFieldsValue({
          requestUrl: e.requestUrl,
          startNow: e.startNow,
          doOnce: e.doOnce,
          interval: e.interval,
          timerType: e.timerType,
          remark: e.remark,
          executeType: e.executeType,
          cron: e.cron,
        })
      }, 100)
    },
 
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          sysTimersEdit(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('编辑失败:' + res.message)
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>