exportData.vue
5.64 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<template>
<el-dialog
:title="$t('allPages.loadData')"
:lock-scroll="false"
:visible="dialogVisible"
width="30%"
@close="closeClick"
class="upDatadialog"
>
<span>{{ $t('allPages.loadType') }}</span>
<el-checkbox
:indeterminate="isIndeterminate"
v-model="checkAll"
@change="handleCheckAllChange"
class="allCheck"
>{{ $t('allPages.all') }}</el-checkbox>
<div style="margin: 15px 0;"></div>
<el-checkbox-group
class="export-checkbox"
v-model="checkedTypes"
@change="handlecheckedTypesChange"
>
<el-checkbox v-for="item in exportTypes" :label="item" :key="item">{{ i18nFomatter(item) }}</el-checkbox>
</el-checkbox-group>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">{{ $t('button.cancel') }}</el-button>
<el-button type="primary" @click="exportHandle">{{ $t('button.confirm') }}</el-button>
</span>
</el-dialog>
</template>
<script>
export default {
data() {
return {
dialogVisible: false,
isIndeterminate: false,
checkAll: false,
checkedTypes: [],
exportTypes: ["Image", "Excel"],
propData: {},
isCallback: false
};
},
methods: {
dialogInit(data, isCallback = false) {
if (data instanceof Array) {
this.propData = data;
} else {
this.propData = [];
this.propData.push(data);
}
this.isCallback = isCallback;
this.dialogVisible = true;
},
i18nFomatter(type) {
let langeType = "allPages." + type;
return this.$t(langeType);
},
handleCheckAllChange(val) {
this.checkedTypes = val ? this.exportTypes : [];
this.isIndeterminate = false;
},
handlecheckedTypesChange(value) {
let checkedCount = value.length;
this.checkAll = checkedCount === this.exportTypes.length;
this.isIndeterminate =
checkedCount > 0 && checkedCount < this.exportTypes.length;
},
exportHandle() {
const { isCallback, checkedTypes, propData } = this;
if (isCallback) {
for (let type of checkedTypes) {
if (type === "Image") {
propData.forEach(item => {
if (item.domId instanceof Array) {
item.domId.forEach((elemId, ix) => {
this.saveFile(elemId, item.title instanceof Array ? item.title[ix] : item.title);
})
} else {
this.saveFile(item.domId, item.title);
}
});
} else {
this.$emit("downloadChange", type);
}
}
return;
}
let exportChartType = "";
let language = localStorage.getItem("lang");
if (language) {
const languageObj = {
mall_CN: "zh_CN",
zh_CN: "zh_CN",
en_US: "en_US",
zh_TW: "zh_TW"
};
language = languageObj[language] || window.navigator.language;
}
for (let type of checkedTypes) {
propData.forEach(item => {
const { domId, title, data } = item;
const { url, params } = data;
const { apiUrl } = window._vionConfig;
if (type === "Image") {
if (domId instanceof Array) {
domId.forEach((elemId, ix) => {
this.saveFile(elemId, item.title instanceof Array ? item.title[ix] : item.title);
})
} else {
this.saveFile(item.domId, item.title);
}
}
if (type === "Excel") {
const downloadUrl = `${apiUrl}${url}?${params}&localLanguage=${language}&authorization=${this.$cookie.get(
"atoken"
)}`;
window.open(downloadUrl);
}
});
}
},
fixType(type) {
type = type.toLowerCase().replace(/jpg/i, "jpeg");
let suffix = type.match(/png|jpeg|bmp|gif/)[0];
return "image/" + suffix;
},
saveFile(domId, title) {
let myChart = {};
let chartDom = document.getElementById(domId);
if (this.$echarts.getInstanceByDom(chartDom)) {
myChart = this.$echarts.getInstanceByDom(chartDom);
} else {
myChart = this.$echarts.init(chartDom);
}
let type = "png";
let imgData = myChart.getDataURL({
type: "png",
pixelRatio: 2,
backgroundColor: "#fff"
});
imgData = imgData.replace(this.fixType(type), "image/octet-stream");
let filename = title + "." + type;
let saveLink = document.createElementNS(
"http://www.w3.org/1999/xhtml",
"a"
);
saveLink.href = imgData;
saveLink.download = filename;
let mouseEvt = document.createEvent("MouseEvents");
mouseEvt.initMouseEvent(
"click",
true,
false,
window,
0,
0,
0,
0,
0,
false,
false,
false,
false,
0,
null
);
saveLink.dispatchEvent(mouseEvt);
},
closeClick() {
this.checkAll = false;
this.isIndeterminate = false;
this.checkedTypes = [];
this.dialogVisible = false;
}
}
};
</script>
<style>
.upDatadialog {
text-align: left;
}
.upDatadialog .el-dialog__body {
padding: 10px 20px !important;
font-size: 17px !important;
}
.upDatadialog .el-dialog__header {
background-color: #409eff;
padding: 10px 20px;
}
.upDatadialog .el-dialog__title {
color: #fff;
}
.upDatadialog .el-dialog__headerbtn .el-dialog__close {
color: #fff;
}
.upDatadialog .el-dialog__footer {
padding: 0px 15px 15px;
}
.allCheck {
position: absolute;
left: 150px;
}
.export-checkbox {
font-size: 0px;
margin-left: 130px;
}
</style>