Skip to content

Commit 0c74a9c

Browse files
authored
Merge pull request #15851 from heweishui/4.x
【harmony-hybrid】chooseMedia适配两种实现方式
2 parents 1a8693e + 8868be6 commit 0c74a9c

5 files changed

Lines changed: 306 additions & 32 deletions

File tree

examples/mini-program-example/src/pages/api/media/video/index.tsx

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -220,36 +220,6 @@ export default class Index extends React.Component {
220220
})
221221
},
222222
},
223-
{
224-
id: 'chooseMedium',
225-
inputData: {
226-
count: 9,
227-
mediaType: ['image'],
228-
sourceType: ['album', 'camera'],
229-
maxDuration: 30,
230-
sizeType: ['original', 'compressed'],
231-
camera: 'back',
232-
mediaId: '',
233-
takingSupported: false,
234-
editSupported: false,
235-
searchSupported: false,
236-
},
237-
func: (apiIndex, data) => {
238-
TestConsole.consoleTest('chooseMedium')
239-
// @ts-ignore
240-
Taro.chooseMedium({
241-
...data,
242-
success: (res) => {
243-
TestConsole.consoleSuccess.call(this, res, apiIndex)
244-
},
245-
fail: (res) => {
246-
TestConsole.consoleFail.call(this, res, apiIndex)
247-
},
248-
}).then((res) => {
249-
TestConsole.consoleResult.call(this, res, apiIndex)
250-
})
251-
},
252-
},
253223
{
254224
id: 'createVideoContext',
255225
func: (apiIndex) => {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import Taro from '@tarojs/taro'
2+
import { showActionSheet } from '@tarojs/taro-h5'
3+
4+
import native from '../../../NativeApi'
5+
import { shouldBeObject } from '../../../utils'
6+
import { MethodHandler } from '../../../utils/handler'
7+
8+
/**
9+
* 拍摄或从手机相册中选择图片或视频
10+
*
11+
* @canUse chooseMedia
12+
* @__object [count, mediaType[video, image, mix], sourceType[album, camera], maxDuration, sizeType, camera[back, front]]
13+
* @__success [tempFiles, type]
14+
*/
15+
export const chooseMedia: typeof Taro.chooseMedia = async (options) => {
16+
const name = 'chooseMedia'
17+
18+
// options must be an Object
19+
const isValid = shouldBeObject(options).flag || typeof options === 'undefined'
20+
if (!isValid) {
21+
const res = { errMsg: `${name}:fail invalid params` }
22+
console.error(res.errMsg)
23+
return Promise.reject(res)
24+
}
25+
26+
const {
27+
count = 9,
28+
mediaType = ['video', 'image'],
29+
sourceType = ['album', 'camera'],
30+
maxDuration = 10,
31+
sizeType = ['original', 'compressed'],
32+
camera = 'back',
33+
success,
34+
fail,
35+
} = options as Exclude<typeof options, undefined>
36+
37+
const handle = new MethodHandler<{
38+
tempFiles?: Taro.chooseMedia.ChooseMedia[]
39+
type?: string
40+
errMsg?: string
41+
}>({ name, success, fail })
42+
43+
let sourceSelected
44+
if (sourceType.length === 1) {
45+
sourceSelected = sourceType[0]
46+
} else if (typeof sourceType !== 'object' || (sourceType.includes('album') && sourceType.includes('camera'))) {
47+
const selected = await showActionSheet({ itemList: ['拍摄', '从相册选择'] }).then(
48+
(res) => {
49+
sourceSelected = res.tapIndex === 0 ? 'camera' : 'album'
50+
return true
51+
},
52+
() => {
53+
return false
54+
}
55+
)
56+
if (!selected) {
57+
return handle.fail({ errMsg: 'fail cancel' })
58+
}
59+
}
60+
61+
return new Promise<Taro.chooseMedia.SuccessCallbackResult>((resolve, reject) => {
62+
native.chooseMediaAssets({
63+
count: count,
64+
mediaType: mediaType,
65+
sourceType: sourceSelected,
66+
maxDuration: maxDuration,
67+
sizeType: sizeType,
68+
camera: camera,
69+
apiName: name,
70+
success: (res: any) => {
71+
const result: Taro.chooseMedia.SuccessCallbackResult = {
72+
tempFiles: res.tempFiles,
73+
type: res.type,
74+
errMsg: res.errMsg,
75+
}
76+
handle.success(result, { resolve, reject })
77+
},
78+
fail: (err: any) => {
79+
handle.fail(err, { resolve, reject })
80+
},
81+
})
82+
})
83+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { showActionSheet } from '@tarojs/taro-h5'
2+
3+
import native from '../../../NativeApi'
4+
import { shouldBeObject } from '../../../utils'
5+
import { MethodHandler } from '../../../utils/handler'
6+
7+
namespace chooseMedium {
8+
export interface Option {
9+
/** 最多可以选择的文件个数
10+
* @default 9
11+
* @supported harmny-hybrid
12+
*/
13+
count?: number
14+
/** 文件类型
15+
* @default ['image', 'video']
16+
* @supported harmny-hybrid
17+
*/
18+
mediaType?: Array<keyof mediaType>
19+
/** 图片和视频选择的来源
20+
* @default ['album', 'camera']
21+
* @supported harmny-hybrid
22+
*/
23+
sourceType?: Array<keyof sourceType>
24+
/** 拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 60s 之间
25+
* @default 10
26+
* @supported harmny-hybrid
27+
*/
28+
maxDuration?: number
29+
/** 仅在 sourceType 为 camera 时生效,是否压缩文件
30+
* @default ['original', 'compressed']
31+
* @supported harmny-hybrid
32+
*/
33+
sizeType?: Array<'original' | 'compressed'>
34+
/** 仅在 sourceType 为 camera 时生效,使用前置或后置摄像头
35+
* @default "back"
36+
* @supported harmny-hybrid
37+
*/
38+
camera?: string
39+
/** 仅在 sourceType 为 album 时生效,相册中是否允许拍照
40+
* @default "false"
41+
* @supported harmny-hybrid
42+
*/
43+
takingSupported?: boolean
44+
/** 仅在 sourceType 为 album 时生效,相册中是否允许编辑照片
45+
* @default "false"
46+
* @supported harmny-hybrid
47+
*/
48+
editSupported?: boolean
49+
/** 仅在 sourceType 为 album 时生效,相册中是否允许搜索
50+
* @default "false"
51+
* @supported harmny-hybrid
52+
*/
53+
searchSupported?: boolean
54+
/** 接口调用结束的回调函数(调用成功、失败都会执行) */
55+
complete?: (res: TaroGeneral.CallbackResult) => void
56+
/** 接口调用失败的回调函数 */
57+
fail?: (res: TaroGeneral.CallbackResult) => void
58+
/** 接口调用成功的回调函数 */
59+
success?: (result: SuccessCallbackResult) => void
60+
/** 用来上传的input元素ID
61+
* @supported harmny-hybrid
62+
*/
63+
mediaId?: string
64+
}
65+
export interface SuccessCallbackResult extends TaroGeneral.CallbackResult {
66+
/** 本地临时文件列表 */
67+
tempFiles: ChooseMedium[]
68+
/** 文件类型,有效值有 image 、video、mix */
69+
type: string
70+
}
71+
72+
/** 本地临时文件列表 */
73+
export interface ChooseMedium {
74+
/** 本地临时文件路径 (本地路径) */
75+
tempFilePath: string
76+
/** 本地临时文件大小,单位 B */
77+
size: number
78+
/** 视频的时间长度 */
79+
duration: number
80+
/** 视频的高度 */
81+
height: number
82+
/** 视频的宽度 */
83+
width: number
84+
/** 视频缩略图临时文件路径 */
85+
thumbTempFilePath: string
86+
/** 选择的文件的类型 */
87+
fileType: string
88+
}
89+
export interface mediaType {
90+
/** 只能拍摄视频或从相册选择视频 */
91+
video
92+
/** 只能拍摄图片或从相册选择图片 */
93+
image
94+
/** 可同时选择图片和视频 */
95+
mix
96+
}
97+
export interface sourceType {
98+
/** 从相册选择 */
99+
album
100+
/** 使用相机拍摄 */
101+
camera
102+
}
103+
export interface camera {
104+
/** 使用后置摄像头 */
105+
back
106+
/** 使用前置摄像头 */
107+
front
108+
}
109+
}
110+
111+
112+
interface harmony {
113+
/** 拍摄或从手机相册中选择图片或视频。
114+
* @supported harmony_hybrid
115+
* @example
116+
* ```tsx
117+
* Taro.chooseMedium({
118+
* count: 9,
119+
* mediaType: ['image','video'],
120+
* sourceType: ['album', 'camera'],
121+
* maxDuration: 30,
122+
* camera: 'back',
123+
* takingSupported: false,
124+
* editSupported: false,
125+
* searchSupported: false,
126+
* success: (res) => {
127+
* console.log(res.tempFiles)
128+
* console.log(res.type)
129+
* }
130+
* })
131+
* ```
132+
*/
133+
chooseMedium (option: chooseMedium.Option): Promise<chooseMedium.SuccessCallbackResult>
134+
}
135+
/**
136+
* 拍摄或从手机相册中选择图片或视频
137+
*
138+
* @canUse chooseMedium
139+
* @__object [count, mediaType[video, image, mix], sourceType[album, camera], maxDuration, sizeType, camera[back, front],takingSupported,editSupported,searchSupported]
140+
* @__success [tempFiles, type]
141+
*/
142+
export const chooseMedium: harmony['chooseMedium'] = async (options) => {
143+
const name = 'chooseMedium'
144+
145+
// options must be an Object
146+
const isValid = shouldBeObject(options).flag || typeof options === 'undefined'
147+
if (!isValid) {
148+
const res = { errMsg: `${name}:fail invalid params` }
149+
console.error(res.errMsg)
150+
return Promise.reject(res)
151+
}
152+
153+
const {
154+
count = 9,
155+
mediaType = ['video', 'image'],
156+
sourceType = ['album', 'camera'],
157+
maxDuration = 10,
158+
sizeType = ['original', 'compressed'],
159+
camera = 'back',
160+
takingSupported = false,
161+
editSupported = false,
162+
searchSupported = false,
163+
success,
164+
fail,
165+
} = options as Exclude<typeof options, undefined>
166+
167+
const handle = new MethodHandler<{
168+
tempFiles?: chooseMedium.ChooseMedium[]
169+
type?: string
170+
errMsg?: string
171+
}>({ name, success, fail })
172+
173+
let sourceSelected
174+
if (sourceType.length === 1) {
175+
sourceSelected = sourceType[0]
176+
} else if (typeof sourceType !== 'object' || (sourceType.includes('album') && sourceType.includes('camera'))) {
177+
const selected = await showActionSheet({ itemList: ['拍摄', '从相册选择'] }).then(
178+
(res) => {
179+
sourceSelected = res.tapIndex === 0 ? 'camera' : 'album'
180+
return true
181+
},
182+
() => {
183+
return false
184+
}
185+
)
186+
if (!selected) {
187+
return handle.fail({ errMsg: 'fail cancel' })
188+
}
189+
}
190+
191+
return new Promise<chooseMedium.SuccessCallbackResult>((resolve, reject) => {
192+
native.chooseMediumAssets({
193+
count: count,
194+
mediaType: mediaType,
195+
sourceType: sourceSelected,
196+
maxDuration: maxDuration,
197+
sizeType: sizeType,
198+
camera: camera,
199+
takingSupported: takingSupported,
200+
editSupported: editSupported,
201+
searchSupported: searchSupported,
202+
apiName: name,
203+
success: (res: any) => {
204+
const result: chooseMedium.SuccessCallbackResult = {
205+
tempFiles: res.tempFiles,
206+
type: res.type,
207+
errMsg: res.errMsg,
208+
}
209+
handle.success(result, { resolve, reject })
210+
},
211+
fail: (err: any) => {
212+
handle.fail(err, { resolve, reject })
213+
},
214+
})
215+
})
216+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { chooseMedia as chooseMediaAlbum } from './chooseMedia'
2+
import { chooseMedium as chooseMediaPicker } from './chooseMedium'
3+
4+
export const chooseMedia = (options: any, usePicker: boolean = true) => {
5+
return usePicker ? chooseMediaPicker(options) : chooseMediaAlbum(options)
6+
}

packages/taro-platform-harmony-hybrid/src/api/apis/media/video/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ export { createVideoContext } from '@tarojs/taro-h5'
1515
/**
1616
* 拍摄视频或从手机相册中选视频。
1717
*/
18-
export * from './chooseMedia'
19-
export * from './chooseMedium'
18+
export * from './chooseMedia/index'
2019

2120
/**
2221
* VideoContext 实例

0 commit comments

Comments
 (0)