CustomToast.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <!-- 自定义提示组件 -->
  3. <!-- 根据type展示成功、失败状态提示 -->
  4. <!-- 根据title/remark,配置提示的文字 -->
  5. <!-- 全局采用ref方式调用打开按钮,默认展示1500ms后关闭,也可以直接调用关闭事件-->
  6. <view class="custom-toast">
  7. <zwy-popup :ishide="isShow" width="660rpx" height="364rpx" radius="12rpx">
  8. <!-- 自定义展示内容 -->
  9. <view class="content">
  10. <view class="bg-box">
  11. <image style="width: 100%;height: 100%;" src="/static/images/components/modal/bg.svg" mode="scaleToFill"></image>
  12. </view>
  13. <view class="icon">
  14. <image style="width: 100%;height: 100%;" :src="`/static/images/components/modal/${toastInfo.type}.svg`" mode="scaleToFill"></image>
  15. </view>
  16. <view class="title">
  17. {{toastInfo.title}}
  18. </view>
  19. <view class="remark">
  20. {{toastInfo.remark}}
  21. </view>
  22. </view>
  23. </zwy-popup>
  24. </view>
  25. </template>
  26. <script setup>
  27. import {
  28. reactive,
  29. ref,
  30. defineExpose
  31. } from 'vue';
  32. const isShow = ref(false)
  33. const timer = ref(null)
  34. const toastInfo = reactive({
  35. type: '',
  36. title: '',
  37. duration: 1500,
  38. remark: ''
  39. })
  40. const baseInfo = {
  41. success: {
  42. title: '操作成功',
  43. remark: '',
  44. duration: 1500
  45. },
  46. error: {
  47. title: '操作失败',
  48. remark: '',
  49. duration: 1500
  50. }
  51. }
  52. /**
  53. * @description 打开弹窗
  54. * @param option 配置项,可以单独配置类型、标题、描述、时间
  55. * 如果没有类型,默认使用success,但其他参数还是可以覆盖
  56. * @param option.type 类型:success/error
  57. * @param option.title 标题
  58. * @param option.remark 描述
  59. * @param option.duration 持续时长
  60. */
  61. const showToast = (option) => {
  62. let tempType = option?.type
  63. if (!tempType || !baseInfo[tempType]) {
  64. tempType = 'success'
  65. }
  66. toastInfo.type = tempType;
  67. toastInfo.title = option.title || baseInfo[tempType].title;
  68. toastInfo.remark = option.remark || baseInfo[tempType].remark;
  69. toastInfo.duration = option.duration || baseInfo[tempType].duration;
  70. isShow.value = true
  71. timer.value && clearTimeout(timer.value)
  72. timer.value = setTimeout(() => {
  73. hideToast()
  74. }, toastInfo.duration)
  75. }
  76. const hideToast = () => {
  77. isShow.value = false
  78. timer.value && clearTimeout(timer.value)
  79. }
  80. defineExpose({
  81. showToast,
  82. hideToast
  83. })
  84. </script>
  85. <style lang="scss" scoped>
  86. .custom-toast{
  87. .content{
  88. position: relative;
  89. width: 100%;
  90. height: 100%;
  91. display: flex;
  92. justify-content: center;
  93. flex-direction: column;
  94. align-items: center;
  95. .bg-box{
  96. position: absolute;
  97. width: 100%;
  98. height: 100%;
  99. left: 0;
  100. top: 0;
  101. z-index: -1;
  102. }
  103. .icon{
  104. margin-left: -10rpx;
  105. width: 210rpx;
  106. height: 144rpx;
  107. }
  108. .title{
  109. margin-top: 8rpx;
  110. font-family: Source Han Sans;
  111. font-size: 50rpx;
  112. font-weight: 500;
  113. font-feature-settings: "kern" on;
  114. color: $uni-text-color;
  115. }
  116. .remark{
  117. margin-top: 16rpx;
  118. font-family: Source Han Sans;
  119. font-size: 36rpx;
  120. font-feature-settings: "kern" on;
  121. color: $uni-text-color;
  122. }
  123. }
  124. }
  125. </style>