wupengfei
2025-03-10 6887129e91d32557c2b57178180329f46df09d12
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
<template>
  <nut-uploader
    v-if="$slots.default"
    v-model:file-list="innerFileList"
    multiple
    :media-type="['image']"
    :before-xhr-upload="beforeXhrUpload"
    @failure="handleFailure"
    :maximize="maximize"
  >
    <template #upload-icon>
      <slot name="upload-icon"></slot>
    </template>
    <template #default>
      <slot></slot>
    </template>
    <template #extra-img>
      <slot name="extra-img"></slot>
    </template>
  </nut-uploader>
  <nut-uploader
    v-else
    v-model:file-list="innerFileList"
    multiple
    :media-type="['image']"
    :before-xhr-upload="beforeXhrUpload"
    @failure="handleFailure"
    :maximize="maximize"
  >
    <template #upload-icon>
      <slot name="upload-icon"></slot>
    </template>
    <template #extra-img="extraImgProps">
      <slot name="extra-img" v-bind="extraImgProps"></slot>
    </template>
  </nut-uploader>
</template>
 
<script setup lang="ts">
import { ossUpload } from '@12333/utils/oss';
import { guid } from '@12333/utils';
import { FileItem } from '@nutui/nutui-taro/dist/types/__VUE/uploader/type';
import { UploadOptions } from '@nutui/nutui-taro/dist/types/__VUE/uploader/uploader';
import Taro from '@tarojs/taro';
 
defineOptions({
  name: 'Uploader',
});
 
type Props = {
  fileList: FileItem[];
  onMySuccess?: (file: FileItem) => any;
  limitFileSize?: number;
};
 
const props = withDefaults(defineProps<Props>(), {
  limitFileSize: 2,
});
 
const emit = defineEmits<{
  (e: 'update:fileList', list: FileItem[]): void;
}>();
 
const innerFileList = computed({
  get() {
    return props.fileList;
  },
  set(val) {
    emit('update:fileList', val);
  },
});
 
const maximize = computed(() => props.limitFileSize * 1024 * 1024);
 
const beforeXhrUpload = (taroUploadFile: any, options: UploadOptions) => {
  const current = innerFileList.value.find((x) => x.path === options.taroFilePath);
  const uploadTask = ossUpload({
    filePath: options.taroFilePath,
    fileType: options.fileType,
    name: options.name || 'file',
    customFileName: () => guid(),
    showLoading: false,
    success(response: { errMsg: any; statusCode: number; data: string }, ossRes) {
      options.onSuccess?.(response, options);
      current.name = ossRes.name;
      current.path = ossRes.path;
      current.url = ossRes.url;
      // innerFileList.value.map((x) => {
      //     if (x.uid === current.uid) {
      //       return {
      //         ...x,
      //         name: ossRes.name,
      //         path: ossRes.path,
      //         url: ossRes.url,
      //       };
      //     }
      //     return { ...x };
      //   })
      // emit('update:fileList', innerFileList.value);
      nextTick(() => {
        props.onMySuccess?.(innerFileList.value.find((x) => x.path === ossRes.path));
      });
    },
    fail(e: any) {
      console.log('e: ', e);
      options.onFailure?.(e, options);
    },
  });
 
  options.onStart?.(options);
  // uploadTask?.progress(
  //   (res: { progress: any; totalBytesSent: any; totalBytesExpectedToSend: any }) => {
  //     options.onProgress?.(res, options);
  //     // console.log('上传进度', res.progress);
  //     // console.log('已经上传的数据长度', res.totalBytesSent);
  //     // console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend);
  //   }
  // );
  // uploadTask.abort(); // 取消上传任务
};
 
function handleFailure(ev) {
  console.log('Failure ev: ', ev);
}
</script>