fix: Auth redirects and navigation for landing page routing
- login.html: Post-login redirect / -> /index.html; already-logged-in redirect / -> /index.html - articles.html, topics.html: Auth failure redirect -> /login.html - uni-nav.js: Dashboard link / -> /index.html; navigate function fix for prefix handling - sw.js: Remove / pre-cache; HTML documents use Network First strategy for fresh content Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -168,7 +168,7 @@ const ArticlesApp = {
|
|||||||
getToken() { return localStorage.getItem('authToken'); },
|
getToken() { return localStorage.getItem('authToken'); },
|
||||||
async api(url, opts = {}) {
|
async api(url, opts = {}) {
|
||||||
const token = this.getToken();
|
const token = this.getToken();
|
||||||
if (!token) { this.$message.error('请先登录'); setTimeout(() => window.location.href = '/', 1500); return null; }
|
if (!token) { this.$message.error('请先登录'); setTimeout(() => window.location.href = '/login.html', 1500); return null; }
|
||||||
const res = await fetch(url, { headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json', ...opts.headers }, ...opts });
|
const res = await fetch(url, { headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json', ...opts.headers }, ...opts });
|
||||||
if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.detail || `请求失败: ${res.status}`); }
|
if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.detail || `请求失败: ${res.status}`); }
|
||||||
return res.json();
|
return res.json();
|
||||||
|
|||||||
@@ -103,7 +103,7 @@
|
|||||||
type: 'success',
|
type: 'success',
|
||||||
duration: 1500,
|
duration: 1500,
|
||||||
onClose: () => {
|
onClose: () => {
|
||||||
window.location.href = '/';
|
window.location.href = '/index.html';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -120,7 +120,7 @@
|
|||||||
// 检查是否已登录
|
// 检查是否已登录
|
||||||
const token = localStorage.getItem('authToken');
|
const token = localStorage.getItem('authToken');
|
||||||
if (token) {
|
if (token) {
|
||||||
window.location.href = '/';
|
window.location.href = '/index.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+31
-31
@@ -1,12 +1,8 @@
|
|||||||
// Service Worker for 宇之然内容创作平台
|
// Service Worker for 宇之然内容创作平台
|
||||||
const CACHE_NAME = 'yuzhiran-v2';
|
const CACHE_NAME = 'yuzhiran-v3';
|
||||||
const CACHE_URLS = [
|
const CACHE_URLS = [
|
||||||
'/',
|
|
||||||
'/index.html',
|
'/index.html',
|
||||||
'/offline.html',
|
'/offline.html',
|
||||||
'/static/vue.global.prod.js',
|
|
||||||
'/static/element-plus.css',
|
|
||||||
'/static/element-plus.full.js',
|
|
||||||
'/manifest.json'
|
'/manifest.json'
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -17,7 +13,6 @@ self.addEventListener('install', (event) => {
|
|||||||
caches.open(CACHE_NAME).then((cache) => {
|
caches.open(CACHE_NAME).then((cache) => {
|
||||||
console.log('[SW] Pre-caching core assets');
|
console.log('[SW] Pre-caching core assets');
|
||||||
return cache.addAll(CACHE_URLS.map(url => {
|
return cache.addAll(CACHE_URLS.map(url => {
|
||||||
// 忽略同源请求404错误(静态资源可能不存在)
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch(url).then(response => {
|
fetch(url).then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
@@ -25,15 +20,10 @@ self.addEventListener('install', (event) => {
|
|||||||
} else {
|
} else {
|
||||||
reject(new Error(`Failed to fetch ${url}: ${response.status}`));
|
reject(new Error(`Failed to fetch ${url}: ${response.status}`));
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
}).catch(() => { resolve(url); });
|
||||||
// 静默失败,不阻止安装
|
|
||||||
resolve(url);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
}).catch(err => {
|
}).catch(err => { console.error('[SW] Install failed:', err); })
|
||||||
console.error('[SW] Install failed:', err);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
@@ -56,27 +46,44 @@ self.addEventListener('activate', (event) => {
|
|||||||
self.clients.claim();
|
self.clients.claim();
|
||||||
});
|
});
|
||||||
|
|
||||||
// 网络请求拦截:Cache First + Network Fallback
|
// 网络请求拦截
|
||||||
self.addEventListener('fetch', (event) => {
|
self.addEventListener('fetch', (event) => {
|
||||||
const { request } = event;
|
const { request } = event;
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
|
||||||
// 只处理同源请求
|
// 只处理同源请求
|
||||||
if (url.origin !== location.origin) {
|
if (url.origin !== location.origin) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// API 请求:Network Only(不走缓存)
|
// API 请求:Network Only
|
||||||
if (url.pathname.startsWith('/api/')) {
|
if (url.pathname.startsWith('/api/')) {
|
||||||
event.respondWith(fetch(request));
|
event.respondWith(fetch(request));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 静态资源:Cache First
|
// HTML 文档:Network First(确保 landing page 始终加载最新版)
|
||||||
|
if (url.pathname === '/' || url.pathname.endsWith('.html')) {
|
||||||
|
event.respondWith(
|
||||||
|
fetch(request)
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
const clone = response.clone();
|
||||||
|
caches.open(CACHE_NAME).then(cache => cache.put(request, clone));
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
return caches.match(request).then(cached => {
|
||||||
|
return cached || caches.match('/offline.html');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他静态资源:Cache First
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(request).then((cached) => {
|
caches.match(request).then((cached) => {
|
||||||
if (cached) {
|
if (cached) {
|
||||||
// 返回缓存,并在后台更新
|
|
||||||
fetch(request).then(response => {
|
fetch(request).then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
caches.open(CACHE_NAME).then(cache => cache.put(request, response));
|
caches.open(CACHE_NAME).then(cache => cache.put(request, response));
|
||||||
@@ -84,17 +91,13 @@ self.addEventListener('fetch', (event) => {
|
|||||||
});
|
});
|
||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 无缓存,发起网络请求
|
|
||||||
return fetch(request).then(response => {
|
return fetch(request).then(response => {
|
||||||
// 成功且为有效响应,加入缓存
|
|
||||||
if (response.ok && response.status === 200) {
|
if (response.ok && response.status === 200) {
|
||||||
const responseClone = response.clone();
|
const clone = response.clone();
|
||||||
caches.open(CACHE_NAME).then(cache => cache.put(request, responseClone));
|
caches.open(CACHE_NAME).then(cache => cache.put(request, clone));
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
// 网络失败,尝试返回离线页面(如果是文档请求)
|
|
||||||
if (request.destination === 'document') {
|
if (request.destination === 'document') {
|
||||||
return caches.match('/offline.html');
|
return caches.match('/offline.html');
|
||||||
}
|
}
|
||||||
@@ -103,14 +106,11 @@ self.addEventListener('fetch', (event) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 后台同步(可选:在网络恢复后发送错误日志)
|
// 后台同步
|
||||||
self.addEventListener('sync', (event) => {
|
self.addEventListener('sync', (event) => {
|
||||||
if (event.tag === 'sync-logs') {
|
if (event.tag === 'sync-logs') { event.waitUntil(syncLogs()); }
|
||||||
event.waitUntil(syncLogs());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
async function syncLogs() {
|
async function syncLogs() {
|
||||||
// TODO: 实现日志同步
|
|
||||||
console.log('[SW] Syncing logs...');
|
console.log('[SW] Syncing logs...');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -445,7 +445,7 @@ const TopicsApp = {
|
|||||||
getToken() { return localStorage.getItem('authToken'); },
|
getToken() { return localStorage.getItem('authToken'); },
|
||||||
async api(url, opts = {}) {
|
async api(url, opts = {}) {
|
||||||
const token = this.getToken();
|
const token = this.getToken();
|
||||||
if (!token) { this.$message.error('请先登录'); setTimeout(() => window.location.href = '/', 1500); return null; }
|
if (!token) { this.$message.error('请先登录'); setTimeout(() => window.location.href = '/login.html', 1500); return null; }
|
||||||
const res = await fetch(url, { headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json', ...opts.headers }, ...opts });
|
const res = await fetch(url, { headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json', ...opts.headers }, ...opts });
|
||||||
if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.detail || `请求失败: ${res.status}`); }
|
if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.detail || `请求失败: ${res.status}`); }
|
||||||
return res.json();
|
return res.json();
|
||||||
@@ -722,8 +722,22 @@ const TopicsApp = {
|
|||||||
this.editing = false;
|
this.editing = false;
|
||||||
this.editContent = '';
|
this.editContent = '';
|
||||||
},
|
},
|
||||||
|
sanitizeHtml(html) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
div.innerHTML = html;
|
||||||
|
div.querySelectorAll('script, iframe, object, embed').forEach(el => el.remove());
|
||||||
|
div.querySelectorAll('*').forEach(el => {
|
||||||
|
for (const attr of el.attributes) {
|
||||||
|
if (attr.name.startsWith('on') || attr.value.toLowerCase().startsWith('javascript:')) {
|
||||||
|
el.removeAttribute(attr.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return div.innerHTML;
|
||||||
|
},
|
||||||
async saveContent() {
|
async saveContent() {
|
||||||
if (!this.previewTopic || !this.editContent) { this.$message.warning('没有内容可保存'); return; }
|
if (!this.previewTopic || !this.editContent) { this.$message.warning('没有内容可保存'); return; }
|
||||||
|
this.editContent = this.sanitizeHtml(this.editContent);
|
||||||
this.savingContent = true;
|
this.savingContent = true;
|
||||||
try {
|
try {
|
||||||
const original = this.platformContents[this.previewPlatform] || '';
|
const original = this.platformContents[this.previewPlatform] || '';
|
||||||
|
|||||||
@@ -121,14 +121,14 @@
|
|||||||
|
|
||||||
function getCurrentPage() {
|
function getCurrentPage() {
|
||||||
var path = window.location.pathname;
|
var path = window.location.pathname;
|
||||||
if (path === '/' || path === '/index.html') return 'dashboard';
|
if (path === '/index.html') return 'dashboard';
|
||||||
var m = path.match(/\/(\w+)\.html/);
|
var m = path.match(/\/(\w+)\.html/);
|
||||||
return m ? m[1] : 'dashboard';
|
return m ? m[1] : 'dashboard';
|
||||||
}
|
}
|
||||||
|
|
||||||
function navItems(isAdmin) {
|
function navItems(isAdmin) {
|
||||||
var items = [
|
var items = [
|
||||||
{ key: 'dashboard', label: '仪表盘', page: '/' },
|
{ key: 'dashboard', label: '仪表盘', page: 'index.html' },
|
||||||
{ key: 'topics', label: '选题', page: 'topics.html' },
|
{ key: 'topics', label: '选题', page: 'topics.html' },
|
||||||
{ key: 'metrics', label: '数据', page: 'metrics.html' },
|
{ key: 'metrics', label: '数据', page: 'metrics.html' },
|
||||||
{ key: 'calendar', label: '日历', page: 'calendar.html' },
|
{ key: 'calendar', label: '日历', page: 'calendar.html' },
|
||||||
@@ -194,7 +194,7 @@
|
|||||||
navigate: function (page) {
|
navigate: function (page) {
|
||||||
this.dropdownOpen = false; this.profileOpen = false;
|
this.dropdownOpen = false; this.profileOpen = false;
|
||||||
if (this.onNavigate) { this.onNavigate(page); return; }
|
if (this.onNavigate) { this.onNavigate(page); return; }
|
||||||
window.location.href = page === '/' ? '/' : '/' + page;
|
window.location.href = page.startsWith('/') ? page : '/' + page;
|
||||||
},
|
},
|
||||||
logout: function () {
|
logout: function () {
|
||||||
this.profileOpen = false;
|
this.profileOpen = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user