Skip to content

Commit

Permalink
feat(inMiniProgram): 在小程序中时,现在会返回小程序 API 挂载的全局对象
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 23, 2020
1 parent 1d341e4 commit ba90ab8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"date-fns": "^2.14.0",
"lodash": "^4.17.15",
"lodash-es": "^4.17.15",
"miniprogram-api-typings": "^2.11.0-1",
"react-use": "^15.1.1",
"tslib": "^2.0.0",
"yup": "^0.29.1"
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/utils/inMiniProgram.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// <reference types="miniprogram-api-typings" />
import { inMiniProgram } from './inMiniProgram'

const fakeMiniProgramFactory = {
getSystemInfo() {
return {}
const fakeMiniProgramFactory: Partial<WechatMiniprogram.Wx> = {
getSystemInfoSync() {
return {} as any
},
}

Expand All @@ -16,13 +17,13 @@ describe('inMiniProgram', () => {
wx: fakeMiniProgramFactory,
my: fakeMiniProgramFactory,
})
expect(inMiniProgram()).toBeTrue()
expect(inMiniProgram('微信')).toBeTrue()
expect(inMiniProgram()).toBe(fakeMiniProgramFactory)
expect(inMiniProgram('微信')).toBe(fakeMiniProgramFactory)
expect(inMiniProgram('百度')).toBeFalse()
expect(inMiniProgram(['微信', '支付宝'])).toBeTrue()
expect(inMiniProgram(['微信', '支付宝'])).toBe(fakeMiniProgramFactory)
Object.assign(window, {
swan: fakeMiniProgramFactory,
})
expect(inMiniProgram('百度')).toBeTrue()
expect(inMiniProgram('百度')).toBe(fakeMiniProgramFactory)
})
})
50 changes: 30 additions & 20 deletions src/utils/inMiniProgram.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/// <reference types="miniprogram-api-typings" />
import { castArray } from 'lodash-es'

declare const wx: any
declare const qq: any
declare const my: any
declare const jd: any
declare const swan: any
declare const tt: any
declare const dd: any
declare const wx: WechatMiniprogram.Wx | undefined
declare const qq: WechatMiniprogram.Wx | undefined
declare const my: WechatMiniprogram.Wx | undefined
declare const jd: WechatMiniprogram.Wx | undefined
declare const swan: WechatMiniprogram.Wx | undefined
declare const tt: WechatMiniprogram.Wx | undefined
declare const dd: WechatMiniprogram.Wx | undefined

const platforms = [
'微信',
Expand All @@ -20,26 +21,35 @@ const platforms = [

export type InMiniProgramPlatform = typeof platforms[number]

const detectors: Record<InMiniProgramPlatform, () => boolean> = {
微信: () => typeof wx !== 'undefined' && !!wx.getSystemInfo,
QQ: () => typeof qq !== 'undefined' && !!qq.getSystemInfo,
支付宝: () => typeof my !== 'undefined' && !!my.getSystemInfo,
京东: () => typeof jd !== 'undefined' && !!jd.getSystemInfo,
百度: () => typeof swan !== 'undefined' && !!swan.getSystemInfo,
字节跳动: () => typeof tt !== 'undefined' && !!tt.getSystemInfo,
钉钉: () => typeof dd !== 'undefined' && !!dd.getSystemInfo,
const factories: Record<
InMiniProgramPlatform,
() => false | WechatMiniprogram.Wx
> = {
微信: () => typeof wx !== 'undefined' && wx,
QQ: () => typeof qq !== 'undefined' && qq,
支付宝: () => typeof my !== 'undefined' && my,
京东: () => typeof jd !== 'undefined' && jd,
百度: () => typeof swan !== 'undefined' && swan,
字节跳动: () => typeof tt !== 'undefined' && tt,
钉钉: () => typeof dd !== 'undefined' && dd,
}

/**
* 检查是否在指定的小程序平台中。
* 检查是否在指定的小程序平台中,若在,返回承载其 API 的全局对象,若不在,返回 false
*
* @param platform 指定的小程序平台,若未指定,则表示所有小程序平台
* @returns 返回检查结果
*/
export function inMiniProgram(
platform?: InMiniProgramPlatform | InMiniProgramPlatform[],
): boolean {
return (platform ? castArray(platform) : platforms).some(platform =>
detectors[platform](),
)
): WechatMiniprogram.Wx | false {
for (const currentPlatform of platform ? castArray(platform) : platforms) {
if (factories[currentPlatform]) {
const mp = factories[currentPlatform]()
if (mp && typeof mp.getSystemInfoSync === 'function') {
return mp
}
}
}
return false
}

0 comments on commit ba90ab8

Please sign in to comment.