cwg-confirm-popup.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <uni-popup ref="popup" type="center" :is-mask-click="false" @change="onChange">
  3. <view class="confirm-popup">
  4. <view class="confirm-title">{{ title }}</view>
  5. <view class="confirm-content">{{ content }}</view>
  6. <view class="confirm-buttons">
  7. <button class="confirm-btn cancel" @click="cancel">{{ cancelText }}</button>
  8. <button class="confirm-btn confirm" @click="confirm">{{ confirmText }}</button>
  9. </view>
  10. </view>
  11. </uni-popup>
  12. </template>
  13. <script setup>
  14. import { ref, onMounted, onUnmounted } from 'vue'
  15. const popup = ref(null)
  16. const title = ref('提示')
  17. const content = ref('')
  18. const confirmText = ref('确定')
  19. const cancelText = ref('取消')
  20. let currentEventId = null
  21. const handleShowConfirm = (options) => {
  22. title.value = options.title || '提示'
  23. content.value = options.content || ''
  24. confirmText.value = options.confirmText || '确定'
  25. cancelText.value = options.cancelText || '取消'
  26. currentEventId = options.eventId
  27. popup.value?.open()
  28. }
  29. const closeAndResult = (result) => {
  30. popup.value?.close()
  31. if (currentEventId) {
  32. uni.$emit(`confirmResult_${currentEventId}`, result)
  33. currentEventId = null
  34. }
  35. }
  36. const confirm = () => closeAndResult(true)
  37. const cancel = () => closeAndResult(false)
  38. const onChange = (e) => {
  39. if (e.type === 'close') closeAndResult(false)
  40. }
  41. onMounted(() => {
  42. uni.$on('showConfirm', handleShowConfirm)
  43. })
  44. onUnmounted(() => {
  45. uni.$off('showConfirm', handleShowConfirm)
  46. })
  47. </script>
  48. <style scoped lang="scss">
  49. @import "@/uni.scss";
  50. .confirm-popup {
  51. width: px2rpx(500);
  52. background-color: #fff;
  53. border-radius: px2rpx(16);
  54. padding: px2rpx(20) px2rpx(16);
  55. text-align: center;
  56. box-shadow: 0 px2rpx(10) px2rpx(20) rgba(0, 0, 0, 0.1);
  57. animation: popup-in 0.3s ease-out;
  58. }
  59. .confirm-title {
  60. font-size: px2rpx(24);
  61. font-weight: 600;
  62. color: #333;
  63. margin-bottom: px2rpx(30);
  64. }
  65. .confirm-content {
  66. font-size: px2rpx(20);
  67. color: #666;
  68. margin-bottom: px2rpx(30);
  69. line-height: 1.5;
  70. word-break: break-word;
  71. }
  72. .confirm-buttons {
  73. display: flex;
  74. justify-content: space-between;
  75. gap: px2rpx(24);
  76. }
  77. .confirm-btn {
  78. flex: 1;
  79. height: px2rpx(40);
  80. line-height: px2rpx(40);
  81. border-radius: px2rpx(20);
  82. font-size: px2rpx(20);
  83. border: none;
  84. background-color: #f5f5f5;
  85. color: #666;
  86. font-weight: 500;
  87. transition: all 0.2s ease;
  88. -webkit-tap-highlight-color: transparent;
  89. }
  90. .confirm-btn:active {
  91. transform: scale(0.98);
  92. opacity: 0.9;
  93. }
  94. .confirm-btn.confirm {
  95. background-color: #2979ff;
  96. color: #fff;
  97. }
  98. .confirm-btn.confirm:active {
  99. background-color: #1a66e5;
  100. }
  101. .confirm-btn.cancel {
  102. background-color: #f5f5f5;
  103. color: #666;
  104. }
  105. .confirm-btn.cancel:active {
  106. background-color: #e6e6e6;
  107. }
  108. @keyframes popup-in {
  109. from {
  110. opacity: 0;
  111. transform: scale(0.9);
  112. }
  113. to {
  114. opacity: 1;
  115. transform: scale(1);
  116. }
  117. }
  118. /* 响应式调整 */
  119. @media screen and (max-width: 750px) {
  120. .confirm-popup {
  121. width: 85vw;
  122. max-width: px2rpx(600);
  123. padding: px2rpx(32) px2rpx(24);
  124. }
  125. .confirm-title {
  126. font-size: px2rpx(24);
  127. margin-bottom: px2rpx(16);
  128. }
  129. .confirm-content {
  130. font-size: px2rpx(20);
  131. margin-bottom: px2rpx(32);
  132. }
  133. .confirm-btn {
  134. height: px2rpx(48);
  135. line-height: px2rpx(48);
  136. font-size: px2rpx(24);
  137. }
  138. }
  139. </style>