feat: 消息通知中心 + 账号安全体系 + Bug修复

## 新增功能

### 消息通知中心 (全新模块)
- 新增 MessageController / NotificationService / NotificationRepo
- SSR 消息页面 (/messages):左侧边栏 + 右侧卡片列表,noindex 元标签
- 通知能力:列表分页、单条标为已读、一键全部标为已读
- 未读数角标 (1/66/99+):侧边栏 + 导航栏铃铛图标
- 导航栏轮询 /api/messages/unread 每 60 秒刷新未读数
- 审核通过/驳回时自动 fire-and-forget 推送通知

### 密码修改
- ChangePassword:验证当前密码 → 新密码强度校验(8位+字母+数字) → 哈希更新
- 修改后递增 token_version 强制所有设备退登

### 账号自助注销
- DeleteAccount:验证密码 → 设置 deleted 状态 → 记录原因 → 吊销 JWT
- 注销后登录二次确认:Login 检测 deleted → 返回 confirm_restore
- ConfirmRestore 恢复账号,重新签发 token
- 注销页文案:"账号将在 7 天后正式注销,期间可随时重新登录恢复"

### IP 审计记录
- 注册时记录 RegIP,登录时记录 LastLoginIP + LastLoginAt
- clientIP() 支持 X-Forwarded-For / X-Real-IP 反向代理

### 安全加固
- Login 防时序攻击:用户不存在时仍执行完整 bcrypt 比对
- FindByEmail 改用 Unscoped() 覆盖软删除用户
- 站长 (owner) 不允许自主注销,避免权限体系死锁

## Bug 修复

1. 注销按钮不触发:JS IIFE 中 profile 代码 return 阻塞了 account 标签页处理器注册
   → 拆分为两层 IIFE,profile 放在内层
2. label 缺少 for 属性导致控制台警告 → 全部补充 for 属性
3. 注销后未自动退登:DeleteAccount 只设状态未吊销 JWT → 末尾加 InvalidateSessions
4. 退登后重定向 500 panic:authenticateToken 中 err||versionMismatch 合并判断
   在 err=nil 但版本不匹配时返回 (nil,nil) → 拆为两个独立判断
   injectUserContext 增加 claims==nil / 类型断言空安全守卫
5. 注销后登录直接提示"登录成功":FindByEmail 默认 scope 排除软删除记录
   → 改用 Unscoped()

## 文件变更
- 新建 17 个文件 (消息/审核/站点设置完整模块)
- 修改 25 个文件 (认证/设置/中间件/前端)
- 统计:+3229 / -161,42 files changed
This commit is contained in:
2026-05-27 13:30:34 +08:00
parent 053ca32a14
commit 0800ee2f3e
42 changed files with 3237 additions and 169 deletions

View File

@ -0,0 +1,112 @@
package service
import (
"metazone.cc/metalab/internal/common"
"metazone.cc/metalab/internal/model"
)
// notifRepo 通知服务所需的最小仓储接口ISP
type notifRepo interface {
Create(n *model.Notification) error
ListByUser(userID uint, offset, limit int) ([]model.Notification, error)
CountByUser(userID uint) (int64, error)
CountUnread(userID uint) (int64, error)
MarkRead(id, userID uint) error
MarkAllRead(userID uint) error
}
// NotificationService 消息/通知业务逻辑
type NotificationService struct {
repo notifRepo
}
// NewNotificationService 构造函数
func NewNotificationService(repo notifRepo) *NotificationService {
return &NotificationService{repo: repo}
}
// Create 创建消息
func (s *NotificationService) Create(userID uint, notifyType, title, content string, relatedID *uint) error {
n := &model.Notification{
UserID: userID,
NotifyType: notifyType,
Title: title,
Content: content,
IsRead: false,
RelatedID: relatedID,
}
return s.repo.Create(n)
}
// List 分页查询用户消息列表
func (s *NotificationService) List(userID uint, page, pageSize int) (*model.NotificationListResult, error) {
p := common.Pagination{Page: page, PageSize: pageSize}
p.DefaultPagination()
total, err := s.repo.CountByUser(userID)
if err != nil {
return nil, err
}
items, err := s.repo.ListByUser(userID, p.Offset(), p.PageSize)
if err != nil {
return nil, err
}
if items == nil {
items = []model.Notification{}
}
totalPages := int((total + int64(p.PageSize) - 1) / int64(p.PageSize))
unread, _ := s.CountUnread(userID)
return &model.NotificationListResult{
Items: items,
Total: total,
Page: p.Page,
TotalPages: totalPages,
Unread: unread,
}, nil
}
// CountUnread 统计未读消息数
func (s *NotificationService) CountUnread(userID uint) (int64, error) {
return s.repo.CountUnread(userID)
}
// MarkRead 标记单条消息为已读
func (s *NotificationService) MarkRead(id, userID uint) error {
return s.repo.MarkRead(id, userID)
}
// MarkAllRead 标记所有未读为已读
func (s *NotificationService) MarkAllRead(userID uint) error {
return s.repo.MarkAllRead(userID)
}
// NotifyAuditApproved 审核通过通知
func (s *NotificationService) NotifyAuditApproved(userID uint, auditType string, relatedID uint) {
typeName := model.AuditTypeNames[auditType]
if typeName == "" {
typeName = auditType
}
_ = s.Create(userID, model.NotifyAuditApproved,
"审核已通过",
"你的"+typeName+"修改已通过审核",
&relatedID)
}
// NotifyAuditRejected 审核拒绝通知
func (s *NotificationService) NotifyAuditRejected(userID uint, auditType, reason string, relatedID uint) {
typeName := model.AuditTypeNames[auditType]
if typeName == "" {
typeName = auditType
}
content := "你的" + typeName + "修改被拒绝"
if reason != "" {
content += ",理由:" + reason
}
_ = s.Create(userID, model.NotifyAuditRejected,
"审核未通过",
content,
&relatedID)
}