From bb07bdd585e76a38ee4ade382156543fb146ffac Mon Sep 17 00:00:00 2001
From: baolhyhzhg <baolh@yhzhg.cn>
Date: Thu, 12 Dec 2024 08:43:40 +0000
Subject: [PATCH] 添加批量导入功能

---
 eladmin-web-master/src/views/contract/contract/index.vue |  138 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 138 insertions(+), 0 deletions(-)

diff --git a/eladmin-web-master/src/views/contract/contract/index.vue b/eladmin-web-master/src/views/contract/contract/index.vue
index 58718d0..1ce6606 100644
--- a/eladmin-web-master/src/views/contract/contract/index.vue
+++ b/eladmin-web-master/src/views/contract/contract/index.vue
@@ -91,12 +91,24 @@
       <crudOperation :permission="permission">
         <el-button
           slot="right"
+          v-permission="permission.edit"
+          class="filter-item"
+          size="mini"
+          type="primary"
+          icon="el-icon-upload"
+          @click="uploadDialogOpen"
+        >
+          导入
+        </el-button>
+        <el-button
+          slot="right"
           v-permission="permission.divide"
           class="filter-item"
           size="mini"
           type="primary"
           icon="el-icon-share"
           :disabled="crud.selections.length !== 1"
+          style="margin-left: 4px !important;"
           @click="toDivide(crud.selections[0])"
         >
           分配
@@ -572,6 +584,36 @@
           <el-button :loading="divideLoading" type="primary" @click="divideSubmit">确认</el-button>
         </div>
       </el-dialog>
+      <!--表单组件(导入)-->
+      <el-dialog :close-on-click-modal="false" :before-close="uploadDialogClose" :visible.sync="uploadDialog.visible" title="导入" width="500px">
+        <el-form ref="uploadForm" :model="uploadDialog.form" :rules="uploadDialog.rules" size="small" label-width="80px">
+          <el-form-item label="文件" prop="file">
+            <el-upload
+              ref="contractUpload"
+              :auto-upload="false"
+              :action="baseApi + '/api/contract/upload'"
+              accept=".xlsx, .xls"
+              :limit="1"
+              drag
+              :headers="headers"
+              :before-upload="beforeUpload"
+              :on-success="handleSuccess"
+              :on-error="handleError"
+              :on-change="handleChange"
+              :on-remove="handleRemove"
+              class="upload-demo"
+            >
+              <i class="el-icon-upload" />
+              <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
+              <div slot="tip" class="el-upload__tip">只能上传xls/xlsx文件,且不超过50M</div>
+            </el-upload>
+          </el-form-item>
+        </el-form>
+        <div slot="footer" class="dialog-footer">
+          <el-button type="text" @click="uploadDialogClose">取消</el-button>
+          <el-button :loading="uploadDialog.loading" type="primary" @click="uploadDialogSubmit">确认</el-button>
+        </div>
+      </el-dialog>
     </div>
   </div>
 </template>
@@ -595,6 +637,8 @@
 import { contractTotalMoney, contractDelayTotalAmount, contractDelayTotalMoney, advancePaymentTotalMoney } from '@/api/contract/contract'
 import { serviceTotalMoney, serviceDelayTotalAmount, serviceDelayTotalMoney, receivePaymentTotalMoney } from '@/api/contract/contract'
 import Dict from '@/components/Dict/Dict'
+import { mapGetters } from 'vuex'
+import { getToken } from '@/utils/auth'
 
 const defaultForm = { contractId: null, contractNumber: null, contractName: null, firstId: null, secondId: null, signDate: null, startDate: null, endDate: null, contractAreaCode: null, contractArea: null, contractType: null, contractSecondType: null, contractContent: null, contractAmount: null, contractPaymentDate: null, contractPaymentState: null, serviceStartsDate: null, serviceAmount: null, servicePaymentDate: null, servicePaymentState: null, relatedDocumentsFirst: null, relatedDocumentsFinal: null, contractStatus: null, acceptStatus: null, remarks: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
 export default {
@@ -819,8 +863,19 @@
         allocatedAmount: [
           { required: true, validator: validateAllocatedAmount, trigger: 'blur' }
         ]
+      },
+      // 请求头
+      headers: { 'Authorization': getToken() },
+      uploadDialog: {
+        visible: false,
+        loading: false,
+        form: { file: [] },
+        rules: { file: [{ required: true, message: '请选择文件', trigger: 'blur' }] }
       }
     }
+  },
+  computed: {
+    ...mapGetters(['baseApi'])
   },
   watch: {
     'crud.query.contractAreaCode'(val) {
@@ -1204,6 +1259,89 @@
           })
         })
       }
+    },
+    // 开启导入弹窗
+    uploadDialogOpen() {
+      const that = this
+      that.uploadDialog.visible = true
+    },
+    // 关闭导入弹窗
+    uploadDialogClose() {
+      const that = this
+      that.$refs.contractUpload.clearFiles()
+      that.$refs.uploadForm.clearValidate()
+      that.uploadDialog.visible = false
+      that.uploadDialog.loading = false
+      that.uploadDialog.form = { file: [] }
+    },
+    // 提交导入信息
+    uploadDialogSubmit() {
+      const that = this
+      that.uploadDialog.loading = true
+      that.$refs.uploadForm.validate(valid => {
+        if (valid) {
+          that.$refs.contractUpload.submit()
+        } else {
+          that.uploadDialog.loading = false
+        }
+      })
+    },
+    // 文件校验
+    beforeUpload(file) {
+      const that = this
+      let isLt2M = true
+      isLt2M = file.size / 1024 / 1024 < 50
+      if (!isLt2M) {
+        that.$message.error('上传文件大小不能超过 50MB!')
+      }
+      return isLt2M
+    },
+    // 监听文件变更
+    handleChange(_, fileList) {
+      const that = this
+      that.uploadDialog.form.file = fileList
+      that.$refs.uploadForm.validateField('file')
+    },
+    // 监听文件移除
+    handleRemove(_, fileList) {
+      const that = this
+      that.uploadDialog.form.file = fileList
+      that.$refs.uploadForm.validateField('file')
+    },
+    // 监听导入成功
+    handleSuccess(res, file, fileList) {
+      const that = this
+
+      if (res.errCode === 0) {
+        that.$notify({
+          title: '导入成功',
+          type: 'success',
+          duration: 3000
+        })
+        that.uploadDialogClose()
+        that.crud.toQuery()
+      } else {
+        that.$notify({
+          title: '导入失败',
+          type: 'error',
+          message: res.errMsg,
+          duration: 5000,
+          dangerouslyUseHTMLString: true
+        })
+        that.uploadDialog.loading = false
+        that.$refs.contractUpload.clearFiles()
+      }
+    },
+    // 监听导入失败
+    handleError(e, file, fileList) {
+      const that = this
+      const msg = JSON.parse(e.message)
+      that.$notify({
+        title: msg.message || '上传失败',
+        type: 'error',
+        duration: 2500
+      })
+      that.uploadDialog.loading = false
     }
   }
 }

--
Gitblit v1.9.3