You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
|
3 weeks ago
|
export const LOADING_SHOW_EVENT = 'common-loading:show'
|
||
|
|
export const LOADING_HIDE_EVENT = 'common-loading:hide'
|
||
|
|
|
||
|
|
const defaultOptions = {
|
||
|
|
title: '加载中...',
|
||
|
|
mask: true
|
||
|
|
}
|
||
|
|
|
||
|
|
const loadingState = {
|
||
|
|
visible: false,
|
||
|
|
title: defaultOptions.title,
|
||
|
|
mask: defaultOptions.mask
|
||
|
|
}
|
||
|
|
|
||
|
|
let installed = false
|
||
|
|
let nativeShowLoading = null
|
||
|
|
let nativeHideLoading = null
|
||
|
|
|
||
|
|
function normalizeOptions(options = {}) {
|
||
|
|
if (typeof options === 'string') {
|
||
|
|
return {
|
||
|
|
...defaultOptions,
|
||
|
|
title: options
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
...defaultOptions,
|
||
|
|
...options,
|
||
|
|
title: options.title || defaultOptions.title,
|
||
|
|
mask: options.mask !== false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getLoadingState() {
|
||
|
|
return {
|
||
|
|
...loadingState
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function showLoading(options) {
|
||
|
|
const loadingOptions = normalizeOptions(options)
|
||
|
|
loadingState.visible = true
|
||
|
|
loadingState.title = loadingOptions.title
|
||
|
|
loadingState.mask = loadingOptions.mask
|
||
|
|
uni.$emit(LOADING_SHOW_EVENT, loadingOptions)
|
||
|
|
loadingOptions.success && loadingOptions.success({ errMsg: 'showLoading:ok' })
|
||
|
|
loadingOptions.complete && loadingOptions.complete({ errMsg: 'showLoading:ok' })
|
||
|
|
}
|
||
|
|
|
||
|
|
export function hideLoading(options = {}) {
|
||
|
|
loadingState.visible = false
|
||
|
|
uni.$emit(LOADING_HIDE_EVENT)
|
||
|
|
nativeHideLoading && nativeHideLoading.call(uni)
|
||
|
|
options.success && options.success({ errMsg: 'hideLoading:ok' })
|
||
|
|
options.complete && options.complete({ errMsg: 'hideLoading:ok' })
|
||
|
|
}
|
||
|
|
|
||
|
|
export function installLoading() {
|
||
|
|
if (installed) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
installed = true
|
||
|
|
nativeShowLoading = uni.showLoading
|
||
|
|
nativeHideLoading = uni.hideLoading
|
||
|
|
uni.$nativeShowLoading = nativeShowLoading
|
||
|
|
uni.$nativeHideLoading = nativeHideLoading
|
||
|
|
uni.showLoading = showLoading
|
||
|
|
uni.hideLoading = hideLoading
|
||
|
|
}
|
||
|
|
|
||
|
|
export default {
|
||
|
|
installLoading,
|
||
|
|
showLoading,
|
||
|
|
hideLoading,
|
||
|
|
getLoadingState
|
||
|
|
}
|