context.ts 2.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { extend, isString, isPlainObject } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7
const DEFAULT_KEYS = [
  'APP',
  'APP_NVUE',
  'APP_PLUS',
  'APP_PLUS_NVUE',
  'APP_VUE',
fxy060608's avatar
fxy060608 已提交
8 9
  'APP_ANDROID',
  'APP_IOS',
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15
  'H5',
  'MP',
  'MP_360',
  'MP_ALIPAY',
  'MP_BAIDU',
  'MP_QQ',
fxy060608's avatar
fxy060608 已提交
16
  'MP_LARK',
fxy060608's avatar
fxy060608 已提交
17 18 19
  'MP_TOUTIAO',
  'MP_WEIXIN',
  'MP_KUAISHOU',
fxy060608's avatar
fxy060608 已提交
20
  'MP_JD',
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
  'QUICKAPP_NATIVE',
  'QUICKAPP_WEBVIEW',
  'QUICKAPP_WEBVIEW_HUAWEI',
  'QUICKAPP_WEBVIEW_UNION',
  'VUE2',
  'VUE3',
] as const

const preVueContext = Object.create(null)
const preNVueContext = Object.create(null)

export function getPreVueContext() {
  return preVueContext
34
}
fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40 41

export function getPreNVueContext() {
  return preNVueContext
}

export function initPreContext(
  platform: UniApp.PLATFORM,
fxy060608's avatar
fxy060608 已提交
42
  userPreContext?: Record<string, boolean> | string
fxy060608's avatar
fxy060608 已提交
43 44 45 46 47 48 49 50 51 52 53 54
) {
  const vueContext = Object.create(null)
  const nvueContext = Object.create(null)

  const defaultContext = Object.create(null)
  DEFAULT_KEYS.forEach((key) => {
    defaultContext[key] = false
  })

  defaultContext[normalizeKey(platform)] = true

  vueContext.VUE3 = true
fxy060608's avatar
fxy060608 已提交
55
  nvueContext.VUE3 = true
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61 62 63 64

  if (platform === 'app' || platform === 'app-plus') {
    defaultContext.APP = true
    defaultContext.APP_PLUS = true

    vueContext.APP_VUE = true

    nvueContext.APP_NVUE = true
    nvueContext.APP_PLUS_NVUE = true
fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70 71 72 73

    if (process.env.UNI_APP_PLATFORM === 'app-android') {
      defaultContext.APP_ANDROID = true
    } else if (process.env.UNI_APP_PLATFORM === 'app-ios') {
      defaultContext.APP_IOS = true
    } else {
      defaultContext.APP_ANDROID = true
      defaultContext.APP_IOS = true
    }
fxy060608's avatar
fxy060608 已提交
74 75 76 77 78 79 80
  } else if (platform.startsWith('mp-')) {
    defaultContext.MP = true
  } else if (platform.startsWith('quickapp-webview')) {
    defaultContext.QUICKAPP_WEBVIEW = true
  }

  if (userPreContext) {
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91 92
    if (isString(userPreContext)) {
      try {
        userPreContext = JSON.parse(userPreContext)
      } catch (e) {}
    }
    if (isPlainObject(userPreContext)) {
      Object.keys(userPreContext).forEach((key) => {
        defaultContext[normalizeKey(key)] = !!(
          userPreContext as Record<string, boolean>
        )[key]
      })
    }
fxy060608's avatar
fxy060608 已提交
93 94 95 96 97 98 99
  }
  extend(preVueContext, defaultContext, vueContext)
  extend(preNVueContext, defaultContext, nvueContext)
}

function normalizeKey(name: string) {
  return name.replace(/-/g, '_').toUpperCase()
100
}