Skip to content

Commit

Permalink
feat(Wechat): chooseImage 返回 previewUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 28, 2023
1 parent ce76118 commit 6a7cf36
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/utils/Wechat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ describe('Wechat', () => {

await expect(wechat.checkJsApi(['chooseImage'])).toResolve()
await expect(wechat.updateShareData({})).toResolve()
await expect(wechat.chooseImage()).toResolve()
await expect(wechat.previewImage({ urls: ['https://foo.bar'] })).toResolve()
await expect(wechat.uploadImage({ localId: 'ss' })).toResolve()
await expect(wechat.closeWindow()).toResolve()
Expand Down
37 changes: 33 additions & 4 deletions src/utils/Wechat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventBus, EventBusOffListener } from './EventBus'
import { loadResource, LoadResourceUrlType } from './loadResource'
import { mapValues, noop } from 'lodash-uni'
import { EventBus, EventBusOffListener } from './EventBus'
import { LoadResourceUrlType, loadResource } from './loadResource'

declare const wx: any

Expand Down Expand Up @@ -47,6 +47,7 @@ export type WechatJsApi =
| 'addCard'
| 'chooseCard'
| 'openCard'
| 'getLocalImgData'

/**
* @public
Expand Down Expand Up @@ -508,13 +509,41 @@ export class Wechat {
* @param params 参数
* @returns 选定照片的本地 ID 列表,它们可以作为 img 标签的 src 属性显示图片
*/
chooseImage(params?: WechatChooseImageParams): Promise<string[]> {
chooseImage(params?: WechatChooseImageParams): Promise<
Array<{
localId: string
previewUrl: string
}>
> {
return this.invoke<
WechatChooseImageParams,
{
localIds: string[]
}
>('chooseImage', params).then(res => res.localIds)
>('chooseImage', params).then(res => {
// @ts-ignore
if (window.__wxjs_is_wkwebview) {
return Promise.all(
res.localIds.map(localId =>
this.invoke<
{
localId: string
},
{
localData: string
}
>('getLocalImgData', { localId }).then(res => ({
localId: localId,
previewUrl: res.localData,
})),
),
)
}
return res.localIds.map(localId => ({
localId: localId,
previewUrl: localId,
}))
})
}

/**
Expand Down

0 comments on commit 6a7cf36

Please sign in to comment.