|
@@ -12,6 +12,7 @@ import (
|
|
|
"fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/pkg/errors"
|
|
|
+ "go.mongodb.org/mongo-driver/v2/bson"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -35,22 +36,22 @@ func ReceiveGameMsg(c *gin.Context) {
|
|
|
|
|
|
gid := c.GetString("gid")
|
|
|
pf := c.GetString("pf")
|
|
|
- //openId := c.GetString("openid")
|
|
|
+ openId := c.GetString("openid")
|
|
|
|
|
|
now := time.UnixMilli(form.Timestamp)
|
|
|
var err error
|
|
|
if form.Action == "login" {
|
|
|
//登录
|
|
|
- err = login(gid, pf, form.UserId, now)
|
|
|
+ err = login(gid, pf, openId, form.UserId, now)
|
|
|
} else if form.Action == "online" {
|
|
|
//活跃
|
|
|
- err = online(gid, pf, form.UserId, now)
|
|
|
+ err = online(gid, pf, openId, form.UserId, now)
|
|
|
} else if form.Action == "offline" {
|
|
|
//断开
|
|
|
- err = offline(gid, pf, form.UserId, now)
|
|
|
+ err = offline(gid, pf, openId, form.UserId, now)
|
|
|
} else if form.Action == "seeAds" {
|
|
|
//观看广告
|
|
|
- err = seeAds(gid, pf, form.UserId, now, form.AdsId, form.AdsType, form.AdsScene, form.AdsState)
|
|
|
+ err = seeAds(gid, pf, openId, form.UserId, now, form.AdsId, form.AdsType, form.AdsScene, form.AdsState)
|
|
|
} else {
|
|
|
//自定义类型
|
|
|
//查询有无对应的行为记录
|
|
@@ -129,7 +130,7 @@ func action(gid string, pf string, gameActionId string, userId int, now time.Tim
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func login(gid string, pf string, userId int, now time.Time) error {
|
|
|
+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 {
|
|
@@ -171,10 +172,15 @@ func login(gid string, pf string, userId int, now time.Time) error {
|
|
|
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, userId int, now time.Time) error {
|
|
|
+func online(gid string, pf string, openId string, userId int, now time.Time) error {
|
|
|
|
|
|
userOnline := model.UserOnline{
|
|
|
Gid: gid,
|
|
@@ -189,10 +195,16 @@ func online(gid string, pf string, userId int, now time.Time) error {
|
|
|
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, userId int, now time.Time) error {
|
|
|
+func offline(gid string, pf string, openId string, userId int, now time.Time) error {
|
|
|
|
|
|
userOnline := model.UserOnline{
|
|
|
Gid: gid,
|
|
@@ -208,10 +220,14 @@ func offline(gid string, pf string, userId int, now time.Time) error {
|
|
|
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, userId int, now time.Time, AdsId string, AdsType string, AdsScene string, AdsState int) error {
|
|
|
+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("参数缺失")
|
|
|
}
|
|
@@ -233,6 +249,11 @@ func seeAds(gid string, pf string, userId int, now time.Time, AdsId string, AdsT
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
+ err = UpdateUserBehaviorAdInfo(gid, pf, openId, AdsState)
|
|
|
+ if err != nil {
|
|
|
+ global.App.Log.Error("存储用户观看广告mongo失败", err.Error(), userOnline)
|
|
|
+ }
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -302,3 +323,52 @@ func InitUser(c *gin.Context) {
|
|
|
|
|
|
response.Success(c, gin.H{"data": "success"})
|
|
|
}
|
|
|
+
|
|
|
+func ReceiveAdsInfo(c *gin.Context) {
|
|
|
+ form := request.Check(c, &struct {
|
|
|
+ Aid string `form:"aid" json:"aid" binding:"required"`
|
|
|
+ Pid string `form:"pid" json:"pid" binding:"required"`
|
|
|
+ Cid string `form:"cid" json:"cid" binding:"required"`
|
|
|
+ }{})
|
|
|
+ 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"})
|
|
|
+}
|