Skip to content

Commit

Permalink
feat: add Wechat
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 30, 2019
1 parent a896699 commit 12258e1
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 0 deletions.
248 changes: 248 additions & 0 deletions src/Wechat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import { EventBus } from './EventBus'

declare const wx: any

export type WechatJsApi = (
'checkJsApi' |
'updateAppMessageShareData' |
'updateTimelineShareData' |
'startRecord' |
'stopRecord' |
'onVoiceRecordEnd' |
'playVoice' |
'pauseVoice' |
'stopVoice' |
'onVoicePlayEnd' |
'uploadVoice' |
'downloadVoice' |
'chooseImage' |
'previewImage' |
'uploadImage' |
'downloadImage' |
'translateVoice' |
'getNetworkType' |
'openLocation' |
'getLocation' |
'hideOptionMenu' |
'showOptionMenu' |
'hideMenuItems' |
'showMenuItems' |
'hideAllNonBaseMenuItem' |
'showAllNonBaseMenuItem' |
'closeWindow' |
'scanQRCode' |
'chooseWXPay' |
'openProductSpecificView' |
'addCard' |
'chooseCard' |
'openCard'
)

export interface WechatConfigParams {
/**
* 开启调试模式。
*
* 调用的所有api的返回值会在客户端 alert 出来,
* 若要查看传入的参数,可以在 pc 端打开,
* 参数信息会通过 log 打出,仅在 pc 端时才会打印。
*
* @default false
*/
debug?: boolean,
/**
* 公众号的唯一标识。
*/
appId: string,
/**
* 生成签名的时间戳。
*/
timestamp: number | string,
/**
* 生成签名的随机串。
*/
nonceStr: string,
/**
* 签名。
*/
signature: string,
/**
* 需要使用的JS接口列表。
*/
jsApiList: WechatJsApi[],
}

export type WechatErrorCallback = (err: any) => void

export interface WechatUpdateShareDataParams {
/** 分享标题 */
title?: string,
/** 分享描述 */
desc?: string,
/** 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致 */
link?: string,
/** 分享图标地址 */
imgUrl?: string,
}

export interface WechatChooseImageParams {
/**
* 选择图片数量。
*
* @default 9
*/
count?: number,
/**
* 图片质量,可以指定是原图还是压缩图。
*
* @default ['original', 'compressed']
*/
sizeType?: Array<'original' | 'compressed'>,
/**
* 选择来源,可以指定是相册还是相机。
*
* @default ['album', 'camera']
*/
sourceType?: Array<'album' | 'camera'>,
}

export interface WechatPreviewImageParams {
/**
* 当前显示图片的链接。
*
* @default urls[0]
*/
current?: string,
/**
* 需要预览的图片链接列表。
*/
urls: string[],
}

export interface WechatUploadImageParams {
/**
* 需要上传的图片的本地 ID,由 chooseImage 接口获得。
*/
localId: string,
/**
* 是否显示进度提示。
*
* @default false
*/
isShowProgressTips?: boolean,
}

export type WechatNonBaseMenuItem = (
'menuItem:share:appMessage' |
'menuItem:share:timeline' |
'menuItem:share:qq' |
'menuItem:share:weiboApp' |
'menuItem:favorite' |
'menuItem:share:facebook' |
'menuItem:share:QZone' |
'menuItem:editTag' |
'menuItem:delete' |
'menuItem:copyUrl' |
'menuItem:originPage' |
'menuItem:readMode' |
'menuItem:openWithQQBrowser' |
'menuItem:openWithSafari' |
'menuItem:share:email' |
'menuItem:share:brand'
)

export class WeChat {
private ready: boolean = false

private bus = new EventBus<{
error: WechatErrorCallback,
}>()

private prevShareParams: WechatUpdateShareDataParams = {}

constructor(params: WechatConfigParams) {
if (typeof wx === 'undefined') {
throw new Error('请先引入微信 JSSDK')
}
wx.config(params)
wx.ready(() => {
this.ready = true
})
wx.error((err: any) => {
this.bus.emit('error', err)
})
}

private invoke(jsApi: WechatJsApi, params: Record<string, any> = {}): Promise<any> {
return new Promise((resolve, reject) => {
if (!wx[jsApi]) reject(`wx.${jsApi} 不可用`)
params.success = resolve
params.fail = reject
if (this.ready) {
wx[jsApi](params)
} else {
wx.ready(() => wx[jsApi](params))
}
})
}

checkJsApi<T extends WechatJsApi>(jsApiList: T[]): Promise<Record<T, boolean>> {
return this.invoke('checkJsApi', { jsApiList })
.then(res => res.checkResult)
}

updateShareData(params: WechatUpdateShareDataParams): Promise<void> {
params = {
...this.prevShareParams,
...params,
}
return Promise.all([
this.invoke('updateAppMessageShareData', params),
this.invoke('updateTimelineShareData', params),
]).then(() => {
this.prevShareParams = params
})
}

chooseImage(params?: WechatChooseImageParams): Promise<string[]> {
return this.invoke('chooseImage', params)
.then(res => res.localIds)
}

previewImage(params: WechatPreviewImageParams): Promise<any> {
return this.invoke('previewImage', {
urls: params.urls,
current: params.current || params.urls[0],
})
}

uploadImage(params: WechatUploadImageParams): Promise<string[]> {
return this.invoke('uploadImage', {
localId: params.localId,
isShowProgressTips: params.isShowProgressTips ? 1 : 0,
})
}

closeWindow(): Promise<any> {
return this.invoke('closeWindow')
}

hideNonBaseMenuItems(menuList: WechatNonBaseMenuItem[]): Promise<any> {
return this.invoke('hideMenuItems', { menuList })
}

showNonBaseMenuItems(menuList: WechatNonBaseMenuItem[]): Promise<any> {
return this.invoke('showMenuItems', { menuList })
}

hideAllNonBaseMenuItems(): Promise<any> {
return this.invoke('hideAllNonBaseMenuItem')
}

showAllNonBaseMenuItems(): Promise<any> {
return this.invoke('showAllNonBaseMenuItem')
}

onError(callback: WechatErrorCallback) {
this.bus.on('error', callback)
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ export * from './upperCaseFirst'
export * from './Validator'
export * from './values'
export * from './wait'
export * from './Wechat'

0 comments on commit 12258e1

Please sign in to comment.