app.js 5.8 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
import {
8 9 10
  NETWORK_TYPES,
  TEMP_PATH,
  TEMP_PATH_BASE
fxy060608's avatar
init v3  
fxy060608 已提交
11 12
} from '../api/constants'

fxy060608's avatar
fxy060608 已提交
13 14 15 16
import {
  initEntryPage
} from './config'

fxy060608's avatar
fxy060608 已提交
17 18 19 20 21
import {
  getCurrentPages
} from './page'

import {
22
  consumePlusMessage
fxy060608's avatar
fxy060608 已提交
23 24
} from './plus-message'

25 26
import tabBar from './tab-bar'

Q
qiang 已提交
27
import {
d-u-a's avatar
d-u-a 已提交
28 29
  publish,
  requireNativePlugin
Q
qiang 已提交
30
} from '../bridge'
fxy060608's avatar
fxy060608 已提交
31

fxy060608's avatar
init v3  
fxy060608 已提交
32 33 34 35 36 37
import {
  initSubscribeHandlers
} from './subscribe-handlers'

import {
  perf
fxy060608's avatar
fxy060608 已提交
38
} from './perf'
雪洛's avatar
雪洛 已提交
39 40 41 42

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

fxy060608's avatar
init v3  
fxy060608 已提交
44
let appCtx
fxy060608's avatar
fxy060608 已提交
45

fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
const defaultApp = {
  globalData: {}
}

export function getApp ({
  allowDefault = false
} = {}) {
  if (appCtx) { // 真实的 App 已初始化
    return appCtx
  }
  if (allowDefault) { // 返回默认实现
    return defaultApp
  }
fxy060608's avatar
fxy060608 已提交
59
  console.error(
Q
qiang 已提交
60
    '[warn]: getApp() failed. Learn more: https://uniapp.dcloud.io/collocation/frame/window?id=getapp.'
fxy060608's avatar
fxy060608 已提交
61
  )
fxy060608's avatar
fxy060608 已提交
62 63
}

fxy060608's avatar
fxy060608 已提交
64
function initGlobalListeners () {
65
  const globalEvent = requireNativePlugin('globalEvent')
fxy060608's avatar
fxy060608 已提交
66
  const emit = UniServiceJSBridge.emit
fxy060608's avatar
fxy060608 已提交
67

fxy060608's avatar
fxy060608 已提交
68 69 70 71
  if (weex.config.preload) {
    if (process.env.NODE_ENV !== 'production') {
      console.log('[uni-app] preload.addEventListener.backbutton')
    }
fxy060608's avatar
fxy060608 已提交
72
    plus.key.addEventListener('backbutton', backbuttonListener)
fxy060608's avatar
fxy060608 已提交
73 74 75 76 77 78
  } else {
    // splashclosed 时开始监听 backbutton
    plus.globalEvent.addEventListener('splashclosed', () => {
      plus.key.addEventListener('backbutton', backbuttonListener)
    })
  }
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83 84 85 86 87 88

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

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

  plus.globalEvent.addEventListener('netchange', () => {
Q
qiang 已提交
89
    const networkType = NETWORK_TYPES[plus.networkinfo.getCurrentType()] || 'unknown'
Q
qiang 已提交
90
    publish('onNetworkStatusChange', {
fxy060608's avatar
fxy060608 已提交
91 92 93
      isConnected: networkType !== 'none',
      networkType
    })
94 95
  })

96
  let keyboardHeightChange = 0
97
  plus.globalEvent.addEventListener('KeyboardHeightChange', function (event) {
98 99 100 101 102 103 104
    // 安卓设备首次获取高度为 0
    if (keyboardHeightChange !== event.height) {
      keyboardHeightChange = event.height
      publish('onKeyboardHeightChange', {
        height: keyboardHeightChange
      })
    }
fxy060608's avatar
fxy060608 已提交
105 106
  })

fxy060608's avatar
fxy060608 已提交
107 108 109 110 111 112 113 114 115 116 117
  globalEvent.addEventListener('uistylechange', function (event) {
    const args = {
      theme: event.uistyle
    }

    callAppHook(appCtx, 'onThemeChange', args)
    publish('onThemeChange', args)

    // 兼容旧版本 API
    publish('onUIStyleChange', {
      style: event.uistyle
fxy060608's avatar
fxy060608 已提交
118 119 120
    })
  })

fxy060608's avatar
fxy060608 已提交
121 122
  globalEvent.addEventListener('uniMPNativeEvent', function (event) {
    publish('uniMPNativeEvent', event)
fxy060608's avatar
fxy060608 已提交
123
  })
124

fxy060608's avatar
fxy060608 已提交
125 126 127
  plus.globalEvent.addEventListener('plusMessage', onPlusMessage)

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

d-u-a's avatar
d-u-a 已提交
131 132 133 134 135 136 137 138
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 已提交
139 140
}

fxy060608's avatar
fxy060608 已提交
141
function initAppLaunch (appVm) {
fxy060608's avatar
fxy060608 已提交
142 143 144 145 146 147 148 149
  const args = {
    path: __uniConfig.entryPagePath,
    query: {},
    scene: 1001
  }

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

fxy060608's avatar
fxy060608 已提交
152
function initTabBar () {
153
  if (!__uniConfig.tabBar || !__uniConfig.tabBar.list || !__uniConfig.tabBar.list.length) {
fxy060608's avatar
fxy060608 已提交
154 155 156
    return
  }

157 158 159
  __uniConfig.tabBar.selected = 0

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

fxy060608's avatar
fxy060608 已提交
161 162 163 164 165 166 167 168 169 170 171 172
  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 已提交
173
    })
fxy060608's avatar
fxy060608 已提交
174
  })
fxy060608's avatar
fxy060608 已提交
175 176 177 178 179 180

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

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
export function clearTempFile () {
  // 统一处理路径
  function getPath (path) {
    path = path.replace(/\/$/, '')
    return path.indexOf('_') === 0 ? plus.io.convertLocalFileSystemURL(path) : path
  }
  var basePath = getPath(TEMP_PATH_BASE)
  var tempPath = getPath(TEMP_PATH)
  // 获取父目录
  var dirPath = tempPath.split('/')
  dirPath.pop()
  dirPath = dirPath.join('/')
  plus.io.resolveLocalFileSystemURL(plus.io.convertAbsoluteFileSystem(dirPath), entry => {
    var reader = entry.createReader()
    reader.readEntries(function (entries) {
      if (entries && entries.length) {
        entries.forEach(function (entry) {
          if (entry.isDirectory && entry.fullPath.indexOf(basePath) === 0 && entry.fullPath
            .indexOf(tempPath) !== 0) {
            entry.removeRecursively()
          }
        })
      }
    })
  })
}

fxy060608's avatar
fxy060608 已提交
210
export function registerApp (appVm) {
fxy060608's avatar
fxy060608 已提交
211
  if (process.env.NODE_ENV !== 'production') {
fxy060608's avatar
fxy060608 已提交
212
    console.log('[uni-app] registerApp')
fxy060608's avatar
fxy060608 已提交
213
  }
fxy060608's avatar
fxy060608 已提交
214
  appCtx = appVm
fxy060608's avatar
fxy060608 已提交
215
  appCtx.$vm = appVm
216

fxy060608's avatar
fxy060608 已提交
217 218 219 220 221
  Object.assign(appCtx, defaultApp) // 拷贝默认实现

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

fxy060608's avatar
fxy060608 已提交
223 224 225 226 227
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })

228
  initEntryPage()
fxy060608's avatar
fxy060608 已提交
229

fxy060608's avatar
init v3  
fxy060608 已提交
230
  initTabBar()
fxy060608's avatar
fxy060608 已提交
231 232

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

fxy060608's avatar
init v3  
fxy060608 已提交
234 235 236 237
  initSubscribeHandlers()

  initAppLaunch(appVm)

238 239 240
  // 10s后清理临时文件
  setTimeout(clearTempFile, 10000)

fxy060608's avatar
fxy060608 已提交
241
  __uniConfig.ready = true
fxy060608's avatar
fxy060608 已提交
242

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