hookehuyr

feat: 优化页面数据初始化逻辑并统一使用useDidShow

refactor(组件): 提取初始化逻辑到独立函数
style: 清理无用导入和注释
...@@ -38,8 +38,8 @@ ...@@ -38,8 +38,8 @@
38 </template> 38 </template>
39 39
40 <script setup> 40 <script setup>
41 -import { ref, onMounted, nextTick, defineProps, defineExpose } from 'vue' 41 +import { ref, onMounted, defineProps, defineExpose } from 'vue'
42 -import Taro from '@tarojs/taro' 42 +import Taro, { useDidShow } from '@tarojs/taro'
43 43
44 // 定义props 44 // 定义props
45 const props = defineProps({ 45 const props = defineProps({
...@@ -240,9 +240,22 @@ defineExpose({ ...@@ -240,9 +240,22 @@ defineExpose({
240 collectAll 240 collectAll
241 }) 241 })
242 242
243 +/**
244 + * 初始化数据
245 + */
246 +const initData = () => {
247 + floatingItems.value = generateMockData();
248 + console.warn('初始化数据')
249 +}
250 +
243 // 组件挂载时初始化数据 251 // 组件挂载时初始化数据
244 onMounted(() => { 252 onMounted(() => {
245 - floatingItems.value = generateMockData(); 253 + initData();
254 +})
255 +
256 +// 每次进入组件时重新获取数据
257 +useDidShow(() => {
258 + initData();
246 }) 259 })
247 260
248 /** 261 /**
......
...@@ -141,6 +141,12 @@ const getWeRunData = async () => { ...@@ -141,6 +141,12 @@ const getWeRunData = async () => {
141 code: loginRes.code 141 code: loginRes.code
142 }) 142 })
143 143
144 + // 提示获取步数成功
145 + // Taro.showToast({
146 + // title: `获取步数成功:${mockSteps}步`,
147 + // icon: 'none'
148 + // })
149 +
144 // TODO: 实际项目中需要将 encryptedData, iv, code 发送到后端解密 150 // TODO: 实际项目中需要将 encryptedData, iv, code 发送到后端解密
145 // const decryptedData = await api.decryptWeRunData({ 151 // const decryptedData = await api.decryptWeRunData({
146 // encryptedData: weRunRes.encryptedData, 152 // encryptedData: weRunRes.encryptedData,
......
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
2 <view class="min-h-screen flex flex-col bg-white pb-16" style="background-color: #F9FAFB;"> 2 <view class="min-h-screen flex flex-col bg-white pb-16" style="background-color: #F9FAFB;">
3 <!-- Hero section with family name and background image --> 3 <!-- Hero section with family name and background image -->
4 <view class="relative h-48"> 4 <view class="relative h-48">
5 - <image src="https://placehold.co/800x400/e2f3ff/0369a1?text=LFX&font=roboto" alt="Family background" class="w-full h-full object-cover" /> 5 + <image :src="familyCover" alt="Family background" class="w-full h-full object-cover" />
6 <view class="absolute inset-0 bg-black bg-opacity-30 flex flex-col justify-end p-5"> 6 <view class="absolute inset-0 bg-black bg-opacity-30 flex flex-col justify-end p-5">
7 <view class="absolute top-4 right-4 text-white flex items-center" @click="goToProfile"> 7 <view class="absolute top-4 right-4 text-white flex items-center" @click="goToProfile">
8 <Setting size="24" /> 8 <Setting size="24" />
9 <text class="ml-2">家庭设置</text> 9 <text class="ml-2">家庭设置</text>
10 </view> 10 </view>
11 - <h1 class="text-white text-2xl font-bold">张爷爷的家庭</h1> 11 + <h1 class="text-white text-2xl font-bold">{{ familyName }}</h1>
12 - <p class="text-white opacity-90">每日走万步,全家一起行</p> 12 + <p class="text-white opacity-90">{{ familySlogn }}</p>
13 </view> 13 </view>
14 </view> 14 </view>
15 15
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
21 > 21 >
22 <template #default="{ steps, isLoading }"> 22 <template #default="{ steps, isLoading }">
23 <!-- Today's steps section --> 23 <!-- Today's steps section -->
24 + <!-- TODO: 今日获取的步数怎么同步到下面那个积分池里面去 -->
24 <view class="px-5 py-6 bg-white rounded-xl shadow-md mx-4 mt-4"> 25 <view class="px-5 py-6 bg-white rounded-xl shadow-md mx-4 mt-4">
25 <view class="flex justify-between items-center mb-1"> 26 <view class="flex justify-between items-center mb-1">
26 <span class="text-gray-500">今日</span> 27 <span class="text-gray-500">今日</span>
...@@ -32,11 +33,16 @@ ...@@ -32,11 +33,16 @@
32 </span> 33 </span>
33 <span class="ml-1 text-gray-500">步</span> 34 <span class="ml-1 text-gray-500">步</span>
34 </view> 35 </view>
36 + <view class="flex gap-2">
37 + <!-- <view class="bg-green-500 text-white px-4 py-2 rounded-full text-xs" @click="handleRefreshSteps">
38 + 更新步数
39 + </view> -->
35 <view class="bg-blue-500 text-white px-4 py-2 rounded-full text-sm" @click="handleCollectAll"> 40 <view class="bg-blue-500 text-white px-4 py-2 rounded-full text-sm" @click="handleCollectAll">
36 一键收取 41 一键收取
37 </view> 42 </view>
38 </view> 43 </view>
39 </view> 44 </view>
45 + </view>
40 46
41 <!-- Points circles --> 47 <!-- Points circles -->
42 <view class="flex justify-between px-5 py-6 my-4 bg-white rounded-xl shadow-md mx-4"> 48 <view class="flex justify-between px-5 py-6 my-4 bg-white rounded-xl shadow-md mx-4">
...@@ -98,7 +104,7 @@ ...@@ -98,7 +104,7 @@
98 104
99 <script setup> 105 <script setup>
100 import { ref, computed, onMounted } from 'vue'; 106 import { ref, computed, onMounted } from 'vue';
101 -import Taro from '@tarojs/taro'; 107 +import Taro, { useDidShow } from '@tarojs/taro';
102 import { Setting, Photograph, Right } from '@nutui/icons-vue-taro'; 108 import { Setting, Photograph, Right } from '@nutui/icons-vue-taro';
103 import BottomNav from '../../components/BottomNav.vue'; 109 import BottomNav from '../../components/BottomNav.vue';
104 import PointsCollector from '@/components/PointsCollector.vue' 110 import PointsCollector from '@/components/PointsCollector.vue'
...@@ -109,6 +115,10 @@ const isWeRunAuthorized = ref(false); ...@@ -109,6 +115,10 @@ const isWeRunAuthorized = ref(false);
109 const pointsCollectorRef = ref(null) 115 const pointsCollectorRef = ref(null)
110 const weRunAuthRef = ref(null) 116 const weRunAuthRef = ref(null)
111 117
118 +const familyName = ref('')
119 +const familySlogn = ref('')
120 +const familyCover = ref('')
121 +
112 /** 122 /**
113 * 触发积分收集组件的一键收取 123 * 触发积分收集组件的一键收取
114 */ 124 */
...@@ -119,6 +129,15 @@ const handleCollectAll = () => { ...@@ -119,6 +129,15 @@ const handleCollectAll = () => {
119 } 129 }
120 130
121 /** 131 /**
132 + * 手动刷新微信步数
133 + */
134 +const handleRefreshSteps = async () => {
135 + if (weRunAuthRef.value) {
136 + await weRunAuthRef.value.refreshSteps()
137 + }
138 +}
139 +
140 +/**
122 * 处理微信步数授权状态变化 141 * 处理微信步数授权状态变化
123 * @param {boolean} authorized - 授权状态 142 * @param {boolean} authorized - 授权状态
124 */ 143 */
...@@ -196,12 +215,20 @@ const openCamera = () => { ...@@ -196,12 +215,20 @@ const openCamera = () => {
196 Taro.navigateTo({ url: '/pages/UploadMedia/index' }); 215 Taro.navigateTo({ url: '/pages/UploadMedia/index' });
197 } 216 }
198 217
199 -onMounted(() => { 218 +const initPageData = async () => {
219 + // 获取用户信息
220 + familyName.value = '张爷爷的家庭'
221 + familySlogn.value = '每日走万步,全家一起行'
222 + familyCover.value = 'https://placehold.co/800x400/e2f3ff/0369a1?text=LFX&font=roboto'
223 +}
224 +
225 +useDidShow(() => {
200 // TODO: 等待真实接口获取用户是否加入家庭 226 // TODO: 等待真实接口获取用户是否加入家庭
201 // const hasJoinedFamily = false; // Change to true to simulate having a family 227 // const hasJoinedFamily = false; // Change to true to simulate having a family
202 228
203 // if (!hasJoinedFamily) { 229 // if (!hasJoinedFamily) {
204 // Taro.redirectTo({ url: '/pages/Welcome/index' }); 230 // Taro.redirectTo({ url: '/pages/Welcome/index' });
205 // } 231 // }
232 + initPageData();
206 }) 233 })
207 </script> 234 </script>
......
1 <!-- 1 <!--
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-08-29 15:27:43 4 + * @LastEditTime: 2025-08-29 20:32:52
5 * @FilePath: /lls_program/src/pages/MyFamily/index.vue 5 * @FilePath: /lls_program/src/pages/MyFamily/index.vue
6 * @Description: 我的家庭页面 - 展示用户加入的家庭列表 6 * @Description: 我的家庭页面 - 展示用户加入的家庭列表
7 --> 7 -->
...@@ -103,7 +103,7 @@ ...@@ -103,7 +103,7 @@
103 @tap="joinNewFamily" 103 @tap="joinNewFamily"
104 class="w-full bg-blue-500 text-white text-center py-3 rounded-lg font-medium" 104 class="w-full bg-blue-500 text-white text-center py-3 rounded-lg font-medium"
105 > 105 >
106 - + 加入新家庭 106 + 加入新家庭
107 </view> 107 </view>
108 </view> 108 </view>
109 109
...@@ -113,7 +113,7 @@ ...@@ -113,7 +113,7 @@
113 113
114 <script setup> 114 <script setup>
115 import { ref, onMounted } from 'vue'; 115 import { ref, onMounted } from 'vue';
116 -import Taro from '@tarojs/taro'; 116 +import Taro, { useDidShow } from '@tarojs/taro';
117 import { Home } from '@nutui/icons-vue-taro'; 117 import { Home } from '@nutui/icons-vue-taro';
118 import './index.less'; 118 import './index.less';
119 119
...@@ -248,7 +248,7 @@ const joinNewFamily = () => { ...@@ -248,7 +248,7 @@ const joinNewFamily = () => {
248 248
249 249
250 // 页面加载时初始化数据 250 // 页面加载时初始化数据
251 -onMounted(() => { 251 +useDidShow(() => {
252 initPageData(); 252 initPageData();
253 }); 253 });
254 </script> 254 </script>
......
...@@ -55,8 +55,7 @@ ...@@ -55,8 +55,7 @@
55 <script setup> 55 <script setup>
56 import { ref, computed } from 'vue'; 56 import { ref, computed } from 'vue';
57 import Taro from '@tarojs/taro'; 57 import Taro from '@tarojs/taro';
58 - 58 +import { useDidShow } from '@tarojs/taro';
59 -import AppHeader from '../../components/AppHeader.vue';
60 59
61 const tabs = ref([ 60 const tabs = ref([
62 { name: 'all', label: '全部' }, 61 { name: 'all', label: '全部' },
...@@ -67,36 +66,7 @@ const tabs = ref([ ...@@ -67,36 +66,7 @@ const tabs = ref([
67 66
68 const activeTab = ref('all'); 67 const activeTab = ref('all');
69 68
70 -const rewards = ref([ 69 +const rewards = ref([]);
71 - {
72 - id: 1,
73 - title: '杏花楼集团 85折券',
74 - expiryDate: '2025-08-28',
75 - status: 'unused',
76 - usedDate: null,
77 - },
78 - {
79 - id: 2,
80 - title: '老凤祥银楼 20元抵用券',
81 - expiryDate: '2025-08-28',
82 - status: 'unused',
83 - usedDate: null,
84 - },
85 - {
86 - id: 3,
87 - title: '吴良材眼镜 5折券',
88 - expiryDate: '2024-05-20',
89 - status: 'used',
90 - usedDate: '2024-05-01',
91 - },
92 - {
93 - id: 4,
94 - title: '沈大成双酿团 免费券',
95 - expiryDate: '2024-03-15',
96 - status: 'expired',
97 - usedDate: null,
98 - },
99 -]);
100 70
101 const filteredRewards = computed(() => { 71 const filteredRewards = computed(() => {
102 if (activeTab.value === 'all') { 72 if (activeTab.value === 'all') {
...@@ -135,4 +105,55 @@ const handleUseReward = (reward) => { ...@@ -135,4 +105,55 @@ const handleUseReward = (reward) => {
135 }) 105 })
136 } 106 }
137 }; 107 };
108 +
109 +useDidShow(() => {
110 + initPageData();
111 +});
112 +
113 +const initPageData = () => {
114 + rewards.value = [
115 + {
116 + id: 1,
117 + title: '杏花楼集团 85折券',
118 + expiryDate: '2025-08-28',
119 + status: 'unused',
120 + usedDate: null,
121 + },
122 + {
123 + id: 2,
124 + title: '老凤祥银楼 20元抵用券',
125 + expiryDate: '2025-08-28',
126 + status: 'unused',
127 + usedDate: null,
128 + },
129 + {
130 + id: 3,
131 + title: '吴良材眼镜 5折券',
132 + expiryDate: '2024-05-20',
133 + status: 'used',
134 + usedDate: '2024-05-01',
135 + },
136 + {
137 + id: 4,
138 + title: '沈大成双酿团 免费券',
139 + expiryDate: '2024-03-15',
140 + status: 'expired',
141 + usedDate: null,
142 + },
143 + {
144 + id: 5,
145 + title: '沈大成双酿团 免费券',
146 + expiryDate: '2024-03-15',
147 + status: 'expired',
148 + usedDate: null,
149 + },
150 + {
151 + id: 6,
152 + title: '沈大成双酿团 免费券',
153 + expiryDate: '2024-03-15',
154 + status: 'expired',
155 + usedDate: null,
156 + },
157 + ];
158 +}
138 </script> 159 </script>
......
1 <!-- 1 <!--
2 * @Date: 2025-08-27 17:47:46 2 * @Date: 2025-08-27 17:47:46
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-08-29 11:12:30 4 + * @LastEditTime: 2025-08-29 20:38:36
5 * @FilePath: /lls_program/src/pages/Profile/index.vue 5 * @FilePath: /lls_program/src/pages/Profile/index.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 <view class="flex items-center"> 16 <view class="flex items-center">
17 <image :src="defaultAvatar" alt="User avatar" class="w-16 h-16 rounded-full border-2 border-white" /> 17 <image :src="defaultAvatar" alt="User avatar" class="w-16 h-16 rounded-full border-2 border-white" />
18 <view class="ml-4"> 18 <view class="ml-4">
19 - <h1 class="text-xl font-bold text-white">张爷爷</h1> 19 + <h1 class="text-xl font-bold text-white">{{ userInfo.nickName }}</h1>
20 </view> 20 </view>
21 </view> 21 </view>
22 <view @tap="goToEditProfile" class="text-white"> 22 <view @tap="goToEditProfile" class="text-white">
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
45 45
46 <script setup> 46 <script setup>
47 import { ref, shallowRef } from 'vue'; 47 import { ref, shallowRef } from 'vue';
48 -import Taro from '@tarojs/taro'; 48 +import Taro, { useDidShow } from '@tarojs/taro';
49 import BottomNav from '../../components/BottomNav.vue'; 49 import BottomNav from '../../components/BottomNav.vue';
50 import { My, Shop3, Cart, Message, Tips, Right } from '@nutui/icons-vue-taro'; 50 import { My, Shop3, Cart, Message, Tips, Right } from '@nutui/icons-vue-taro';
51 // 默认头像 51 // 默认头像
...@@ -96,7 +96,24 @@ const menuItems = shallowRef([ ...@@ -96,7 +96,24 @@ const menuItems = shallowRef([
96 } 96 }
97 ]); 97 ]);
98 98
99 +const userInfo = ref({
100 + nickName: '',
101 + avatarUrl: '',
102 +});
103 +
99 const goToEditProfile = () => { 104 const goToEditProfile = () => {
100 Taro.navigateTo({ url: '/pages/EditProfile/index' }); 105 Taro.navigateTo({ url: '/pages/EditProfile/index' });
101 }; 106 };
107 +
108 +const initPageData = () => {
109 + // 获取用户信息
110 + userInfo.value = {
111 + nickName: '用户10086',
112 + avatarUrl: defaultAvatar,
113 + }
114 +}
115 +
116 +useDidShow(() => {
117 + initPageData();
118 +});
102 </script> 119 </script>
......
1 <!-- 1 <!--
2 * @Date: 2025-08-27 17:47:26 2 * @Date: 2025-08-27 17:47:26
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-08-28 09:37:06 4 + * @LastEditTime: 2025-08-29 20:32:08
5 * @FilePath: /lls_program/src/pages/Rewards/index.vue 5 * @FilePath: /lls_program/src/pages/Rewards/index.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 <view class="relative z-10 flex-1 pb-20"> 14 <view class="relative z-10 flex-1 pb-20">
15 <!-- Points display --> 15 <!-- Points display -->
16 <view class="pt-8 pb-8 flex flex-col items-center"> 16 <view class="pt-8 pb-8 flex flex-col items-center">
17 - <h2 class="text-4xl font-bold text-white mb-1">2580分</h2> 17 + <h2 class="text-4xl font-bold text-white mb-1">{{ totalPoints }}分</h2>
18 <p class="text-white text-opacity-80">我的积分</p> 18 <p class="text-white text-opacity-80">我的积分</p>
19 </view> 19 </view>
20 <!-- Main content --> 20 <!-- Main content -->
...@@ -71,16 +71,16 @@ ...@@ -71,16 +71,16 @@
71 </template> 71 </template>
72 72
73 <script setup> 73 <script setup>
74 -import { ref, computed } from 'vue'; 74 +import { ref, computed, onMounted } from 'vue';
75 -import Taro from '@tarojs/taro'; 75 +import Taro, { useDidShow } from '@tarojs/taro';
76 import { ScreenLittle, Search2 } from '@nutui/icons-vue-taro'; 76 import { ScreenLittle, Search2 } from '@nutui/icons-vue-taro';
77 -import AppHeader from '../../components/AppHeader.vue';
78 -import BottomNav from '../../components/BottomNav.vue';
79 77
80 const searchQuery = ref(''); 78 const searchQuery = ref('');
81 const selectedPoints = ref(null); 79 const selectedPoints = ref(null);
82 const sortOrder = ref('desc'); // 'asc' or 'desc' 80 const sortOrder = ref('desc'); // 'asc' or 'desc'
83 81
82 +const totalPoints = ref(0);
83 +
84 const sortedRewardItems = computed(() => { 84 const sortedRewardItems = computed(() => {
85 let items = [...rewardItems.value]; 85 let items = [...rewardItems.value];
86 86
...@@ -154,4 +154,13 @@ const goToRewardDetail = (reward) => { ...@@ -154,4 +154,13 @@ const goToRewardDetail = (reward) => {
154 url: `/pages/RewardDetail/index?id=${reward.id}` 154 url: `/pages/RewardDetail/index?id=${reward.id}`
155 }); 155 });
156 }; 156 };
157 +
158 +const initData = async () => {
159 + totalPoints.value = '9856';
160 + console.warn('初始化数据')
161 +}
162 +
163 +useDidShow(() => {
164 + initData();
165 +})
157 </script> 166 </script>
......