index.vue
6.66 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-26 08:14:03
* @FilePath: /lls_program/src/pages/AdPage/index.vue
* @Description: 广告页面
-->
<template>
<view class="ad-page">
<!-- 骨架屏 -->
<view v-if="imageLoading" class="skeleton-container">
<view class="skeleton-image">
<view class="skeleton-shimmer"></view>
<view class="skeleton-text">
<view class="loading-title">
{{ retryCount > 0 ? `正在重试加载 (${retryCount}/${maxRetries})` : '正在加载精彩内容' }}
</view>
<view class="loading-subtitle">请稍候...</view>
</view>
</view>
</view>
<!-- 手动刷新提示 -->
<view v-if="showManualRefresh" class="manual-refresh-container">
<view class="refresh-content">
<view class="refresh-icon">⚠️</view>
<view class="refresh-title">网络连接异常</view>
<view class="refresh-subtitle">请检查网络连接后重新进入小程序</view>
<view class="refresh-steps">
<view class="step-item">1. 点击右上角 ⋯ 菜单</view>
<view class="step-item">2. 选择"重新进入小程序"</view>
</view>
</view>
</view>
<!-- 背景图片 -->
<image
v-show="!imageLoading && !imageLoadError"
:src="adImageUrl"
class="ad-background"
mode="scaleToFill"
@tap="handleImageClick"
@load="handleImageLoad"
@error="handleImageError"
/>
<!-- 图片加载失败提示 -->
<view v-if="imageLoadError" class="error-container">
<view class="error-text">图片加载失败</view>
<view class="retry-button" @tap="retryLoadImage">重试</view>
</view>
<!-- 右上角分享按钮 -->
<view class="share-button-container" @tap.stop>
<button
class="share-button"
open-type="share"
>
推荐给朋友
</button>
</view>
<!-- 加载状态 -->
<view v-if="loading" class="loading-container">
<view class="loading-text">加载中...</view>
</view>
</view>
</template>
<script setup>
import { ref, onMounted } from "vue";
import Taro from "@tarojs/taro";
import { getBootPageAPI } from "@/api";
import { getMyFamiliesAPI } from "@/api/family";
import { silentAuth } from "@/utils/authRedirect";
import "./index.less";
// 定义响应式数据
const adImageUrl = ref('');
const loading = ref(true);
const hasFamily = ref(false);
const imageLoading = ref(true); // 图片加载状态
const imageLoadError = ref(false); // 图片加载错误状态
const loadingTimeout = ref(null); // 加载超时定时器
const retryCount = ref(0); // 重试次数
const maxRetries = ref(3); // 最大重试次数
const isTimeout = ref(false); // 是否超时
const showManualRefresh = ref(false); // 是否显示手动刷新按钮
/**
* 获取广告配置
*/
const fetchAdConfig = async () => {
try {
const { code, data } = await getBootPageAPI();
if (code && data) {
adImageUrl.value = data.adImageUrl;
}
} catch (error) {
console.error('获取广告配置失败:', error);
}
};
/**
* 检查用户是否加入家庭
*/
const checkUserFamily = async () => {
try {
const { code, data } = await getMyFamiliesAPI();
hasFamily.value = code && data && data?.families?.length > 0;
} catch (error) {
console.error('检查用户家庭状态失败:', error);
hasFamily.value = false;
}
};
/**
* 静默授权
*/
const performSilentAuth = async () => {
try {
await silentAuth();
} catch (error) {
console.error('静默授权失败:', error);
}
};
/**
* 点击图片处理
*/
const handleImageClick = () => {
if (hasFamily.value) {
// 已加入家庭,跳转到dashboard页面
Taro.redirectTo({
url: '/pages/Dashboard/index'
});
} else {
// 未加入家庭,跳转到welcome页面
Taro.redirectTo({
url: '/pages/Welcome/index'
});
}
};
/**
* 图片加载完成处理
*/
const handleImageLoad = () => {
clearLoadingTimeout();
imageLoading.value = false;
imageLoadError.value = false;
isTimeout.value = false;
showManualRefresh.value = false;
retryCount.value = 0;
};
/**
* 图片加载失败处理
*/
const handleImageError = () => {
clearLoadingTimeout();
imageLoading.value = false;
imageLoadError.value = true;
handleLoadingTimeout();
};
/**
* 重试加载图片
*/
const retryLoadImage = () => {
imageLoading.value = true;
imageLoadError.value = false;
isTimeout.value = false;
showManualRefresh.value = false;
// 开始超时检测
startLoadingTimeout();
// 重新获取广告配置
fetchAdConfig();
};
/**
* 开始加载超时检测
*/
const startLoadingTimeout = () => {
// 清除之前的定时器
if (loadingTimeout.value) {
clearTimeout(loadingTimeout.value);
}
// 设置10秒超时
loadingTimeout.value = setTimeout(() => {
if (imageLoading.value) {
console.warn('图片加载超时');
isTimeout.value = true;
handleLoadingTimeout();
}
}, 10000);
};
/**
* 处理加载超时
*/
const handleLoadingTimeout = () => {
if (retryCount.value < maxRetries.value) {
// 自动重试
retryCount.value++;
console.log(`第${retryCount.value}次自动重试加载`);
retryLoadImage();
} else {
// 超过最大重试次数,显示手动刷新选项
imageLoading.value = false;
showManualRefresh.value = true;
console.error('图片加载失败,已达到最大重试次数');
}
};
/**
* 清除加载超时定时器
*/
const clearLoadingTimeout = () => {
if (loadingTimeout.value) {
clearTimeout(loadingTimeout.value);
loadingTimeout.value = null;
}
};
/**
* 页面初始化逻辑
*/
const initializePage = async () => {
try {
// 开始超时检测
startLoadingTimeout();
// 执行静默授权
await performSilentAuth();
// 并行获取广告配置和检查家庭状态
await Promise.all([
fetchAdConfig(),
checkUserFamily()
]);
} catch (error) {
console.error('页面初始化失败:', error);
// 初始化失败时也要处理超时
handleLoadingTimeout();
} finally {
loading.value = false;
}
};
/**
* 页面初始化
*/
onMounted(() => {
initializePage();
});
/**
* 定义分享给朋友的内容
* @returns {Object} 分享配置对象
*/
const onShareAppMessage = (res) => {
const shareData = {
title: '和家长一起走路、一起打卡、一起兑换',
path: `/pages/AdPage/index`,
imageUrl: ''
};
return shareData;
}
// 使用Taro的useShareAppMessage Hook来处理分享
Taro.useShareAppMessage((res) => {
return onShareAppMessage(res);
});
</script>