35 lines
880 B
Go
35 lines
880 B
Go
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,避免未来扩展时频繁改动(不影响逻辑)
|
||
}
|
||
}
|