app.ts 4.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { extend, hasOwn, isFunction } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2
import { ComponentPublicInstance, ComponentOptions, ref } from 'vue'
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7

import { initBaseInstance } from './componentInstance'
import { initHooks, initUnknownHooks } from './componentHooks'

import App = WechatMiniprogram.App
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15
import {
  ON_ERROR,
  ON_HIDE,
  ON_LAUNCH,
  ON_PAGE_NOT_FOUND,
  ON_SHOW,
  ON_THEME_CHANGE,
  ON_UNHANDLE_REJECTION,
16
  ON_SHARE_APP_MESSAGE,
fxy060608's avatar
fxy060608 已提交
17
} from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
18 19

import { injectAppLaunchHooks } from '../api/hook'
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28
export interface CustomAppInstanceProperty extends Record<string, any> {
  globalData: Record<string, any>
  $vm?: ComponentPublicInstance
}

export type MiniProgramAppOptions = App.Options<CustomAppInstanceProperty>
export type MiniProgramAppInstance = App.Instance<CustomAppInstanceProperty>

const HOOKS = [
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34
  ON_SHOW,
  ON_HIDE,
  ON_ERROR,
  ON_THEME_CHANGE,
  ON_PAGE_NOT_FOUND,
  ON_UNHANDLE_REJECTION,
fxy060608's avatar
fxy060608 已提交
35 36
]

37 38 39 40
if (__PLATFORM__ === 'mp-alipay') {
  HOOKS.push(ON_SHARE_APP_MESSAGE)
}

fxy060608's avatar
fxy060608 已提交
41 42 43 44
export interface ParseAppOptions {
  parse: (appOptions: MiniProgramAppOptions) => void
}

fxy060608's avatar
fxy060608 已提交
45
export function parseApp(
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52 53 54
  instance: ComponentPublicInstance,
  parseAppOptions?: ParseAppOptions
) {
  const internalInstance = instance.$
  const appOptions: MiniProgramAppOptions = {
    globalData: (instance.$options && instance.$options.globalData) || {},
    $vm: instance, // mp-alipay 组件 data 初始化比 onLaunch 早,提前挂载
    onLaunch(options: App.LaunchShowOption) {
      const ctx = (internalInstance as any).ctx as Record<string, any>
fxy060608's avatar
fxy060608 已提交
55
      if (this.$vm && ctx.$scope) {
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60 61
        // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
        return
      }
      initBaseInstance(internalInstance, {
        mpType: 'app',
        mpInstance: this,
62
        slots: [],
fxy060608's avatar
fxy060608 已提交
63
      })
fxy060608's avatar
fxy060608 已提交
64
      injectAppLaunchHooks(internalInstance)
fxy060608's avatar
fxy060608 已提交
65
      ctx.globalData = this.globalData
66 67 68 69 70 71 72
      instance.$callHook(
        ON_LAUNCH,
        extend(
          { app: { mixin: internalInstance.appContext.app.mixin } },
          options
        )
      )
73
    },
fxy060608's avatar
fxy060608 已提交
74 75
  }

fxy060608's avatar
fxy060608 已提交
76 77
  initLocale(instance)

fxy060608's avatar
fxy060608 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  const vueOptions = instance.$.type as ComponentOptions

  initHooks(appOptions, HOOKS)
  initUnknownHooks(appOptions, vueOptions)

  if (__VUE_OPTIONS_API__) {
    const methods = vueOptions.methods
    methods && extend(appOptions, methods)
  }

  if (parseAppOptions) {
    parseAppOptions.parse(appOptions)
  }

  return appOptions
}

export function initCreateApp(parseAppOptions?: ParseAppOptions) {
  return function createApp(vm: ComponentPublicInstance) {
    return App(parseApp(vm, parseAppOptions))
  }
}
fxy060608's avatar
fxy060608 已提交
100

fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
export function initCreateSubpackageApp(parseAppOptions?: ParseAppOptions) {
  return function createApp(vm: ComponentPublicInstance) {
    const appOptions = parseApp(vm, parseAppOptions)
    const app = getApp({
      allowDefault: true,
    })
    ;(vm.$ as any).ctx.$scope = app
    const globalData = app.globalData
    if (globalData) {
      Object.keys(appOptions.globalData).forEach((name) => {
        if (!hasOwn(globalData, name)) {
          globalData[name] = appOptions.globalData[name]
        }
      })
    }
    Object.keys(appOptions).forEach((name) => {
      if (!hasOwn(app, name)) {
        app[name] = appOptions[name]
      }
    })
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    initAppLifecycle(appOptions, vm)
  }
}

export function initAppLifecycle(
  appOptions: MiniProgramAppOptions,
  vm: ComponentPublicInstance
) {
  if (isFunction(appOptions.onShow) && __GLOBAL__.onAppShow) {
    __GLOBAL__.onAppShow((args: unknown) => {
      vm.$callHook('onShow', args)
    })
  }
  if (isFunction(appOptions.onHide) && __GLOBAL__.onAppHide) {
    __GLOBAL__.onAppHide((args: unknown) => {
      vm.$callHook('onHide', args)
    })
  }
  if (isFunction(appOptions.onLaunch)) {
    const args =
      __GLOBAL__.getLaunchOptionsSync && __GLOBAL__.getLaunchOptionsSync()
    vm.$callHook('onLaunch', args || {})
fxy060608's avatar
fxy060608 已提交
143 144 145
  }
}

fxy060608's avatar
fxy060608 已提交
146
function initLocale(appVm: ComponentPublicInstance) {
fxy060608's avatar
fxy060608 已提交
147 148 149
  const locale = ref<string>(
    __GLOBAL__.getSystemInfoSync().language || 'zh-Hans'
  )
fxy060608's avatar
fxy060608 已提交
150 151 152 153 154 155 156 157 158
  Object.defineProperty(appVm, '$locale', {
    get() {
      return locale.value
    },
    set(v) {
      locale.value = v
    },
  })
}