<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>
|