Commit b21b967b by Jaxon

feat: 全局loading组件调优

1 parent bb4223cd
<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>
import Vue from "vue";
import GlobalLoading from "./index.vue";
const LoadingConstructor = Vue.extend(GlobalLoading);
const DEFAULT_OPTIONS = {
delay: 150,
minDuration: 300,
};
let instance = null;
let pendingCount = 0;
let showTimer = null;
let hideTimer = null;
let latestOptions = { ...DEFAULT_OPTIONS };
let shownAt = 0;
function getInstance() {
if (instance)
return instance;
instance = new LoadingConstructor({
el: document.createElement("div"),
});
document.body.appendChild(instance.$el);
return instance;
}
function normalizeOptions(options = {}) {
const config = typeof options === "string" ? { text: options } : options;
return {
...DEFAULT_OPTIONS,
...config,
};
}
function clearShowTimer() {
if (!showTimer)
return;
clearTimeout(showTimer);
showTimer = null;
}
function clearHideTimer() {
if (!hideTimer)
return;
clearTimeout(hideTimer);
hideTimer = null;
}
function openLoading(options) {
const loadingInstance = getInstance();
loadingInstance.open(options);
shownAt = Date.now();
return loadingInstance;
}
export const loading = {
show(options = {}) {
pendingCount += 1;
const config = normalizeOptions(options);
latestOptions = config;
clearHideTimer();
const loadingInstance = getInstance();
if (loadingInstance.visible) {
loadingInstance.open(config);
return loadingInstance;
}
clearShowTimer();
if (config.delay <= 0)
return openLoading(config);
showTimer = setTimeout(() => {
showTimer = null;
if (pendingCount <= 0)
return;
openLoading(latestOptions);
}, config.delay);
return loadingInstance;
},
hide(force = false) {
if (force) {
pendingCount = 0;
} else {
pendingCount = Math.max(pendingCount - 1, 0);
}
if (pendingCount > 0)
return;
clearShowTimer();
if (!instance || !instance.visible)
return;
clearHideTimer();
const minDuration = Number(latestOptions.minDuration) || 0;
const visibleDuration = Date.now() - shownAt;
const remainingDuration = Math.max(minDuration - visibleDuration, 0);
if (remainingDuration === 0) {
instance.close();
return;
}
hideTimer = setTimeout(() => {
hideTimer = null;
if (pendingCount === 0 && instance)
instance.close();
}, remainingDuration);
},
clear() {
this.hide(true);
},
};
export default {
install(VueInstance) {
if (VueInstance.prototype.$loading)
return;
VueInstance.prototype.$loading = loading;
},
};
......@@ -7,6 +7,9 @@
* @LastEditTime: 2021-06-25 18:09:28
*/
import Vue from "vue";
import loadingPlugin from "./globalLoading/plugin";
Vue.use(loadingPlugin);
const componentsFiles = require.context(`./`, true, /.vue$/);
componentsFiles.keys().forEach((path) => {
......
import dayjs from "dayjs";
import { Dialog, Toast } from "vant";
import Vue from "vue";
import { loading } from "@/components/globalLoading/plugin";
/**
* 日期格式化
......@@ -42,20 +43,15 @@ function throttle(fn, gapTime) {
}
/** 防止重复点击 */
function showLoading(title = "加载中...", callBack, custom) {
Toast({
type: "loading",
message: title,
forbidClick: true,
duration: 0,
});
loading.show({ text: title });
if (callBack && typeof callBack === "function") {
if (!custom)
Toast.clear();
hideLoading();
callBack();
}
}
function hideLoading() {
Toast.clear();
loading.hide(true);
}
/** num:要转换的数值,fixed:精确几位小数 */
function decimal(num, fixed) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!