index.vue 1.67 KB
<template>
  <div v-show="visible"
       class="global-loading"
       :style="{ backgroundColor: overlayColor }"
       @touchmove.prevent>
    <div class="global-loading__panel">
      <van-loading vertical
                   :color="spinnerColor"
                   :size="size"
                   :type="type">
        {{ text }}
      </van-loading>
    </div>
  </div>
</template>

<script>
export default {
	name: "GlobalLoading",
	data() {
		return {
			visible: false,
			text: "加载中...",
			type: "spinner",
			size: "24px",
			spinnerColor: "#ffffff",
			overlayColor: "rgba(0, 0, 0, 0.35)",
		};
	},
	methods: {
		open(options = {}) {
			const config = typeof options === "string" ? { text: options } : options;
			this.text = config.text || config.message || "加载中...";
			this.type = config.type || "spinner";
			this.size = config.size || "24px";
			this.spinnerColor = config.color || "#ffffff";
			this.overlayColor = config.overlayColor || "rgba(0, 0, 0, 0.35)";
			this.visible = true;
		},
		close() {
			this.visible = false;
		},
	},
};
</script>

<style lang="scss" scoped>
.global-loading {
	position: fixed;
	inset: 0;
	z-index: 3000;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 24px;
	box-sizing: border-box;

	&__panel {
		min-width: 96px;
		max-width: min(60vw, 180px);
		padding: 16px 18px;
		border-radius: 12px;
		background: rgba(0, 0, 0, 0.72);
		backdrop-filter: blur(4px);
		box-sizing: border-box;

		::v-deep .van-loading {
			align-items: center;
		}

		::v-deep .van-loading__text {
			margin-top: 10px;
			font-size: 14px;
			line-height: 20px;
			color: #ffffff;
			text-align: center;
			word-break: break-word;
		}
	}
}
</style>