package v1 import ( "context" "designs/app/common/request" "designs/app/common/response" "designs/config" "designs/global" "designs/model" "designs/utils" "encoding/json" "fmt" "github.com/gin-gonic/gin" "github.com/pkg/errors" "go.mongodb.org/mongo-driver/v2/bson" "strconv" "strings" "time" ) func ReceiveGameMsg(c *gin.Context) { form := request.Check(c, &struct { //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"` 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:""` AdsState int `form:"adsState" json:"adsState" binding:""` }{}) gid := c.GetString("gid") pf := c.GetString("pf") openId := c.GetString("openid") now := time.UnixMilli(form.Timestamp) var err error if form.Action == "login" { //登录 err = login(gid, pf, openId, form.UserId, now) } else if form.Action == "online" { //活跃 err = online(gid, pf, openId, form.UserId, now) } else if form.Action == "offline" { //断开 err = offline(gid, pf, openId, form.UserId, now) } else if form.Action == "seeAds" { //观看广告 err = seeAds(gid, pf, openId, 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 { response.Fail(c, 1003, err.Error()) return } 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, openId string, userId int, now time.Time) error { var user model.User 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 user.Pf = pf user.CreatedAt = now err = global.App.DB.Table("user").Create(&user).Error if err != nil { global.App.Log.Error("创建用户失败", err.Error(), user) return err } } userLogin := model.UserLogin{ Gid: gid, Pf: pf, UserId: userId, LoginTime: now, } err = global.App.DB.Table("user_login").Create(&userLogin).Error if err != nil { global.App.Log.Error("存储用户登录信息失败", err.Error(), user) return err } userOnline := model.UserOnline{ Gid: gid, Pf: pf, UserId: userId, LogTime: now, Date: now.Format("20060102"), Type: 1, } err = global.App.DB.Table("user_online").Create(&userOnline).Error if err != nil { global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline) return err } err = CreateUserBehavior(gid, pf, openId) if err != nil { global.App.Log.Error("存储用户信息进入mongo失败", err.Error(), userOnline) } return nil } func online(gid string, pf string, openId string, userId int, now time.Time) error { userOnline := model.UserOnline{ Gid: gid, Pf: pf, UserId: userId, LogTime: now, Date: now.Format("20060102"), Type: 1, } err := global.App.DB.Table("user_online").Create(&userOnline).Error if err != nil { global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline) return err } err = UpdateUserBehaviorDuration(gid, pf, openId, now, 1) if err != nil { global.App.Log.Error("存储用户在线时长mongo失败", err.Error(), userOnline) } return nil } func offline(gid string, pf string, openId string, userId int, now time.Time) error { userOnline := model.UserOnline{ Gid: gid, Pf: pf, UserId: userId, LogTime: now, Date: now.Format("20060102"), Type: 2, } err := global.App.DB.Table("user_online").Create(&userOnline).Error if err != nil { global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline) return err } err = UpdateUserBehaviorDuration(gid, pf, openId, now, 2) if err != nil { global.App.Log.Error("存储用户在线时长mongo失败", err.Error(), userOnline) } return nil } func seeAds(gid string, pf string, openId string, userId int, now time.Time, AdsId string, AdsType string, AdsScene string, AdsState int) error { if AdsId == "" || AdsType == "" || AdsScene == "" { return errors.New("参数缺失") } userOnline := model.UserSeeAds{ Gid: gid, Pf: pf, UserId: userId, CreatedAt: now, Date: now.Format("20060102"), AdsId: AdsId, AdsType: AdsType, AdsState: AdsState, AdsScene: AdsScene, } err := global.App.DB.Table("user_see_ads").Create(&userOnline).Error if err != nil { global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline) return err } err = UpdateUserBehaviorAdInfo(gid, pf, openId, AdsState) if err != nil { global.App.Log.Error("存储用户观看广告mongo失败", err.Error(), userOnline) } return nil } func InitUser(c *gin.Context) { gidKey := config.Get("app.gid") + "*" keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result() 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") + "*" webKey, _ := global.App.Redis.Keys(context.Background(), userKeyWeb).Result() for _, v := range webKey { 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, }) } 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: 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{"data": "success"}) } func ReceiveAdsInfo(c *gin.Context) { form := request.Check(c, &struct { Aid string `form:"aid" json:"aid" binding:""` Pid string `form:"pid" json:"pid" binding:""` Cid string `form:"cid" json:"cid" binding:""` }{}) gid := c.GetString("gid") pf := c.GetString("pf") openId := c.GetString("openid") id := gid + "|" + pf + "|" + openId collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior") filter := bson.M{"_id": id} result := make(map[string]interface{}) err := collection.FindOne(context.Background(), filter).Decode(&result) if err != nil { response.Fail(c, 1003, err.Error()) return } adData := AdData{ Aid: form.Aid, Pid: form.Pid, Cid: form.Cid, Reported: false, Duration: 0, AdReqCount: 0, AdEposedcount: 0, CreateTime: int(time.Now().Unix()), } //更新到MongoDB 中 update := bson.M{ "$set": struct { AdData AdData `bson:"adData" json:"adData"` }{ AdData: adData, }, } _, err = collection.UpdateOne(context.Background(), filter, update) if err != nil { response.Fail(c, 1003, "写入数据失败"+err.Error()) return } response.Success(c, gin.H{"data": "success"}) }