修复问题
This commit is contained in:
@@ -15,10 +15,16 @@ self.addEventListener('install', (event) => {
|
|||||||
console.log('[Service Worker] 缓存文件');
|
console.log('[Service Worker] 缓存文件');
|
||||||
return cache.addAll(urlsToCache);
|
return cache.addAll(urlsToCache);
|
||||||
})
|
})
|
||||||
.then(() => self.skipWaiting())
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听跳过等待消息
|
||||||
|
self.addEventListener('message', (event) => {
|
||||||
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
||||||
|
self.skipWaiting();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 激活 Service Worker
|
// 激活 Service Worker
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
console.log('[Service Worker] 激活中...');
|
console.log('[Service Worker] 激活中...');
|
||||||
|
|||||||
@@ -20,6 +20,11 @@
|
|||||||
</van-tabbar-item>
|
</van-tabbar-item>
|
||||||
</van-tabbar>
|
</van-tabbar>
|
||||||
<GlobalAddBill @success="handleAddTransactionSuccess"/>
|
<GlobalAddBill @success="handleAddTransactionSuccess"/>
|
||||||
|
|
||||||
|
<div v-if="needRefresh" class="update-toast" @click="updateServiceWorker">
|
||||||
|
<van-icon name="upgrade" class="update-icon" />
|
||||||
|
<span>新版本可用,点击刷新</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</van-config-provider>
|
</van-config-provider>
|
||||||
</template>
|
</template>
|
||||||
@@ -29,6 +34,7 @@ import { RouterView, useRoute } from 'vue-router'
|
|||||||
import { ref, onMounted, onUnmounted, computed, watch } from 'vue'
|
import { ref, onMounted, onUnmounted, computed, watch } from 'vue'
|
||||||
import { useMessageStore } from '@/stores/message'
|
import { useMessageStore } from '@/stores/message'
|
||||||
import GlobalAddBill from '@/components/Global/GlobalAddBill.vue'
|
import GlobalAddBill from '@/components/Global/GlobalAddBill.vue'
|
||||||
|
import { needRefresh, updateServiceWorker } from './registerServiceWorker'
|
||||||
import '@/styles/common.css'
|
import '@/styles/common.css'
|
||||||
|
|
||||||
const messageStore = useMessageStore()
|
const messageStore = useMessageStore()
|
||||||
@@ -221,4 +227,31 @@ const subscribeToPush = async () => {
|
|||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
pointer-events: auto;
|
pointer-events: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.update-toast {
|
||||||
|
position: fixed;
|
||||||
|
bottom: 80px;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
background-color: var(--van-primary-color);
|
||||||
|
color: white;
|
||||||
|
padding: 10px 20px;
|
||||||
|
border-radius: 24px;
|
||||||
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
||||||
|
z-index: 2000;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
font-size: 14px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.update-toast:active {
|
||||||
|
transform: translateX(-50%) scale(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
.update-icon {
|
||||||
|
font-size: 18px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,4 +1,13 @@
|
|||||||
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
export const needRefresh = ref(false);
|
||||||
|
let swRegistration = null;
|
||||||
|
|
||||||
|
export function updateServiceWorker() {
|
||||||
|
if (swRegistration && swRegistration.waiting) {
|
||||||
|
swRegistration.waiting.postMessage({ type: 'SKIP_WAITING' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function register() {
|
export function register() {
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
@@ -8,6 +17,7 @@ export function register() {
|
|||||||
navigator.serviceWorker
|
navigator.serviceWorker
|
||||||
.register(swUrl)
|
.register(swUrl)
|
||||||
.then((registration) => {
|
.then((registration) => {
|
||||||
|
swRegistration = registration;
|
||||||
console.log('[SW] Service Worker 注册成功:', registration.scope);
|
console.log('[SW] Service Worker 注册成功:', registration.scope);
|
||||||
|
|
||||||
// 检查更新
|
// 检查更新
|
||||||
@@ -20,7 +30,7 @@ export function register() {
|
|||||||
if (navigator.serviceWorker.controller) {
|
if (navigator.serviceWorker.controller) {
|
||||||
// 新的 Service Worker 已安装,提示用户刷新
|
// 新的 Service Worker 已安装,提示用户刷新
|
||||||
console.log('[SW] 新版本可用,请刷新页面');
|
console.log('[SW] 新版本可用,请刷新页面');
|
||||||
showUpdateNotification();
|
needRefresh.value = true;
|
||||||
} else {
|
} else {
|
||||||
// 首次安装
|
// 首次安装
|
||||||
console.log('[SW] 内容已缓存,可离线使用');
|
console.log('[SW] 内容已缓存,可离线使用');
|
||||||
@@ -59,13 +69,6 @@ export function unregister() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 显示更新提示
|
|
||||||
function showUpdateNotification() {
|
|
||||||
// 你可以使用 Vant 的 Dialog 或 Notify 组件
|
|
||||||
if (window.confirm('发现新版本,是否立即更新?')) {
|
|
||||||
window.location.reload();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 请求通知权限
|
// 请求通知权限
|
||||||
export function requestNotificationPermission() {
|
export function requestNotificationPermission() {
|
||||||
|
|||||||
@@ -68,7 +68,7 @@
|
|||||||
</van-list>
|
</van-list>
|
||||||
|
|
||||||
<!-- 底部安全距离 -->
|
<!-- 底部安全距离 -->
|
||||||
<div style="height: calc(80px + env(safe-area-inset-bottom, 0px))"></div>
|
<div style="height: calc(50px + env(safe-area-inset-bottom, 0px))"></div>
|
||||||
</van-pull-refresh>
|
</van-pull-refresh>
|
||||||
|
|
||||||
<!-- 底部新增按钮 -->
|
<!-- 底部新增按钮 -->
|
||||||
|
|||||||
@@ -50,6 +50,8 @@
|
|||||||
<van-cell title="退出登录" is-link @click="handleLogout" />
|
<van-cell title="退出登录" is-link @click="handleLogout" />
|
||||||
</van-cell-group>
|
</van-cell-group>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- 底部安全距离 -->
|
||||||
|
<div style="height: calc(50px + env(safe-area-inset-bottom, 0px))"></div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user