1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * @Author: your name
 * @Date: 2020-09-14 15:13:28
 * @LastEditTime: 2020-09-24 11:39:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \webadmin\src\utils\export.js
 */
import {
  MessageBox,
  Message,
  Notification
} from 'element-ui'
import axios from 'axios'
// 导出
export function outExcel(name, url, data, info) {
  MessageBox.confirm(name, "提示", {
      type: "warning"
    }).then(async () => {
      Notification.info({
        title: '提示',
        message: '正在导出,请稍后 <i class="el-icon-loading" style="font-size:20px"></i>',
        dangerouslyUseHTMLString: true,
        position: 'bottom-left',
        duration: 0
      })
      const time = data;
      let formData = new FormData();
      if (time != null) {
        for (var p in time) {
          formData.append(p, time[p]);
        }
      }
      axios({
          method: "post",
          url: process.env.VUE_APP_BASE_API + url,
          data: formData,
          headers: {
            Authorization: "token " + JSON.parse(sessionStorage.getItem("UserInfo")).token,
            "Content-Type": "multipart/form-data"
          },
          responseType: "blob"
        })
        .then(data => {
 
          if (data.data.type === "application/json") {
            var reader = new FileReader();
            reader.onloadend = function () {
              let res = JSON.parse(reader.result);
              if (res && res.msg) {
                Message.warning(res.msg + "," + res.data);
                setTimeout(() => {
                  Notification.closeAll()
                }, 1000);
              }
            };
            reader.readAsText(data.data);
            return;
          }
 
          let url = window.URL.createObjectURL(new Blob([data.data]));
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", info);
          document.body.appendChild(link);
          link.click();
          if (info.indexOf('模板') !== -1) {
            Message.success('模板下载成功')
            setTimeout(() => {
              Notification.closeAll()
            }, 1000);
          } else {
            Notification.closeAll()
            Notification.success({
              title: '提示',
              message: '导出成功',
              position: 'bottom-left',
              duration: 2000
            })
            setTimeout(() => {
              Notification.closeAll()
            }, 2000);
            // Message.success('导出成功')
          }
        })
        .catch(() => {
          if (info.indexOf('模板') !== -1) {
            Message.error('模板下载失败')
          } else {
            Message.error('导出失败')
           
          }
        });
    })
    .catch(() => {
      return false;
    });
}