根因:access_token每2小时过期,前端收到401直接清token跳登录,没有用refresh_token续期 后端修复: - adminauth模块新增 POST /admin/auth/refresh 接口 - Service 注入 JWTManager,支持 admin refresh token 换新 token pair - Refresh 方法验证 subjectType=admin + tokenType=refresh 前端修复: - 401 拦截器核心改造:收到401先调 refresh 接口续期 - 加 isRefreshing 锁 + pendingRequests 队列防止并发刷新 - refresh 用原生 axios.post 避免拦截器递归 - 成功则更新 localStorage + 重试原请求,失败才清 token 跳登录 - 排除 /auth/refresh 自身避免死循环 - 支持 /admin/ 请求独立 token 管理 - auth.ts/adminAuth.ts 新增手动 refreshUserToken/refreshAdminSession - 路由守卫给所有需登录路由添加 meta.requiresAuth - 守卫同时支持 PC 端 /login 和移动端 /m/login
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package adminaudit
|
|
|
|
import (
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
|
|
"gorm.io/datatypes"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Repository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB) *Repository {
|
|
return &Repository{db: db}
|
|
}
|
|
|
|
func (r *Repository) List(query Query) (*PaginatedResult, error) {
|
|
db := r.db.Table("audit_logs AS al").
|
|
Select(`al.id, al.actor_type, al.actor_id, COALESCE(au.username, '') AS actor_username,
|
|
COALESCE(au.nickname, '') AS actor_nickname, al.action, al.biz_type, al.biz_id,
|
|
al.ip, al.user_agent, al.detail, al.created_at`).
|
|
Joins("LEFT JOIN admin_users AS au ON au.id = al.actor_id AND al.actor_type = ?", "admin")
|
|
|
|
countDB := r.db.Model(&model.AuditLog{})
|
|
if query.ActorID > 0 {
|
|
db = db.Where("al.actor_id = ?", query.ActorID)
|
|
countDB = countDB.Where("actor_id = ?", query.ActorID)
|
|
}
|
|
if query.Action != "" {
|
|
db = db.Where("al.action = ?", query.Action)
|
|
countDB = countDB.Where("action = ?", query.Action)
|
|
}
|
|
if query.BizType != "" {
|
|
db = db.Where("al.biz_type = ?", query.BizType)
|
|
countDB = countDB.Where("biz_type = ?", query.BizType)
|
|
}
|
|
|
|
var total int64
|
|
if err := countDB.Count(&total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
offset := (query.Page - 1) * query.PageSize
|
|
var rows []auditLogRow
|
|
if err := db.Order("al.id DESC").Offset(offset).Limit(query.PageSize).Scan(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]LogDTO, 0, len(rows))
|
|
for _, row := range rows {
|
|
items = append(items, row.toDTO())
|
|
}
|
|
return &PaginatedResult{Items: items, Total: total, Page: query.Page, PageSize: query.PageSize}, nil
|
|
}
|
|
|
|
type auditLogRow struct {
|
|
ID uint64
|
|
ActorType string
|
|
ActorID uint64
|
|
ActorUsername string
|
|
ActorNickname string
|
|
Action string
|
|
BizType string
|
|
BizID *uint64
|
|
IP string
|
|
UserAgent string
|
|
Detail datatypes.JSON
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func (row auditLogRow) toDTO() LogDTO {
|
|
return LogDTO{
|
|
ID: row.ID,
|
|
ActorType: row.ActorType,
|
|
ActorID: row.ActorID,
|
|
ActorUsername: row.ActorUsername,
|
|
ActorNickname: row.ActorNickname,
|
|
Action: row.Action,
|
|
BizType: row.BizType,
|
|
BizID: row.BizID,
|
|
IP: row.IP,
|
|
UserAgent: row.UserAgent,
|
|
Detail: row.Detail,
|
|
CreatedAt: row.CreatedAt,
|
|
}
|
|
}
|