hookehuyr

feat(分享功能): 添加ShareButton测试页面并优化分享功能

添加ShareButtonTest测试页面用于验证分享功能
重构ShareButton组件,增加分享配置属性和默认值
优化RewardDetail页面的分享逻辑,增加错误处理
...@@ -35,6 +35,7 @@ export default { ...@@ -35,6 +35,7 @@ export default {
35 'pages/FamilyRank/index', 35 'pages/FamilyRank/index',
36 'pages/PosterCheckin/index', 36 'pages/PosterCheckin/index',
37 'pages/CheckinList/index', 37 'pages/CheckinList/index',
38 + 'pages/ShareButtonTest/index',
38 ], 39 ],
39 window: { 40 window: {
40 backgroundTextStyle: 'light', 41 backgroundTextStyle: 'light',
......
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 * @LastEditTime: 2025-09-15 12:52:03 4 * @LastEditTime: 2025-09-15 12:52:03
5 * @FilePath: /lls_program/src/components/ShareButton/index.vue 5 * @FilePath: /lls_program/src/components/ShareButton/index.vue
6 - * @Description: 文件描述
7 --->
8 -<!--
9 * @Description: 分享按钮组件 - 点击后弹出分享选项 6 * @Description: 分享按钮组件 - 点击后弹出分享选项
10 --> 7 -->
11 <template> 8 <template>
...@@ -25,6 +22,7 @@ ...@@ -25,6 +22,7 @@
25 <script setup> 22 <script setup>
26 import { ref } from 'vue' 23 import { ref } from 'vue'
27 import Taro from '@tarojs/taro' 24 import Taro from '@tarojs/taro'
25 +import { addShareFlag } from '@/utils/authRedirect'
28 26
29 // 组件属性 27 // 组件属性
30 const props = defineProps({ 28 const props = defineProps({
...@@ -32,6 +30,15 @@ const props = defineProps({ ...@@ -32,6 +30,15 @@ const props = defineProps({
32 activityData: { 30 activityData: {
33 type: Object, 31 type: Object,
34 default: () => ({}) 32 default: () => ({})
33 + },
34 + // 分享配置
35 + shareConfig: {
36 + type: Object,
37 + default: () => ({
38 + title: '精彩活动等你参与',
39 + path: '/pages/ActivitiesCover/index',
40 + imageUrl: ''
41 + })
35 } 42 }
36 }) 43 })
37 44
...@@ -44,6 +51,38 @@ const emit = defineEmits(['shareActivity', 'sharePoster']) ...@@ -44,6 +51,38 @@ const emit = defineEmits(['shareActivity', 'sharePoster'])
44 const handleSharePoster = () => { 51 const handleSharePoster = () => {
45 emit('sharePoster', props.activityData) 52 emit('sharePoster', props.activityData)
46 } 53 }
54 +
55 +/**
56 + * 定义分享给朋友的内容
57 + * @param {Object} res - 分享来源信息
58 + * @returns {Object} 分享配置对象
59 + */
60 +const onShareAppMessage = (res) => {
61 + // 构建分享数据
62 + const shareData = {
63 + title: props.shareConfig.title || '精彩活动等你参与',
64 + path: addShareFlag(props.shareConfig.path || '/pages/ActivitiesCover/index'),
65 + imageUrl: props.shareConfig.imageUrl || props.activityData.banner || ''
66 + }
67 +
68 + // 触发分享事件
69 + emit('shareActivity', {
70 + ...props.activityData,
71 + shareData
72 + })
73 +
74 + return shareData
75 +}
76 +
77 +// 使用Taro的useShareAppMessage Hook来处理分享
78 +Taro.useShareAppMessage((res) => {
79 + return onShareAppMessage(res)
80 +})
81 +
82 +// 导出分享方法供Taro使用(保持兼容性)
83 +defineExpose({
84 + onShareAppMessage,
85 +})
47 </script> 86 </script>
48 87
49 <style lang="less"> 88 <style lang="less">
......
...@@ -255,20 +255,28 @@ useLoad((options) => { ...@@ -255,20 +255,28 @@ useLoad((options) => {
255 * 定义分享给朋友的内容 255 * 定义分享给朋友的内容
256 * @returns {Object} 分享配置对象 256 * @returns {Object} 分享配置对象
257 */ 257 */
258 -const onShareAppMessage = () => { 258 +const onShareAppMessage = (res) => {
259 - return { 259 + // 确保数据已加载,提供默认值
260 - title: `${reward.value.title} - 仅需${reward.value.points_cost}积分`, 260 + const title = reward.value?.title ? `${reward.value.title} - 仅需${reward.value.points_cost || 0}积分` : '精彩优惠券等你来兑换';
261 - path: addShareFlag(`/pages/RewardDetail/index?id=${reward.value.id}`), 261 + const shareId = reward.value?.id || couponId.value || '';
262 - imageUrl: reward.value.banner 262 + // const imageUrl = reward.value?.banner || '';
263 - } 263 +
264 + const shareData = {
265 + title: title,
266 + path: addShareFlag(`/pages/RewardDetail/index?id=${shareId}`),
267 + // imageUrl: imageUrl
268 + };
269 +
270 + return shareData;
264 } 271 }
265 272
266 -// 导出分享方法供Taro使用 273 +// 使用Taro的useShareAppMessage Hook来处理分享
274 +Taro.useShareAppMessage((res) => {
275 + return onShareAppMessage(res);
276 +});
277 +
278 +// 导出分享方法供Taro使用(保持兼容性)
267 defineExpose({ 279 defineExpose({
268 onShareAppMessage, 280 onShareAppMessage,
269 }) 281 })
270 </script> 282 </script>
271 -
272 -<style lang="less">
273 -
274 -</style>
......
1 +export default definePageConfig({
2 + navigationBarTitleText: 'ShareButton测试',
3 + enableShareAppMessage: true,
4 + enableShareTimeline: true
5 +})
...\ No newline at end of file ...\ No newline at end of file
1 +<template>
2 + <view class="test-page">
3 + <view class="test-header">
4 + <text class="test-title">ShareButton组件分享功能测试</text>
5 + </view>
6 +
7 + <view class="test-content">
8 + <!-- 测试数据展示 -->
9 + <view class="test-section">
10 + <text class="section-title">测试数据:</text>
11 + <view class="data-display">
12 + <text>活动标题:{{ testActivityData.title }}</text>
13 + <text>活动描述:{{ testActivityData.description }}</text>
14 + <text>分享标题:{{ testShareConfig.title }}</text>
15 + <text>分享路径:{{ testShareConfig.path }}</text>
16 + </view>
17 + </view>
18 +
19 + <!-- ShareButton组件测试 -->
20 + <view class="test-section">
21 + <text class="section-title">ShareButton组件:</text>
22 + <ShareButton
23 + :activity-data="testActivityData"
24 + :share-config="testShareConfig"
25 + @share-activity="onShareActivity"
26 + @share-poster="onSharePoster"
27 + />
28 + </view>
29 +
30 + <!-- 分享日志显示 -->
31 + <view class="test-section">
32 + <text class="section-title">分享日志:</text>
33 + <view class="log-display">
34 + <text v-for="(log, index) in shareLogs" :key="index" class="log-item">
35 + {{ log }}
36 + </text>
37 + </view>
38 + </view>
39 + </view>
40 + </view>
41 +</template>
42 +
43 +<script setup>
44 +import { ref } from 'vue'
45 +import Taro from '@tarojs/taro'
46 +import ShareButton from '@/components/ShareButton/index.vue'
47 +
48 +/**
49 + * ShareButton组件分享功能测试页面
50 + */
51 +
52 +// 测试用的活动数据
53 +const testActivityData = ref({
54 + id: 'test-activity-001',
55 + title: '测试活动 - 南京路商圈时尚Citywalk',
56 + description: '这是一个用于测试ShareButton组件分享功能的测试活动',
57 + banner: 'https://img.yzcdn.cn/vant/cat.jpeg',
58 + dateRange: '2024年1月15日 - 2024年1月31日'
59 +})
60 +
61 +// 测试用的分享配置
62 +const testShareConfig = ref({
63 + title: '精彩活动等你参与 - 测试分享',
64 + path: '/pages/ShareButtonTest/index',
65 + imageUrl: 'https://img.yzcdn.cn/vant/cat.jpeg'
66 +})
67 +
68 +// 分享日志
69 +const shareLogs = ref([
70 + '页面初始化完成',
71 + 'ShareButton组件已加载'
72 +])
73 +
74 +/**
75 + * 处理分享活动事件
76 + * @param {Object} data - 分享数据
77 + */
78 +const onShareActivity = (data) => {
79 + console.log('=== 测试页面收到分享活动事件 ===')
80 + console.log('分享数据:', data)
81 +
82 + const logMessage = `[${new Date().toLocaleTimeString()}] 分享活动事件触发 - 标题: ${data.shareData?.title || '未知'}`
83 + shareLogs.value.push(logMessage)
84 +
85 + Taro.showToast({
86 + title: '分享活动事件已触发',
87 + icon: 'success',
88 + duration: 2000
89 + })
90 +}
91 +
92 +/**
93 + * 处理分享海报事件
94 + * @param {Object} data - 活动数据
95 + */
96 +const onSharePoster = (data) => {
97 + console.log('=== 测试页面收到分享海报事件 ===')
98 + console.log('活动数据:', data)
99 +
100 + const logMessage = `[${new Date().toLocaleTimeString()}] 分享海报事件触发 - 活动: ${data.title || '未知'}`
101 + shareLogs.value.push(logMessage)
102 +
103 + Taro.showToast({
104 + title: '分享海报事件已触发',
105 + icon: 'success',
106 + duration: 2000
107 + })
108 +}
109 +
110 +/**
111 + * 页面分享配置(用于测试页面本身的分享)
112 + */
113 +const onShareAppMessage = () => {
114 + const logMessage = `[${new Date().toLocaleTimeString()}] 页面分享函数被调用`
115 + shareLogs.value.push(logMessage)
116 +
117 + return {
118 + title: 'ShareButton组件测试页面',
119 + path: '/pages/ShareButtonTest/index'
120 + }
121 +}
122 +
123 +// 导出分享方法供Taro使用
124 +defineExpose({
125 + onShareAppMessage
126 +})
127 +</script>
128 +
129 +<style lang="less" scoped>
130 +.test-page {
131 + min-height: 100vh;
132 + background: #f5f5f5;
133 + padding: 40rpx;
134 +}
135 +
136 +.test-header {
137 + text-align: center;
138 + margin-bottom: 60rpx;
139 +}
140 +
141 +.test-title {
142 + font-size: 36rpx;
143 + font-weight: bold;
144 + color: #333;
145 +}
146 +
147 +.test-content {
148 + display: flex;
149 + flex-direction: column;
150 + gap: 40rpx;
151 +}
152 +
153 +.test-section {
154 + background: white;
155 + border-radius: 16rpx;
156 + padding: 30rpx;
157 + box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
158 +}
159 +
160 +.section-title {
161 + font-size: 32rpx;
162 + font-weight: bold;
163 + color: #333;
164 + margin-bottom: 20rpx;
165 + display: block;
166 +}
167 +
168 +.data-display {
169 + display: flex;
170 + flex-direction: column;
171 + gap: 16rpx;
172 +}
173 +
174 +.data-display text {
175 + font-size: 28rpx;
176 + color: #666;
177 + padding: 12rpx;
178 + background: #f8f9fa;
179 + border-radius: 8rpx;
180 +}
181 +
182 +.log-display {
183 + max-height: 400rpx;
184 + overflow-y: auto;
185 + background: #f8f9fa;
186 + border-radius: 8rpx;
187 + padding: 20rpx;
188 +}
189 +
190 +.log-item {
191 + display: block;
192 + font-size: 24rpx;
193 + color: #666;
194 + margin-bottom: 8rpx;
195 + padding: 8rpx;
196 + background: white;
197 + border-radius: 4rpx;
198 + border-left: 4rpx solid #007aff;
199 +}
200 +</style>
...\ No newline at end of file ...\ No newline at end of file