app.js 2.7 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 7 8 9 10 11 12 13 14 15 16 17 18
import initOn from 'uni-core/service/bridge/on'

import {
  getCurrentPages
} from './page'

import {
  registerPlusMessage
} from './plus-message'

import {
  isTabBarPage
} from '../api/util'

Q
qiang 已提交
19 20 21 22 23
import tabBar from './tab-bar'

import {
  publish
} from '../bridge'
fxy060608's avatar
fxy060608 已提交
24

fxy060608's avatar
fxy060608 已提交
25 26
let appCtx

fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32 33 34 35 36
const NETWORK_TYPES = [
  'unknown',
  'none',
  'ethernet',
  'wifi',
  '2g',
  '3g',
  '4g'
]

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

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

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

  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 已提交
61
    publish('onNetworkStatusChange', {
fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67
      isConnected: networkType !== 'none',
      networkType
    })
  })
}

fxy060608's avatar
fxy060608 已提交
68
function initAppLaunch (appVm) {
fxy060608's avatar
fxy060608 已提交
69 70 71 72 73 74 75 76
  const args = {
    path: __uniConfig.entryPagePath,
    query: {},
    scene: 1001
  }

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

fxy060608's avatar
fxy060608 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
function initTabBar () {
  if (!__uniConfig.tabBar || !__uniConfig.tabBar.list.length) {
    return
  }

  const currentTab = isTabBarPage(__uniConfig.entryPagePath)
  if (currentTab) {
    // 取当前 tab 索引值
    __uniConfig.tabBar.selected = __uniConfig.tabBar.list.indexOf(currentTab)
    // 如果真实的首页与 condition 都是 tabbar,无需启用 realEntryPagePath 机制
    if (__uniConfig.realEntryPagePath && isTabBarPage(__uniConfig.realEntryPagePath)) {
      delete __uniConfig.realEntryPagePath
    }
  }

  __uniConfig.__ready__ = true

  const onLaunchWebviewReady = function onLaunchWebviewReady () {
    const tabBarView = tabBar.init(__uniConfig.tabBar, (item) => {
      uni.switchTab({
        url: '/' + item.pagePath,
        openType: 'switchTab',
        from: 'tabbar'
      })
    })
    tabBarView && plus.webview.getLaunchWebview().append(tabBarView)
  }
  if (plus.webview.getLaunchWebview()) {
    onLaunchWebviewReady()
  } else {
    registerPlusMessage('UniWebviewReady-' + plus.runtime.appid, onLaunchWebviewReady, false)
  }
}

export function registerApp (appVm) {
fxy060608's avatar
fxy060608 已提交
114 115 116
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] registerApp`)
  }
fxy060608's avatar
fxy060608 已提交
117

fxy060608's avatar
fxy060608 已提交
118
  appCtx = appVm
fxy060608's avatar
fxy060608 已提交
119

fxy060608's avatar
fxy060608 已提交
120 121 122 123 124 125 126 127
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })

  initAppLaunch(appVm)

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

fxy060608's avatar
fxy060608 已提交
129
  initTabBar()
fxy060608's avatar
fxy060608 已提交
130
}