소스 검색

去掉了body大小验证

wucan 4 주 전
부모
커밋
0df77f7723
3개의 변경된 파일94개의 추가작업 그리고 37개의 파일을 삭제
  1. 1 0
      controller/v1/ecpm.go
  2. 61 5
      controller/v1/user.go
  3. 32 32
      middleware/auth.go

+ 1 - 0
controller/v1/ecpm.go

@@ -53,6 +53,7 @@ func GetAccessToken(gid string, ttAppid string, ttSecret string) (string, error)
 			"secret":     ttSecret,
 			"grant_type": "client_credential",
 		}
+
 		//请求接口获取token
 		content, err := service.CurlPost("https://minigame.zijieapi.com/mgplatform/api/apps/v2/token", reportDat, nil)
 		if err != nil {

+ 61 - 5
controller/v1/user.go

@@ -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
+}

+ 32 - 32
middleware/auth.go

@@ -37,33 +37,33 @@ func TokenAuthMiddleware() gin.HandlerFunc {
 		//	c.Abort()
 		//	return
 		//}
-		//token := c.GetHeader("Authorization")
-		////校验token
-		//if token == "" {
-		//	common.RetJson(-2, "Unauthorized", "", c)
-		//	c.Abort()
-		//	return
-		//}
-		//ok, openid, gid, pf := isValidToken(token)
-		//if !ok {
-		//	common.RetJson(-1, "authorized invalid!", "", c)
-		//	c.Abort()
-		//	return
-		//}
-		//
-		////校验数据合法性(用户信息是否有效)
-		//userKey := gid + ":" + pf + ":" + config.Get("app.user_table_key") + openid
-		//userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
-		//if err != nil {
-		//	common.RetJson(-1, "authorized invalid,redis cant find!", "", c)
-		//	c.Abort()
-		//	return
-		//}
-		//if len(userData) == 0 {
-		//	common.RetJson(-1, "用户信息不在数据库中", "", c)
-		//	c.Abort()
-		//	return
-		//}
+		token := c.GetHeader("Authorization")
+		//校验token
+		if token == "" {
+			common.RetJson(-2, "Unauthorized", "", c)
+			c.Abort()
+			return
+		}
+		ok, openid, gid, pf := isValidToken(token)
+		if !ok {
+			common.RetJson(-1, "authorized invalid!", "", c)
+			c.Abort()
+			return
+		}
+
+		//校验数据合法性(用户信息是否有效)
+		userKey := gid + ":" + pf + ":" + config.Get("app.user_table_key") + openid
+		userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
+		if err != nil {
+			common.RetJson(-1, "authorized invalid,redis cant find!", "", c)
+			c.Abort()
+			return
+		}
+		if len(userData) == 0 {
+			common.RetJson(-1, "用户信息不在数据库中", "", c)
+			c.Abort()
+			return
+		}
 
 		////校验请求次数
 		//apiPath := c.FullPath()
@@ -83,11 +83,11 @@ func TokenAuthMiddleware() gin.HandlerFunc {
 		//	return
 		//}
 
-		////设置上下文数据
-		//c.Set("openid", openid)
-		//c.Set("gid", gid)
-		//c.Set("pf", pf)
-		////如果校验通过
+		//设置上下文数据
+		c.Set("openid", openid)
+		c.Set("gid", gid)
+		c.Set("pf", pf)
+		//如果校验通过
 
 	}
 }