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 19 20
import initOn from 'uni-core/service/bridge/on'

import {
  getCurrentPages
} from './page'

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

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

import tabBar from './tab-bar'

fxy060608's avatar
fxy060608 已提交
21 22
let appCtx

fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31 32
const NETWORK_TYPES = [
  'unknown',
  'none',
  'ethernet',
  'wifi',
  '2g',
  '3g',
  '4g'
]

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

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

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

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

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

  plus.globalEvent.addEventListener('netchange', () => {
    const networkType = NETWORK_TYPES[plus.networkinfo.getCurrentType()]
    emit('onNetworkStatusChange', {
      isConnected: networkType !== 'none',
      networkType
    })
  })
}

fxy060608's avatar
fxy060608 已提交
64
function initAppLaunch (appVm) {
fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70 71 72
  const args = {
    path: __uniConfig.entryPagePath,
    query: {},
    scene: 1001
  }

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

fxy060608's avatar
fxy060608 已提交
75 76 77 78 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
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 已提交
110 111 112
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] registerApp`)
  }
fxy060608's avatar
fxy060608 已提交
113

fxy060608's avatar
fxy060608 已提交
114
  appCtx = appVm
fxy060608's avatar
fxy060608 已提交
115

fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121 122 123
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })

  initAppLaunch(appVm)

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

fxy060608's avatar
fxy060608 已提交
125
  initTabBar()
fxy060608's avatar
fxy060608 已提交
126
}