app.js 3.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  callAppHook
} from 'uni-core/service/plugins/util'

fxy060608's avatar
fxy060608 已提交
5 6
import initOn from 'uni-core/service/bridge/on'

fxy060608's avatar
init v3  
fxy060608 已提交
7 8 9 10
import {
  NETWORK_TYPES
} from '../api/constants'

fxy060608's avatar
fxy060608 已提交
11 12 13 14 15
import {
  getCurrentPages
} from './page'

import {
16
  consumePlusMessage
fxy060608's avatar
fxy060608 已提交
17 18
} from './plus-message'

19 20
import tabBar from './tab-bar'

Q
qiang 已提交
21 22 23
import {
  publish
} from '../bridge'
fxy060608's avatar
fxy060608 已提交
24

fxy060608's avatar
init v3  
fxy060608 已提交
25 26 27 28 29 30 31
import {
  initSubscribeHandlers
} from './subscribe-handlers'

import {
  perf
} from './perf'
fxy060608's avatar
fxy060608 已提交
32

fxy060608's avatar
init v3  
fxy060608 已提交
33
let appCtx
fxy060608's avatar
fxy060608 已提交
34

fxy060608's avatar
fxy060608 已提交
35 36 37 38
export function getApp () {
  return appCtx
}

fxy060608's avatar
fxy060608 已提交
39
function initGlobalListeners () {
fxy060608's avatar
fxy060608 已提交
40 41
  const emit = UniServiceJSBridge.emit

fxy060608's avatar
fxy060608 已提交
42
  plus.key.addEventListener('backbutton', () => {
fxy060608's avatar
fxy060608 已提交
43 44 45
    uni.navigateBack({
      from: 'backbutton'
    })
fxy060608's avatar
fxy060608 已提交
46
  })
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52 53 54 55 56 57

  plus.globalEvent.addEventListener('pause', () => {
    emit('onAppEnterBackground')
  })

  plus.globalEvent.addEventListener('resume', () => {
    emit('onAppEnterForeground')
  })

  plus.globalEvent.addEventListener('netchange', () => {
    const networkType = NETWORK_TYPES[plus.networkinfo.getCurrentType()]
Q
qiang 已提交
58
    publish('onNetworkStatusChange', {
fxy060608's avatar
fxy060608 已提交
59 60 61
      isConnected: networkType !== 'none',
      networkType
    })
62 63 64 65 66 67 68 69 70 71
  })

  plus.globalEvent.addEventListener('KeyboardHeightChange', function (event) {
    publish('onKeyboardHeightChange', {
      height: event.height
    })
  })

  plus.globalEvent.addEventListener('plusMessage', function (e) {
    if (process.env.NODE_ENV !== 'production') {
fxy060608's avatar
init v3  
fxy060608 已提交
72
      console.log('[plusMessage]:[' + Date.now() + ']' + JSON.stringify(e.data))
73 74 75 76 77
    }
    if (e.data && e.data.type) {
      const type = e.data.type
      consumePlusMessage(type, e.data.args || {})
    }
fxy060608's avatar
fxy060608 已提交
78 79 80
  })
}

fxy060608's avatar
fxy060608 已提交
81
function initAppLaunch (appVm) {
fxy060608's avatar
fxy060608 已提交
82 83 84 85 86 87 88 89
  const args = {
    path: __uniConfig.entryPagePath,
    query: {},
    scene: 1001
  }

  callAppHook(appVm, 'onLaunch', args)
  callAppHook(appVm, 'onShow', args)
fxy060608's avatar
fxy060608 已提交
90 91
}

fxy060608's avatar
fxy060608 已提交
92 93 94 95 96
function initTabBar () {
  if (!__uniConfig.tabBar || !__uniConfig.tabBar.list.length) {
    return
  }

97 98 99 100
  __uniConfig.tabBar.selected = 0

  const selected = __uniConfig.tabBar.list.findIndex(page => page.pagePath === __uniConfig.entryPagePath)
  if (selected !== -1) {
fxy060608's avatar
fxy060608 已提交
101
    // 取当前 tab 索引值
102
    __uniConfig.tabBar.selected = selected
fxy060608's avatar
fxy060608 已提交
103 104
  }

fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110 111 112 113 114 115 116
  tabBar.init(__uniConfig.tabBar, (item, index) => {
    uni.switchTab({
      url: '/' + item.pagePath,
      openType: 'switchTab',
      from: 'tabBar',
      success () {
        UniServiceJSBridge.emit('onTabItemTap', {
          index,
          text: item.text,
          pagePath: item.pagePath
        })
      }
fxy060608's avatar
fxy060608 已提交
117
    })
fxy060608's avatar
fxy060608 已提交
118
  })
fxy060608's avatar
fxy060608 已提交
119 120 121
}

export function registerApp (appVm) {
fxy060608's avatar
fxy060608 已提交
122 123 124
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] registerApp`)
  }
fxy060608's avatar
fxy060608 已提交
125

126 127
  appCtx = appVm

fxy060608's avatar
fxy060608 已提交
128
  appCtx.globalData = appVm.$options.globalData || {}
fxy060608's avatar
fxy060608 已提交
129

fxy060608's avatar
fxy060608 已提交
130 131 132 133 134
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })

fxy060608's avatar
init v3  
fxy060608 已提交
135
  initTabBar()
fxy060608's avatar
fxy060608 已提交
136 137

  initGlobalListeners()
fxy060608's avatar
fxy060608 已提交
138

fxy060608's avatar
init v3  
fxy060608 已提交
139 140 141 142
  initSubscribeHandlers()

  initAppLaunch(appVm)

fxy060608's avatar
fxy060608 已提交
143 144
  __uniConfig.ready = true

fxy060608's avatar
init v3  
fxy060608 已提交
145
  process.env.NODE_ENV !== 'production' && perf('registerApp')
fxy060608's avatar
fxy060608 已提交
146
}