fix: 修复通知系统链接错误、标记已读失败及帖子404页渲染异常

- 修复 post_rejected 通知链接到帖子详情页(原来链接到 /studio/posts)
- 为 audit_approved/audit_rejected 通知添加链接到 /settings
- 通知已读标记改用 fetch keepalive 代替 sendBeacon,附带 CSRF token,避免页面跳转时请求丢失
- 修复 repository 层 id 列名应为 uid(BaseModel 定义 primarykey column:uid)
- 帖子404页添加 ExtraCSS 引入 posts.css,修复 SVG 图标无尺寸约束全屏渲染异常
This commit is contained in:
2026-05-31 13:33:12 +08:00
parent f0bf450725
commit 59af6b2635
4 changed files with 44 additions and 32 deletions

View File

@ -40,11 +40,9 @@
{{range $i, $m := .Messages}}
{{$link := ""}}
{{if or (eq $m.NotifyType "post_approved") (eq $m.NotifyType "post_rejected")}}
{{if eq $m.NotifyType "post_approved"}}
{{$link = printf "/posts/%d" $m.RelatedID}}
{{else}}
{{$link = "/studio/posts"}}
{{end}}
{{$link = printf "/posts/%d" $m.RelatedID}}
{{else if or (eq $m.NotifyType "audit_approved") (eq $m.NotifyType "audit_rejected")}}
{{$link = "/settings"}}
{{end}}
{{if $link}}
<a href="{{$link}}" class="msg-card-link">
@ -107,29 +105,37 @@
(function (card) {
card.addEventListener('click', function (e) {
var link = card.getAttribute('data-link');
// 如果是可跳转的通知post 审核),不阻止默认行为,只标记已读
// 标记已读通过 fetch 发送,不阻止链接跳转
var id = card.getAttribute('data-id');
if (!id) return;
var xhr = new XMLHttpRequest();
xhr.open('POST', '/api/messages/' + id + '/read', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
card.classList.remove('unread');
var badge = card.querySelector('.msg-badge');
if (badge) badge.remove();
updateUnreadCounts();
}
};
xhr.send();
// 如果有链接,在标记已读后跳转(非 post 通知直接标记已读)
if (link) {
// 有链接的通知:使用 fetch keepalive 确保页面跳转前标记已读不会丢失
e.preventDefault();
var self = this;
setTimeout(function () {
window.location.href = link;
}, 200);
var csrfMeta = document.querySelector('meta[name="csrf-token"]');
var csrfToken = csrfMeta ? csrfMeta.getAttribute('content') : '';
fetch('/api/messages/' + id + '/read', {
method: 'POST',
keepalive: true,
headers: { 'X-CSRF-Token': csrfToken }
});
card.classList.remove('unread');
var badge = card.querySelector('.msg-badge');
if (badge) badge.remove();
updateUnreadCounts();
window.location.href = link;
} else {
// 无链接的通知:使用 XHR 标记已读
var xhr = new XMLHttpRequest();
xhr.open('POST', '/api/messages/' + id + '/read', true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
card.classList.remove('unread');
var badge = card.querySelector('.msg-badge');
if (badge) badge.remove();
updateUnreadCounts();
}
};
xhr.send();
}
});
})(cards[i]);