lex-xin 4 лет назад
Родитель
Сommit
c54f62b098
2 измененных файлов с 95 добавлено и 53 удалено
  1. 66 0
      src/utils/downLoadFile.js
  2. 29 53
      src/views/businessManager/shopManager/shopList.vue

+ 66 - 0
src/utils/downLoadFile.js

@@ -0,0 +1,66 @@
+import axios from 'axios'
+import qs from 'qs'
+import {
+  getToken
+} from '@/utils/auth'
+import load from '@/utils/loading'
+
+/**
+ * 导出模板
+ * @param {*} params
+ * params: {
+ *      url: xxx,
+ *      params: {},
+ *      fileName: xxx.xls
+ * }
+ */
+export const Export = (that, params) => {
+    // 报表导出
+    let url = params.url
+    const options = {
+        method: "get",
+        headers: {
+            Authorization: getToken()
+        },
+        params: params.params,
+        url,
+        responseType: "blob"
+    };
+    that.$confirm("您确定下载模板", "提示", {
+            confirmButtonText: "确定",
+            cancelButtonText: "取消",
+            type: "warning"
+        })
+        .then(() => {
+            load.startLoading()
+            axios(options).then(res => {
+                let blob = new Blob([res.data], {
+                // type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
+                type: "application/vnd.ms-excel;charset=utf-8"
+                //word文档为application/msword,pdf文档为application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
+                });
+                let text = (new Response(blob)).text()
+                text.then(res => {
+                // 判断是否报错
+                if (res.indexOf('code') != -1) {
+                    let json = JSON.parse(res)
+                    that.$message.error(json.msg)
+                } else {
+                    let objectUrl = URL.createObjectURL(blob);
+                    let link = document.createElement("a");
+                    let fname = params.fileName || "导出文件.xlsx"; //下载文件的名字
+                    link.href = objectUrl;
+                    link.setAttribute("download", fname);
+                    document.body.appendChild(link);
+                    link.click();
+                }
+                })
+
+                load.endLoading();
+            }).catch(error => {
+                that.$message.error('下载失败,请联系管理员');
+                load.endLoading();
+            });
+        })
+        .catch(() => {});
+}

+ 29 - 53
src/views/businessManager/shopManager/shopList.vue

@@ -5,7 +5,15 @@
     </h2>
     <div class="m-core">
       <el-button class="btn-primary" @click="onShopOperation('create')" v-permission="'/shopOperation'">添加</el-button>
-      <el-button class="btn-primary">批量导入</el-button>
+      <el-upload
+        action="import/goods"
+        :show-file-list="false"
+        :before-upload="beforeUpload"
+        accept=".xlsx,.xls"
+        :limit="1"
+        :on-success="handleSuccess">
+        <el-button class="btn-primary" @click="onImport">批量导入</el-button>
+      </el-upload>
       <el-button class="btn-primary" @click="onDownload">下载模板</el-button>
       <!-- 搜索类型 -->
       <!-- <el-form :inline="true"
@@ -138,11 +146,7 @@ import pagination from '@/components/Pagination/index'
 import { goodsQuery, goodsUpdate, goodsDelete } from '@/api/businessManager'
 import store from '@/store'
 import Tooltip from '@/components/Tooltip/index'
-import axios from 'axios'
-import qs from 'qs'
-import { getToken } from '@/utils/auth'
-import load from '@/utils/loading'
-import dayjs from 'dayjs'
+import { Export } from '@/utils/downLoadFile'
 export default {
   components: { pagination, Tooltip },
   name: 'shopList',
@@ -237,55 +241,27 @@ export default {
         query: { paramInfo, pageInfo }
       })
     },
-    onDownload() { // 下载模板
+    beforeUpload() {
+      console.log(file.type)
+      const isJPG = file.type === '.xlsx' || file.type === '.xls';
+      // const isLt2M = file.size / 1024 / 1024 < 2;
+
+      if (!isJPG) {
+        this.$message.error('上传头像图片只能是 JPG 格式!');
+      }
+      return isJPG;
+    },
+    handleSuccess() { // 导入商品
       // 报表导出
-      let url = "/api-web/import/downloadTemplate";
-      const options = {
-        method: "get",
-        headers: {
-          Authorization: getToken()
-        },
-        params: { templateType: 'goods' },
-        url,
-        responseType: "blob"
-      };
-      this.$confirm("您确定下载模板", "提示", {
-        confirmButtonText: "确定",
-        cancelButtonText: "取消",
-        type: "warning"
+      
+    },
+    onDownload() { // 下载模板
+      // // 报表导出
+      Export(this, {
+        url: "/api-web/import/downloadTemplate",
+        params: {templateType: 'goods'},
+        fileName: '商品导入模板.xlsx'
       })
-        .then(() => {
-          load.startLoading()
-          axios(options).then(res => {
-            let blob = new Blob([res.data], {
-              // type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
-              type: "application/vnd.ms-excel;charset=utf-8"
-              //word文档为application/msword,pdf文档为application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
-            });
-            let text = (new Response(blob)).text()
-            text.then(res => {
-              // 判断是否报错
-              if (res.indexOf('code') != -1) {
-                let json = JSON.parse(res)
-                this.$message.error(json.msg)
-              } else {
-                let objectUrl = URL.createObjectURL(blob);
-                let link = document.createElement("a");
-                let fname = "商品模板" + new Date().getTime() + '.xls'; //下载文件的名字
-                link.href = objectUrl;
-                link.setAttribute("download", fname);
-                document.body.appendChild(link);
-                link.click();
-              }
-            })
-
-            load.endLoading();
-          }).catch(error => {
-            this.$message.error('下载失败,请联系管理员');
-            load.endLoading();
-          });
-        })
-        .catch(() => { });
     }
   }
 }