Procházet zdrojové kódy

增加下载接口

wucan před 2 měsíci
rodič
revize
0858581a24

+ 1 - 0
controller/v1/gameAction.go

@@ -38,6 +38,7 @@ func UserActionList(c *gin.Context) {
 	var userLogin []model.UserLogin
 	query1 := global.App.DB.Table("user_login")
 	if len(form.Pf) > 0 {
+
 		query1 = query1.WhereIn("pf", form.Pf)
 	}
 	err = query1.

+ 123 - 11
controller/v1/gameConfig.go

@@ -8,12 +8,46 @@ import (
 	"designs/global"
 	"designs/model"
 	"designs/utils"
+	"encoding/json"
 	"github.com/gin-gonic/gin"
 	"github.com/go-playground/validator/v10"
 	"strconv"
 	"time"
 )
 
+func InitGidConfig(c *gin.Context) {
+	gidKey := config.Get("app.gid") + "*"
+	keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
+	var gameData = []interface{}{}
+	for _, val := range keys {
+		res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
+		gameData = append(gameData, res)
+	}
+
+	now := model.XTime{
+		Time: time.Now(),
+	}
+	for _, v := range gameData {
+		var game model.Game
+		data, _ := json.Marshal(v)
+		json.Unmarshal(data, &game)
+
+		game.CreatedAt = now
+		game.UpdatedAt = now
+
+		err := global.App.DB.Table("game").Create(&game).Error
+		if err != nil {
+			response.Fail(c, 111, "数据插入错误"+err.Error())
+			return
+		}
+	}
+
+	response.Success(c, gin.H{
+		"res": "gid写入数据库完成",
+	})
+
+}
+
 /* 添加游戏配置 */
 //http://127.0.0.1:8787/v1/user/AddGidConfig
 func AddGidConfig(c *gin.Context) {
@@ -26,6 +60,57 @@ func AddGidConfig(c *gin.Context) {
 		return
 	}
 
+	now := time.Now()
+
+	//更新数据库
+	var game model.Game
+	global.App.DB.Table("game").Where("gid", form.Gid).First(&game)
+
+	if game.Gid == "" {
+		//新增
+		gameNew := model.Game{
+			Gid:        form.Gid,
+			Pid:        form.Pid,
+			GameName:   form.GameName,
+			WxAppid:    form.WxAppid,
+			WxSecret:   form.WxSecret,
+			TtAppid:    form.TtAppid,
+			TtSecret:   form.TtSecret,
+			TtTplId:    form.TtTplId,
+			WxTplId:    form.WxTplId,
+			CreatedAt:  model.XTime{Time: now},
+			UpdatedAt:  model.XTime{Time: now},
+			ConfigPath: form.ConfigPath,
+		}
+
+		err := global.App.DB.Table("game").Create(&gameNew).Error
+		if err != nil {
+			response.Fail(c, 1001, err.Error())
+			return
+		}
+	} else {
+		//更新
+		err := global.App.DB.Table("game").Where("gid", form.Gid).Updates(&model.Game{
+			Gid:        form.Gid,
+			Pid:        form.Pid,
+			GameName:   form.GameName,
+			WxAppid:    form.WxAppid,
+			WxSecret:   form.WxSecret,
+			TtAppid:    form.TtAppid,
+			TtSecret:   form.TtSecret,
+			TtTplId:    form.TtTplId,
+			WxTplId:    form.WxTplId,
+			ConfigPath: form.ConfigPath,
+			CreatedAt:  model.XTime{Time: now},
+			UpdatedAt:  model.XTime{Time: now},
+		}).Error
+		if err != nil {
+			response.Fail(c, 1001, err.Error())
+			return
+		}
+	}
+
+	//更新redis
 	gameConfigData := make(map[string]interface{})
 	gameConfigData["gid"] = form.Gid
 	gameConfigData["gameName"] = form.GameName
@@ -35,6 +120,7 @@ func AddGidConfig(c *gin.Context) {
 	gameConfigData["ttSecret"] = form.TtSecret
 	gameConfigData["ttTplId"] = form.TtTplId
 	gameConfigData["wxTplId"] = form.WxTplId
+	gameConfigData["configPath"] = form.ConfigPath
 	gidKey := config.Get("app.gid") + form.Gid
 	err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
 	if err != nil {
@@ -49,22 +135,48 @@ func AddGidConfig(c *gin.Context) {
 
 /* 获取配置 */
 func GetGidConfig(c *gin.Context) {
-	var data GetGameCfg
-	form := request.Check(c, &data)
-	if form.AppSecret != config.Get("app.app_secret") {
-		response.Fail(c, 1003, "密钥不对")
+	form := request.Check(c, &struct {
+		Offset int    `form:"offset" binding:"" json:"offset"`
+		Limit  int    `form:"limit" binding:"" json:"limit"`
+		Search string `form:"search" binding:"" json:"search"`
+	}{})
+
+	var gameList []model.Game
+	var count int64
+	query := global.App.DB.Table("game")
+
+	if form.Search != "" {
+		query = query.Where("gid", "like", "%"+form.Search+"%").
+			Or("pid LIKE ?", "%"+form.Search+"%").
+			Or("gameName LIKE ?", "%"+form.Search+"%")
 	}
 
-	gidKey := config.Get("app.gid") + "*"
-	keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
-	var gameData = []interface{}{}
-	for _, val := range keys {
-		res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
-		gameData = append(gameData, res)
+	err := query.Count(&count).Error
+	if err != nil {
+		response.Fail(c, 1001, err.Error())
+		return
 	}
 
+	if form.Limit != 0 {
+		query = query.Limit(form.Limit)
+	}
+	err = query.Offset(form.Offset).Scan(&gameList).Error
+	if err != nil {
+		response.Fail(c, 1002, err.Error())
+		return
+	}
+
+	//gidKey := config.Get("app.gid") + "*"
+	//keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
+	//var gameData = []interface{}{}
+	//for _, val := range keys {
+	//	res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
+	//	gameData = append(gameData, res)
+	//}
+
 	response.Success(c, gin.H{
-		"data": gameData,
+		"data":  gameList,
+		"count": count,
 	})
 }
 

+ 46 - 0
controller/v1/user.go

@@ -12,6 +12,8 @@ import (
 	"designs/utils"
 	"fmt"
 	"go.mongodb.org/mongo-driver/v2/bson"
+	"os"
+	"path/filepath"
 	"strings"
 	"time"
 
@@ -20,6 +22,7 @@ import (
 
 type GameConfig struct {
 	Gid       string `form:"gid" json:"gid" binding:"required"`             //游戏id
+	Pid       string `form:"pid" json:"pid" binding:""`                     //游戏pid
 	GameName  string `form:"gameName" json:"gameName" binding:"required"`   //游戏名称
 	WxAppid   string `form:"wxAppid" json:"wxAppid" binding:"required"`     //微信appid
 	WxSecret  string `form:"wxSecret" json:"wxSecret" binding:"required"`   //微信secret
@@ -28,6 +31,8 @@ type GameConfig struct {
 	AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
 	TtTplId   string `form:"ttTplId" json:"ttTplId" binding:""`
 	WxTplId   string `form:"wxTplId" json:"wxTplId" binding:""`
+
+	ConfigPath string `form:"configPath" json:"configPath" binding:""`
 }
 
 type GetGameCfg struct {
@@ -244,3 +249,44 @@ func UserBehaviorList(c *gin.Context) {
 	fmt.Println(res)
 
 }
+
+func DownloadFile(c *gin.Context) {
+	filename := c.Param("filename")
+
+	// 验证文件是否存在
+	if _, err := os.Stat(filename); os.IsNotExist(err) {
+		c.JSON(404, gin.H{"error": "文件不存在"})
+		return
+	}
+
+	// 设置响应头,触发浏览器下载行为
+	c.Header("Content-Disposition", "attachment; filename="+filename)
+	c.Header("Content-Type", "application/octet-stream")
+	c.File(filename)
+}
+
+// 上传图片到服务器
+func Upload(c *gin.Context) {
+	// 获取文件
+	file, err := c.FormFile("file")
+	if err != nil {
+		c.JSON(400, gin.H{"error": "未选择文件"})
+		return
+	}
+
+	// 存储路径生成
+	saveDir := fmt.Sprintf("uploads/%s", time.Now().Format("2006-01-02"))
+	os.MkdirAll(saveDir, 0755)
+	savePath := filepath.Join(saveDir, file.Filename)
+
+	// 保存文件
+	if err := c.SaveUploadedFile(file, savePath); err != nil {
+		c.JSON(500, gin.H{"error": "文件保存失败"})
+		return
+	}
+	fmt.Println(savePath)
+
+	response.Success(c, gin.H{
+		"path": savePath,
+	})
+}

+ 56 - 0
controller/v1/userAds.go

@@ -194,6 +194,7 @@ func UserAdsDetail(c *gin.Context) {
 	if form.StartTime != "" && form.EndTime != "" {
 		query = query.Where("user_see_ads.createdAt", ">=", form.StartTime).Where("user_see_ads.createdAt", "<=", form.EndTime)
 	}
+
 	if len(form.AdsState) > 0 {
 		query = query.WhereIn("adsState", form.AdsState)
 	}
@@ -229,3 +230,58 @@ func UserAdsDetail(c *gin.Context) {
 	})
 
 }
+
+// 广告详细数据 --- 饼状图
+func UserAdsCake(c *gin.Context) {
+	form := request.Check(c, &struct {
+		Gid       string   `json:"gid" binding:"required"`
+		Pf        []string `form:"pf" binding:""`
+		StartTime string   `form:"startTime" json:"startTime" binding:""`
+		EndTime   string   `form:"endTime" json:"endTime" binding:""`
+		AdsState  []int    `json:"adsState" binding:""`
+		AdsType   string   `json:"adsType" binding:""`
+	}{})
+
+	form.EndTime = form.EndTime + " 23:59:59"
+
+	query := global.App.DB.Table("user_see_ads").Where("gid", form.Gid).Group("adsScene")
+
+	if len(form.Pf) > 0 {
+		query = query.WhereIn("pf", form.Pf)
+	}
+
+	if form.StartTime != "" && form.EndTime != "" {
+		query = query.Where("user_see_ads.createdAt", ">=", form.StartTime).Where("user_see_ads.createdAt", "<=", form.EndTime)
+	}
+	if len(form.AdsState) > 0 {
+		query = query.WhereIn("adsState", form.AdsState)
+	}
+	if form.AdsType != "" {
+		query = query.Where("adsType", form.AdsType)
+	}
+
+	var data []struct {
+		Count    int64  `json:"count" gorm:"column:count;"`
+		AdsScene string `json:"adsScene" gorm:"not null;column:adsScene;"`
+		SumType1 int    `json:"sumType1" gorm:"not null;column:sumType1;"`
+		SumType2 int    `json:"sumType2" gorm:"not null;column:sumType2;"`
+		SumType0 int    `json:"sumType0" gorm:"not null;column:sumType0;"`
+	}
+
+	err := query.Select(
+		"adsScene",
+		"adsState",
+		"count(*) as count",
+		"SUM(adsState = 1) AS sumType1",
+		"SUM(adsState = 2) AS sumType2",
+		"SUM(adsState = 0) AS sumType0",
+	).Scan(&data).Error
+	if err != nil {
+		response.Fail(c, 1001, err.Error())
+		return
+	}
+
+	response.Success(c, gin.H{
+		"data": data,
+	})
+}

+ 2 - 2
controller/v1/userBehavior.go

@@ -779,7 +779,7 @@ func AdRelatedList(c *gin.Context) {
 		Offset int `form:"offset" json:"offset" binding:""`
 		Limit  int `form:"limit" json:"limit" binding:""`
 
-		Pid int    `form:"pid" json:"pid" binding:""`
+		Pid string `form:"pid" json:"pid" binding:""`
 		Aid int    `form:"aid" json:"aid" binding:""`
 		Cid string `form:"cid" json:"cid" binding:""`
 
@@ -795,7 +795,7 @@ func AdRelatedList(c *gin.Context) {
 
 	ctx := context.Background()
 	filter := bson.M{}
-	if form.Pid != 0 {
+	if form.Pid != "" {
 		filter["pid"] = form.Pid
 	}
 	if form.Aid != 0 {

+ 17 - 0
model/game.go

@@ -0,0 +1,17 @@
+package model
+
+type Game struct {
+	Id         int    `json:"id" gorm:"primary_key"`
+	Pid        string `json:"pid" gorm:"column:pid; "`
+	Gid        string `json:"gid" gorm:"column:gid; "`
+	GameName   string `json:"gameName" gorm:"column:gameName; "`
+	WxAppid    string `json:"wxAppid" gorm:"column:wxAppid; "`
+	WxSecret   string `json:"wxSecret" gorm:"column:wxSecret; "`
+	TtAppid    string `json:"ttAppid" gorm:"column:ttAppid; "`
+	TtSecret   string `json:"ttSecret" gorm:"column:ttSecret; "`
+	TtTplId    string `json:"ttTplId" gorm:"column:ttTplId; "`
+	WxTplId    string `json:"wxTplId" gorm:"column:wxTplId; "`
+	ConfigPath string `json:"configPath" gorm:"column:configPath; "`
+	CreatedAt  XTime  `json:"createdAt" gorm:"column:createdAt; "`
+	UpdatedAt  XTime  `json:"updatedAt" gorm:"column:updatedAt; "`
+}

+ 7 - 0
route/api.go

@@ -14,6 +14,9 @@ func SetApiGroupRoutes(router *gin.RouterGroup) {
 	router.POST("/user/refreshToken", middleware.RefreshTokenAuthMiddleware(), v1.RefreshToken) //token刷新
 	router.POST("/user/getSysTime", v1.GetSysTime)
 
+	router.GET("/download", v1.DownloadFile) //文件下载
+	router.POST("/download/upload", v1.Upload)
+
 	GroupV1 := router.Group("")
 	GroupV1.Use(middleware.TokenAuthMiddleware()).Use()
 	{
@@ -69,6 +72,8 @@ func SetApiGroupRoutes(router *gin.RouterGroup) {
 		GroupV1.POST("/user/userAdsOverview", v1.UserAdsOverview)
 		GroupV1.POST("/user/userAdsDaily", v1.UserAdsDaily)
 		GroupV1.POST("/user/userAdsDetail", v1.UserAdsDetail)
+
+		GroupV1.POST("/user/userAdsCake", v1.UserAdsCake)
 	}
 
 	router.POST("/SetUserBehaviorBak", v1.SetUserBehaviorBak)       //测试接口
@@ -79,4 +84,6 @@ func SetApiGroupRoutes(router *gin.RouterGroup) {
 	router.POST("/Websocket1", v1.Websocket1)                       //测试接口
 	router.POST("/Websocket2", v1.Websocket2)                       //测试接口
 
+	router.POST("/InitGidConfig", v1.InitGidConfig)
+
 }