wangrong
10 天以前 a611c225b17219e873b60888e03c330fbe0c57a4
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<template>
    <view class="cl-toast__wrap">
        <template v-for="item in list" :key="item.id">
            <cl-popup
                v-model="item.visible"
                :padding="0"
                :modal="false"
                :direction="item.position"
                background-color="transparent"
            >
                <view
                    class="cl-toast"
                    :class="{
                        'is-icon': item.image || item.icon,
                    }"
                >
                    <view class="cl-toast__icon" v-if="item.image || item.icon">
                        <image
                            :src="item.image.url"
                            :mode="item.image.mode || 'aspectFill'"
                            :style="item.image.style"
                            v-if="item.image"
                        />
                        <text :class="item.icon" v-else></text>
                    </view>
 
                    <slot>
                        <text class="cl-toast__content">{{ item.message }}</text>
                    </slot>
                </view>
            </cl-popup>
        </template>
    </view>
</template>
 
<script lang="ts">
import { defineComponent, ref } from "vue";
import { isFunction, last, has } from "lodash-es";
 
export default defineComponent({
    name: "cl-toast",
 
    setup() {
        let id = 0;
 
        // 列表
        const list = ref<any[]>([]);
 
        // 打开
        function open(d: ClToast.Options) {
            // 默认
            const options = {
                id: id++,
                visible: false,
                closed: false,
                icon: "",
                image: null,
                message: "",
                duration: 2000,
                type: "",
                position: "center",
                clear: false,
            };
 
            // 合并
            if (has(d, "message")) {
                Object.assign(options, d);
            } else {
                options.message = String(d);
            }
 
            switch (options.type) {
                case "success":
                    options.icon = "cl-icon-check-border";
                    break;
 
                case "warning":
                    options.icon = "cl-icon-warning-border";
                    break;
 
                case "info":
                    options.icon = "cl-icon-help-border";
                    break;
 
                case "error":
                    options.icon = "cl-icon-close-border";
                    break;
            }
 
            // 是否清除之前的
            if (options.clear) {
                list.value = [options];
            } else {
                list.value.push(options);
            }
 
            // 有图标默认居中显示
            if (options.image || options.icon) {
                options.position = "center";
            }
 
            setTimeout(() => {
                create(last(list.value));
            }, 50);
        }
 
        // 关闭
        function close(item: any) {
            // 清空计时器
            clearTimeout(item.timer);
            item.visible = false;
 
            // 关闭回调
            if (isFunction(item.onClose)) {
                item.onClose();
            }
 
            setTimeout(() => {
                item.closed = true;
            }, 300);
        }
 
        // 创建
        function create(item: any) {
            const { duration } = item || {};
 
            if (duration > 0) {
                item.visible = true;
 
                item.timer = setTimeout(() => {
                    close(item);
                }, duration);
            }
        }
 
        return {
            list,
            open,
            close,
            create,
        };
    },
});
</script>