app.js 3.9 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
  }
Q
qiang 已提交
52
  console.error('[warn]: getApp() 操作失败,v3模式加速了首页 nvue 的启动速度,当在首页 nvue 中使用 getApp() 不一定可以获取真正的 App 对象。详情请参考:https://uniapp.dcloud.io/collocation/frame/window?id=getapp')
fxy060608's avatar
fxy060608 已提交
53 54
}

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

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

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

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

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

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

d-u-a's avatar
d-u-a 已提交
91 92 93 94 95 96 97 98
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 已提交
99 100
}

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

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

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

117 118 119 120
  __uniConfig.tabBar.selected = 0

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

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

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

146 147
  appCtx = appVm

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

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

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

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

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

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

  initAppLaunch(appVm)

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

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