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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!--
* 上传器
-->
<script setup lang="ts">
import { ref, reactive, computed, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import { useMessage } from 'src/common/hooks';
import { getObjectURL } from './utils';
var XLSX = require('xlsx');
const { t } = useI18n();
const { warn } = useMessage();
interface IDetailProps {
bgColor?: string; // 背景色
multiple?: boolean; // 多选
accept?: string; // 上传类型 'image/*'
dark?: boolean; // 启用深色背景
noShadow?: boolean; // 阴影
}
const props = withDefaults(defineProps<IDetailProps>(), {
bgColor: '#96C0CE',
multiple: false,
accept: '',
dark: false,
noShadow: false,
});
const emit = defineEmits<{
(e: 'onOk', value: any): void;
}>();
const darkDeep = '#222831';
const darkLight = '#393E46';
const imageTypeList = ['png', 'jpg', 'gif', 'svg'];
const excelTypeList = ['xlsx', 'xls'];
const otherTypeList = ['pdf'];
const viewModel = ref(false);
const fileType = ref('');
const viewDialogTitle = ref('');
const state = reactive({
fileList: [] as any[],
viewTableHtml: null as any,
});
const canPreview = computed(() => (type: any) => {
const viewTypeList = imageTypeList.concat(excelTypeList, otherTypeList);
return viewTypeList.includes(type);
});
const totalSize = computed(() => {
const total = state.fileList.reduce((a, b) => a + b.size, 0);
let unit: string; // 单位
let size: number | string = 0;
if (total < 1000) {
unit = 'B';
size = total.toFixed(3);
} else {
const KB_size = total * 0.0009765625;
if (KB_size < 1000) {
unit = 'KB';
size = KB_size.toFixed(3);
} else {
const MB_size = KB_size * 0.0009765625;
if (MB_size < 1000) {
unit = 'MB';
size = MB_size.toFixed(2);
} else {
const GB_size = MB_size * 0.0009765625;
unit = 'GB';
size = GB_size.toFixed(2);
}
}
}
return size + unit;
});
function selectFile() {
let _input: HTMLInputElement = document.createElement('input'); // 创建一个隐藏的input
_input.setAttribute('type', 'file');
if (props.multiple) {
_input.setAttribute('multiple', true); // 多选
}
// _input.setAttribute('accept', 'image/*'); // 上传类型
if (props.accept) {
_input.setAttribute('accept', props.accept);
}
_input.onchange = function (e: any) {
let arr = [] as any[];
for (let i = 0; i < e.target.files.length; i++) {
const _file = e.target.files[i];
const typeArr = _file.name.split('.');
const type = typeArr.length > 1 ? typeArr[typeArr.length - 1] : '';
let unit: string; // 单位
let size: number | string = 0;
if (_file.size < 1000) {
unit = 'B';
size = _file.size.toFixed(3);
} else {
const KB_size = _file.size * 0.0009765625;
if (KB_size < 1000) {
unit = 'KB';
size = KB_size.toFixed(3);
} else {
const MB_size = KB_size * 0.0009765625;
if (MB_size < 1000) {
unit = 'MB';
size = MB_size.toFixed(2);
} else {
const GB_size = MB_size * 0.0009765625;
if (GB_size <= 2) {
unit = 'GB';
size = GB_size.toFixed(2);
} else {
const msg = '文件过大,请选择不超过2GB的文件';
warn(msg);
console.warn(msg);
return;
}
}
}
}
arr.push({
file: _file,
name: _file.name,
sizeFormat: size + unit,
fileType: type,
size: _file.size,
});
}
nextTick(() => {
state.fileList = state.fileList.concat(arr);
});
};
_input.click();
}
function removeItem(index: number) {
state.fileList.splice(index, 1);
}
function removeAll() {
state.fileList = [];
}
function Ok() {
let list = [];
for (const item of state.fileList) {
list.push(item);
}
emit('onOk', list);
}
// 预览
function onView(data: any) {
if (excelTypeList.includes(data.fileType)) {
fileType.value = data.fileType;
viewDialogTitle.value = data.name;
viewModel.value = true;
void viewXlsx(data);
} else {
const url = getObjectURL(data.file);
window.open(url);
window.URL.revokeObjectURL(url);
}
}
async function viewXlsx(data: any) {
const file = data.file;
const _data = await file.arrayBuffer();
const wb = XLSX.read(_data);
const ws = wb.Sheets[wb.SheetNames[0]];
const ele = XLSX.utils.sheet_to_html(ws, { id: 'tabeller' });
state.viewTableHtml = ele;
}
function closeViewDialog() {
viewModel.value = false;
fileType.value = '';
state.viewTableHtml = null;
}
</script>
<template>
<div
class="com-uploader"
:class="{ 'shadow-4': !props.noShadow }"
:style="{
borderColor: props.dark ? darkDeep : props.bgColor,
backgroundColor: props.dark ? darkLight : '#ffffff',
}"
>
<div
class="uploader-title"
:style="{ backgroundColor: props.dark ? darkDeep : props.bgColor }"
>
<div class="icon">
<q-btn
v-if="state.fileList.length > 0"
style="width: 28px"
class="q-mr-xs"
dense
size="12px"
label="OK"
:title="t('Confirm upload')"
:style="{
backgroundColor: props.dark ? props.bgColor : '#fff',
color: props.dark ? darkLight : props.bgColor,
}"
@click="Ok"
/>
<q-btn
v-if="state.fileList.length > 0"
dense
flat
icon="bi-trash"
size="12px"
:title="t('clear all')"
:style="{ color: props.dark ? props.bgColor : '#fff' }"
@click="removeAll"
/>
<span
v-if="state.fileList.length > 0"
:style="{
color: props.dark ? props.bgColor : '#fff',
}"
class="total-file-size"
>{{ totalSize }}</span
>
</div>
<div class="btn">
<q-btn
square
dense
push
icon="add"
size="12px"
:title="t('Select file')"
:label="t('Select file')"
:style="{
backgroundColor: props.dark ? props.bgColor : '#fff',
color: props.dark ? darkLight : props.bgColor,
}"
@click="selectFile"
/>
</div>
</div>
<div class="uploader-content">
<div class="uploader-main-content">
<div
class="file-item"
:style="{ borderColor: props.bgColor }"
v-for="(item, index) in state.fileList"
:key="index"
>
<div
class="name"
:title="item.name + ' ' + item.sizeFormat"
:style="{ color: props.bgColor }"
>
<span class="file-name">{{ item.name }}</span>
<span class="file-size"> {{ item.sizeFormat }}</span>
</div>
<div class="btns">
<div>
<q-btn
v-if="canPreview(item.fileType)"
flat
dense
size="12px"
:title="t('preview')"
icon="bi-eye"
:style="{ color: props.bgColor }"
@click="onView(item)"
/>
</div>
<div>
<q-btn
flat
dense
size="12px"
:title="t('remove from list')"
icon="bi-x-circle"
:style="{ color: props.bgColor }"
@click="removeItem(index)"
/>
</div>
</div>
</div>
</div>
</div>
</div>
<q-dialog v-model="viewModel" persistent>
<q-card
style="min-height: 600px; min-width: 600px; max-width: 1400px"
class="column no-wrap"
>
<q-card-section class="row items-center q-pb-none">
<div class="text-h6">{{ viewDialogTitle }}</div>
<q-space />
<q-btn icon="close" flat round dense @click="closeViewDialog" />
</q-card-section>
<q-card-section class="q-pt-none overflow-auto" style="flex: 1">
<div v-if="excelTypeList.includes(fileType)">
<div v-html="state.viewTableHtml" class="table-view"></div>
</div>
</q-card-section>
</q-card>
</q-dialog>
</template>
<style lang="scss" scoped>
// 上传器
.com-uploader {
width: 100%;
min-width: 400px;
height: 100%;
min-height: 200px;
border-radius: 4px;
box-sizing: border-box;
border: 1px solid;
display: flex;
flex-flow: column nowrap;
// box-shadow: rgba(50, 50, 93, 0.25) 0px 6px 12px -2px,
// rgba(0, 0, 0, 0.3) 0px 3px 7px -3px;
.total-file-size {
display: inline-block;
font-size: 12px;
}
.uploader-title {
height: 46px;
padding: 8px;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
}
.uploader-content {
flex: 1;
overflow: auto;
}
.uploader-main-content {
display: grid;
grid-template-columns: 100%;
row-gap: 4px;
margin: 4px;
.file-item {
height: 40px;
box-sizing: border-box;
border-bottom: 2px solid;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
.name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
.file-name {
font-weight: bold;
}
.file-size {
font-size: 12px;
}
}
.btns {
display: grid;
grid-template-columns: repeat(2, 1fr);
column-gap: 2px;
> div {
width: 28px;
}
}
}
}
// .dialog-box {
// // min-width: 600px;
// min-height: 600px;
// }
}
</style>