Skip to content

Commit 3cd5137

Browse files
authored
feat: ✨ Upload 支持文件重传
1 parent 6f58e72 commit 3cd5137

4 files changed

Lines changed: 90 additions & 48 deletions

File tree

docs/component/upload.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ const action: string = 'https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86e
9797
></wd-upload>
9898
```
9999

100+
## 关闭预览点击文件替换
101+
102+
上传组件可通过设置 `use-preview` 来关闭文件预览并启用点击文件替换。
103+
104+
```html
105+
<wd-upload
106+
:file-list="fileList"
107+
:use-preview="false"
108+
action="https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86eb04dc1e8fd64865cb5/upload"
109+
@change="handleChange"
110+
></wd-upload>
111+
```
112+
100113
## 拦截预览图片操作
101114

102115
设置 `before-preview` 函数,在用户点击图片进行预览时,会执行 `before-preview` 函数,接收 { file: 预览文件, index: 当前预览的下标, imgList: 所有图片地址列表, resolve },通过 `resolve` 函数告知组件是否确定通过,`resolve` 接受 1 个 boolean 值,`resolve(true)` 表示选项通过,`resolve(false)` 表示选项不通过,不通过时不会执行预览图片操作。
@@ -601,6 +614,7 @@ const action: string = 'https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86e
601614
| header | 设置上传的请求头部 | object | - | - | - |
602615
| multiple | 是否支持多选文件 | boolean | - | - | - |
603616
| disabled | 是否禁用 | boolean | - | false | - |
617+
| use-preview | 是否点击已上传的图片或视频可预览,值为false的情况下再次弹出上传 | boolean | - | true | 1.3.15 |
604618
| limit | 最大允许上传个数 | number | - | - | - |
605619
| show-limit-num | 限制上传个数的情况下,是否展示当前上传的个数 | boolean | - | false | - |
606620
| max-size | 文件大小限制,单位为`byte` | number | - | - | - |

src/pages/upload/Index.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<demo-block title="最大上传数限制">
1212
<wd-upload :file-list="fileList3" :limit="3" :action="action" @change="handleChange3"></wd-upload>
1313
</demo-block>
14+
<demo-block title="关闭预览点击文件替换">
15+
<wd-upload accept="image" :use-preview="false" v-model:file-list="fileList17" image-mode="aspectFill" :action="action"></wd-upload>
16+
</demo-block>
1417
<demo-block title="拦截预览图片操作">
1518
<wd-upload :file-list="fileList4" :action="action" @change="handleChange4" :before-preview="beforePreview"></wd-upload>
1619
</demo-block>
@@ -121,6 +124,11 @@ const fileList16 = ref<UploadFile[]>([
121124
name: 'panda'
122125
}
123126
])
127+
const fileList17 = ref<UploadFile[]>([
128+
{
129+
url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/panda.jpg'
130+
}
131+
])
124132
125133
const upload14 = ref<UploadInstance>()
126134

src/uni_modules/wot-design-uni/components/wd-upload/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ export const uploadProps = {
316316
* 类型:boolean
317317
*/
318318
autoUpload: makeBooleanProp(true),
319+
/**
320+
* 是否点击已上传的图片或视频可预览,值为false的情况下再次弹出上传
321+
* 类型:boolean
322+
*/
323+
usePreview: makeBooleanProp(true),
319324
/**
320325
* 自定义上传文件的请求方法
321326
* 类型:UploadMethod

src/uni_modules/wot-design-uni/components/wd-upload/wd-upload.vue

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ function getImageInfo(img: string) {
325325
* @description 初始化文件数据
326326
* @param {Object} file 上传的文件
327327
*/
328-
function initFile(file: ChooseFile) {
328+
function initFile(file: ChooseFile, currentIndex?: number) {
329329
const { statusKey } = props
330330
// 状态初始化
331331
const initState: UploadFileItem = {
@@ -338,8 +338,11 @@ function initFile(file: ChooseFile) {
338338
url: file.path,
339339
percent: 0
340340
}
341-
342-
uploadFiles.value.push(initState)
341+
if (typeof currentIndex === 'number') {
342+
uploadFiles.value.splice(currentIndex, 1, initState)
343+
} else {
344+
uploadFiles.value.push(initState)
345+
}
343346
if (props.autoUpload) {
344347
startUploadFiles()
345348
}
@@ -442,7 +445,7 @@ function handleProgress(res: UniApp.OnProgressUpdateResult, file: UploadFileItem
442445
/**
443446
* @description 选择文件的实际操作,将chooseFile自己用promise包了一层
444447
*/
445-
function onChooseFile() {
448+
function onChooseFile(currentIndex?: number) {
446449
const { multiple, maxSize, accept, sizeType, limit, sourceType, compressed, maxDuration, camera, beforeUpload } = props
447450
// 文件选择
448451
chooseFile({
@@ -470,7 +473,7 @@ function onChooseFile() {
470473
const imageInfo = await getImageInfo(file.path)
471474
file.size = imageInfo.width * imageInfo.height
472475
}
473-
Number(file.size) <= maxSize ? initFile(file) : emit('oversize', { file })
476+
Number(file.size) <= maxSize ? initFile(file, currentIndex) : emit('oversize', { file })
474477
}
475478
}
476479
@@ -495,7 +498,7 @@ function onChooseFile() {
495498
/**
496499
* @description 选择文件,内置拦截选择操作
497500
*/
498-
function handleChoose() {
501+
function handleChoose(index?: number) {
499502
if (props.disabled) return
500503
const { beforeChoose } = props
501504
@@ -504,11 +507,11 @@ function handleChoose() {
504507
beforeChoose({
505508
fileList: uploadFiles.value,
506509
resolve: (isPass: boolean) => {
507-
isPass && onChooseFile()
510+
isPass && onChooseFile(index)
508511
}
509512
})
510513
} else {
511-
onChooseFile()
514+
onChooseFile(index)
512515
}
513516
}
514517
@@ -617,65 +620,77 @@ function handlePreviewVieo(index: number, lists: UploadFileItem[]) {
617620
}
618621
619622
function onPreviewImage(file: UploadFileItem) {
620-
const { beforePreview } = props
623+
const { beforePreview, usePreview } = props
621624
const lists = uploadFiles.value.filter((file) => isImage(file))
622625
const index: number = lists.findIndex((item) => item.url === file.url)
623-
if (beforePreview) {
624-
beforePreview({
625-
file,
626-
index,
627-
imgList: lists.map((file) => file.url),
628-
resolve: (isPass: boolean) => {
629-
isPass &&
630-
handlePreviewImage(
631-
index,
632-
lists.map((file) => file.url)
633-
)
634-
}
635-
})
626+
if (usePreview) {
627+
if (beforePreview) {
628+
beforePreview({
629+
file,
630+
index,
631+
imgList: lists.map((file) => file.url),
632+
resolve: (isPass: boolean) => {
633+
isPass &&
634+
handlePreviewImage(
635+
index,
636+
lists.map((file) => file.url)
637+
)
638+
}
639+
})
640+
} else {
641+
handlePreviewImage(
642+
index,
643+
lists.map((file) => file.url)
644+
)
645+
}
636646
} else {
637-
handlePreviewImage(
638-
index,
639-
lists.map((file) => file.url)
640-
)
647+
handleChoose(index)
641648
}
642649
}
643650
644651
function onPreviewVideo(file: UploadFileItem) {
645-
const { beforePreview } = props
652+
const { beforePreview, usePreview } = props
646653
const lists = uploadFiles.value.filter((file) => isVideo(file))
647654
const index: number = lists.findIndex((item) => item.url === file.url)
648-
if (beforePreview) {
649-
beforePreview({
650-
file,
651-
index,
652-
imgList: [],
653-
resolve: (isPass: boolean) => {
654-
isPass && handlePreviewVieo(index, lists)
655-
}
656-
})
655+
if (usePreview) {
656+
if (beforePreview) {
657+
beforePreview({
658+
file,
659+
index,
660+
imgList: [],
661+
resolve: (isPass: boolean) => {
662+
isPass && handlePreviewVieo(index, lists)
663+
}
664+
})
665+
} else {
666+
handlePreviewVieo(index, lists)
667+
}
657668
} else {
658-
handlePreviewVieo(index, lists)
669+
handleChoose(index)
659670
}
660671
}
661672
662673
function onPreviewFile(file: UploadFileItem) {
663-
const { beforePreview } = props
674+
const { beforePreview, usePreview } = props
664675
const lists = uploadFiles.value.filter((file) => {
665676
return !isVideo(file) && !isImage(file)
666677
})
667678
const index: number = lists.findIndex((item) => item.url === file.url)
668-
if (beforePreview) {
669-
beforePreview({
670-
file,
671-
index,
672-
imgList: [],
673-
resolve: (isPass: boolean) => {
674-
isPass && handlePreviewFile(file)
675-
}
676-
})
679+
if (usePreview) {
680+
if (beforePreview) {
681+
beforePreview({
682+
file,
683+
index,
684+
imgList: [],
685+
resolve: (isPass: boolean) => {
686+
isPass && handlePreviewFile(file)
687+
}
688+
})
689+
} else {
690+
handlePreviewFile(file)
691+
}
677692
} else {
678-
handlePreviewFile(file)
693+
handleChoose(index)
679694
}
680695
}
681696

0 commit comments

Comments
 (0)