index.vue 3.34 KB
<template>
  <div>
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-model-item label="文档标题" prop="docTitle">
        <a-input v-model="form.docTitle" />
      </a-form-model-item>
      <a-form-model-item label="文档地址" prop="docUrl">
        <a-input v-model="form.docUrl" />
      </a-form-model-item>
      <a-form-model-item label="文档描述" prop="docDescription">
        <a-input v-model="form.docDescription" 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 type="text/ecmascript-6">
export default {
  name: "",
  data() {
    return {
      isNewAdd: true,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      form: {
        docTitle: "",
        docUrl: "",
        docDescription: "",
      },
      rules: {
        docTitle: [
          { required: true, message: "请输入文档名称", trigger: "blur" },
          { min: 1, max: 30, message: "请输入字符1~50之间", trigger: "blur" },
        ],
        docUrl: [
          { required: true, message: "请输入文档地址", trigger: "blur" },
        ],
        docDescription: [
          { required: true, message: "请输入文档描述", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.editeItemFn();
  },
  methods: {
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.isNewAdd) {
            //新增
            this.api.documentation.create(this.form).then((res) => {
              // console.log(res);
              if (res.id) {
                this.$message.success("添加成功");
                this.closeForm();
                this.resetForm();
              } else {
                this.$message.error("添加失败");
              }
            });
          } else {
            //编辑
            this.api.documentation.update(this.form).then((res) => {
              console.log(res);
              if (res.id) {
                this.$message.success("编辑成功");
                this.closeForm();
                this.resetForm();
              } else {
                this.$message.error("编辑失败");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.isNewAdd = true;
      this.$refs.ruleForm.resetFields();
    },
    closeForm() {
      this.to(-1);
    },
    //编辑项目
    editeItemFn() {
      const editeItem = this.$route.params;
      console.log('editeItem: ', editeItem);
      if (Object.keys(editeItem).length === 0) return;
      this.isNewAdd = false;
      this.form = {
        docTitle: editeItem.doc_title,
        docUrl: editeItem.doc_url,
        docDescription: editeItem.doc_description,
        id: editeItem.id,
      };
    },
  },
  // 重置
  beforeDestroy() {
    this.resetForm();
  },
};
</script>

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