wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
89
90
<template>
    <cl-loading-mask
        :loading="mask.loading && !mask.disabled && loading"
        :modal="mask.modal"
        :color="mask.color"
        :text="mask.text"
        :fullscreen="mask.fullscreen"
        :background="mask.background"
        :loading-theme="mask.loadingTheme"
    >
        <slot :data="data" :loading="loading"></slot>
    </cl-loading-mask>
</template>
 
<script lang="ts">
import { assign } from "lodash-es";
import type { PropType } from "vue";
import { ref, onMounted, defineComponent, computed } from "vue";
 
interface Mask {
    loading: boolean;
    modal: boolean;
    fullscreen: boolean;
    color: string;
    text: string;
    background: string;
    loadingTheme: string;
    disabled: Boolean;
}
 
export default defineComponent({
    name: "cl-service",
 
    props: {
        service: null,
        mask: {
            type: Object as PropType<Mask>,
            default() {
                return {};
            },
        },
        immediate: {
            type: Boolean,
            default: true,
        },
    },
 
    setup(props) {
        // 数据
        const data = ref();
 
        // 加载中
        const loading = ref(false);
 
        // 遮罩层
        const mask = computed(() => {
            return assign(
                {
                    loading: true,
                    modal: true,
                    fullscreen: false,
                },
                props.mask || {},
            );
        });
 
        // 请求
        async function get() {
            loading.value = true;
            const res = await props.service;
            loading.value = false;
 
            return (data.value = res);
        }
 
        onMounted(() => {
            if (props.immediate) {
                get();
            }
        });
 
        return {
            mask,
            loading,
            data,
            get,
        };
    },
});
</script>