From 6b02bd2ded747210349e934837548f4f23b8e89c Mon Sep 17 00:00:00 2001 From: mofangmin Date: Wed, 7 Aug 2024 15:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=A2=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- call/redpoint.go | 14 +++++++-- call/user.go | 1 + modules/hall/player.go | 2 +- modules/web/handler/account.go | 6 ++++ modules/web/handler/activity.go | 46 ++++++++++++++++++++++++++--- modules/web/handler/share.go | 11 +++++++ modules/web/handler/task.go | 19 ++++++++++++ modules/web/handler/user.go | 46 +++++++++++++++++++++++++++++ modules/web/handler/vip.go | 18 +++++++++-- modules/web/routers/routers_user.go | 1 + pb/proto/platform.proto | 14 ++++++++- util/http.go | 18 +++++++++++ 12 files changed, 185 insertions(+), 11 deletions(-) diff --git a/call/redpoint.go b/call/redpoint.go index f459111..71ceb8b 100644 --- a/call/redpoint.go +++ b/call/redpoint.go @@ -8,6 +8,7 @@ import ( "server/pb" "strings" + "github.com/liangdas/mqant/log" "gorm.io/gorm" ) @@ -39,15 +40,22 @@ func UpsertRedPointAndNotify(uid, num int, module string) { db.Mysql().Update(&common.PlayerRed{UID: uid}, update) } send := &pb.RedPoint{} - reflect.ValueOf(send).Elem().FieldByName(module).SetUint(uint64(diff)) + send.List = append(send.List, &pb.RedInfo{ModuleName: pb.RedPointModule_RedPointMail, Num: uint32(diff)}) SendNR(uid, int(pb.ServerCommonResp_CommonRedPointResp), send, "common") } -func PushRed(uid int) { +func PushMailRed(uid int) { redPoints := &common.PlayerRed{UID: uid} db.Mysql().Get(redPoints) checkMail(uid, redPoints) one := new(pb.RedPoint) - one.List = append(one.List, &pb.RedInfo{ModuleName: "mail", Num: uint32(redPoints.Mail)}) + one.List = append(one.List, &pb.RedInfo{ModuleName: pb.RedPointModule_RedPointMail, Num: uint32(redPoints.Mail)}) + SendNR(uid, int(pb.ServerCommonResp_CommonRedPointResp), one, "common") +} + +func PushRed(uid int, module pb.RedPointModule, num uint32) { + one := new(pb.RedPoint) + one.List = append(one.List, &pb.RedInfo{ModuleName: module, Num: num}) + log.Debug("redpoint,uid:%v,module:%v,info:%v", uid, module, one) SendNR(uid, int(pb.ServerCommonResp_CommonRedPointResp), one, "common") } diff --git a/call/user.go b/call/user.go index 684297f..8300268 100644 --- a/call/user.go +++ b/call/user.go @@ -393,6 +393,7 @@ func UpdateVip(uid int, exp, bet, settle int64) { // 通知客户端等级变动 if update["level"] != nil { SendNR(uid, int(pb.ServerCommonResp_CommonVipResp), &pb.ConfigChangeResp{Type: pb.ConfigChangeType_ConfigVipLevel}, "common") + PushRed(uid, pb.RedPointModule_RedPointVipReward, 1) } break } diff --git a/modules/hall/player.go b/modules/hall/player.go index 4a9dffb..ad6f885 100644 --- a/modules/hall/player.go +++ b/modules/hall/player.go @@ -20,7 +20,7 @@ type player struct { } func (p *player) PushRedPoint() { - call.PushRed(p.db.Id) + call.PushMailRed(p.db.Id) } // func (p *player) Send(topic string, data proto.Message) { diff --git a/modules/web/handler/account.go b/modules/web/handler/account.go index d3b7084..ea45b0f 100644 --- a/modules/web/handler/account.go +++ b/modules/web/handler/account.go @@ -1,12 +1,14 @@ package handler import ( + "fmt" "server/call" "server/common" "server/config" "server/db" "server/modules/web/app" "server/modules/web/values" + "server/pb" "server/util" "time" @@ -101,6 +103,10 @@ func onLogin(user *common.PlayerDBInfo, a *app.Gin, isNew bool) { } call.InsertLoginRecord(uid, cid, ip, birth, deviceType) + // 第二次登录 + if db.Mysql().Count(&common.LoginRecord{}, fmt.Sprintf(" uid = %d", uid)) == 1 { + call.PushRed(uid, pb.RedPointModule_RedPointLoginAgain, 1) + } // if db.Mysql().CountTable(common.PlayerRechargeTableName, fmt.Sprintf("uid = %d", uid)) == 0 { // db.Mysql().C().Table(common.PlayerRechargeTableName).Create(&common.PlayerCurrency{UID: uid, ChannelID: cid}) // } diff --git a/modules/web/handler/activity.go b/modules/web/handler/activity.go index 573cf58..b62790f 100644 --- a/modules/web/handler/activity.go +++ b/modules/web/handler/activity.go @@ -9,6 +9,7 @@ import ( "server/db" "server/modules/web/app" "server/modules/web/values" + "server/pb" "server/util" "sort" "time" @@ -104,6 +105,15 @@ func GetPromotions(c *gin.Context) { a.GetUID() ret.ActivityList = call.GetConfigActivityActiveAll(a.UID) ret.TaskList = GetUserTaskStatus(a) + num := 0 + for _, task := range ret.TaskList { + if task.Status == 1 { + num++ + } + } + if num > 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointTask, uint32(num)) + } } func UploadActivityData(c *gin.Context) { @@ -543,6 +553,9 @@ func ActivityFreeSpinInfo(c *gin.Context) { } else { resp.Count = common.DefaultFreeSpinNum } + if resp.Count > 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointSign, 1) + } } func ActivityFreeSpinDraw(c *gin.Context) { @@ -974,6 +987,7 @@ func ActivitySignInfo(c *gin.Context) { return } a.Data = resp + } func ActivitySignDraw(c *gin.Context) { @@ -1159,6 +1173,13 @@ func ActivitySignNewInfo(c *gin.Context) { return } call.UploadActivityData(a.UID, common.ActivityIDSign, common.ActivityDataClick, 0) + num := 0 + if resp.CanSign { + num = 1 + } + if num > 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointSign, uint32(num)) + } a.Data = resp } @@ -1217,9 +1238,7 @@ func ActivitySignNewDraw(c *gin.Context) { // ok newSign := Sign(day) updates := map[string]interface{}{"sign": newSign, "finish": false, "reward": gorm.Expr("reward + ?", reward)} - if config.GetBase().Release { - updates["sign_time"] = now.Unix() - } + updates["sign_time"] = now.Unix() rows, err := db.Mysql().UpdateRes(&common.ActivitySignData{UID: a.UID, Sign: data.Sign}, updates) if rows == 0 || err != nil { log.Error("err:%v", err) @@ -1243,6 +1262,7 @@ func ActivitySignNewDraw(c *gin.Context) { } a.Data = values.ActivitySignDrawResp{Reward: reward, Day: day, Sign: newSign} call.UploadActivityData(a.UID, common.ActivityIDSign, common.ActivityDataJoin, reward) + call.PushRed(a.UID, pb.RedPointModule_RedPointSign, uint32(0)) } // 破产礼包活动 @@ -1851,10 +1871,17 @@ func ActivityBetDrawInfo(c *gin.Context) { db.Mysql().Create(drawInfo) } } - a.Data = &values.ActivityBetDrawInfoResp{ + resp := &values.ActivityBetDrawInfoResp{ List: call.GetConfigBetDraw(), Lucky: drawInfo.Lucky, } + a.Data = resp + for _, item := range resp.List { + if resp.Lucky >= item.Cost { + call.PushRed(a.UID, pb.RedPointModule_RedPointFreeSpin, uint32(1)) + break + } + } call.UploadActivityData(a.UID, common.ActivityIDBetDraw, common.ActivityDataClick, 0) } @@ -1921,6 +1948,17 @@ func ActivityBetDrawDraw(c *gin.Context) { a.Data = &values.ActivityBetDrawDrawResp{ Reward: reward, } + + drawInfo = common.ActivityBetDrawData{UID: a.UID} + db.Mysql().Get(&drawInfo) + list := call.GetConfigBetDraw() + num := 0 + for _, item := range list { + if drawInfo.Lucky >= item.Cost { + num++ + } + } + call.PushRed(a.UID, pb.RedPointModule_RedPointFreeSpin, uint32(num)) } func ActivityBetDrawHistory(c *gin.Context) { diff --git a/modules/web/handler/share.go b/modules/web/handler/share.go index 957149b..1e1e6b6 100644 --- a/modules/web/handler/share.go +++ b/modules/web/handler/share.go @@ -7,6 +7,7 @@ import ( "server/db" "server/modules/web/app" "server/modules/web/values" + "server/pb" "server/util" "time" @@ -113,6 +114,13 @@ func ShareInfo(c *gin.Context) { } resp.ShareLink = "https://" + resp.ShareLink } + num := 0 + if resp.AvailableReward > 0 { + num = 1 + } + if num > 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointShare, uint32(num)) + } } func GetActivityCode(c *gin.Context) { @@ -308,6 +316,9 @@ func ShareWithdraw(c *gin.Context) { db.Mysql().Update(&common.RechargeInfo{UID: a.UID}, u) a.Data = values.ShareWithdrawResp{Available: availableReward - req.Amount} + + call.PushRed(a.UID, pb.RedPointModule_RedPointShare, uint32(0)) + } // func ShareReference(c *gin.Context) { diff --git a/modules/web/handler/task.go b/modules/web/handler/task.go index 6077883..97fb100 100644 --- a/modules/web/handler/task.go +++ b/modules/web/handler/task.go @@ -6,6 +6,7 @@ import ( "server/db" "server/modules/web/app" "server/modules/web/values" + "server/pb" "github.com/gin-gonic/gin" "github.com/liangdas/mqant/log" @@ -24,6 +25,15 @@ func GetTaskInfo(c *gin.Context) { a.GetUID() resp := &TaskInfoResp{TaskList: GetUserTaskStatus(a)} a.Data = resp + num := 0 + for _, task := range resp.TaskList { + if task.Status == 1 { + num++ + } + } + if num > 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointTask, uint32(num)) + } } type DrawTaskReq struct { @@ -88,4 +98,13 @@ func DrawTask(c *gin.Context) { } TaskComplete(a, req) a.Data = req + + num := 0 + for _, task := range GetUserTaskStatus(a) { + if task.Status == 1 { + num++ + } + } + call.PushRed(a.UID, pb.RedPointModule_RedPointTask, uint32(num)) + } diff --git a/modules/web/handler/user.go b/modules/web/handler/user.go index f27987d..09bf20d 100644 --- a/modules/web/handler/user.go +++ b/modules/web/handler/user.go @@ -2,12 +2,14 @@ package handler import ( "fmt" + "net/http/httptest" "reflect" "server/call" "server/common" "server/db" "server/modules/web/app" "server/modules/web/values" + "server/util" "time" "github.com/gin-gonic/gin" @@ -96,6 +98,9 @@ func GetUserInfo(c *gin.Context) { a.Code = values.CodeOK // } a.Data = resp + util.Go(func() { + CheckRedPoint(uid, c) + }) } func EditUserInfo(c *gin.Context) { @@ -245,3 +250,44 @@ func FavoriteGame(c *gin.Context) { } } } + +func GetRedPoint(c *gin.Context) { + a := app.NewApp(c) + defer func() { + a.Response() + }() + CheckRedPoint(a.UID, c) +} + +func newContext(c *gin.Context) *gin.Context { + // 创建一个新的 HTTP 请求 + req := httptest.NewRequest(c.Request.Method, c.Request.URL.String(), c.Request.Body) + // 复制头部信息 + for key, values := range c.Request.Header { + for _, value := range values { + req.Header.Add(key, value) + } + } + // 创建一个新的 gin.Context + w := httptest.NewRecorder() + nContext, _ := gin.CreateTestContext(w) + nContext.Request = req + return nContext +} + +func CheckRedPoint(uid int, c *gin.Context) { + // 防止玩家未连接网关,所以延迟3秒 + time.AfterFunc(time.Second*3, func() { + nc := newContext(c) + // 是否可以转动转盘 + ActivityBetDrawInfo(nc) + // 是否可以签到 + ActivitySignNewInfo(nc) + // 判断是否有任务可以领取 + GetPromotions(nc) + // 邀请奖励 + ShareInfo(nc) + // vip奖励 + GetVipInfo(nc) + }) +} diff --git a/modules/web/handler/vip.go b/modules/web/handler/vip.go index adcd2c5..a065648 100644 --- a/modules/web/handler/vip.go +++ b/modules/web/handler/vip.go @@ -8,6 +8,7 @@ import ( "server/db" "server/modules/web/app" "server/modules/web/values" + "server/pb" "server/util" "time" @@ -22,7 +23,8 @@ func GetVipInfo(c *gin.Context) { a.Response() }() req := new(values.VipInfoReq) - if !a.S(req) { + ok, _ := util.CheckRequestBody(c) + if ok && !a.S(req) { return } if req.Num > 100 { @@ -101,7 +103,14 @@ func GetVipInfo(c *gin.Context) { q.Filter(elastic.NewTermQuery("uid", a.UID)) q.Filter(elastic.NewTermQuery("event", common.CurrencyEventVIPCashback)) resp.TotalCashback = db.ES().SumByInt64(common.ESIndexBalance, "value", q) - db.ES().QueryList(common.ESIndexBalance, req.Page, req.Num, q, &resp.CashbackList, "time", false) + // db.ES().QueryList(common.ESIndexBalance, req.Page, req.Num, q, &resp.CashbackList, "time", false) + num := 0 + if resp.Info.Cashback > 0 { + num = 1 + } + if num > 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointVipCashback, uint32(num)) + } } func DrawVipBonus(c *gin.Context) { @@ -173,6 +182,10 @@ func DrawVipBonus(c *gin.Context) { a.Code = values.CodeRetry return } + vip = call.GetVIP(a.UID) + if vip.Draws == 0 { + call.PushRed(a.UID, pb.RedPointModule_RedPointVipReward, uint32(0)) + } } func DrawVipCashback(c *gin.Context) { @@ -261,4 +274,5 @@ func DrawVipCashback(c *gin.Context) { resp.NextCashback = util.GetNext5MinUnix() - now.Unix() } a.Data = resp + call.PushRed(a.UID, pb.RedPointModule_RedPointVipCashback, uint32(0)) } diff --git a/modules/web/routers/routers_user.go b/modules/web/routers/routers_user.go index 27632b6..64ac49a 100644 --- a/modules/web/routers/routers_user.go +++ b/modules/web/routers/routers_user.go @@ -11,4 +11,5 @@ func user(e *gin.RouterGroup) { e.POST("/user/info/edit", handler.EditUserInfo) e.POST("/user/feedback", handler.Feedback) e.POST("/favorite/game", handler.FavoriteGame) + e.POST("/redpoint/get", handler.GetRedPoint) } diff --git a/pb/proto/platform.proto b/pb/proto/platform.proto index 30da3b1..11f7346 100644 --- a/pb/proto/platform.proto +++ b/pb/proto/platform.proto @@ -46,8 +46,20 @@ message LoginResponse{ uint32 user_id = 2; // UID:这个地方必定和请求中的uid一致,但是为了方便客户端异步处理,把UID返回了 } +enum RedPointModule{ + RedPointInvalid = 0; + RedPointTask = 1; + RedPointSign = 2; + RedPointShare = 3; + RedPointVipCashback = 4; + RedPointMail = 5; + RedPointFreeSpin = 6; // 免费转盘 + RedPointVipReward = 7; // vip升级奖励 + RedPointLoginAgain = 8; // 第二次登录游戏 +} + message RedInfo { - string module_name = 1;// 模块名称 + RedPointModule module_name = 1;// 模块名称 uint32 num =2 ; // 数量 } // 红点 diff --git a/util/http.go b/util/http.go index 294d723..19be594 100644 --- a/util/http.go +++ b/util/http.go @@ -17,6 +17,7 @@ import ( "strings" "time" + "github.com/gin-gonic/gin" "github.com/liangdas/mqant/log" ) @@ -262,3 +263,20 @@ func ReplaceHTTPWithHTTPS(text string) string { return httpsText } + +func CheckRequestBody(c *gin.Context) (bool, []byte) { + // 读取请求体 + bodyBytes, err := ioutil.ReadAll(c.Request.Body) + if err != nil { + return false, nil + } + + // 恢复请求体的内容,以便后续处理 + c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) + + // 检查请求体长度 + if len(bodyBytes) == 0 { + return false, nil + } + return true, bodyBytes +}