|
@@ -7,9 +7,11 @@ import (
|
|
|
"designs/config"
|
|
|
"designs/global"
|
|
|
"designs/model"
|
|
|
+ "designs/utils"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/pkg/errors"
|
|
|
- "gorm.io/gorm"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -30,6 +32,8 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
Action string `form:"action" json:"action" binding:"required"`
|
|
|
Timestamp int64 `form:"timestamp" json:"timestamp" binding:"required"`
|
|
|
|
|
|
+ Data string `form:"data" json:"data" binding:""`
|
|
|
+
|
|
|
AdsId string `form:"adsId" json:"adsId" binding:""`
|
|
|
AdsType string `form:"adsType" json:"adsType" binding:""`
|
|
|
AdsScene string `form:"adsScene" json:"adsScene" binding:""`
|
|
@@ -53,6 +57,10 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
} else if form.Action == "seeAds" {
|
|
|
//观看广告
|
|
|
err = seeAds(gid, pf, form.UserId, now, form.AdsId, form.AdsType, form.AdsScene, form.AdsState)
|
|
|
+ } else {
|
|
|
+ //自定义类型
|
|
|
+ //查询有无对应的行为记录
|
|
|
+ err = action(gid, pf, form.Action, form.UserId, now, form.Data)
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
@@ -63,10 +71,74 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
response.Success(c, gin.H{})
|
|
|
}
|
|
|
|
|
|
+func action(gid string, pf string, gameActionId string, userId int, now time.Time, option string) error {
|
|
|
+ //fmt.Print(gameActionId)
|
|
|
+ var gameAction model.GameAction
|
|
|
+ err := global.App.DB.Table("game_action").
|
|
|
+ Where("gid", gid).
|
|
|
+ Where("actionId", gameActionId).
|
|
|
+ First(&gameAction).Error
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("该行为记录不存在")
|
|
|
+ }
|
|
|
+ if gameAction.Status == 0 {
|
|
|
+ return errors.New("该行为记录未启用")
|
|
|
+ }
|
|
|
+
|
|
|
+ var userActionOption []model.UserActionOption
|
|
|
+ if option != "" {
|
|
|
+ optionMap := make(map[string]interface{})
|
|
|
+ err = json.Unmarshal([]byte(option), &optionMap)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("选项解析错误")
|
|
|
+ }
|
|
|
+ for k, v := range optionMap {
|
|
|
+ userActionOption = append(userActionOption, model.UserActionOption{
|
|
|
+ OptionId: k,
|
|
|
+ Value: fmt.Sprintf("%v", v),
|
|
|
+ CreatedAt: model.XTime{
|
|
|
+ Time: now,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ userAction := model.UserAction{
|
|
|
+ UserId: userId,
|
|
|
+ Gid: gid,
|
|
|
+ Pf: pf,
|
|
|
+ ActionId: gameActionId,
|
|
|
+ CreatedAt: model.XTime{Time: now},
|
|
|
+ }
|
|
|
+
|
|
|
+ err = global.App.DB.Transaction(func(tx *utils.WtDB) error {
|
|
|
+ err := tx.Table("user_action").Create(&userAction).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if len(userActionOption) > 0 {
|
|
|
+ for _, option := range userActionOption {
|
|
|
+ option.UserActionId = userAction.ID
|
|
|
+ err = tx.Table("user_action_option").Create(&option).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func login(gid string, pf string, userId int, now time.Time) error {
|
|
|
var user model.User
|
|
|
- err := global.App.DB.Table("user").Where("id", gid).Where("pf", pf).Where("userId", userId).Find(&user).Error
|
|
|
- if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ err := global.App.DB.Table("user").Where("gid", gid).Where("pf", pf).Where("userId", userId).First(&user).Error
|
|
|
+ if user.ID == 0 {
|
|
|
//没有用户,需要新增
|
|
|
user.UserId = userId
|
|
|
user.Gid = gid
|
|
@@ -176,8 +248,8 @@ func InitUser(c *gin.Context) {
|
|
|
for _, key := range keys {
|
|
|
gid := strings.Split(key, ":")[1]
|
|
|
userKeyWeb := gid + ":" + "web" + ":" + config.Get("app.user_table_key") + "*"
|
|
|
- //userKeyTt := gid + ":" + "tt" + ":" + config.Get("app.user_table_key")
|
|
|
- //userKeyWx := gid + ":" + "wx" + ":" + config.Get("app.user_table_key")
|
|
|
+ userKeyTt := gid + ":" + "tt" + ":" + config.Get("app.user_table_key") + "*"
|
|
|
+ userKeyWx := gid + ":" + "wx" + ":" + config.Get("app.user_table_key") + "*"
|
|
|
|
|
|
webKey, _ := global.App.Redis.Keys(context.Background(), userKeyWeb).Result()
|
|
|
for _, v := range webKey {
|
|
@@ -186,15 +258,53 @@ func InitUser(c *gin.Context) {
|
|
|
continue
|
|
|
}
|
|
|
userId, _ := strconv.Atoi(res["userId"])
|
|
|
+ registerTimeUnix, _ := strconv.Atoi(res["registerTime"])
|
|
|
+ registerTime := time.Unix(int64(registerTimeUnix), 0)
|
|
|
+ global.App.DB.Table("user").Create(&model.User{
|
|
|
+ Gid: res["gid"],
|
|
|
+ Pf: res["pf"],
|
|
|
+ UserId: userId,
|
|
|
+ CreatedAt: registerTime,
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ ttKey, _ := global.App.Redis.Keys(context.Background(), userKeyTt).Result()
|
|
|
+ for _, v := range ttKey {
|
|
|
+ res, err2 := global.App.Redis.HGetAll(context.Background(), v).Result()
|
|
|
+ if err2 != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ userId, _ := strconv.Atoi(res["userId"])
|
|
|
+ registerTimeUnix, _ := strconv.Atoi(res["registerTime"])
|
|
|
+ registerTime := time.Unix(int64(registerTimeUnix), 0)
|
|
|
global.App.DB.Table("user").Create(&model.User{
|
|
|
Gid: res["gid"],
|
|
|
Pf: res["pf"],
|
|
|
UserId: userId,
|
|
|
- CreatedAt: time.Now(),
|
|
|
+ CreatedAt: registerTime,
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ wxKey, _ := global.App.Redis.Keys(context.Background(), userKeyWx).Result()
|
|
|
+ for _, v := range wxKey {
|
|
|
+ res, err2 := global.App.Redis.HGetAll(context.Background(), v).Result()
|
|
|
+ if err2 != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ userId, _ := strconv.Atoi(res["userId"])
|
|
|
+ registerTimeUnix, _ := strconv.Atoi(res["registerTime"])
|
|
|
+ registerTime := time.Unix(int64(registerTimeUnix), 0)
|
|
|
+ global.App.DB.Table("user").Create(&model.User{
|
|
|
+ Gid: res["gid"],
|
|
|
+ Pf: res["pf"],
|
|
|
+ UserId: userId,
|
|
|
+ CreatedAt: registerTime,
|
|
|
})
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- response.Success(c, gin.H{})
|
|
|
+ response.Success(c, gin.H{"data": "success"})
|
|
|
}
|