Skip to content

Commit

Permalink
feat(inAndroid): 支持在小程序中使用
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 23, 2020
1 parent e804fb8 commit b3db043
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/utils/inAndroid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,32 @@ describe('inAndroid', () => {
window.navigator.userAgent = 'xx android yy'
expect(inAndroid()).toBeTrue()
})

test('<小程序> 不在 Android 设备中', async () => {
const { inAndroid } = await import('./inAndroid')
// @ts-ignore
window.wx = {
getSystemInfoSync() {
return {
platform: 'devtools',
system: '123',
}
},
} as Partial<WechatMiniprogram.Wx>
expect(inAndroid()).toBeFalse()
})

test('<小程序> 在 Android 设备中', async () => {
const { inAndroid } = await import('./inAndroid')
// @ts-ignore
window.wx = {
getSystemInfoSync() {
return {
platform: 'android',
system: '123',
}
},
} as Partial<WechatMiniprogram.Wx>
expect(inAndroid()).toBeTrue()
})
})
15 changes: 11 additions & 4 deletions src/utils/inAndroid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { inBrowser } from './inBrowser'
import { inMiniProgram } from './inMiniProgram'

let yes!: boolean

Expand All @@ -15,10 +16,16 @@ let yes!: boolean
*/
export function inAndroid(): boolean {
if (yes == null) {
yes =
inBrowser() &&
typeof window.navigator === 'object' &&
/Android/i.test(window.navigator.userAgent || '')
const mp = inMiniProgram()
if (mp) {
const sysInfo = mp.getSystemInfoSync()
yes = sysInfo.platform === 'android' || /Android/i.test(sysInfo.system)
} else {
yes =
inBrowser() &&
typeof window.navigator === 'object' &&
/Android/i.test(window.navigator.userAgent || '')
}
}
return yes
}
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export * from './inIOS'
export * from './inMiniProgram'
export * from './inNodeJS'
export * from './inTaro'
export * from './inWechat'
export * from './inWechatWebView'
export * from './isChineseIDCardNumber'
export * from './isNumeric'
export * from './isPossibleChineseMobilePhoneNumber'
Expand Down

0 comments on commit b3db043

Please sign in to comment.