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

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

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
import {
d-u-a's avatar
d-u-a 已提交
22 23
  publish,
  requireNativePlugin
Q
qiang 已提交
24
} from '../bridge'
fxy060608's avatar
fxy060608 已提交
25

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

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

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

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

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

export function getApp ({
  allowDefault = false
} = {}) {
  if (appCtx) { // 真实的 App 已初始化
    return appCtx
  }
  if (allowDefault) { // 返回默认实现
    return defaultApp
  }
fxy060608's avatar
fxy060608 已提交
53 54 55
  console.error(
    '[warn]: getApp() 操作失败,v3模式加速了首页 nvue 的启动速度,当在首页 nvue 中使用 getApp() 不一定可以获取真正的 App 对象。详情请参考:https://uniapp.dcloud.io/collocation/frame/window?id=getapp'
  )
fxy060608's avatar
fxy060608 已提交
56 57
}

fxy060608's avatar
fxy060608 已提交
58
function initGlobalListeners () {
59
  const globalEvent = requireNativePlugin('globalEvent')
fxy060608's avatar
fxy060608 已提交
60
  const emit = UniServiceJSBridge.emit
fxy060608's avatar
fxy060608 已提交
61

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

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

  plus.globalEvent.addEventListener('KeyboardHeightChange', function (event) {
    publish('onKeyboardHeightChange', {
      height: event.height
    })
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92 93 94
  })

  globalEvent.addEventListener('uistylechange', function (event) {
    publish('onUIStyleChange', {
      style: event.uistyle
    })
  })

fxy060608's avatar
fxy060608 已提交
95 96
  globalEvent.addEventListener('uniMPNativeEvent', function (event) {
    publish('uniMPNativeEvent', event)
fxy060608's avatar
fxy060608 已提交
97
  })
98

fxy060608's avatar
fxy060608 已提交
99 100 101
  plus.globalEvent.addEventListener('plusMessage', onPlusMessage)

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

d-u-a's avatar
d-u-a 已提交
105 106 107 108 109 110 111 112
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 已提交
113 114
}

fxy060608's avatar
fxy060608 已提交
115
function initAppLaunch (appVm) {
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121 122 123
  const args = {
    path: __uniConfig.entryPagePath,
    query: {},
    scene: 1001
  }

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

fxy060608's avatar
fxy060608 已提交
126
function initTabBar () {
127
  if (!__uniConfig.tabBar || !__uniConfig.tabBar.list || !__uniConfig.tabBar.list.length) {
fxy060608's avatar
fxy060608 已提交
128 129 130
    return
  }

131 132 133
  __uniConfig.tabBar.selected = 0

  const selected = __uniConfig.tabBar.list.findIndex(page => page.pagePath === __uniConfig.entryPagePath)
fxy060608's avatar
fxy060608 已提交
134

fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140 141 142 143 144 145 146
  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 已提交
147
    })
fxy060608's avatar
fxy060608 已提交
148
  })
fxy060608's avatar
fxy060608 已提交
149 150 151 152 153 154

  if (selected !== -1) {
    // 取当前 tab 索引值
    __uniConfig.tabBar.selected = selected
    selected !== 0 && tabBar.switchTab(__uniConfig.entryPagePath)
  }
fxy060608's avatar
fxy060608 已提交
155 156
}

157 158 159 160
function initEntryPage () {
  let entryPagePath
  let entryPageQuery

fxy060608's avatar
fxy060608 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  const weexPlus = weex.requireModule('plus')

  if (weexPlus.getRedirectInfo) {
    const info = weexPlus.getRedirectInfo() || {}
    entryPagePath = info.path
    entryPageQuery = info.query ? ('?' + info.query) : ''
  } else {
    const argsJsonStr = plus.runtime.arguments
    if (!argsJsonStr) {
      return
    }
    try {
      const args = JSON.parse(argsJsonStr)
      entryPagePath = args.path || args.pathName
      entryPageQuery = args.query ? ('?' + args.query) : ''
    } catch (e) {}
  }

179
  if (!entryPagePath || entryPagePath === __uniConfig.entryPagePath) {
fxy060608's avatar
fxy060608 已提交
180 181
    return
  }
182 183 184

  const entryRoute = '/' + entryPagePath
  const routeOptions = __uniRoutes.find(route => route.path === entryRoute)
fxy060608's avatar
fxy060608 已提交
185 186 187
  if (!routeOptions) {
    return
  }
188

fxy060608's avatar
fxy060608 已提交
189 190 191
  if (!routeOptions.meta.isTabBar) {
    __uniConfig.realEntryPagePath = __uniConfig.realEntryPagePath || __uniConfig.entryPagePath
  }
192 193 194 195

  __uniConfig.entryPagePath = entryPagePath
  __uniConfig.entryPageQuery = entryPageQuery

fxy060608's avatar
fxy060608 已提交
196
  if (process.env.NODE_ENV !== 'production') {
197
    console.log(`[uni-app] entryPagePath(${entryPagePath + entryPageQuery})`)
fxy060608's avatar
fxy060608 已提交
198 199 200
  }
}

fxy060608's avatar
fxy060608 已提交
201
export function registerApp (appVm) {
fxy060608's avatar
fxy060608 已提交
202
  if (process.env.NODE_ENV !== 'production') {
fxy060608's avatar
fxy060608 已提交
203
    console.log('[uni-app] registerApp')
fxy060608's avatar
fxy060608 已提交
204
  }
fxy060608's avatar
fxy060608 已提交
205 206

  appCtx = appVm
fxy060608's avatar
fxy060608 已提交
207
  appCtx.$vm = appVm
208

fxy060608's avatar
fxy060608 已提交
209 210 211 212 213
  Object.assign(appCtx, defaultApp) // 拷贝默认实现

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

fxy060608's avatar
fxy060608 已提交
215 216 217 218 219
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })

220
  initEntryPage()
fxy060608's avatar
fxy060608 已提交
221

fxy060608's avatar
init v3  
fxy060608 已提交
222
  initTabBar()
fxy060608's avatar
fxy060608 已提交
223 224

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

fxy060608's avatar
init v3  
fxy060608 已提交
226 227 228 229
  initSubscribeHandlers()

  initAppLaunch(appVm)

fxy060608's avatar
fxy060608 已提交
230
  __uniConfig.ready = true
fxy060608's avatar
fxy060608 已提交
231

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