user.go 8.0 KB

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