zenglf
2023-09-18 92ff846fb659c62037a32b1d8c15eae9df9d9b54
src/views/eam/modules/equipmentNew/EquipmentDocumentModal.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,294 @@
<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="form"
        :model="model"
        :rules="validatorRules"
      >
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="文档编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="num"
            >
              <a-input
                placeholder="请输入文档编号"
                v-model="model.num"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="文档类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="fileType"
            >
              <j-dict-select-tag
                allow-clear
                placeholder="请选择文档类型"
                :triggerChange="true"
                dictCode="mom_eam_document_type,name,id, del_flag!='1'"
                v-model="model.fileType"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="文档上传"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <!-- prop="fileType" -->
              <a-upload-dragger
                name="file"
                :customRequest="customRequest"
                @change="handleChange"
                :file-list="fileList"
                :multiple="false"
              >
                <p class="ant-upload-drag-icon">
                  <a-icon type="inbox" />
                </p>
                <p class="ant-upload-text">
                  ç‚¹å‡»ä¸Šä¼ æˆ–拖拽文件至该区域进行上传
                </p>
                <p class="ant-upload-hint">
                  ä»…支持单文件上传
                </p>
              </a-upload-dragger>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="文件名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="name"
            >
              <a-input
                placeholder="请输入文件名称"
                v-model="model.name"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="remark"
            >
              <a-textarea
                placeholder="请输入备注"
                allow-clear
                v-model="model.remark"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>
<script>
import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getAction, postFileAction } from '../../../../api/manage'
import { ACCESS_TOKEN, TENANT_ID } from "@/store/mutation-types"
import Vue from 'vue'
export default {
  name: "EquipmentDocumentModal",
  components: {
  },
  props: {
    mainId: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      title: "操作",
      width: 800,
      visible: false,
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      headers: {},
      confirmLoading: false,
      validatorRules: {
        num: [
          { required: true, message: '请输入文档编号!' },
        ],
        fileType: [
          { required: true, message: '请输入选择类型!' },
        ]
      },
      url: {
        add: "/eam/equipmentDocument/add",
        edit: "/eam/equipmentDocument/edit",
        listByBusIdAndBusType: "/system/sysUploadRela/listByBusIdAndBusType",
      },
      fileList: [],
      fileObject: {},
      isFileChange: false,
      uploadId: '',
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    // this.initFileTypes();
    const token = Vue.ls.get(ACCESS_TOKEN);
    const tenantid = Vue.ls.get(TENANT_ID)
    this.headers = {
      'Content-Type': 'multipart/form-data',
      "X-Access-Token": token,
      'X-Access-Tenant': tenantid
    };
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    customRequest(val) {
      if (this.fileList.length == 0) {
        this.fileObject = val;
      }
    },
    handleChange(info) {
      //默认给附件状态,仅控制样式
      let file = info.file;
      //文件更改,标记更新
      if (this.model.id) {
        this.isFileChange = true;
      }
      //删除
      if (file.status == "removed") {
        this.fileList = [];
        this.fileObject.file = {};
        this.model.name = ''
        return false;
      }
      //单文件控制
      if (this.fileList.length > 0) {
        this.$message.warning("当前仅允许上传一个文件!");
        return false;
      }
      file.status = 'done';
      //页面展示上传文件
      this.fileList.push(file);
      this.model.name = file.name;
      this.model = Object.assign({}, this.model);
    },
    edit(record) {
      this.fileList = [];
      this.uploadId = ''
      this.model = Object.assign({}, record);
      var that = this;
      getAction(that.url.listByBusIdAndBusType, { busId: record.id, busType: 'equipment_file' }).then((res) => {
        if (res.success) {
          for (let i = 0; i < res.result.length; i++) {
            that.fileList.push({
              uid: res.result[i].upload.id,
              name: res.result[i].upload.name,
              status: "done",
            })
            that.$nextTick(() => {
              that.model.name = res.result[i].upload.name
            });
          }
        }
      })
      this.visible = true;
    },
    close() {
      this.$emit('close');
      this.visible = false;
      this.$refs.form.clearValidate();
    },
    handleOk() {
      const that = this;
      // è§¦å‘表单验证
      this.$refs.form.validate(valid => {
        if (JSON.stringify(that.fileObject.file) == '{}' && that.uploadId == '' && that.fileList.length == 0) {
          that.$message.warning("请上传文件");
          return false
        }
        if (valid) {
          that.confirmLoading = true;
          let formData = Object.assign({}, that.model);
          let httpurl = ''
          let method = ''
          if (!that.model.id) {
            httpurl += that.url.add;
            method = 'post';
            formData.equipmentId = that.mainId;
          } else {
            httpurl += that.url.edit;
            method = 'put';
          }
          formData.type = "equipment_file";
          formData.uploadId = that.uploadId;
          // formData.fileType = "7";//文件类型  7:图片
          var saveDate = new FormData();
          formData.isFileChange = that.isFileChange;
          saveDate.append("file", that.fileObject.file);
          saveDate.append('data', JSON.stringify(formData));
          postFileAction(httpurl, saveDate, this.headers).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>