package handler import ( "fmt" "net/http/httptest" "reflect" "server/call" "server/common" "server/db" "server/modules/web/app" "server/modules/web/values" "server/pb" "server/util" "time" "github.com/gin-gonic/gin" "github.com/liangdas/mqant/log" "gorm.io/gorm" ) func getUserInfo(uid int, isNew bool) (resp values.UserInfoResp, err error) { pd := &common.PlayerData{UID: uid} db.Mysql().Get(pd) ret, err := call.GetUserXInfo(uid, "avatar", "nick", "mobile", "birth") if err != nil { log.Error("err:%v", err) return } resp = values.UserInfoResp{ UID: uid, Nick: ret.Nick, Avatar: ret.Avatar, VIPLevel: call.GetVIP(uid).Level, Currencys: make(map[common.CurrencyType]int64), Feedback: pd.FeedbackTime > 0, NewPddShare: call.HasNewAcitivityPddShare(uid), Birth: ret.Birth, Phone: ret.Mobile, CurrentVip: &values.OneUserInfoVip{}, NextVip: &values.OneUserInfoVip{}, } resp.Activitys.RechargeBack = call.ShouldShowActivityFirstRechargeBack(uid) resp.Activitys.DaySign = call.ShouldShowActivitySign(uid) resp.Activitys.WeekCard = call.ShouldShowActivityWeekCard(uid) resp.Activitys.LuckyShop = call.ShouldShowActivityLuckShop(uid) if ticker := call.GetUserValidItemsMax(uid, common.ItemDiscountTicket); ticker.ID > 0 { resp.Activitys.DiscountTicket = &values.DiscountTicketV{Amount: ticker.Exi2, DiscountAmount: ticker.Exi1} } vip := call.GetVipCon(uid) var nextVip *common.ConfigVIP if vip != nil { nextVip = call.GetConfigVIPByLevel(vip.Level + 1) resp.CurrentVip.Bonus = vip.Bonus resp.CurrentVip.BonusWeek = vip.BonusWeek resp.CurrentVip.WithdrawFee = vip.Fee resp.CurrentVip.WithdrawCount = vip.WithdrawCount resp.CurrentVip.WithdrawAmount = vip.WithdrawAmount } if nextVip != nil { resp.NextVip.Bonus = nextVip.Bonus resp.NextVip.BonusWeek = nextVip.BonusWeek resp.NextVip.WithdrawFee = nextVip.Fee resp.NextVip.WithdrawCount = nextVip.WithdrawCount resp.NextVip.WithdrawAmount = nextVip.WithdrawAmount resp.NextVip.Exp = nextVip.Exp resp.NextVip.Level = nextVip.Level } else { resp.NextVip = resp.CurrentVip } if pd.LastAppSpinDraw == 0 { resp.AppSpinCount++ } currency := &common.PlayerCurrency{UID: uid} db.Mysql().Get(currency) ref := reflect.ValueOf(currency).Elem() for i := common.CurrencyTypeZero + 1; i < common.CurrencyAll; i++ { if i == common.CurrencyUSDT { continue } resp.Currencys[i] = ref.Field(int(i) + 1).Int() } if isNew { if resp.Currencys[common.CurrencyINR] == 0 && call.GetConfigPlatform().NewPlayerGift > 0 { resp.Currencys[common.CurrencyINR] += call.GetConfigPlatform().NewPlayerGift } if resp.Currencys[common.CurrencyINR] == 0 && call.GetConfigPlatform().NewPlayerGift > 0 { resp.Currencys[common.CurrencyINR] += call.GetConfigPlatform().NewPlayerGift } } re := &common.RechargeInfo{UID: uid} db.Mysql().Get(re) resp.Recharge = re.TotalRecharge return } func GetUserInfo(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.GetUserInfoReq) if !a.S(req) { return } uid := a.UID if req.UID > 0 { uid = req.UID } CheckTask(a) // todo pd := &common.PlayerData{UID: uid} db.Mysql().Get(pd) ret, err := call.GetUserXInfo(uid, "avatar", "nick", "mobile", "birth") if err != nil { log.Error("err:%v", err) a.Code = values.CodeRetry return } resp := values.UserInfoResp{ UID: uid, Nick: ret.Nick, Avatar: ret.Avatar, VIPLevel: call.GetVIP(uid).Level, Currencys: make(map[common.CurrencyType]int64), Feedback: pd.FeedbackTime > 0, NewPddShare: call.HasNewAcitivityPddShare(a.UID), Birth: ret.Birth, Phone: ret.Mobile, CurrentVip: &values.OneUserInfoVip{}, NextVip: &values.OneUserInfoVip{}, } resp.Activitys.RechargeBack = call.ShouldShowActivityFirstRechargeBack(uid) resp.Activitys.DaySign = call.ShouldShowActivitySign(uid) resp.Activitys.WeekCard = call.ShouldShowActivityWeekCard(uid) resp.Activitys.LuckyShop = call.ShouldShowActivityLuckShop(uid) if ticker := call.GetUserValidItemsMax(uid, common.ItemDiscountTicket); ticker.ID > 0 { resp.Activitys.DiscountTicket = &values.DiscountTicketV{Amount: ticker.Exi2, DiscountAmount: ticker.Exi1} } vip := call.GetVipCon(a.UID) nextVip := call.GetConfigVIPByLevel(vip.Level + 1) resp.CurrentVip.Bonus = vip.Bonus resp.CurrentVip.BonusWeek = vip.BonusWeek resp.CurrentVip.WithdrawFee = vip.Fee resp.CurrentVip.WithdrawCount = vip.WithdrawCount resp.CurrentVip.WithdrawAmount = vip.WithdrawAmount // resp.CurrentVip.Cashback = util.FormatFloat(float64(vip.Cashback)/10, 2) + "%" // resp.CurrentVip.WithdrawFee = util.FormatFloat(float64(vip.Fee)/10, 2) + "%" if nextVip != nil { resp.NextVip.Bonus = nextVip.Bonus resp.NextVip.BonusWeek = nextVip.BonusWeek resp.NextVip.WithdrawFee = nextVip.Fee resp.NextVip.WithdrawCount = nextVip.WithdrawCount resp.NextVip.WithdrawAmount = nextVip.WithdrawAmount // resp.NextVip.Cashback = util.FormatFloat(float64(nextVip.Cashback)/10, 2) + "%" // resp.NextVip.WithdrawFee = util.FormatFloat(float64(nextVip.Fee)/10, 2) + "%" } else { resp.NextVip = resp.CurrentVip } if pd.LastAppSpinDraw == 0 { resp.AppSpinCount++ } // call.GetUserCurrency() currency := &common.PlayerCurrency{UID: uid} db.Mysql().Get(currency) // currencyRe := &common.PlayerCurrency{UID: uid} // db.Mysql().C().Table(common.PlayerRechargeTableName).Where("uid = ?", uid).Scan(currencyRe) ref := reflect.ValueOf(currency).Elem() // refRe := reflect.ValueOf(currencyRe).Elem() for i := common.CurrencyTypeZero + 1; i < common.CurrencyAll; i++ { if i == common.CurrencyUSDT { continue } resp.Currencys[i] = ref.Field(int(i) + 1).Int() } // 一些参数只发给玩家自己 // if req.UID <= 0 { re := &common.RechargeInfo{UID: uid} db.Mysql().Get(re) resp.Recharge = re.TotalRecharge a.Code = values.CodeOK // } a.Data = resp util.Go(func() { CheckRedPoint(uid, c) }) } func EditUserInfo(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.EditUserInfoReq) if !a.S(req) { return } if req.Avatar == nil && req.Nick == nil { a.Code = values.CodeParam return } tx := db.Mysql().Begin() uid := a.UID defer func() { a.MCommit(tx) }() update := map[string]interface{}{} if req.Nick != nil { if !call.IsUserNickValid(*req.Nick) { a.Code = values.CodeNickInvalid return } // pd := &common.PlayerData{UID: uid} // db.Mysql().Get(pd) // if pd.LastNickEdit > 0 { // if time.Now().Unix()-pd.LastNickEdit < 24*60*60 { // a.Code = values.CodeNickEditBusy // return // } // if _, err := call.UpdateCurrencyAndNotify(&common.UpdateCurrencyNotify{ // UID: uid, // Event: common.CurrencyEventChangeNick, // Pairs: []*common.CurrencyPair{{Type: common.CurrencyTypeCash, Value: -200}}, // }, true, tx); err != nil { // log.Error("err:%v", err) // a.Code = values.CodeRetry // return // } // } // if err := tx.Model(pd).Where("uid = ? and last_nick_edit = ?", uid, pd.LastNickEdit).Updates(&common.PlayerData{LastNickEdit: time.Now().Unix()}).Error; err != nil { // log.Error("err:%v", err) // a.Code = values.CodeRetry // return // } update["nick"] = *req.Nick } if req.Avatar != nil { update["avatar"] = *req.Avatar } search := &common.PlayerDBInfo{Id: uid} if err := tx.Model(search).Where(search).Updates(update).Error; err != nil { log.Error("err:%v", err) a.Code = values.CodeRetry return } if err := db.Redis().UpdateUserFields(uid, update); err != nil { log.Error("err:%v", err) a.Code = values.CodeRetry return } } func Feedback(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.FeedbackReq) if !a.S(req) { return } if len(req.List) == 0 { return } // questions := []int{} // all := []values.OneFeedback{} for _, v := range req.List { for _, j := range v.Choose { if j == 0 && v.Context == "" { a.Code = values.CodeParam a.Msg = "context should not be empty." return } } // if !util.SliceContain(questions, v.Index) { // questions = append(questions, v.Index) // all = append(all, v) // } } pd := &common.PlayerData{UID: a.UID} db.Mysql().Get(pd) if pd.FeedbackTime > 0 { a.Code = values.CodeRetry return } now := time.Now().Unix() rows, err := db.Mysql().UpdateResW(&common.PlayerData{}, map[string]interface{}{"feedback_time": now}, fmt.Sprintf("uid = %d and feedback_time = 0", a.UID)) if err != nil { log.Error("err:%v", err) a.Code = values.CodeRetry return } if rows == 0 { a.Code = values.CodeRetry return } call.GetAcitivityPddData(a.UID) db.Mysql().Update(&common.PddData{UID: a.UID}, map[string]interface{}{"spin": gorm.Expr("spin + 1")}) for _, v := range req.List { for _, j := range v.Choose { db.ES().InsertToESGO(common.ESIndexBackFeedback, &common.ESFeedback{ UID: a.UID, QuestionIndex: v.Index, Choose: j, Context: v.Context, Time: now, }) } } } func FavoriteGame(c *gin.Context) { a := app.NewApp(c) defer func() { a.Response() }() req := new(values.FavoriteGameReq) if !a.S(req) { return } if req.Cancel { err := call.DelPlayerFavorite(a.UID, req.GameProvider, req.GameId) if err != nil { a.Code = values.CodeRetry a.Msg = err.Error() return } } else { err := call.AddPlayerFavorite(a.UID, req.GameProvider, req.GameId) if err != nil { a.Code = values.CodeRetry a.Msg = err.Error() } } } 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 } // CheckTask 完成任务 func CheckTask(a *app.Gin) { taskList := GetUserTaskStatus(a) num := 0 for _, task := range taskList { if task.Status == 1 { num++ } } if num > 0 { call.PushRed(a.UID, pb.RedPointModule_RedPointTaskDraw, uint32(num)) } //var taskList []*values.OneTask //tasks := call.GetConfigTask() //for _, v := range tasks { // one := &values.OneTask{ // ID: v.ID, // TaskID: v.TaskID, // Target: v.Target, // Reward: v.Reward, // Kind: v.Kind, // Type: int(v.Type), // Icon: v.Icon, // Title: common.GetTaskTitle(v), // Action: v.Action, // } // taskList = append(taskList, one) //} //for _, v := range taskList { // if v.Type == int(common.TaskTypeDownload) && v.Progess != -1 { // 已下载直接标记为完成 // if a.DeviceType == common.DeviceTypeWebview || a.DeviceType == common.DeviceTypePWA { // // 直接领取下载奖励 // taskId := call.CheckTask(call.Task{Uid: a.UID, Value: 0, Types: []common.TaskType{common.TaskTypeDownload}}) // TaskComplete(a, &DrawTaskReq{TaskID: taskId}) // } // } //} } 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) }) }