Skip to content

Commit fe712ba

Browse files
authored
feat(upload): support video preview (#3741)
* feat(upload): support video preview * docs: update base library version
1 parent 8bb5c49 commit fe712ba

4 files changed

Lines changed: 87 additions & 19 deletions

File tree

site/docs/getting-started.en-US.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ TDesign MiniProgram already supports using NPM to install third-party packages.
2626
npm i tdesign-miniprogram -S --production
2727
```
2828

29-
> After installation, npm needs to be built in WeChat developer tools: `tool - build npm`
29+
> After installation, npm needs to be built in WeChat developer tools: `tool - build npm`.(If `NPM packages not found` appears during the build, please go to the `project.config.json` file to add the `packNpmManually` and `packNpmRelationList` configuration items. For details, see [NPM Support](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html?search-key=npm))
30+
31+
> After the build is successful, check the box `Compile JS to ES5`
32+
> <br/>
33+
><img width="200" src="https://tdesign.gtimg.com/miniprogram/docs/getting-started.png" />
3034
3135
## Modify app.json
3236

@@ -78,4 +82,14 @@ Open [WeChat Developer Tools](https://mp.weixin.qq.com/debug/wxadoc/dev/devtools
7882

7983
## Base library version
8084

81-
Minimum base library version `^2.6.5`
85+
Minimum base library version `^2.12.0`
86+
87+
### Correspondence between component and basic library versions
88+
89+
| 组件 | API | 最低基础库 | 描述 |
90+
| ----- | ------ | ---------- | ---- |
91+
| Upload | [wx.previewMedia](https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.previewMedia.html) | 2.12.0 | - |
92+
| Upload | [wx.chooseMedia](https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseMedia.html) | 2.10.0 | - |
93+
| Upload | [wx.chooseMessageFile](https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseMessageFile.html) | 2.5.0 | - |
94+
| Navbar | [wx.getMenuButtonBoundingClientRect](https://developers.weixin.qq.com/miniprogram/dev/api/ui/menu/wx.getMenuButtonBoundingClientRect.html) | 2.1.0 | - |
95+

site/docs/getting-started.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ npm run dev
8181

8282
## 基础库版本
8383

84-
最低基础库版本`^2.6.5`
84+
最低基础库版本`^2.12.0`
85+
86+
### 组件与基础库版本对应关系
87+
88+
| 组件 | API | 最低基础库 | 描述 |
89+
| ----- | ------ | ---------- | ---- |
90+
| Upload | [wx.previewMedia](https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.previewMedia.html) | 2.12.0 | - |
91+
| Upload | [wx.chooseMedia](https://developers.weixin.qq.com/miniprogram/dev/api/media/video/wx.chooseMedia.html) | 2.10.0 | - |
92+
| Upload | [wx.chooseMessageFile](https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseMessageFile.html) | 2.5.0 | - |
93+
| Navbar | [wx.getMenuButtonBoundingClientRect](https://developers.weixin.qq.com/miniprogram/dev/api/ui/menu/wx.getMenuButtonBoundingClientRect.html) | 2.1.0 | - |
94+

src/upload/upload.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ export default class Upload extends SuperComponent {
5656
},
5757
};
5858

59-
onProofTap(e: any) {
60-
this.onFileClick(e);
61-
const { preview } = this.properties;
62-
if (!preview) return;
63-
const { index } = e.currentTarget.dataset;
64-
wx.previewImage({
65-
urls: this.data.customFiles.filter((file) => file.percent !== -1).map((file) => file.url),
66-
current: this.data.customFiles[index]?.url,
67-
});
68-
}
69-
7059
handleLimit(customFiles: UploadFile[], max: number) {
7160
if (max === 0) {
7261
max = Number.MAX_SAFE_INTEGER;
@@ -87,7 +76,7 @@ export default class Upload extends SuperComponent {
8776
this.triggerEvent('fail', err);
8877
}
8978

90-
onFileClick(e) {
79+
onFileClick(e: WechatMiniprogram.BaseEvent) {
9180
const { file } = e.currentTarget.dataset;
9281
this.triggerEvent('click', { file });
9382
}
@@ -226,6 +215,59 @@ export default class Upload extends SuperComponent {
226215
}
227216

228217
methods = {
218+
getPreviewMediaSources() {
219+
const previewMediaSources: WechatMiniprogram.MediaSource[] = [];
220+
this.data.customFiles.forEach((ele) => {
221+
const mediaSource: WechatMiniprogram.MediaSource = {
222+
url: ele.url,
223+
type: ele.type,
224+
poster: ele.thumb || undefined,
225+
};
226+
previewMediaSources.push(mediaSource);
227+
});
228+
229+
return previewMediaSources;
230+
},
231+
232+
onPreview(e: WechatMiniprogram.BaseEvent) {
233+
this.onFileClick(e);
234+
const { preview } = this.properties;
235+
236+
if (!preview) return;
237+
238+
const usePreviewMedia = this.data.customFiles.some((file: UploadFile) => file.type === 'video');
239+
if (usePreviewMedia) {
240+
this.onPreviewMedia(e);
241+
} else {
242+
this.onPreviewImage(e);
243+
}
244+
},
245+
246+
onPreviewImage(e: WechatMiniprogram.BaseEvent) {
247+
const { index } = e.currentTarget.dataset;
248+
const urls = this.data.customFiles.filter((file: UploadFile) => file.percent !== -1).map((file) => file.url);
249+
const current = this.data.customFiles[index]?.url;
250+
wx.previewImage({
251+
urls,
252+
current,
253+
fail() {
254+
wx.showToast({ title: '预览图片失败', icon: 'none' });
255+
},
256+
});
257+
},
258+
259+
onPreviewMedia(e: WechatMiniprogram.BaseEvent) {
260+
const { index: current } = e.currentTarget.dataset;
261+
const sources = this.getPreviewMediaSources();
262+
wx.previewMedia({
263+
sources,
264+
current,
265+
fail() {
266+
wx.showToast({ title: '预览视频失败', icon: 'none' });
267+
},
268+
});
269+
},
270+
229271
uploadFiles(files: UploadFile[]) {
230272
return new Promise((resolve) => {
231273
// 开始调用上传函数

src/upload/upload.wxml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
shape="{{imageProps && imageProps.shape || 'round'}}"
4141
webp="{{imageProps && imageProps.webp || false}}"
4242
showMenuByLongpress="{{imageProps && imageProps.showMenuByLongpress || false}}"
43-
bind:tap="onProofTap"
43+
bind:tap="onPreview"
4444
/>
4545
<video
4646
wx:if="{{file.type === 'video'}}"
@@ -51,7 +51,8 @@
5151
autoplay="{{false}}"
5252
objectFit="contain"
5353
data-file="{{file}}"
54-
bind:tap="onFileClick"
54+
data-index="{{index}}"
55+
bind:tap="onPreview"
5556
/>
5657
<!-- 失败重试 -->
5758
<view
@@ -149,7 +150,7 @@
149150
shape="{{imageProps && imageProps.shape || 'round'}}"
150151
webp="{{imageProps && imageProps.webp || false}}"
151152
showMenuByLongpress="{{imageProps && imageProps.showMenuByLongpress || false}}"
152-
bind:tap="onProofTap"
153+
bind:tap="onPreview"
153154
/>
154155
<video
155156
wx:if="{{file.type === 'video'}}"
@@ -160,7 +161,8 @@
160161
autoplay="{{false}}"
161162
objectFit="contain"
162163
data-file="{{file}}"
163-
bind:tap="onFileClick"
164+
data-index="{{index}}"
165+
bind:tap="onPreview"
164166
/>
165167
<!-- 失败重试 -->
166168
<view

0 commit comments

Comments
 (0)