123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- 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"
- "strconv"
- "strings"
- "time"
- )
- var ActionModes = map[string]string{
- "login": "login",
- "alive": "alive",
- "close": "close",
- "seeAds": "seeAds",
- }
- 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")
- now := time.UnixMilli(form.Timestamp)
- var err error
- if form.Action == "login" {
- //登录
- err = login(gid, pf, form.UserId, now)
- } else if form.Action == "online" {
- //活跃
- err = online(gid, pf, form.UserId, now)
- } else if form.Action == "offline" {
- //断开
- err = offline(gid, pf, form.UserId, now)
- } 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 {
- 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, 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
- }
- return nil
- }
- func online(gid string, pf 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
- }
- return nil
- }
- func offline(gid string, pf 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
- }
- return nil
- }
- func seeAds(gid string, pf 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
- }
- 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"})
- }
|