Newer
Older
casic-monitoring-web / src / views / job / editJob.vue
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model.trim="form.jobName" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="表达式" prop="cronExpression">
            <el-input v-model.trim="form.cronExpression" type="text" placeholder="cron表达式"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="分片数" prop="shardingCount">
            <el-input v-model.number="form.shardingCount" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分片参数" prop="shardingParameter">
            <el-input v-model.trim="form.shardingParameter" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="参数设置" prop="policySettings">
            <el-input v-model="form.policySettings" type="textarea" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update } from '@/api/common/job'
import { getDictByCode } from '@/api/system/dict'
export default {
  name: 'EditJob',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型:create,update
      form: {
        id: '',
        basicJobClass: '',
        cronExpression: '',
        jobName: '',
        policySettings: '',
        remarks: '',
        shardingCount: '',
        shardingParameter: '',
        status: ''
      },
      btnLoading: false, // 保存按钮的加载中状态
      multiData: false, // 组织树是否为多级列表
      deptTreeList: [], // 组织树列表数据
      resTypeDicts: [], // 资源类型字典集合
      routeTypeDicts: [], // 访问类型字典集合
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      rules: {
        jobName: [{ required: true, message: '任务名称必填', trigger: ['blur', 'change'] }],
        cronExpression: [{ required: true, message: 'cron表达式必填', trigger: ['blur', 'change'] }],
        shardingCount: [{ required: true, message: '分片数必填', trigger: ['blur', 'change'] }, { type: 'number', message: '必须为数字值' }]
      } // 前端校验规则
    }
  },
  created() {
    this.fetchData()
  },
  mounted: function() {
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      this.btnLoading = false
      if (dialogStatus === 'create') { // 如果是新增,清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改,将row中数据填写到输入框中
        this.form = {
          id: row.id,
          basicJobClass: row.basicJobClass,
          cronExpression: row.cronExpression,
          jobName: row.jobName,
          policySettings: row.policySettings,
          remarks: row.remarks,
          shardingCount: row.shardingCount,
          shardingParameter: row.shardingParameter,
          status: row.status
        }
      }
    },
    // 重置表单
    resetForm() {
      this.form = {
        id: '',
        basicJobClass: '',
        cronExpression: '',
        jobName: '',
        policySettings: '',
        remarks: '',
        shardingCount: '',
        shardingParameter: '',
        status: ''
      }
    },
    // 获取字典数据
    fetchData() {
      // 字典类型加载
      getDictByCode('viewResType').then(response => {
        this.resTypeDicts = response.data
      })
      getDictByCode('viewRouteType').then(response => {
        this.routeTypeDicts = response.data
      })
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          add(this.form).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功,是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.btnLoading = false
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          }).catch(_ => { // 异常情况,loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          this.dialogFormVisible = true
          update(this.form).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况,loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      // this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-dialog{
    width:700px
  }
  .el-select{
    width: 100%;
  }
</style>