All files / utils inMiniProgram.ts

100% Statements 16/16
72.73% Branches 16/22
100% Functions 8/8
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                    27x                                   27x 40x 29x 30x 29x 32x 29x 29x                       43x 218x 218x 218x   8x 8x       35x    
import { castArray } from 'lodash-uni'
 
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 brands = [
  '微信',
  'QQ',
  '支付宝',
  '京东',
  '百度',
  '字节跳动',
  '钉钉',
] as const
 
export type MiniProgramBrand = typeof brands[number]
 
export type MiniProgramApi = WechatMiniprogram.Wx & {
  /** 小程序品牌 */
  readonly $brand: MiniProgramBrand
}
 
const factories: Record<MiniProgramBrand, () => 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 brand 指定的小程序品牌,若未指定,则表示所有小程序品牌
 * @returns 返回检查结果
 */
export function inMiniProgram(
  brand?: MiniProgramBrand | MiniProgramBrand[],
): MiniProgramApi | false {
  for (const currentBrand of brand ? castArray(brand) : brands) {
    if (factories[currentBrand]) {
      const mp = factories[currentBrand]()
      if (mp && typeof mp.getSystemInfoSync === 'function') {
        // @ts-ignore
        ;(mp as any as MiniProgramApi).$brand = currentBrand
        return mp as any
      }
    }
  }
  return false
}