loading-util.ts 1.6 KB
Newer Older
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
import { ILoadingUtil } from '@ibiz-template/runtime';
import Vue from 'vue';

/**
 * 全局加载动画工具
 *
 * @author chitanda
 * @date 2022-08-17 17:08:44
 * @export
 * @class LoadingUtil
 * @implements {ILoadingUtil}
 */
export class LoadingUtil implements ILoadingUtil {
  /**
   * 当前只在触发的全局加载次数
   *
   * @author chitanda
   * @date 2022-08-17 17:08:44
   * @protected
   */
  protected count = 0;

  /**
   * 当前在触发的重定向加载动画次数
   *
   * @author chitanda
   * @date 2022-10-08 17:10:02
   * @protected
   */
  protected countRedirect = 0;

  /**
   * 显示全局加载动画
   *
   * @author chitanda
   * @date 2022-08-17 17:08:41
   * @param {string} [message='加载中...']
   */
  show(): void {
    if (this.count === 0) {
      Vue.prototype.$Spin.show();
    }
    this.count += 1;
  }

  /**
   * 隐藏全局加载动画
   *
   * @author chitanda
   * @date 2022-08-17 17:08:11
   */
  hide(): void {
    if (this.count > 0) {
      this.count -= 1;
    }
    if (this.count === 0) {
      Vue.prototype.$Spin.hide();
    }
  }

  /**
   * 显示顶部加载动画
   *
   * @author chitanda
   * @date 2022-10-08 16:10:01
   */
  showRedirect(): void {
    if (this.countRedirect === 0) {
      Vue.prototype.$Loading.start();
    }
    this.countRedirect += 1;
  }

  /**
   * 隐藏顶部加载动画
   *
   * @author chitanda
   * @date 2022-10-08 16:10:09
   */
  hideRedirect(): void {
    if (this.countRedirect > 0) {
      this.countRedirect -= 1;
    }
    if (this.countRedirect === 0) {
      Vue.prototype.$Loading.finish();
    }
  }
}