ShortLinkModal.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <script setup lang="ts">
  2. // 监听showModal变化,当弹窗显示时自动获取分享短链
  3. import { watch } from 'vue'
  4. import { useCouponShare } from '../hooks/useCouponShare'
  5. // 定义props
  6. const props = defineProps<{
  7. couponId?: string // 优惠券ID,从父组件传入
  8. showModal?: boolean // 控制弹窗显示/隐藏的prop
  9. }>()
  10. // 定义emit
  11. const emit = defineEmits<{
  12. // 弹窗关闭时触发
  13. (e: 'close'): void
  14. // 用于v-model的update事件
  15. (e: 'update:showModal', value: boolean): void
  16. }>()
  17. const { shareLink, copySuccess, shareLoading, shareCoupon, copyShareLink, cleanShareLink } = useCouponShare()
  18. watch(() => props.showModal, (newVal) => {
  19. console.log('showModal', newVal)
  20. if (newVal && props.couponId) {
  21. shareCoupon(props.couponId)
  22. }
  23. })
  24. // 弹窗关闭时触发事件
  25. function handleClose() {
  26. emit('update:showModal', false)
  27. emit('close')
  28. cleanShareLink()
  29. }
  30. </script>
  31. <template>
  32. <!-- 分享弹窗 - 使用uview-plus的Modal组件 -->
  33. <up-modal title="分享优惠券" style="z-index: 1000;" :show="props.showModal" :show-confirm-button="false">
  34. <view class="share-modal-content">
  35. <view class="share-link-container">
  36. <up-loading-icon v-if="shareLoading" mode="semicircle" text="正在获取分享链接中" text-size="18" />
  37. <template v-else>
  38. <text class="share-link">{{ shareLink }}</text>
  39. <button class="copy-btn" @click="copyShareLink">
  40. 复制
  41. </button>
  42. </template>
  43. </view>
  44. <view v-if="copySuccess" class="copy-success">
  45. 复制成功!
  46. </view>
  47. </view>
  48. <template #popupBottom>
  49. <view class="rounded" style="margin-top: 20px;" @click="handleClose">
  50. <up-icon name="close-circle" size="72rpx" color="#fff" />
  51. </view>
  52. </template>
  53. </up-modal>
  54. </template>
  55. <style scoped>
  56. .share-modal-content {
  57. margin: 20rpx 0;
  58. }
  59. .share-link-container {
  60. display: flex;
  61. align-items: center;
  62. justify-content: space-between;
  63. padding: 20rpx;
  64. background-color: #f5f5f5;
  65. border-radius: 8rpx;
  66. margin-bottom: 20rpx;
  67. }
  68. .share-link {
  69. flex: 1;
  70. font-size: 28rpx;
  71. color: #333;
  72. overflow: hidden;
  73. text-overflow: ellipsis;
  74. white-space: nowrap;
  75. margin-right: 20rpx;
  76. }
  77. .copy-btn {
  78. padding: 10rpx 20rpx;
  79. font-size: 28rpx;
  80. color: #007aff;
  81. background-color: transparent;
  82. border: 1px solid #007aff;
  83. border-radius: 8rpx;
  84. }
  85. .copy-success {
  86. text-align: center;
  87. font-size: 28rpx;
  88. color: #07c160;
  89. }
  90. </style>