#
zhou zhou
1 天以前 979bc5d5616d8f43681251a93a546008c83a4683
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
import { fourDotsSpinnerSvg } from '@/assets/svg/loading'
const getLoadingBackground = () => {
  const isDark = document.documentElement.classList.contains('dark')
  return isDark ? 'rgba(7, 7, 7, 0.85)' : '#fff'
}
const DEFAULT_LOADING_CONFIG = {
  lock: true,
  get background() {
    return getLoadingBackground()
  },
  svg: fourDotsSpinnerSvg,
  svgViewBox: '0 0 40 40',
  customClass: 'art-loading-fix'
}
let loadingInstance = null
let loadingTimer = null
const DEFAULT_LOADING_DELAY = 180
const loadingService = {
  /**
   * 显示 loading
   * @returns 关闭 loading 的函数
   */
  showLoading() {
    if (!loadingInstance && !loadingTimer) {
      loadingTimer = setTimeout(() => {
        loadingTimer = null
        if (loadingInstance) {
          return
        }
        const config = {
          ...DEFAULT_LOADING_CONFIG,
          background: getLoadingBackground()
        }
        loadingInstance = ElLoading.service(config)
      }, DEFAULT_LOADING_DELAY)
    }
    return () => this.hideLoading()
  },
  /**
   * 隐藏 loading
   */
  hideLoading() {
    if (loadingTimer) {
      clearTimeout(loadingTimer)
      loadingTimer = null
    }
    if (loadingInstance) {
      loadingInstance.close()
      loadingInstance = null
    }
  }
}
export { loadingService }