'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import {
  initAppLocale
} from 'uni-helpers/i18n'
 
import {
  ON_THEME_CHANGE
} from 'uni-helpers/constants'
 
import {
  callAppHook
} from 'uni-core/service/plugins/util'
 
import initOn from 'uni-core/service/bridge/on'
 
import {
  NETWORK_TYPES,
  TEMP_PATH,
  TEMP_PATH_BASE
} from '../api/constants'
 
import {
  initEntryPage
} from './config'
 
import {
  getCurrentPages,
  getCurrentPageId
} from './page'
 
import {
  consumePlusMessage
} from './plus-message'
 
import tabBar from './tab-bar'
 
import {
  publish,
  requireNativePlugin
} from '../bridge'
 
import {
  initSubscribeHandlers
} from './subscribe-handlers'
 
import {
  perf
} from './perf'
 
import {
  backbuttonListener
} from './backbutton'
 
import {
  getEnterOptions,
  initEnterOptions,
  initLaunchOptions,
  parseRedirectInfo
} from './utils'
 
import { changePagesNavigatorStyle } from './theme'
 
let appCtx
 
const defaultApp = {
  globalData: {}
}
 
export function getApp ({
  allowDefault = false
} = {}) {
  if (appCtx) { // 真实的 App 已初始化
    return appCtx
  }
  if (allowDefault) { // 返回默认实现
    return defaultApp
  }
  console.error(
    '[warn]: getApp() failed. Learn more: https://uniapp.dcloud.io/collocation/frame/window?id=getapp.'
  )
}
 
function initGlobalListeners () {
  const globalEvent = requireNativePlugin('globalEvent')
  const emit = UniServiceJSBridge.emit
 
  if (weex.config.preload) {
    if (process.env.NODE_ENV !== 'production') {
      console.log('[uni-app] preload.addEventListener.backbutton')
    }
    plus.key.addEventListener('backbutton', backbuttonListener)
  } else {
    // splashclosed 时开始监听 backbutton
    plus.globalEvent.addEventListener('splashclosed', () => {
      plus.key.addEventListener('backbutton', backbuttonListener)
    })
  }
 
  plus.globalEvent.addEventListener('pause', () => {
    emit('onAppEnterBackground')
  })
 
  plus.globalEvent.addEventListener('resume', () => {
    const info = parseRedirectInfo()
    if (info && info.userAction) {
      initEnterOptions(info)
    }
    emit('onAppEnterForeground', getEnterOptions())
  })
 
  plus.globalEvent.addEventListener('netchange', () => {
    const networkType = NETWORK_TYPES[plus.networkinfo.getCurrentType()] || 'unknown'
    publish('onNetworkStatusChange', {
      isConnected: networkType !== 'none',
      networkType
    })
  })
 
  let keyboardHeightChange = 0
  plus.globalEvent.addEventListener('KeyboardHeightChange', function (event) {
    // 安卓设备首次获取高度为 0
    if (keyboardHeightChange !== event.height) {
      keyboardHeightChange = event.height
      publish('onKeyboardHeightChange', {
        height: keyboardHeightChange
      })
    }
  })
 
  globalEvent.addEventListener('uistylechange', function (event) {
    const args = {
      theme: event.uistyle
    }
 
    callAppHook(appCtx, ON_THEME_CHANGE, args)
    publish(ON_THEME_CHANGE, args)
    UniServiceJSBridge.publishHandler(ON_THEME_CHANGE, args, getCurrentPageId())
    // 兼容旧版本 API
    publish('onUIStyleChange', {
      style: event.uistyle
    })
    changePagesNavigatorStyle()
  })
 
  globalEvent.addEventListener('uniMPNativeEvent', function (event) {
    publish('uniMPNativeEvent', event)
  })
 
  plus.globalEvent.addEventListener('plusMessage', onPlusMessage)
 
  // nvue webview post message
  plus.globalEvent.addEventListener('WebviewPostMessage', onPlusMessage)
}
 
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 || {})
  }
}
 
function initAppLaunch (appVm) {
  const args = initLaunchOptions({
    path: __uniConfig.entryPagePath,
    query: __uniConfig.entryPageQuery,
    referrerInfo: __uniConfig.referrerInfo
  })
 
  callAppHook(appVm, 'onLaunch', args)
  callAppHook(appVm, 'onShow', args)
  // https://tower.im/teams/226535/todos/16905/
  const getAppState = weex.requireModule('plus').getAppState
  const appState = getAppState && Number(getAppState())
  if (appState === 2) {
    callAppHook(appVm, 'onHide', args)
  }
}
 
function initTabBar () {
  if (!__uniConfig.tabBar || !__uniConfig.tabBar.list || !__uniConfig.tabBar.list.length) {
    return
  }
 
  __uniConfig.tabBar.selected = 0
 
  const selected = __uniConfig.tabBar.list.findIndex(page => page.pagePath === __uniConfig.entryPagePath)
 
  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
        })
      }
    })
  })
 
  if (selected !== -1) {
    // 取当前 tab 索引值
    __uniConfig.tabBar.selected = selected
    selected !== 0 && tabBar.switchTab(__uniConfig.entryPagePath)
  }
}
 
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()
          }
        })
      }
    })
  })
}
 
export function registerApp (appVm, Vue) {
  if (process.env.NODE_ENV !== 'production') {
    console.log('[uni-app] registerApp')
  }
  appCtx = appVm
  appCtx.$vm = appVm
  initAppLocale(Vue, appVm)
 
  Object.assign(appCtx, defaultApp) // 拷贝默认实现
 
  const globalData = appVm.$options.globalData || {}
  // merge globalData
  appCtx.globalData = Object.assign(globalData, appCtx.globalData)
 
  initOn(UniServiceJSBridge.on, {
    getApp,
    getCurrentPages
  })
 
  initEntryPage()
 
  initTabBar()
 
  initGlobalListeners()
 
  initSubscribeHandlers()
 
  initAppLaunch(appVm)
 
  // 10s后清理临时文件
  setTimeout(clearTempFile, 10000)
 
  __uniConfig.ready = true
 
  process.env.NODE_ENV !== 'production' && perf('registerApp')
}