up. 新增 workbox-webpack-plugin 包

This commit is contained in:
u2nyakim 2025-08-29 13:45:50 +08:00
parent 7fff0b31b4
commit 847352a788
7 changed files with 109 additions and 6 deletions

View File

@ -38,14 +38,15 @@
"nprogress": "0.2.0",
"pinia": "3.0.2",
"sortablejs": "1.15.6",
"tinymce": "5.10.9",
"tinymce": "^8.0.2",
"vue": "3.5.15",
"vue-echarts": "7.0.3",
"vue-i18n": "11.1.5",
"vue-i18n": "^11.1.11",
"vue-router": "4.5.1",
"vuedraggable": "4.1.0",
"workbox-webpack-plugin": "^7.3.0",
"xgplayer": "3.0.22",
"xgplayer-hls": "3.0.22",
"xgplayer-hls": "^2.5.2",
"xgplayer-music": "3.0.22"
},
"devDependencies": {

View File

@ -0,0 +1,63 @@
// 存储所有客户端ID用于定向通知
let controlledClients = [];
// 监听安装事件
self.addEventListener('install', e => e.waitUntil(self.skipWaiting()));
// 监听激活事件
self.addEventListener('activate', e =>
e.waitUntil(clients.claim().then(() => self.clients.matchAll())
.then(clients => {
// 获取所有受控客户端ID
controlledClients = clients.map(c => c.id);
})
)
);
// 监听客户端连接变化(窗口打开/关闭)
self.addEventListener('message', event => {
if (event.data.type === 'REGISTER_CLIENT') {
if (!controlledClients.includes(event.data.clientId)) {
controlledClients.push(event.data.clientId);
}
}
});
// 核心:处理锁屏/解锁广播
self.addEventListener('message', event => {
const { action, senderId } = event.data;
if (action === 'LOCK' || action === 'UNLOCK') {
// 向所有其他客户端广播
self.clients.matchAll().then(clients => {
clients.forEach(client => {
// 排除发送者
if (client.id !== senderId) {
client.postMessage({
action: `${action}_REQUESTED`,
timestamp: Date.now()
});
}
});
});
}
});
self.addEventListener('push', event => {
const title = '新消息';
const options = {
body: event.data.text(),
// icon: '/images/icon.png',
// badge: '/images/badge.png'
};
event.waitUntil(
self.registration.showNotification(title, options)
);
});
self.addEventListener('notificationclick', event => {
event.notification.close();
event.waitUntil(
clients.openWindow('https://example.com/notifications')
);
});

View File

@ -8,6 +8,8 @@ import i18n from './i18n';
import installer from './as-needed';
import { iconsInstaller } from '@/components/IconSelect/util';
import { WsConfig, WsPlugin } from '@/plugins/websocket';
import "@/plugins/notification"
import "@/plugins/service-worker"
import 'element-plus/theme-chalk/display.css';
import 'ele-admin-plus/es/style/nprogress.scss';
import './styles/themes/rounded.scss';

View File

@ -0,0 +1,11 @@
/*
*
*/
Notification && Notification.requestPermission().then(permission => {
if (permission === 'granted') {
console.debug("[.permission] 通知权限已授予");
}else{
console.error("[.permission] 通知权限未授权");
}
});

View File

@ -0,0 +1,4 @@
export function lock(){
}

View File

@ -0,0 +1,13 @@
function serviceWorkerRegister() {
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js').then(registration => {
console.log('Service Worker 注册成功:', registration);
}).catch(error => {
console.error('Service Worker 注册失败:', error);
});
});
}
}
serviceWorkerRegister();

View File

@ -5,14 +5,23 @@ import Compression from 'vite-plugin-compression';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { EleAdminResolver } from 'ele-admin-plus/es/utils/resolvers';
import GenSW from 'workbox-webpack-plugin';
export default defineConfig(({ command }) => {
const isBuild = command === 'build';
const alias = {
'@/': resolve('src') + '/',
'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
};
const plugins = [vue()];
const plugins = [
vue(),
new GenSW.GenerateSW({
// 配置选项
swDest: 'service-worker.js', // Service Worker 的输出文件路径
clientsClaim: true,
skipWaiting: true,
exclude: [/\.map$/, /asset-manifest\.json$/], // 可选:排除某些文件
})
];
if (isBuild) {
// 组件按需引入
plugins.push(