diff --git a/app/middleware.php b/app/middleware.php
index 19ac7ef0..1e65b740 100755
--- a/app/middleware.php
+++ b/app/middleware.php
@@ -7,4 +7,6 @@ return [
// \think\middleware\LoadLangPack::class,
// Session初始化
\think\middleware\SessionInit::class,
+ // 给所有服务端 HTML 页面注入 PWA 元数据和注册脚本。
+ \app\middleware\Pwa::class,
];
diff --git a/app/middleware/Pwa.php b/app/middleware/Pwa.php
new file mode 100644
index 00000000..b009162f
--- /dev/null
+++ b/app/middleware/Pwa.php
@@ -0,0 +1,47 @@
+
+
+
+
+
+
+
+
+HTML;
+
+ public function handle($request, Closure $next): Response
+ {
+ /** @var Response $response */
+ $response = $next($request);
+ $content = $response->getContent();
+
+ if (stripos($content, '') === false || stripos($content, '/i',
+ self::HEAD_MARKUP . "\n ",
+ $content,
+ 1
+ );
+
+ return $response->content($content ?? $response->getContent());
+ }
+}
diff --git a/public/manifest.webmanifest b/public/manifest.webmanifest
new file mode 100644
index 00000000..03ab15f5
--- /dev/null
+++ b/public/manifest.webmanifest
@@ -0,0 +1,18 @@
+{
+ "name": "码蚁成绩管理系统",
+ "short_name": "码蚁成绩",
+ "description": "码蚁免费成绩管理系统",
+ "start_url": "/login",
+ "scope": "/",
+ "display": "standalone",
+ "background_color": "#f5f7fa",
+ "theme_color": "#1677ff",
+ "icons": [
+ {
+ "src": "/pwa/app-icon.svg",
+ "sizes": "any",
+ "type": "image/svg+xml",
+ "purpose": "any maskable"
+ }
+ ]
+}
diff --git a/public/offline.html b/public/offline.html
new file mode 100644
index 00000000..ddbc3c4b
--- /dev/null
+++ b/public/offline.html
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+ 网络不可用
+
+
+
+
+ 分
+ 当前网络不可用
+ 成绩、学生信息和提交操作需要联网后才能继续使用。请检查网络连接后重试。
+
+
+
+
diff --git a/public/pwa/app-icon.svg b/public/pwa/app-icon.svg
new file mode 100644
index 00000000..afffcbe8
--- /dev/null
+++ b/public/pwa/app-icon.svg
@@ -0,0 +1,5 @@
+
diff --git a/public/pwa/register.js b/public/pwa/register.js
new file mode 100644
index 00000000..f93dcfbc
--- /dev/null
+++ b/public/pwa/register.js
@@ -0,0 +1,26 @@
+(() => {
+ 'use strict';
+
+ if (!('serviceWorker' in navigator) || !window.isSecureContext) {
+ return;
+ }
+
+ window.addEventListener('load', () => {
+ navigator.serviceWorker.register('/service-worker.js', { scope: '/' })
+ .then((registration) => {
+ registration.addEventListener('updatefound', () => {
+ const worker = registration.installing;
+ if (!worker) return;
+
+ worker.addEventListener('statechange', () => {
+ if (worker.state === 'installed' && navigator.serviceWorker.controller) {
+ window.dispatchEvent(new CustomEvent('pwa:update-available'));
+ }
+ });
+ });
+ })
+ .catch(() => {
+ // PWA enhancement must never affect normal use of the system.
+ });
+ });
+})();
diff --git a/public/service-worker.js b/public/service-worker.js
new file mode 100644
index 00000000..f5aa5984
--- /dev/null
+++ b/public/service-worker.js
@@ -0,0 +1,57 @@
+const CACHE_NAME = 'shangma-pwa-v1';
+const APP_SHELL = [
+ '/offline.html',
+ '/manifest.webmanifest',
+ '/pwa/app-icon.svg',
+ '/pwa/register.js'
+];
+
+self.addEventListener('install', (event) => {
+ event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)));
+ self.skipWaiting();
+});
+
+self.addEventListener('activate', (event) => {
+ event.waitUntil(
+ caches.keys().then((keys) => Promise.all(
+ keys.filter((key) => key.startsWith('shangma-pwa-') && key !== CACHE_NAME)
+ .map((key) => caches.delete(key))
+ ))
+ );
+ self.clients.claim();
+});
+
+self.addEventListener('fetch', (event) => {
+ const request = event.request;
+ const url = new URL(request.url);
+
+ if (request.method !== 'GET' || url.origin !== self.location.origin) {
+ return;
+ }
+
+ if (request.mode === 'navigate') {
+ event.respondWith(
+ fetch(request).catch(() => caches.match('/offline.html'))
+ );
+ return;
+ }
+
+ // Only public versioned/static UI assets are cached. HTML, APIs, downloads,
+ // uploads, scores and all authenticated data remain network-only.
+ if (!url.pathname.startsWith('/static/') && !url.pathname.startsWith('/pwa/')) {
+ return;
+ }
+
+ event.respondWith(
+ caches.match(request).then((cached) => {
+ if (cached) return cached;
+
+ return fetch(request).then((response) => {
+ if (!response.ok || response.type !== 'basic') return response;
+ const copy = response.clone();
+ caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
+ return response;
+ });
+ })
+ );
+});