ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { computed, nextTick, onMounted, reactive, ref, unref, watch } from 'vue';
import VueSimpleUploader from 'vue-simple-uploader';
import 'vue-simple-uploader/dist/style.css';
 
import { useGlobSetting } from '@jnpf/hooks';
import { buildBitUUID } from '@jnpf/utils';
 
import { $t } from '@vben/locales';
import { useAccessStore } from '@vben/stores';
 
import { globalShareState } from '@vben-core/shared/global-state';
 
import { message } from 'ant-design-vue';
import SparkMD5 from 'spark-md5';
 
import { units, uploadFileProps } from '../props';
import FileItem from './FileItem.vue';
 
const props = defineProps(uploadFileProps);
 
const emit = defineEmits(['fileSuccess']);
 
const { chunkMerge } = globalShareState.getApi();
 
const Uploader = VueSimpleUploader.Uploader;
const UploaderBtn = VueSimpleUploader.UploaderBtn;
const UploaderUnsupport = VueSimpleUploader.UploaderUnsupport;
const UploaderList = VueSimpleUploader.UploaderList;
const accessStore = useAccessStore();
const globSetting = useGlobSetting();
const uploaderRef = ref<any>(null);
const uploaderBtnRef = ref<any>(null);
const options = reactive({
  // 服务器分片校验函数,秒传及断点续传基础
  checkChunkUploadedByResponse(chunk, message) {
    const objMessage = JSON.parse(message);
    if (objMessage.code === 200) {
      if (objMessage.data.uploaded) {
        return true;
      }
      const chunkNumbers = objMessage.data.chunkNumbers;
      return (chunkNumbers || []).includes(chunk.offset + 1);
    } else {
      return true;
    }
  },
  chunkSize: 1024 * 1024 * 5,
  headers: {
    Authorization: accessStore.accessToken,
  },
  maxChunkRetries: 5,
  query: {
    extension: '',
    fileType: '',
  },
  singleFile: props.limit === 1,
  target: `${globSetting.apiURL}/api/file/chunk`,
  testChunks: true, // 是否开启服务器分片校验
});
const messageKey = 'upload';
const attrs = ref({ accept: props.accept || '*' });
const statusText = {
  error: $t('component.upload.uploadError'),
  paused: $t('component.upload.paused'),
  success: $t('component.upload.uploadSuccess'),
  uploading: $t('component.upload.uploading'),
  waiting: $t('component.upload.waiting'),
};
 
const getAcceptText = computed(() => {
  let txt = '';
  if (props.accept.includes('image/*')) txt += `、${$t('component.upload.image')}`;
  if (props.accept.includes('video/*')) txt += `、${$t('component.upload.video')}`;
  if (props.accept.includes('audio/*')) txt += `、${$t('component.upload.audio')}`;
  if (props.accept.includes('.xls,.xlsx')) txt += '、excel';
  if (props.accept.includes('.doc,.docx')) txt += '、word';
  if (props.accept.includes('.pdf')) txt += '、pdf';
  if (props.accept.includes('.txt')) txt += '、txt';
  return txt.slice(1);
});
 
watch(
  () => props.accept,
  (val) => {
    attrs.value.accept = val || '*';
  },
);
 
defineExpose({ openUploader });
 
function openUploader() {
  uploaderBtnRef.value.$el.click();
}
// 上传前校验
function beforeUpload(file) {
  const isTopLimit = props.limit ? props.value.length >= props.limit : false;
  if (isTopLimit) {
    message.error({ content: $t('component.upload.fileMaxNumber', [props.limit]), key: messageKey });
    return false;
  }
  const isRightSize = true;
  if (props.fileSize) {
    const unitNum = units[props.sizeUnit];
    const isRightSize = file.size / unitNum < props.fileSize;
    if (!isRightSize) {
      message.error({ content: $t('component.upload.fileMaxSize', { size: props.fileSize, unit: props.sizeUnit }), key: messageKey });
      return isRightSize;
    }
  }
  const isAccept = checkAccept(file);
  if (!isAccept) {
    message.error({ content: $t('component.upload.fileTypeCheck', [unref(getAcceptText)]), key: messageKey });
    return isAccept;
  }
  return isRightSize && isAccept;
}
// 校验格式
function checkAccept(file) {
  if (!props.accept || props.accept === '*') return true;
  const extension = file.getExtension();
  const fileType = file.fileType;
  if (props.accept.includes(extension)) return true;
  if (props.accept.includes('image/*') && /image\/*/.test(fileType)) return true;
  if (props.accept.includes('video/*') && /video\/*/.test(fileType)) return true;
  if (props.accept.includes('audio/*') && /audio\/*/.test(fileType)) return true;
  return false;
}
function onFileAdded(file) {
  if (beforeUpload && typeof beforeUpload === 'function' && !beforeUpload(file)) {
    file.cancel();
    return false;
  }
  // 自定义状态
  file.customStatus = 'check';
  options.query.fileType = file.fileType;
  options.query.extension = file.getExtension();
  computeMD5(file);
}
/**
 * 计算md5,实现断点续传及秒传
 * @param file
 */
function computeMD5(file) {
  const fileReader = new FileReader();
  const blobSlice = File.prototype.slice || (File.prototype as any).mozSlice || (File.prototype as any).webkitSlice;
  let currentChunk = 0;
  const chunkSize = 10 * 1024 * 1000;
  const chunks = Math.ceil(file.size / chunkSize);
  const spark = new SparkMD5.ArrayBuffer();
 
  file.pause();
  loadNext();
 
  fileReader.addEventListener('load', (e) => {
    spark.append(e.target?.result);
    if (currentChunk < chunks) {
      currentChunk++;
      loadNext();
    } else {
      const md5 = spark.end();
      computeMD5Success(md5, file);
    }
  });
  // eslint-disable-next-line unicorn/prefer-add-event-listener
  fileReader.onerror = function () {
    message.error($t('component.upload.fileTypeCheck', [file.name]));
    file.cancel();
  };
 
  function loadNext() {
    const start = currentChunk * chunkSize;
    const end = Math.min(start + chunkSize, file.size);
    // eslint-disable-next-line unicorn/prefer-blob-reading-methods
    fileReader.readAsArrayBuffer(blobSlice.call(file.file, start, end));
  }
}
function computeMD5Success(md5, file) {
  file.uniqueIdentifier = md5 + buildBitUUID(); // 把md5值+随机码作为文件的识别码
  file.customStatus = 'uploading';
  file.resume(); // 开始上传
}
function onFileSuccess(_rootFile, file, response, _chunk) {
  const res = JSON.parse(response);
  if (res.code != 200) {
    message.error(res.msg);
    file.cancel();
    return;
  }
  setTimeout(() => {
    // 秒传 直接展示
    if (res.data.uploaded) {
      // 秒传结果
    } else if (res.data.merge) {
      // 需要合并
      handleSuccess(file);
    } else {
      // 上传错误
      file.cancel();
      message.error($t('component.upload.uploadError'));
    }
  }, 300);
}
function onFileProgress(_rootFile, _file, _chunk) {
  // console.log(`上传中 ${_file.name},chunk:${_chunk.startByte / 1024 / 1024} ~ ${_chunk.endByte / 1024 / 1024}`);
}
function onFileError(_rootFile, file, _response, _chunk) {
  file.cancel();
  message.error($t('component.upload.uploadError'));
}
function handleSuccess(file) {
  const query = {
    extension: file.getExtension(),
    fileName: file.name.replaceAll('#', ''),
    fileSize: file.size,
    fileType: file.getType(),
    folder: props.folder,
    identifier: file.uniqueIdentifier,
    pathType: props.pathType,
    sortRule: (props.sortRule || []).join(','),
    timeFormat: props.timeFormat,
    type: props.type,
  };
  chunkMerge(query)
    .then((res) => {
      // 自定义完成状态
      file.customCompleted = true;
      const data = {
        fileExtension: res.data.fileExtension,
        fileId: res.data.name,
        fileSize: res.data.fileSize,
        fileVersionId: res.data.fileVersionId,
        name: file.name.replaceAll('#', ''),
        url: res.data.url,
      };
      emit('fileSuccess', data);
      file.cancel();
    })
    .catch(() => {
      file.cancel();
    });
}
 
onMounted(() => {
  nextTick(() => {
    (window as any).uploader = uploaderRef.value?.uploader;
  });
});
</script>
 
<template>
  <Uploader
    ref="uploaderRef"
    :auto-start="false"
    :class="{ isFirst: value.length === 0 }"
    :file-status-text="statusText"
    :options="options"
    class="uploader-app"
    @file-added="onFileAdded"
    @file-error="onFileError"
    @file-progress="onFileProgress"
    @file-success="onFileSuccess">
    <UploaderUnsupport />
    <UploaderBtn id="file-uploader-btn" ref="uploaderBtnRef" :attrs="attrs">选择文件</UploaderBtn>
    <UploaderList>
      <template #default="{ fileList }">
        <div class="upload-file-list">
          <div v-for="file in fileList" :key="file.id" class="upload-file-list__item">
            <FileItem :class="`file_${file.id}`" :file="file" />
          </div>
        </div>
      </template>
    </UploaderList>
  </Uploader>
</template>
<style lang="scss">
.uploader-app {
  padding: 0;
  margin: 0;
 
  &.isFirst .upload-file-list {
    .upload-file-list__item {
      margin-top: 10px;
    }
  }
 
  .upload-file-list {
    .upload-file-list__item {
      margin-top: 5px;
      overflow: hidden;
      border-radius: 4px;
    }
 
    .uploader-file {
      height: 26px !important;
      line-height: 26px;
      border-bottom: none;
 
      &:hover {
        background-color: var(--selected-hover-bg);
      }
    }
  }
 
  .uploader-file-icon {
    &::before {
      content: '' !important;
    }
  }
}
 
/* 隐藏上传按钮 */
#file-uploader-btn {
  position: absolute;
  clip: rect(0, 0, 0, 0);
}
</style>