index.ts 4.9 KB
Newer Older
D
DCloud_LXH 已提交
1
import { invokeArrayFns, isPlainObject } from '@vue/shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7 8 9 10 11 12 13
import {
  ComponentInternalInstance,
  ComponentPublicInstance,
  createBlock,
  DefineComponent,
  getCurrentInstance,
  onMounted,
  openBlock,
  onBeforeActivate,
  onBeforeDeactivate,
  onBeforeMount,
} from 'vue'
fxy060608's avatar
fxy060608 已提交
14
import { useRouter } from 'vue-router'
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21 22 23
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 已提交
24 25
import { LayoutComponent } from '../..'
import { initApp } from './app'
fxy060608's avatar
fxy060608 已提交
26
import { initPage, onPageShow, onPageReady } from './page'
fxy060608's avatar
fxy060608 已提交
27
import { usePageMeta, usePageRoute } from './provide'
fxy060608's avatar
fxy060608 已提交
28 29 30 31
import {
  API_ON_WINDOW_RESIZE,
  API_TYPE_ON_WINDOW_RESIZE,
} from '@dcloudio/uni-api'
fxy060608's avatar
fxy060608 已提交
32 33 34

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

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

function setupComponent(comp: any, options: SetupComponentOptions) {
  if (comp && (comp.__esModule || comp[Symbol.toStringTag] === 'Module')) {
    wrapperComponentSetup(comp.default, options)
  } else {
    wrapperComponentSetup(comp, options)
  }
  return comp
}

export function setupPage(comp: any) {
  return setupComponent(comp, {
    init: initPage,
    setup(instance) {
fxy060608's avatar
fxy060608 已提交
68
      instance.root = instance // 组件root指向页面
fxy060608's avatar
fxy060608 已提交
69
      const route = usePageRoute()
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74
      // node环境不触发Page生命周期
      if (__NODE_JS__) {
        return route.query
      }

fxy060608's avatar
fxy060608 已提交
75
      const pageMeta = usePageMeta()
fxy060608's avatar
fxy060608 已提交
76

fxy060608's avatar
fxy060608 已提交
77
      onBeforeMount(() => {
fxy060608's avatar
fxy060608 已提交
78
        onPageShow(instance, pageMeta)
fxy060608's avatar
fxy060608 已提交
79
        const { onLoad, onShow } = instance
fxy060608's avatar
fxy060608 已提交
80
        onLoad && invokeArrayFns(onLoad, decodedQuery(route.query))
fxy060608's avatar
fxy060608 已提交
81 82 83 84
        instance.__isVisible = true
        onShow && invokeArrayFns(onShow)
      })
      onMounted(() => {
fxy060608's avatar
fxy060608 已提交
85
        onPageReady(instance)
fxy060608's avatar
fxy060608 已提交
86 87 88 89 90
        const { onReady } = instance
        onReady && invokeArrayFns(onReady)
      })
      onBeforeActivate(() => {
        if (!instance.__isVisible) {
fxy060608's avatar
fxy060608 已提交
91
          onPageShow(instance, pageMeta)
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97 98 99 100 101 102 103
          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 已提交
104

fxy060608's avatar
fxy060608 已提交
105
      return route.query
fxy060608's avatar
fxy060608 已提交
106 107 108 109 110 111 112 113
    },
  })
}

export function setupApp(comp: any) {
  return setupComponent(comp, {
    init: initApp,
    setup(instance) {
fxy060608's avatar
fxy060608 已提交
114
      const route = usePageRoute()
fxy060608's avatar
fxy060608 已提交
115 116 117 118
      // node环境不触发App生命周期
      if (__NODE_JS__) {
        return route.query
      }
fxy060608's avatar
fxy060608 已提交
119
      const onLaunch = () => {
fxy060608's avatar
fxy060608 已提交
120
        const { onLaunch, onShow } = instance
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        const path = route.path.substr(1)
        const launchOptions = {
          path: path || __uniRoutes[0].meta.route,
          query: decodedQuery(route.query),
          scene: 1001,
        }
        onLaunch && invokeArrayFns(onLaunch, launchOptions)
        onShow && invokeArrayFns(onShow, launchOptions)
      }
      if (__UNI_FEATURE_PAGES__) {
        // 等待ready后,再onLaunch,可以顺利获取到正确的path和query
        useRouter().isReady().then(onLaunch)
      } else {
        onBeforeMount(onLaunch)
      }
fxy060608's avatar
fxy060608 已提交
136
      onMounted(() => {
fxy060608's avatar
fxy060608 已提交
137 138 139
        window.addEventListener('resize', debounce(onResize, 50))
        window.addEventListener('message', onMessage)
        document.addEventListener('visibilitychange', onVisibilityChange)
fxy060608's avatar
fxy060608 已提交
140
      })
fxy060608's avatar
fxy060608 已提交
141
      return route.query
fxy060608's avatar
fxy060608 已提交
142
    },
fxy060608's avatar
fxy060608 已提交
143
    before(comp) {
fxy060608's avatar
fxy060608 已提交
144
      comp.mpType = 'app'
fxy060608's avatar
fxy060608 已提交
145 146 147
      comp.setup = () => () => {
        return openBlock(), createBlock(LayoutComponent)
      }
fxy060608's avatar
fxy060608 已提交
148 149 150
    },
  })
}
fxy060608's avatar
fxy060608 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

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() {
  UniServiceJSBridge.emit(
    document.visibilityState === 'visible'
      ? ON_APP_ENTER_FOREGROUND
      : ON_APP_ENTER_BACKGROUND
  )
}