z8018
9 天以前 d5317aef1dbb595923af02ede8bfa33ba37d6eb6
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<template>
    <!-- 主容器 - 带入场动画 -->
    <transition :name="animationType">
        <view v-if="visible" class="super-toast" :class="[position, type, { 'has-icon': showIcon }]"
            :style="toastStyle">
            <!-- 图标容器(左侧) -->
            <view v-if="showIcon" class="icon-box" :style="iconBoxStyle">
                <template v-if="!isLoading">
                    <uni-icons v-if="!customIcon" :type="iconTypeMap[type]" size="22" :color="iconColor" />
                    <image v-else :src="customIcon" class="custom-icon" mode="aspectFit" />
                </template>
                <uni-icons v-else type="refresh" size="22" :color="iconColor" class="loading-rotate" />
            </view>
 
            <!-- 文字内容(中间) -->
            <text class="content-text" :style="{ color: textColor }">
                {{ message }}
            </text>
 
            <!-- 关闭按钮(右侧) -->
            <view v-if="showClose" class="close-btn" @click="handleClose">
                <uni-icons type="closeempty" size="16" :color="closeColor" />
            </view>
 
            <!-- 进度条(底部) -->
            <view v-if="showProgressBar" class="progress-bar" :style="progressBarStyle"></view>
        </view>
    </transition>
</template>
 
<script>
    const DEFAULT_DURATION = 2000
    const TYPE_CONFIG = {
        info: {
            icon: 'info',
            bg: 'rgba(24, 144, 255, 0.9)',
            iconColor: '#fff',
            progressColor: '#1890ff'
        },
        success: {
            icon: 'checkmarkempty',
            bg: 'rgba(82, 196, 26, 0.9)',
            iconColor: '#fff',
            progressColor: '#52c41a'
        },
        warning: {
            icon: 'alert',
            bg: 'rgba(250, 173, 20, 0.9)',
            iconColor: '#fff',
            progressColor: '#faad14'
        },
        error: {
            icon: 'closeempty',
            bg: 'rgba(245, 34, 45, 0.9)',
            iconColor: '#fff',
            progressColor: '#f5222d'
        },
        loading: {
            icon: 'refresh',
            bg: 'rgba(24, 144, 255, 0.9)',
            iconColor: '#fff',
            progressColor: '#1890ff'
        }
    }
 
    export default {
        name: 'SuperToast',
        props: {
        },
        data() {
            return {
                visible: false,
                progress: 100,
                timer: null,
                progressTimer: null,
                message: '',
                type: 'info',
                position: 'center',
                duration: 2000,
                bgColor: '',
                textColor: '#fff',
                iconColor: '',
                closeColor: 'rgba(255,255,255,0.7)',
                progressColor: '',
                showIcon: true,
                showClose: false,
                showProgressBar: true,
                customIcon: '',
                isLoading: false,
                animationType: 'fade'
            }
        },
        computed: {
            toastStyle() {
                const typeConfig = TYPE_CONFIG[this.type]
                return {
                    backgroundColor: this.bgColor || typeConfig.bg,
                    boxShadow: `0 4px 12px ${this.bgColor || typeConfig.bg}33`
                }
            },
            iconBoxStyle() {
                return {
                    backgroundColor: this.bgColor ?
                        `${this.bgColor}33` : `${TYPE_CONFIG[this.type].bg}33`
                }
            },
            progressBarStyle() {
                const typeConfig = TYPE_CONFIG[this.type]
                return {
                    width: `${this.progress}%`,
                    backgroundColor: this.progressColor || typeConfig.progressColor
                }
            },
            iconTypeMap() {
                return Object.fromEntries(
                    Object.entries(TYPE_CONFIG).map(([key, val]) => [key, val.icon])
                )
            }
        },
        methods: {
            show(config) {
                this.message = config.message;
                this.clearTimers()
                this.progress = 100 // 重置进度条
                this.visible = true
                this.startCountdown()
            },
            hide() {
                this.visible = false
                this.clearTimers()
                this.$emit('close')
            },
            handleClose() {
                this.hide()
                this.$emit('close-click')
            },
            startCountdown() {
                this.clearTimers()
 
                if (this.duration <= 0) return
 
                // 主计时器
                this.timer = setTimeout(() => {
                    this.hide()
                }, this.duration)
 
                // 进度条动画
                if (this.showProgressBar) {
                    const interval = 50
                    const steps = this.duration / interval
                    const stepSize = 100 / steps
 
                    this.progressTimer = setInterval(() => {
                        this.progress -= stepSize
                        if (this.progress <= 0) {
                            clearInterval(this.progressTimer)
                        }
                    }, interval)
                }
            },
            clearTimers() {
                clearTimeout(this.timer)
                clearInterval(this.progressTimer)
            }
        },
        beforeDestroy() {
            this.clearTimers()
            // 确保DOM节点被移除
            if (this.$el && this.$el.parentNode) {
                this.$el.parentNode.removeChild(this.$el)
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    /* 基础容器样式 */
    .super-toast {
        position: fixed;
        left: 50%;
        transform: translateX(-50%);
        min-width: 280px;
        max-width: 90%;
        padding: 16px 20px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        z-index: 9999;
        box-sizing: border-box;
        transition: all 0.3s ease;
 
        /* 玻璃拟态效果 */
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        background: rgba(255, 255, 255, 0.15);
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        border: 1px solid rgba(255, 255, 255, 0.18);
 
        /* 文字不可选中 */
        user-select: none;
    }
 
    /* 位置调整 */
    .top {
        top: 20%;
        animation: slideDown 0.4s cubic-bezier(0.22, 0.61, 0.36, 1);
    }
 
    .center {
        top: 50%;
        transform: translate(-50%, -50%);
        animation: fadeIn 0.3s ease-out;
    }
 
    .bottom {
        bottom: 15%;
        animation: slideUp 0.4s cubic-bezier(0.22, 0.61, 0.36, 1);
    }
 
    /* 图标区域 */
    .icon-box {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 12px;
        flex-shrink: 0;
 
        .custom-icon {
            width: 20px;
            height: 20px;
        }
 
        .loading-rotate {
            animation: rotate 1s linear infinite;
        }
    }
 
    /* 内容文字 */
    .content-text {
        font-size: 15px;
        line-height: 1.5;
        font-weight: 500;
        flex: 1;
        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    }
 
    /* 关闭按钮 */
    .close-btn {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-left: 8px;
        cursor: pointer;
        transition: all 0.2s;
 
        &:active {
            background: rgba(255, 255, 255, 0.2);
            transform: scale(0.9);
        }
    }
 
    /* 进度条 */
    .progress-bar {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 3px;
        border-radius: 0 0 12px 12px;
        transition: width 0.1s linear;
    }
 
    /* 动画定义 */
    @keyframes fadeIn {
        from {
            opacity: 0;
        }
 
        to {
            opacity: 1;
        }
    }
 
    @keyframes slideDown {
        from {
            opacity: 0;
            transform: translate(-50%, -20px);
        }
 
        to {
            opacity: 1;
            transform: translate(-50%, 0);
        }
    }
 
    @keyframes slideUp {
        from {
            opacity: 0;
            transform: translate(-50%, 20px);
        }
 
        to {
            opacity: 1;
            transform: translate(-50%, 0);
        }
    }
 
    @keyframes rotate {
        from {
            transform: rotate(0deg);
        }
 
        to {
            transform: rotate(360deg);
        }
    }
 
    /* 响应式调整 */
    @media (max-width: 480px) {
        .super-toast {
            min-width: 240px;
            padding: 12px 16px;
 
            .content-text {
                font-size: 14px;
            }
        }
    }
 
    .v-enter-active,
    .v-leave-active {
        transition: all 0.3s ease;
    }
 
    .v-enter-from,
    .v-leave-to {
        opacity: 0;
        transform: translate(-50%, 20px);
    }
 
    .v-enter-to,
    .v-leave-from {
        opacity: 1;
        transform: translate(-50%, 0);
    }
 
    /* 针对不同位置调整动画 */
    .top .v-enter-from,
    .top .v-leave-to {
        transform: translate(-50%, -20px);
    }
 
    .center .v-enter-from,
    .center .v-leave-to {
        transform: translate(-50%, -50%) scale(0.9);
    }
</style>