index.ts 6.3 KB
Newer Older
1
import { extend, invokeArrayFns, isPlainObject } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7 8 9 10 11 12
import {
  ComponentInternalInstance,
  ComponentPublicInstance,
  createBlock,
  DefineComponent,
  getCurrentInstance,
  onMounted,
  openBlock,
  onBeforeActivate,
  onBeforeDeactivate,
  onBeforeMount,
13
  onBeforeUnmount,
fxy060608's avatar
fxy060608 已提交
14
} from 'vue'
fxy060608's avatar
fxy060608 已提交
15
import { useRouter } from 'vue-router'
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24
import {
  debounce,
  decodedQuery,
  ON_APP_ENTER_BACKGROUND,
  ON_APP_ENTER_FOREGROUND,
  ON_RESIZE,
  ON_WEB_INVOKE_APP_SERVICE,
  WEB_INVOKE_APPSERVICE,
} from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
25
import { injectAppLaunchHooks } from '@dcloudio/uni-api'
26
import { subscribeViewMethod, unsubscribeViewMethod } from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
27 28
import { LayoutComponent } from '../..'
import { initApp } from './app'
fxy060608's avatar
fxy060608 已提交
29
import { initPage, onPageShow, onPageReady } from './page'
fxy060608's avatar
fxy060608 已提交
30
import { usePageMeta, usePageRoute } from './provide'
31
import { initLaunchOptions, getEnterOptions } from './utils'
fxy060608's avatar
fxy060608 已提交
32 33

interface SetupComponentOptions {
34
  clone?: boolean
fxy060608's avatar
fxy060608 已提交
35
  init: (vm: ComponentPublicInstance) => void
fxy060608's avatar
fxy060608 已提交
36
  setup: (instance: ComponentInternalInstance) => Record<string, any> | void
fxy060608's avatar
fxy060608 已提交
37
  before?: (comp: DefineComponent) => void
fxy060608's avatar
fxy060608 已提交
38 39 40 41
}

function wrapperComponentSetup(
  comp: DefineComponent,
42
  { clone, init, setup, before }: SetupComponentOptions
fxy060608's avatar
fxy060608 已提交
43
) {
44 45 46
  if (clone) {
    comp = extend({}, comp)
  }
fxy060608's avatar
fxy060608 已提交
47
  before && before(comp)
fxy060608's avatar
fxy060608 已提交
48 49 50 51
  const oldSetup = comp.setup
  comp.setup = (props, ctx) => {
    const instance = getCurrentInstance()!
    init(instance.proxy!)
fxy060608's avatar
fxy060608 已提交
52
    const query = setup(instance)
fxy060608's avatar
fxy060608 已提交
53
    if (oldSetup) {
fxy060608's avatar
fxy060608 已提交
54
      return oldSetup(query || props, ctx)
fxy060608's avatar
fxy060608 已提交
55 56
    }
  }
57
  return comp
fxy060608's avatar
fxy060608 已提交
58 59 60 61
}

function setupComponent(comp: any, options: SetupComponentOptions) {
  if (comp && (comp.__esModule || comp[Symbol.toStringTag] === 'Module')) {
62
    return wrapperComponentSetup(comp.default, options)
fxy060608's avatar
fxy060608 已提交
63
  }
64
  return wrapperComponentSetup(comp, options)
fxy060608's avatar
fxy060608 已提交
65 66
}

fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
export function setupWindow(comp: any, id: number) {
  return setupComponent(comp, {
    init: (vm) => {
      vm.$page = {
        id,
      } as Page.PageInstance['$page']
    },
    setup(instance) {
      instance.root = instance // windows 中组件 root 指向 window
    },
  })
}

fxy060608's avatar
fxy060608 已提交
80
export function setupPage(comp: any) {
fxy060608's avatar
fxy060608 已提交
81 82 83
  if (__DEV__) {
    comp.__mpType = 'page'
  }
fxy060608's avatar
fxy060608 已提交
84
  return setupComponent(comp, {
85
    clone: true,
fxy060608's avatar
fxy060608 已提交
86 87
    init: initPage,
    setup(instance) {
fxy060608's avatar
fxy060608 已提交
88
      instance.root = instance // 组件 root 指向页面
fxy060608's avatar
fxy060608 已提交
89
      const route = usePageRoute()
fxy060608's avatar
fxy060608 已提交
90 91
      // 存储参数,让 initHooks 中执行 onLoad 时,可以访问到
      instance.attrs.__pageQuery = decodedQuery(route.query)
fxy060608's avatar
fxy060608 已提交
92
      if (__NODE_JS__) {
fxy060608's avatar
fxy060608 已提交
93
        return instance.attrs.__pageQuery as Record<string, unknown>
fxy060608's avatar
fxy060608 已提交
94
      }
fxy060608's avatar
fxy060608 已提交
95
      const pageMeta = usePageMeta()
fxy060608's avatar
fxy060608 已提交
96
      onBeforeMount(() => {
fxy060608's avatar
fxy060608 已提交
97
        onPageShow(instance, pageMeta)
fxy060608's avatar
fxy060608 已提交
98 99
      })
      onMounted(() => {
fxy060608's avatar
fxy060608 已提交
100
        onPageReady(instance)
fxy060608's avatar
fxy060608 已提交
101
        const { onReady } = instance
102
        onReady && invokeArrayFns(onReady)
fxy060608's avatar
fxy060608 已提交
103 104 105
      })
      onBeforeActivate(() => {
        if (!instance.__isVisible) {
fxy060608's avatar
fxy060608 已提交
106
          onPageShow(instance, pageMeta)
fxy060608's avatar
fxy060608 已提交
107 108 109 110 111 112 113 114 115 116 117 118
          instance.__isVisible = true
          const { onShow } = instance
          onShow && invokeArrayFns(onShow)
        }
      })
      onBeforeDeactivate(() => {
        if (instance.__isVisible && !instance.__isUnload) {
          instance.__isVisible = false
          const { onHide } = instance
          onHide && invokeArrayFns(onHide)
        }
      })
fxy060608's avatar
fxy060608 已提交
119

120 121 122 123 124
      subscribeViewMethod(pageMeta.id!)
      onBeforeUnmount(() => {
        unsubscribeViewMethod(pageMeta.id!)
      })

fxy060608's avatar
fxy060608 已提交
125
      return route.query
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130
    },
  })
}

export function setupApp(comp: any) {
fxy060608's avatar
fxy060608 已提交
131 132 133
  if (__DEV__) {
    comp.__mpType = 'app'
  }
fxy060608's avatar
fxy060608 已提交
134 135 136
  return setupComponent(comp, {
    init: initApp,
    setup(instance) {
fxy060608's avatar
fxy060608 已提交
137
      const route = usePageRoute()
fxy060608's avatar
fxy060608 已提交
138 139 140 141
      // node环境不触发App生命周期
      if (__NODE_JS__) {
        return route.query
      }
fxy060608's avatar
fxy060608 已提交
142
      const onLaunch = () => {
fxy060608's avatar
fxy060608 已提交
143
        const { onLaunch, onShow, onPageNotFound } = instance
fxy060608's avatar
fxy060608 已提交
144
        const path = route.path.substr(1)
145 146 147 148 149 150 151 152 153
        const launchOptions = extend(
          {
            app: { mixin: instance.appContext.app.mixin },
          },
          initLaunchOptions({
            path: path || __uniRoutes[0].meta.route,
            query: decodedQuery(route.query),
          })
        )
fxy060608's avatar
fxy060608 已提交
154 155
        onLaunch && invokeArrayFns(onLaunch, launchOptions)
        onShow && invokeArrayFns(onShow, launchOptions)
fxy060608's avatar
fxy060608 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
        if (__UNI_FEATURE_PAGES__) {
          if (!route.matched.length) {
            const pageNotFoundOptions = {
              notFound: true,
              openType: 'appLaunch',
              path: route.path,
              query: {},
              scene: 1001,
            }
            onPageNotFound &&
              invokeArrayFns(onPageNotFound, pageNotFoundOptions)
          }
        }
fxy060608's avatar
fxy060608 已提交
169
      }
fxy060608's avatar
fxy060608 已提交
170
      injectAppLaunchHooks(instance)
fxy060608's avatar
fxy060608 已提交
171 172 173 174 175 176
      if (__UNI_FEATURE_PAGES__) {
        // 等待ready后,再onLaunch,可以顺利获取到正确的path和query
        useRouter().isReady().then(onLaunch)
      } else {
        onBeforeMount(onLaunch)
      }
fxy060608's avatar
fxy060608 已提交
177
      onMounted(() => {
fxy060608's avatar
fxy060608 已提交
178 179 180
        window.addEventListener('resize', debounce(onResize, 50))
        window.addEventListener('message', onMessage)
        document.addEventListener('visibilitychange', onVisibilityChange)
fxy060608's avatar
fxy060608 已提交
181
      })
fxy060608's avatar
fxy060608 已提交
182
      return route.query
fxy060608's avatar
fxy060608 已提交
183
    },
fxy060608's avatar
fxy060608 已提交
184
    before(comp) {
fxy060608's avatar
fxy060608 已提交
185
      comp.mpType = 'app'
fxy060608's avatar
fxy060608 已提交
186 187 188 189 190 191
      const { setup } = comp
      comp.setup = (props, ctx) => {
        setup && setup(props, ctx)
        return () => {
          return openBlock(), createBlock(LayoutComponent)
        }
fxy060608's avatar
fxy060608 已提交
192
      }
fxy060608's avatar
fxy060608 已提交
193 194 195
    },
  })
}
fxy060608's avatar
fxy060608 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

function onResize() {
  const { windowWidth, windowHeight, screenWidth, screenHeight } =
    uni.getSystemInfoSync()
  const landscape = Math.abs(Number(window.orientation)) === 90
  const deviceOrientation = landscape ? 'landscape' : 'portrait'
  UniServiceJSBridge.emit(ON_RESIZE, {
    deviceOrientation,
    size: {
      windowWidth,
      windowHeight,
      screenWidth,
      screenHeight,
    },
  })
}
function onMessage(evt: {
  data?: { type: string; data: any; pageId: number }
}) {
  if (isPlainObject(evt.data) && evt.data.type === WEB_INVOKE_APPSERVICE) {
    UniServiceJSBridge.emit(
      ON_WEB_INVOKE_APP_SERVICE,
      evt.data.data,
      evt.data.pageId
    )
  }
}
function onVisibilityChange() {
224 225 226 227 228 229
  const { emit } = UniServiceJSBridge
  if (document.visibilityState === 'visible') {
    emit(ON_APP_ENTER_FOREGROUND, getEnterOptions())
  } else {
    emit(ON_APP_ENTER_BACKGROUND)
  }
fxy060608's avatar
fxy060608 已提交
230
}