app.js 3.6 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
import {
  initSubscribeHandlers
} from './subscribe-handlers'

import {
  perf
fxy060608's avatar
fxy060608 已提交
31
} from './perf'
雪洛's avatar
雪洛 已提交
32 33 34 35

import {
  backbuttonListener
} from './backbutton'
fxy060608's avatar
fxy060608 已提交
36

fxy060608's avatar
init v3  
fxy060608 已提交
37
let appCtx
fxy060608's avatar
fxy060608 已提交
38

fxy060608's avatar
fxy060608 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
const defaultApp = {
  globalData: {}
}

export function getApp ({
  allowDefault = false
} = {}) {
  if (appCtx) { // 真实的 App 已初始化
    return appCtx
  }
  if (allowDefault) { // 返回默认实现
    return defaultApp
  }
fxy060608's avatar
fxy060608 已提交
52 53
}

fxy060608's avatar
fxy060608 已提交
54
function initGlobalListeners () {
fxy060608's avatar
fxy060608 已提交
55
  const emit = UniServiceJSBridge.emit
fxy060608's avatar
fxy060608 已提交
56

fxy060608's avatar
fxy060608 已提交
57 58 59 60
  // splashclosed 时开始监听 backbutton
  plus.globalEvent.addEventListener('splashclosed', () => {
    plus.key.addEventListener('backbutton', backbuttonListener)
  })
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69 70 71

  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 已提交
72
    publish('onNetworkStatusChange', {
fxy060608's avatar
fxy060608 已提交
73 74 75
      isConnected: networkType !== 'none',
      networkType
    })
76 77 78 79 80 81 82 83
  })

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

fxy060608's avatar
fxy060608 已提交
84 85 86
  plus.globalEvent.addEventListener('plusMessage', onPlusMessage)

  // nvue webview post message
d-u-a's avatar
d-u-a 已提交
87
  plus.globalEvent.addEventListener('WebviewPostMessage', onPlusMessage)
fxy060608's avatar
fxy060608 已提交
88 89
}

d-u-a's avatar
d-u-a 已提交
90 91 92 93 94 95 96 97
function onPlusMessage (e) {
  if (process.env.NODE_ENV !== 'production') {
    console.log('[plusMessage]:[' + Date.now() + ']' + JSON.stringify(e.data))
  }
  if (e.data && e.data.type) {
    const type = e.data.type
    consumePlusMessage(type, e.data.args || {})
  }
fxy060608's avatar
fxy060608 已提交
98 99
}

fxy060608's avatar
fxy060608 已提交
100
function initAppLaunch (appVm) {
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105 106 107 108
  const args = {
    path: __uniConfig.entryPagePath,
    query: {},
    scene: 1001
  }

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

fxy060608's avatar
fxy060608 已提交
111 112 113 114 115
function initTabBar () {
  if (!__uniConfig.tabBar || !__uniConfig.tabBar.list.length) {
    return
  }

116 117 118 119
  __uniConfig.tabBar.selected = 0

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

fxy060608's avatar
fxy060608 已提交
124 125 126 127 128 129 130 131 132 133 134 135
  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 已提交
136
    })
fxy060608's avatar
fxy060608 已提交
137
  })
fxy060608's avatar
fxy060608 已提交
138 139 140
}

export function registerApp (appVm) {
fxy060608's avatar
fxy060608 已提交
141 142 143
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] registerApp`)
  }
fxy060608's avatar
fxy060608 已提交
144

145 146
  appCtx = appVm

fxy060608's avatar
fxy060608 已提交
147 148 149 150 151
  Object.assign(appCtx, defaultApp) // 拷贝默认实现

  const globalData = appVm.$options.globalData || {}
  // merge globalData
  appCtx.globalData = Object.assign(globalData, appCtx.globalData)
fxy060608's avatar
fxy060608 已提交
152

fxy060608's avatar
fxy060608 已提交
153 154 155 156 157
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })

fxy060608's avatar
init v3  
fxy060608 已提交
158
  initTabBar()
fxy060608's avatar
fxy060608 已提交
159 160

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

fxy060608's avatar
init v3  
fxy060608 已提交
162 163 164 165
  initSubscribeHandlers()

  initAppLaunch(appVm)

fxy060608's avatar
fxy060608 已提交
166
  __uniConfig.ready = true
fxy060608's avatar
fxy060608 已提交
167

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