feat(首页): 添加背景图和logo的版本控制及离线状态自动刷新
添加版本号参数到背景图和logo的URL,实现缓存刷新 优化离线状态切换逻辑,当网络恢复时自动刷新页面 移除CSS中硬编码的背景图URL,改为动态计算
Showing
1 changed file
with
75 additions
and
15 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2023-06-21 10:23:09 | 2 | * @Date: 2023-06-21 10:23:09 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2026-01-16 00:29:52 | 4 | + * @LastEditTime: 2026-01-16 01:02:45 |
| 5 | * @FilePath: /xyxBooking-weapp/src/pages/index/index.vue | 5 | * @FilePath: /xyxBooking-weapp/src/pages/index/index.vue |
| 6 | * @Description: 预约页首页 | 6 | * @Description: 预约页首页 |
| 7 | --> | 7 | --> |
| 8 | <template> | 8 | <template> |
| 9 | - <view class="index-page" :class="{ 'is-offline': is_offline }"> | 9 | + <view class="index-page" :class="{ 'is-offline': is_offline }" :style="page_style"> |
| 10 | <view v-if="is_offline" class="offline-banner mx-6 mt-4 rounded-xl px-4 py-3"> | 10 | <view v-if="is_offline" class="offline-banner mx-6 mt-4 rounded-xl px-4 py-3"> |
| 11 | <view class="flex items-center"> | 11 | <view class="flex items-center"> |
| 12 | <IconFont name="tips" size="14" /> | 12 | <IconFont name="tips" size="14" /> |
| ... | @@ -18,7 +18,7 @@ | ... | @@ -18,7 +18,7 @@ |
| 18 | <view style="height: 28vh;"> | 18 | <view style="height: 28vh;"> |
| 19 | <swiper class="my-swipe" :autoplay="true" :interval="3000" indicator-dots indicator-color="white" :circular="true"> | 19 | <swiper class="my-swipe" :autoplay="true" :interval="3000" indicator-dots indicator-color="white" :circular="true"> |
| 20 | <swiper-item> | 20 | <swiper-item> |
| 21 | - <image style="height: 28vh; width: 100vw;" src="https://cdn.ipadbiz.cn/xys/booking/banner01.png?imageMogr2/thumbnail/500x/strip/quality/100" /> | 21 | + <image style="height: 28vh; width: 100vw;" :src="banner_url" /> |
| 22 | </swiper-item> | 22 | </swiper-item> |
| 23 | </swiper> | 23 | </swiper> |
| 24 | </view> | 24 | </view> |
| ... | @@ -31,7 +31,7 @@ | ... | @@ -31,7 +31,7 @@ |
| 31 | </view> | 31 | </view> |
| 32 | </view> | 32 | </view> |
| 33 | </view> | 33 | </view> |
| 34 | - <view class="logo"></view> | 34 | + <view class="logo" :style="logo_style"></view> |
| 35 | </view> | 35 | </view> |
| 36 | <view class="index-nav"> | 36 | <view class="index-nav"> |
| 37 | <view class="nav-logo"> | 37 | <view class="nav-logo"> |
| ... | @@ -52,9 +52,9 @@ | ... | @@ -52,9 +52,9 @@ |
| 52 | </template> | 52 | </template> |
| 53 | 53 | ||
| 54 | <script setup> | 54 | <script setup> |
| 55 | -import Taro, { useDidShow, useShareAppMessage } from '@tarojs/taro' | 55 | +import Taro, { getCurrentInstance, useDidShow, useShareAppMessage } from '@tarojs/taro' |
| 56 | import { IconFont } from '@nutui/icons-vue-taro' | 56 | import { IconFont } from '@nutui/icons-vue-taro' |
| 57 | -import { ref, onMounted, onUnmounted } from 'vue' | 57 | +import { ref, onMounted, onUnmounted, computed } from 'vue' |
| 58 | import { useGo } from '@/hooks/useGo' | 58 | import { useGo } from '@/hooks/useGo' |
| 59 | import { get_network_type, is_usable_network } from '@/utils/network' | 59 | import { get_network_type, is_usable_network } from '@/utils/network' |
| 60 | import { weak_network_text } from '@/utils/uiText' | 60 | import { weak_network_text } from '@/utils/uiText' |
| ... | @@ -67,16 +67,78 @@ import icon_5 from '@/assets/images/我的01@2x.png' | ... | @@ -67,16 +67,78 @@ import icon_5 from '@/assets/images/我的01@2x.png' |
| 67 | const go = useGo(); | 67 | const go = useGo(); |
| 68 | const is_offline = ref(false) | 68 | const is_offline = ref(false) |
| 69 | const weak_network_banner_desc = weak_network_text.banner_desc | 69 | const weak_network_banner_desc = weak_network_text.banner_desc |
| 70 | +// 背景图版本号, 用于刷新背景图 | ||
| 71 | +const initial_t = Number(getCurrentInstance()?.router?.params?._t) | ||
| 72 | +const bg_version = ref(Number.isFinite(initial_t) ? initial_t : 0) | ||
| 73 | +let is_reloading = false | ||
| 74 | + | ||
| 75 | +const reload_page = () => { | ||
| 76 | + if (is_reloading) return | ||
| 77 | + is_reloading = true | ||
| 78 | + Taro.reLaunch({ | ||
| 79 | + url: `/pages/index/index?_t=${Date.now()}`, | ||
| 80 | + }) | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +const banner_url = computed(() => { | ||
| 84 | + return `https://cdn.ipadbiz.cn/xys/booking/banner01.png?imageMogr2/thumbnail/500x/strip/quality/100&v=${bg_version.value}` | ||
| 85 | +}) | ||
| 86 | +const normal_bg_url = computed(() => { | ||
| 87 | + return `https://cdn.ipadbiz.cn/xys/booking/bg.jpg?imageMogr2/thumbnail/200x/strip/quality/50&v=${bg_version.value}` | ||
| 88 | +}) | ||
| 89 | +const logo_url = computed(() => { | ||
| 90 | + return `https://cdn.ipadbiz.cn/xys/booking/logo.png?imageMogr2/thumbnail/50x/strip/quality/50&v=${bg_version.value}` | ||
| 91 | +}) | ||
| 92 | + | ||
| 93 | +/** | ||
| 94 | + * 页面样式 | ||
| 95 | + * - 离线状态: 背景颜色 + 背景图片 (包含渐变) | ||
| 96 | + * - 在线状态: 背景颜色 + 背景图片 | ||
| 97 | + */ | ||
| 98 | + | ||
| 99 | +const page_style = computed(() => { | ||
| 100 | + if (is_offline.value) { | ||
| 101 | + return { | ||
| 102 | + backgroundColor: '#F3EEE3', | ||
| 103 | + backgroundImage: `linear-gradient(180deg, rgba(166, 121, 57, 0.10) 0%, rgba(255, 255, 255, 0.90) 60%, rgba(243, 238, 227, 1) 100%), url('${normal_bg_url.value}')`, | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | + return { | ||
| 107 | + backgroundColor: '#F3EEE3', | ||
| 108 | + backgroundImage: `url('${normal_bg_url.value}')`, | ||
| 109 | + } | ||
| 110 | +}) | ||
| 111 | + | ||
| 112 | +const logo_style = computed(() => { | ||
| 113 | + return { | ||
| 114 | + backgroundImage: `url('${logo_url.value}')`, | ||
| 115 | + } | ||
| 116 | +}) | ||
| 117 | + | ||
| 118 | +/** | ||
| 119 | + * 应用离线状态 | ||
| 120 | + * - 检查当前状态是否需要刷新 | ||
| 121 | + * - 更新 is_offline 状态 | ||
| 122 | + */ | ||
| 123 | + | ||
| 124 | +const apply_offline_state = (next_offline) => { | ||
| 125 | + if (is_offline.value === true && next_offline === false) { | ||
| 126 | + reload_page() | ||
| 127 | + return true | ||
| 128 | + } | ||
| 129 | + is_offline.value = next_offline | ||
| 130 | + return false | ||
| 131 | +} | ||
| 70 | 132 | ||
| 71 | /** | 133 | /** |
| 72 | * 刷新离线状态 | 134 | * 刷新离线状态 |
| 73 | * - 检查当前网络类型是否可用 | 135 | * - 检查当前网络类型是否可用 |
| 74 | - * - 更新 is_offline 状态 | ||
| 75 | */ | 136 | */ |
| 76 | const refresh_offline_state = async () => { | 137 | const refresh_offline_state = async () => { |
| 77 | try { | 138 | try { |
| 78 | const network_type = await get_network_type() | 139 | const network_type = await get_network_type() |
| 79 | - is_offline.value = !is_usable_network(network_type) | 140 | + const next_offline = !is_usable_network(network_type) |
| 141 | + apply_offline_state(next_offline) | ||
| 80 | } catch (e) { | 142 | } catch (e) { |
| 81 | console.error('refresh_offline_state failed:', e) | 143 | console.error('refresh_offline_state failed:', e) |
| 82 | } | 144 | } |
| ... | @@ -99,8 +161,11 @@ const setup_network_listener = () => { | ... | @@ -99,8 +161,11 @@ const setup_network_listener = () => { |
| 99 | const is_connected = res?.isConnected !== false | 161 | const is_connected = res?.isConnected !== false |
| 100 | const network_type = res?.networkType | 162 | const network_type = res?.networkType |
| 101 | if (typeof network_type === 'string' && network_type) { | 163 | if (typeof network_type === 'string' && network_type) { |
| 102 | - is_offline.value = !(is_connected && is_usable_network(network_type)) | 164 | + // 有网, 检查是否可用 |
| 103 | - return | 165 | + const next_offline = !(is_connected && is_usable_network(network_type)) |
| 166 | + // 检查是否需要刷新 | ||
| 167 | + const is_handled = apply_offline_state(next_offline) | ||
| 168 | + if (is_handled) return | ||
| 104 | } | 169 | } |
| 105 | // 还没有网, 再次刷新 | 170 | // 还没有网, 再次刷新 |
| 106 | refresh_offline_state() | 171 | refresh_offline_state() |
| ... | @@ -176,16 +241,12 @@ useShareAppMessage(() => { | ... | @@ -176,16 +241,12 @@ useShareAppMessage(() => { |
| 176 | .index-page { | 241 | .index-page { |
| 177 | position: relative; | 242 | position: relative; |
| 178 | min-height: 100vh; | 243 | min-height: 100vh; |
| 179 | - background-image: url('https://cdn.ipadbiz.cn/xys/booking/bg.jpg?imageMogr2/thumbnail/200x/strip/quality/50'); | ||
| 180 | background-repeat: no-repeat; | 244 | background-repeat: no-repeat; |
| 181 | background-position: center; | 245 | background-position: center; |
| 182 | background-size: cover; /* 确保背景覆盖 */ | 246 | background-size: cover; /* 确保背景覆盖 */ |
| 183 | 247 | ||
| 184 | &.is-offline { | 248 | &.is-offline { |
| 185 | background-color: #F3EEE3; | 249 | background-color: #F3EEE3; |
| 186 | - background-image: | ||
| 187 | - linear-gradient(180deg, rgba(166, 121, 57, 0.10) 0%, rgba(255, 255, 255, 0.90) 60%, rgba(243, 238, 227, 1) 100%), | ||
| 188 | - url('https://cdn.ipadbiz.cn/xys/booking/bg.jpg?imageMogr2/thumbnail/200x/strip/quality/50'); | ||
| 189 | } | 250 | } |
| 190 | 251 | ||
| 191 | .offline-banner { | 252 | .offline-banner { |
| ... | @@ -277,7 +338,6 @@ useShareAppMessage(() => { | ... | @@ -277,7 +338,6 @@ useShareAppMessage(() => { |
| 277 | bottom: 200rpx; | 338 | bottom: 200rpx; |
| 278 | height: 400rpx; | 339 | height: 400rpx; |
| 279 | width: 150rpx; | 340 | width: 150rpx; |
| 280 | - background-image: url('https://cdn.ipadbiz.cn/xys/booking/logo.png?imageMogr2/thumbnail/50x/strip/quality/50'); | ||
| 281 | background-repeat: no-repeat; | 341 | background-repeat: no-repeat; |
| 282 | background-size: contain; | 342 | background-size: contain; |
| 283 | background-position: center; | 343 | background-position: center; | ... | ... |
-
Please register or login to post a comment