Files
Go-Web-Template/server/middleware/app_activity.go
2026-04-23 15:29:07 +08:00

35 lines
880 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package middleware
import (
"time"
appSvc "git.echol.cn/loser/Go-Web-Template/server/service/app"
"git.echol.cn/loser/Go-Web-Template/server/utils"
"github.com/gin-gonic/gin"
)
// AppActivity 玩家端活跃记录:每次请求写 last_active_at轻量 upsert
func AppActivity() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
uid := utils.GetUserID(c)
if uid == 0 {
return
}
// 避免极端高频写:简单节流(同请求链路内只写一次即可)
if _, exists := c.Get("__app_activity_written"); exists {
return
}
c.Set("__app_activity_written", true)
// 尽量不阻塞响应:允许轻微延迟
go func(userID uint, ip string) {
_ = appSvc.AppActivityServiceApp.TouchActive(userID, ip)
}(uid, c.ClientIP())
_ = time.Now() // 保留 import time避免未来扩展时频繁改动不影响逻辑
}
}