lifecycle.js 2.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2 3
  hasOwn,
  decodedQuery
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9
} from 'uni-shared'

import {
  hasLifecycleHook
} from 'uni-helpers/index'

fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15
import {
  callPageHook
} from 'uni-core/service/plugins/util'

import {
  lifecycleMixin
fxy060608's avatar
init v3  
fxy060608 已提交
16
}
fxy060608's avatar
fxy060608 已提交
17 18
  from 'uni-core/service/plugins/lifecycle'

fxy060608's avatar
fxy060608 已提交
19
import {
Q
qiang 已提交
20 21
  ON_REACH_BOTTOM_DISTANCE,
  TITLEBAR_HEIGHT
fxy060608's avatar
fxy060608 已提交
22 23 24
}
  from '../../constants'

Q
qiang 已提交
25 26
import tabBar from '../tab-bar'

Q
qiang 已提交
27 28 29 30
import {
  getStatusbarHeight
} from '../../api/util'

fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39 40 41 42
function parsePageCreateOptions (vm, route) {
  const pagePath = '/' + route
  const routeOptions = __uniRoutes.find(route => route.path === pagePath)

  const windowOptions = Object.assign({}, __uniConfig.window, routeOptions.window)
  const disableScroll = windowOptions.disableScroll === true ? 1 : 0
  const onReachBottomDistance = hasOwn(windowOptions, 'onReachBottomDistance')
    ? parseInt(windowOptions.onReachBottomDistance)
    : ON_REACH_BOTTOM_DISTANCE

  const onPageScroll = hasLifecycleHook(vm.$options, 'onPageScroll') ? 1 : 0
  const onPageReachBottom = hasLifecycleHook(vm.$options, 'onReachBottom') ? 1 : 0
Q
qiang 已提交
43
  const statusbarHeight = getStatusbarHeight()
fxy060608's avatar
fxy060608 已提交
44 45 46 47 48

  return {
    disableScroll,
    onPageScroll,
    onPageReachBottom,
49
    onReachBottomDistance,
Q
qiang 已提交
50
    statusbarHeight,
fxy060608's avatar
fxy060608 已提交
51 52
    windowTop: windowOptions.titleNView && windowOptions.titleNView.type === 'float' ? (statusbarHeight +
      TITLEBAR_HEIGHT) : 0,
Q
qiang 已提交
53
    windowBottom: (tabBar.indexOf(route) >= 0 && tabBar.cover) ? tabBar.height : 0
fxy060608's avatar
fxy060608 已提交
54 55 56
  }
}

fxy060608's avatar
fxy060608 已提交
57 58 59 60
export function initLifecycle (Vue) {
  lifecycleMixin(Vue)

  Vue.mixin({
fxy060608's avatar
fxy060608 已提交
61 62 63 64
    beforeCreate () {
      // TODO 临时解决方案,service 层也注入 wxs (适用于工具类)
      const options = this.$options

fxy060608's avatar
fxy060608 已提交
65 66 67 68 69
      // 自动挂载 $store
      if (options.store && !Vue.prototype.$store) {
        Vue.prototype.$store = options.store
      }

fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75
      const wxs = options.wxs
      if (wxs) {
        Object.keys(wxs).forEach(module => {
          this[module] = wxs[module]
        })
      }
fxy060608's avatar
fxy060608 已提交
76

fxy060608's avatar
fxy060608 已提交
77 78 79 80
      if (this.mpType === 'page') {
        this.$scope = this.$options.pageInstance
        this.$scope.$vm = this
        delete this.$options.pageInstance
fxy060608's avatar
fxy060608 已提交
81 82 83 84

        const route = this.$scope.route
        const pageId = this.$scope.$page.id
        // 通知页面已开始创建
85
        this._$vd.sendPageCreate([pageId, route, parsePageCreateOptions(this, route)])
fxy060608's avatar
fxy060608 已提交
86 87 88 89
      }
    },
    created () {
      if (this.mpType === 'page') {
fxy060608's avatar
fxy060608 已提交
90 91
        // 理论上应该从最开始的 parseQuery 的地方直接 decode 两次,为了减少影响范围,先仅处理 onLoad 参数
        callPageHook(this.$scope, 'onLoad', decodedQuery(this.$options.pageQuery))
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
        callPageHook(this.$scope, 'onShow')
      }
    },
    beforeDestroy () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onUnload')
      }
    },
    mounted () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onReady')
      }
    }
  })
fxy060608's avatar
fxy060608 已提交
106
}