123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- 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:""`
- AdStartTime int64 `form:"adStartTime" json:"adStartTime" binding:""`
- }{})
- gid := c.GetString("gid")
- pf := c.GetString("pf")
- openId := c.GetString("openid")
- if gid == "linkup" {
- global.App.Log.Info("用户请求ReceiveGameMsg,参数打印:")
- utils.PrintStructFields(form)
- }
- now := time.UnixMilli(form.Timestamp)
- adStartTime := time.UnixMilli(form.AdStartTime)
- 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, adStartTime)
- } else {
- //自定义类型
- //查询有无对应的行为记录
- err = action(gid, pf, form.Action, form.UserId, now, form.Data)
- }
- //调用接口,检测需不需要上传到巨量
- CheckUserActive(gid, pf, openId)
- 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
- user.OpenId = openId
- 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, now)
- 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, adStartTime time.Time) 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,
- StartTime: adStartTime,
- }
- 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:""`
- }{})
- //存储参数
- global.App.Log.Info("ReceiveAdsInfo", form)
- gid := c.GetString("gid")
- pf := c.GetString("pf")
- openId := c.GetString("openid")
- userId := gid + "|" + pf + "|" + openId
- global.App.Log.Info("userId", userId)
- id := userId + "|" + form.Aid
- aid, _ := strconv.Atoi(form.Aid)
- pid, _ := strconv.Atoi(form.Pid)
- collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")
- var adRelated model.AdRelated
- filter := bson.M{"_id": id}
- collection.FindOne(context.Background(), filter).Decode(&adRelated)
- if adRelated.Id == "" {
- //新增
- adData := model.AdRelated{
- UserId: userId,
- Id: id,
- Aid: int64(aid),
- Pid: int64(pid),
- Cid: form.Cid,
- Duration: 0,
- StartNum: 1,
- Revenue: 0,
- ReqCount: 0,
- ExpCount: 0,
- CreateTime: time.Now().Unix(),
- }
- //更新到MongoDB 中
- _, err := collection.InsertOne(context.Background(), adData)
- if err != nil {
- response.Fail(c, 1003, "写入数据失败"+err.Error())
- return
- }
- }
- ////关联到userInfo 上
- //filter = bson.M{"_id": userId}
- //update := bson.M{
- // "$set": struct {
- // RelatedAid int64 `bson:"relatedAid" json:"relatedAid"`
- // }{
- // RelatedAid: int64(aid),
- // },
- //}
- //collection = global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- //_, err := collection.UpdateOne(context.Background(), filter, update)
- //if err != nil {
- // response.Fail(c, 1003, err.Error())
- // return
- //}
- response.Success(c, gin.H{"data": "success"})
- }
|