cloudroam
10 天以前 05316275ee6f1623cc022a3cb4967a440c105a6b
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
<template>
  <view class="share-popup" v-if="show">
    <view class="share-mask" @click="closePopup"></view>
    <view class="share-content">
      <view class="share-title">分享到</view>
      <view class="share-options">
        <!-- 分享给好友 -->
        <button class="share-item" open-type="share">
          <image src="/static/common/wechat.png" class="share-icon" />
          <text>微信好友</text>
        </button>
        <!-- 分享到朋友圈 -->
        <view class="share-item" @click="handleShareTimeline">
          <image src="/static/common/wechat-moments.png" class="share-icon" />
          <text>朋友圈</text>
        </view>
        <view class="share-item" @click="handleCopyLink">
          <image src="/static/common/link.png" class="share-icon" />
          <text>复制链接</text>
        </view>
      </view>
      <view class="share-cancel" @click="closePopup">取消</view>
    </view>
  </view>
</template>
 
<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue'
 
const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  shareData: {
    type: Object,
    default: () => ({
      title: '',
      desc: '',
      image: '',
      url: ''
    })
  }
})
 
const emit = defineEmits(['update:show'])
 
// 关闭弹窗
const closePopup = () => {
  emit('update:show', false)
}
 
// 处理分享到朋友圈
const handleShareTimeline = () => {
  // #ifdef MP-WEIXIN
  // 使用微信原生朋友圈分享
  wx.openChannelsActivity({
    finderUserName: '', // 视频号用户名,可选
    success: (res) => {
      console.log('打开朋友圈成功', res)
      uni.showToast({
        title: '已打开朋友圈',
        icon: 'success'
      })
    },
    fail: (err) => {
      console.error('打开朋友圈失败', err)
      // 如果打开失败,尝试使用小程序分享
      uni.showModal({
        title: '提示',
        content: '无法直接打开朋友圈,请点击右上角分享到朋友圈',
        showCancel: false
      })
    }
  })
  // #endif
  
  // #ifndef MP-WEIXIN
  // 非微信小程序环境
  uni.showToast({
    title: '请点击右上角分享到朋友圈',
    icon: 'none',
    duration: 2000
  })
  // #endif
  
  closePopup()
}
 
// 复制链接
const handleCopyLink = () => {
  uni.setClipboardData({
    data: props.shareData.url,
    success: () => {
      uni.showToast({
        title: '链接已复制',
        icon: 'success'
      })
      closePopup()
    }
  })
}
</script>
 
<style lang="scss" scoped>
.share-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
 
  .share-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
  }
 
  .share-content {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    background: #fff;
    border-radius: 20rpx 20rpx 0 0;
    padding: 30rpx;
    transform: translateY(0);
    transition: transform 0.3s ease-out;
 
    .share-title {
      text-align: center;
      font-size: 32rpx;
      color: #333;
      margin-bottom: 30rpx;
    }
 
    .share-options {
      display: flex;
      justify-content: space-around;
      padding: 20rpx 0;
 
      .share-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 120rpx;
        background: none;
        border: none;
        padding: 0;
        margin: 0;
        line-height: normal;
 
        &::after {
          border: none;
        }
 
        .share-icon {
          width: 80rpx;
          height: 80rpx;
          margin-bottom: 10rpx;
        }
 
        text {
          font-size: 24rpx;
          color: #666;
        }
      }
    }
 
    .share-cancel {
      text-align: center;
      color: #888;
      font-size: 28rpx;
      padding: 20rpx 0;
      border-top: 1px solid #eee;
      margin-top: 20rpx;
    }
  }
}
</style>