|
@@ -13,6 +13,8 @@ import (
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/pkg/errors"
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -24,8 +26,8 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
//return
|
|
|
|
|
|
form := request.Check(c, &struct {
|
|
|
- //Gid string `form:"gid" json:"gid" binding:"required"`
|
|
|
- //Pf string `form:"pf" json:"pf" binding:"required"`
|
|
|
+ //Gid string `form:"gid" json:"gid" binding:"required"`
|
|
|
+ //Pf string `form:"pf" json:"pf" binding:"required"`
|
|
|
UserId int `form:"userId" json:"userId" binding:"required"`
|
|
|
Action string `form:"action" json:"action" binding:"required"`
|
|
|
Timestamp int64 `form:"timestamp" json:"timestamp" binding:"required"`
|
|
@@ -43,9 +45,28 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
pf := c.GetString("pf")
|
|
|
openId := c.GetString("openid")
|
|
|
|
|
|
- if gid == "linkup" {
|
|
|
- global.App.Log.Info("用户请求ReceiveGameMsg,参数打印:")
|
|
|
- utils.PrintStructFields(form)
|
|
|
+ if gid == "zmybp_ds2" && form.Action == "seeAds" {
|
|
|
+
|
|
|
+ // 生成日期目录路径(示例:./data/2024-06-15)
|
|
|
+ now := time.Now()
|
|
|
+ dateDir := now.Format("2006-01-02")
|
|
|
+ dirPath := filepath.Join("storage", dateDir)
|
|
|
+
|
|
|
+ // 创建日期目录(若不存在)
|
|
|
+ os.MkdirAll(dirPath, 0755)
|
|
|
+
|
|
|
+ // 生成文件名(示例:g123_wechat.txt)
|
|
|
+ filePath := filepath.Join(dirPath, "adsLog.txt")
|
|
|
+
|
|
|
+ // 追加写入文件
|
|
|
+ file, _ := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
+
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ // 写入数据(示例:1584,oXyZ123456\n)
|
|
|
+ if _, err := fmt.Fprintf(file, "%d,%s,%s,%s,%d,%s\n", form.UserId, form.AdsId, form.AdsScene, form.AdsType, form.AdsState, now.Format("2006-01-02 15:04:05")); err != nil {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
now := time.UnixMilli(form.Timestamp)
|
|
@@ -63,10 +84,15 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
} else if form.Action == "online" {
|
|
|
//活跃
|
|
|
err = online(gid, pf, openId, form.UserId, now)
|
|
|
+
|
|
|
+ err = SetLocalActiveLog(gid, pf, openId, form.UserId, 1, now)
|
|
|
} else if form.Action == "offline" {
|
|
|
//断开
|
|
|
err = offline(gid, pf, openId, form.UserId, now)
|
|
|
+
|
|
|
+ err = SetLocalActiveLog(gid, pf, openId, form.UserId, 2, now)
|
|
|
} else if form.Action == "seeAds" {
|
|
|
+
|
|
|
//观看广告
|
|
|
err = seeAds(gid, pf, openId, form.UserId, now, form.AdsId, form.AdsType, form.AdsScene, form.AdsState, adStartTime)
|
|
|
} else {
|
|
@@ -244,6 +270,7 @@ func offline(gid string, pf string, openId string, userId int, now time.Time) er
|
|
|
if err != nil {
|
|
|
global.App.Log.Error("存储用户在线时长mongo失败", err.Error(), userOnline)
|
|
|
}
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -417,3 +444,32 @@ func ReceiveAdsInfo(c *gin.Context) {
|
|
|
|
|
|
response.Success(c, gin.H{"data": "success"})
|
|
|
}
|
|
|
+
|
|
|
+func SetLocalActiveLog(gid string, pf string, openId string, userId int, types int, now time.Time) error {
|
|
|
+ // 生成日期目录路径(示例:./data/2024-06-15)
|
|
|
+ dateDir := now.Format("2006-01-02")
|
|
|
+ dirPath := filepath.Join("storage", dateDir)
|
|
|
+
|
|
|
+ // 创建日期目录(若不存在)
|
|
|
+ if err := os.MkdirAll(dirPath, 0755); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成文件名(示例:g123_wechat.txt)
|
|
|
+ fileName := fmt.Sprintf("%s_%s.txt", gid, pf)
|
|
|
+ filePath := filepath.Join(dirPath, fileName)
|
|
|
+
|
|
|
+ // 追加写入文件
|
|
|
+ file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("failed to open file: %v", err)
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ // 写入数据(示例:1584,oXyZ123456\n)
|
|
|
+ if _, err := fmt.Fprintf(file, "%d,%d,%s\n", userId, types, now.Format("2006-01-02 15:04:05")); err != nil {
|
|
|
+ return fmt.Errorf("failed to write file: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|