user.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/config"
  7. "designs/global"
  8. "designs/model"
  9. "designs/utils"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "github.com/pkg/errors"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. func ReceiveGameMsg(c *gin.Context) {
  19. form := request.Check(c, &struct {
  20. //Gid string `form:"gid" json:"gid" binding:"required"`
  21. //Pf string `form:"pf" json:"pf" binding:"required"`
  22. UserId int `form:"userId" json:"userId" binding:"required"`
  23. Action string `form:"action" json:"action" binding:"required"`
  24. Timestamp int64 `form:"timestamp" json:"timestamp" binding:"required"`
  25. Data string `form:"data" json:"data" binding:""`
  26. AdsId string `form:"adsId" json:"adsId" binding:""`
  27. AdsType string `form:"adsType" json:"adsType" binding:""`
  28. AdsScene string `form:"adsScene" json:"adsScene" binding:""`
  29. AdsState int `form:"adsState" json:"adsState" binding:""`
  30. }{})
  31. gid := c.GetString("gid")
  32. pf := c.GetString("pf")
  33. //openId := c.GetString("openid")
  34. now := time.UnixMilli(form.Timestamp)
  35. var err error
  36. if form.Action == "login" {
  37. //登录
  38. err = login(gid, pf, form.UserId, now)
  39. } else if form.Action == "online" {
  40. //活跃
  41. err = online(gid, pf, form.UserId, now)
  42. } else if form.Action == "offline" {
  43. //断开
  44. err = offline(gid, pf, form.UserId, now)
  45. } else if form.Action == "seeAds" {
  46. //观看广告
  47. err = seeAds(gid, pf, form.UserId, now, form.AdsId, form.AdsType, form.AdsScene, form.AdsState)
  48. } else {
  49. //自定义类型
  50. //查询有无对应的行为记录
  51. err = action(gid, pf, form.Action, form.UserId, now, form.Data)
  52. }
  53. if err != nil {
  54. response.Fail(c, 1003, err.Error())
  55. return
  56. }
  57. response.Success(c, gin.H{})
  58. }
  59. func action(gid string, pf string, gameActionId string, userId int, now time.Time, option string) error {
  60. //fmt.Print(gameActionId)
  61. var gameAction model.GameAction
  62. err := global.App.DB.Table("game_action").
  63. Where("gid", gid).
  64. Where("actionId", gameActionId).
  65. First(&gameAction).Error
  66. if err != nil {
  67. return errors.New("该行为记录不存在")
  68. }
  69. if gameAction.Status == 0 {
  70. return errors.New("该行为记录未启用")
  71. }
  72. var userActionOption []model.UserActionOption
  73. if option != "" {
  74. optionMap := make(map[string]interface{})
  75. err = json.Unmarshal([]byte(option), &optionMap)
  76. if err != nil {
  77. return errors.New("选项解析错误")
  78. }
  79. for k, v := range optionMap {
  80. userActionOption = append(userActionOption, model.UserActionOption{
  81. OptionId: k,
  82. Value: fmt.Sprintf("%v", v),
  83. CreatedAt: model.XTime{
  84. Time: now,
  85. },
  86. })
  87. }
  88. }
  89. userAction := model.UserAction{
  90. UserId: userId,
  91. Gid: gid,
  92. Pf: pf,
  93. ActionId: gameActionId,
  94. CreatedAt: model.XTime{Time: now},
  95. }
  96. err = global.App.DB.Transaction(func(tx *utils.WtDB) error {
  97. err := tx.Table("user_action").Create(&userAction).Error
  98. if err != nil {
  99. return err
  100. }
  101. if len(userActionOption) > 0 {
  102. for _, option := range userActionOption {
  103. option.UserActionId = userAction.ID
  104. err = tx.Table("user_action_option").Create(&option).Error
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. }
  110. return nil
  111. })
  112. if err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. func login(gid string, pf string, userId int, now time.Time) error {
  118. var user model.User
  119. err := global.App.DB.Table("user").Where("gid", gid).Where("pf", pf).Where("userId", userId).First(&user).Error
  120. if user.ID == 0 {
  121. //没有用户,需要新增
  122. user.UserId = userId
  123. user.Gid = gid
  124. user.Pf = pf
  125. user.CreatedAt = now
  126. err = global.App.DB.Table("user").Create(&user).Error
  127. if err != nil {
  128. global.App.Log.Error("创建用户失败", err.Error(), user)
  129. return err
  130. }
  131. }
  132. userLogin := model.UserLogin{
  133. Gid: gid,
  134. Pf: pf,
  135. UserId: userId,
  136. LoginTime: now,
  137. }
  138. err = global.App.DB.Table("user_login").Create(&userLogin).Error
  139. if err != nil {
  140. global.App.Log.Error("存储用户登录信息失败", err.Error(), user)
  141. return err
  142. }
  143. userOnline := model.UserOnline{
  144. Gid: gid,
  145. Pf: pf,
  146. UserId: userId,
  147. LogTime: now,
  148. Date: now.Format("20060102"),
  149. Type: 1,
  150. }
  151. err = global.App.DB.Table("user_online").Create(&userOnline).Error
  152. if err != nil {
  153. global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline)
  154. return err
  155. }
  156. return nil
  157. }
  158. func online(gid string, pf string, userId int, now time.Time) error {
  159. userOnline := model.UserOnline{
  160. Gid: gid,
  161. Pf: pf,
  162. UserId: userId,
  163. LogTime: now,
  164. Date: now.Format("20060102"),
  165. Type: 1,
  166. }
  167. err := global.App.DB.Table("user_online").Create(&userOnline).Error
  168. if err != nil {
  169. global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline)
  170. return err
  171. }
  172. return nil
  173. }
  174. func offline(gid string, pf string, userId int, now time.Time) error {
  175. userOnline := model.UserOnline{
  176. Gid: gid,
  177. Pf: pf,
  178. UserId: userId,
  179. LogTime: now,
  180. Date: now.Format("20060102"),
  181. Type: 2,
  182. }
  183. err := global.App.DB.Table("user_online").Create(&userOnline).Error
  184. if err != nil {
  185. global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline)
  186. return err
  187. }
  188. return nil
  189. }
  190. func seeAds(gid string, pf string, userId int, now time.Time, AdsId string, AdsType string, AdsScene string, AdsState int) error {
  191. if AdsId == "" || AdsType == "" || AdsScene == "" {
  192. return errors.New("参数缺失")
  193. }
  194. userOnline := model.UserSeeAds{
  195. Gid: gid,
  196. Pf: pf,
  197. UserId: userId,
  198. CreatedAt: now,
  199. Date: now.Format("20060102"),
  200. AdsId: AdsId,
  201. AdsType: AdsType,
  202. AdsState: AdsState,
  203. AdsScene: AdsScene,
  204. }
  205. err := global.App.DB.Table("user_see_ads").Create(&userOnline).Error
  206. if err != nil {
  207. global.App.Log.Error("存储用户活跃信息失败", err.Error(), userOnline)
  208. return err
  209. }
  210. return nil
  211. }
  212. func InitUser(c *gin.Context) {
  213. gidKey := config.Get("app.gid") + "*"
  214. keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
  215. for _, key := range keys {
  216. gid := strings.Split(key, ":")[1]
  217. userKeyWeb := gid + ":" + "web" + ":" + config.Get("app.user_table_key") + "*"
  218. userKeyTt := gid + ":" + "tt" + ":" + config.Get("app.user_table_key") + "*"
  219. userKeyWx := gid + ":" + "wx" + ":" + config.Get("app.user_table_key") + "*"
  220. webKey, _ := global.App.Redis.Keys(context.Background(), userKeyWeb).Result()
  221. for _, v := range webKey {
  222. res, err2 := global.App.Redis.HGetAll(context.Background(), v).Result()
  223. if err2 != nil {
  224. continue
  225. }
  226. userId, _ := strconv.Atoi(res["userId"])
  227. registerTimeUnix, _ := strconv.Atoi(res["registerTime"])
  228. registerTime := time.Unix(int64(registerTimeUnix), 0)
  229. global.App.DB.Table("user").Create(&model.User{
  230. Gid: res["gid"],
  231. Pf: res["pf"],
  232. UserId: userId,
  233. CreatedAt: registerTime,
  234. })
  235. }
  236. ttKey, _ := global.App.Redis.Keys(context.Background(), userKeyTt).Result()
  237. for _, v := range ttKey {
  238. res, err2 := global.App.Redis.HGetAll(context.Background(), v).Result()
  239. if err2 != nil {
  240. continue
  241. }
  242. userId, _ := strconv.Atoi(res["userId"])
  243. registerTimeUnix, _ := strconv.Atoi(res["registerTime"])
  244. registerTime := time.Unix(int64(registerTimeUnix), 0)
  245. global.App.DB.Table("user").Create(&model.User{
  246. Gid: res["gid"],
  247. Pf: res["pf"],
  248. UserId: userId,
  249. CreatedAt: registerTime,
  250. })
  251. }
  252. wxKey, _ := global.App.Redis.Keys(context.Background(), userKeyWx).Result()
  253. for _, v := range wxKey {
  254. res, err2 := global.App.Redis.HGetAll(context.Background(), v).Result()
  255. if err2 != nil {
  256. continue
  257. }
  258. userId, _ := strconv.Atoi(res["userId"])
  259. registerTimeUnix, _ := strconv.Atoi(res["registerTime"])
  260. registerTime := time.Unix(int64(registerTimeUnix), 0)
  261. global.App.DB.Table("user").Create(&model.User{
  262. Gid: res["gid"],
  263. Pf: res["pf"],
  264. UserId: userId,
  265. CreatedAt: registerTime,
  266. })
  267. }
  268. }
  269. response.Success(c, gin.H{"data": "success"})
  270. }