index.vue 5.07 KB
<template>
  <div>
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="项目名称" prop="project_title">
        <a-input v-model="form.project_title" />
      </a-form-model-item>
      <a-form-model-item label="开发者名称" prop="developer_name">
        <a-input v-model="form.developer_name" />
      </a-form-model-item>
      <a-form-model-item label="配置名称" prop="config_name">
        <a-input v-model="form.config_name" />
      </a-form-model-item>
      <a-form-model-item label="测试环境地址" prop="test_url">
        <a-input v-model="form.test_url" />
      </a-form-model-item>
      <a-form-model-item label="生产环境地址" prop="product_url">
        <a-input v-model="form.product_url" />
      </a-form-model-item>
      <a-form-model-item label="相关技术栈" prop="technology">
        <a-input v-model="form.technology" />
      </a-form-model-item>
      <a-form-model-item label="git地址" prop="git_url">
        <a-input v-model="form.git_url" />
      </a-form-model-item>
      <a-form-model-item label="git项目ID" prop="git_id">
        <a-input v-model="form.git_id" />
      </a-form-model-item>
      <a-form-model-item label="请输入项目描述" prop="project_description">
        <a-input v-model="form.project_description" type="textarea" />
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
        <a-button type="primary" @click="onSubmit">{{ isNewAdd ? '创建' : '提交' }}</a-button>
        <a-button style="margin-left: 10px" @click="resetForm">重置</a-button>
        <a-button style="margin-left: 10px" @click="closeForm">关闭</a-button>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      isNewAdd: true,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        project_title: '',
        project_description: '',
        developer_name: '',
        config_name: '',
        test_url: '',
        product_url: '',
        technology: '',
        git_url: '',
        git_id: ''
      },
      rules: {
        project_title: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
          { min: 1, max: 30, message: '请输入字符1~50之间', trigger: 'blur' }
        ],
        project_description: [{ required: true, message: '请输入项目描述', trigger: 'blur' }],
        developer_name: [{ required: true, message: '请输入开发者名称', trigger: 'blur' }],
        config_name: [{ required: true, message: '请输入配置名称', trigger: 'blur' }],
        test_url: [{ required: true, message: '请输入测试环境地址', trigger: 'blur' }],
        product_url: [{ required: true, message: '请输入生产环境地址', trigger: 'blur' }],
        technology: [{ required: true, message: '请输入相关技术栈', trigger: 'blur' }],
        git_url: [{ required: true, message: '请输入gitlab地址', trigger: 'blur' }],
        git_id: [{ required: true, message: '请输入项目gitlab id', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.editeItemFn()
  },
  methods: {
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // const classify = sessionStorage.getItem("classify");

          const pramas = {
            ...this.form
            // classify,
          }

          // console.log("pramas: " + JSON.stringify(pramas));

          if (this.isNewAdd) {
            //新增
            this.api.project.create(pramas).then(res => {
              //  console.log('res: ', res);
              if (res.id) {
                this.$message.success('添加成功')
                this.closeForm()
                this.resetData()
              } else {
                this.$message.error('添加失败')
              }
            })
          } else {
            //编辑
            this.api.project.update(pramas).then(res => {
              // console.log("res: ", res);
              if (res.id) {
                this.$message.success('编辑成功')
                this.closeForm()
                this.resetData()
              } else {
                this.$message.error('编辑失败')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.isNewAdd = true
      this.$refs.ruleForm.resetFields()
      // this.resetData();
    },
    resetData() {
      this.form = {
        project_title: '',
        project_description: '',
        developer_name: '',
        config_name: '',
        test_url: '',
        product_url: '',
        technology: ''
      }
    },
    closeForm() {
      this.to(-1)
    },
    //编辑项目
    editeItemFn() {
      const editeItem = this.$route.query
      if (Object.keys(editeItem).length === 0) return
      this.isNewAdd = false
      this.form = {
        ...editeItem
      }
    }
  },
  // 重置
  beforeDestroy() {
    this.resetForm()
  }
}
</script>

<style scoped lang="scss"></style>