- 所有Handler方法使用标准{code:0,message:"success",data:...}响应格式
- 修复Cursor分页响应包装(GetAllDevices,GetLoginLogs,ListUsers等)
- 修复AuthHandler和SMSHandler认证方法响应格式
- 修复operation_log.go admin用户operation_type前缀问题
- 修复DashboardPage嵌套stats结构
- 修复LoginLogsPage reset功能stale closure问题
- 修复UsersPage批量操作API调用
- 修复多个前端测试(mock格式、按钮选择、断言逻辑)
- 添加OAuth测试域名白名单
- 新增代码审查流程文档
165 lines
3.9 KiB
Go
165 lines
3.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/user-management-system/internal/service"
|
|
)
|
|
|
|
// LogHandler handles log requests
|
|
type LogHandler struct {
|
|
loginLogService *service.LoginLogService
|
|
operationLogService *service.OperationLogService
|
|
}
|
|
|
|
// NewLogHandler creates a new LogHandler
|
|
func NewLogHandler(loginLogService *service.LoginLogService, operationLogService *service.OperationLogService) *LogHandler {
|
|
return &LogHandler{
|
|
loginLogService: loginLogService,
|
|
operationLogService: operationLogService,
|
|
}
|
|
}
|
|
|
|
func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
|
|
userID, ok := getUserIDFromContext(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
|
return
|
|
}
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
logs, total, err := h.loginLogService.GetMyLoginLogs(c.Request.Context(), userID, page, pageSize)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"list": logs,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
})
|
|
}
|
|
|
|
func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
|
|
userID, ok := getUserIDFromContext(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
|
return
|
|
}
|
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
logs, total, err := h.operationLogService.GetMyOperationLogs(c.Request.Context(), userID, page, pageSize)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"list": logs,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
})
|
|
}
|
|
|
|
func (h *LogHandler) GetLoginLogs(c *gin.Context) {
|
|
var req service.ListLoginLogRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Use cursor-based pagination when cursor is provided
|
|
if req.Cursor != "" || req.Size > 0 {
|
|
result, err := h.loginLogService.GetLoginLogsCursor(c.Request.Context(), &req)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": result,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Fallback to legacy offset-based pagination
|
|
logs, total, err := h.loginLogService.GetLoginLogs(c.Request.Context(), &req)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"list": logs,
|
|
"total": total,
|
|
"page": req.Page,
|
|
"page_size": req.PageSize,
|
|
})
|
|
}
|
|
|
|
func (h *LogHandler) GetOperationLogs(c *gin.Context) {
|
|
var req service.ListOperationLogRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Use cursor-based pagination when cursor is provided
|
|
if req.Cursor != "" || req.Size > 0 {
|
|
result, err := h.operationLogService.GetOperationLogsCursor(c.Request.Context(), &req)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": result,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Fallback to legacy offset-based pagination
|
|
logs, total, err := h.operationLogService.GetOperationLogs(c.Request.Context(), &req)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"list": logs,
|
|
"total": total,
|
|
"page": req.Page,
|
|
"page_size": req.PageSize,
|
|
})
|
|
}
|
|
|
|
func (h *LogHandler) ExportLoginLogs(c *gin.Context) {
|
|
var req service.ExportLoginLogRequest
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data, filename, contentType, err := h.loginLogService.ExportLoginLogs(c.Request.Context(), &req)
|
|
if err != nil {
|
|
handleError(c, err)
|
|
return
|
|
}
|
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
|
c.Data(http.StatusOK, contentType, data)
|
|
}
|