Blame view

src/utils/requestLoading.js 1.25 KB
1 2 3 4 5 6
/*
 * @Description: 定义全局loading
 * @Autor: renchao
 * @LastEditTime: 2023-05-16 14:08:10
 */

任超 committed
7
import Loading from '@/components/Loading/index.js';
yuanbo committed
8
// 定义 loading
9 10 11
let loading

// loading开始 方法
yuanbo committed
12 13 14 15 16
/**
 * @description: loading开始 方法
 * @param {*} loadingText
 * @author: renchao
 */
任超 committed
17
function startLoading (loadingText = '正在加载中...') {
18
  loading = Loading.service({
19
    text: loadingText,
任超 committed
20 21
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)',
任超 committed
22
    target: document.querySelector('.loadingtext')
23 24 25
  })
}
// loading结束 方法
yuanbo committed
26 27 28 29
/**
 * @description: loading结束 方法
 * @author: renchao
 */
30 31 32 33 34 35 36 37 38 39
function endLoading () {
  loading.close()
}
// 定义一个变量,有请求变量加一,收到响应变量减一
let loadingCount = 0

/**
 * 调用一次startLoadingAddCount() 方法 开启Loading 并 loadingCount + 1
 * 调用一次endLoadingSubCount() 方法 loadingCount - 1 直到为0 关闭loading
 */
任超 committed
40
export function startLoadingAddCount (LoadingText, target) {
41
  if (loadingCount === 0) {
任超 committed
42
    startLoading(LoadingText, target)
43 44 45
  }
  loadingCount++
}
yuanbo committed
46 47 48 49
/**
 * @description: endLoadingSubCount
 * @author: renchao
 */
50 51 52 53 54
export function endLoadingSubCount () {
  loadingCount--
  if (loadingCount === 0) {
    endLoading()
  }
yuanbo committed
55
}