user.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/common"
  7. "designs/config"
  8. "designs/global"
  9. "designs/service"
  10. "designs/utils"
  11. "strings"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. )
  15. type GameConfig struct {
  16. Gid string `form:"gid" json:"gid" binding:"required"` //游戏id
  17. GameName string `form:"gameName" json:"gameName" binding:"required"` //游戏名称
  18. WxAppid string `form:"wxAppid" json:"wxAppid" binding:"required"` //微信appid
  19. WxSecret string `form:"wxSecret" json:"wxSecret" binding:"required"` //微信secret
  20. TtAppid string `form:"ttAppid" json:"ttAppid" binding:"required"` //抖音appid
  21. TtSecret string `form:"ttSecret" json:"ttSecret" binding:"required"` //抖音secret
  22. AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
  23. }
  24. type GetGameCfg struct {
  25. AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
  26. }
  27. /* code 结构体 */
  28. type CodeData struct {
  29. Code string `form:"code" json:"code" binding:"required"`
  30. Gid string `form:"gid" json:"gid" binding:"required"`
  31. Pf string `form:"pf" json:"pf" binding:"required"`
  32. }
  33. /* 保存数据结构体 */
  34. type SaveData struct {
  35. Extend string `form:"extend" json:"extend" binding:"required"`
  36. Data string `form:"data" json:"data" binding:"required"`
  37. Secret string `form:"secret" json:"secret" binding:""`
  38. }
  39. /* 获取结构体 */
  40. type GetSaveData struct {
  41. Extend string `form:"extend" json:"extend" binding:"required"`
  42. }
  43. /*
  44. *登陆
  45. */
  46. func Login(c *gin.Context) {
  47. //http://127.0.0.1:8787/v1/user/login
  48. // 使用 ShouldBind 方法自动绑定:
  49. form := request.Check(c, &struct {
  50. UserName string `form:"userName" json:"userName" binding:"required"`
  51. Password string `form:"password" json:"password" binding:"required"`
  52. }{})
  53. //验证
  54. adminUser := config.Get("app.admin_user")
  55. adminSecret := config.Get("app.admin_secret")
  56. if form.UserName != adminUser || form.Password != adminSecret {
  57. response.Fail(c, 1000, "账号密码错误")
  58. return
  59. }
  60. //获取token
  61. token, err := common.GetJwtToken(form.UserName, 0)
  62. //刷新refreshtoken
  63. refresh, _ := common.GetJwtToken(form.UserName, 1)
  64. if err != nil {
  65. response.Fail(c, 1003, err.Error())
  66. return
  67. }
  68. //回传
  69. response.Success(c, gin.H{
  70. "token": token,
  71. "refreshToken": refresh,
  72. "loginTime": time.Now().Unix(),
  73. "ip": c.ClientIP(),
  74. })
  75. }
  76. /* 刷新token */
  77. func RefreshToken(c *gin.Context) {
  78. userName := c.GetString("userName")
  79. //获取token
  80. token, err0 := common.GetJwtToken(userName, 0)
  81. if err0 != nil {
  82. global.App.Log.Info("RefreshToken err")
  83. response.Fail(c, 1003, "RefreshToken err")
  84. return
  85. }
  86. data := map[string]interface{}{
  87. "token": token,
  88. }
  89. //返回数据
  90. response.Success(c, gin.H{
  91. "data": data,
  92. })
  93. }
  94. /* 获取时间 */
  95. func GetSysTime(c *gin.Context) {
  96. time := time.Now().Unix()
  97. common.RetJson(0, "获取系统时间", time, c)
  98. }
  99. type User struct {
  100. UserId string `json:"userId"`
  101. Gid string `json:"gid"`
  102. Pf string `json:"pf"`
  103. NickName string `json:"nickName"`
  104. Head string `json:"head"`
  105. OpenId string `json:"openId"`
  106. InBlack bool `json:"inBlack"`
  107. Option string `json:"option"`
  108. }
  109. func UserList(c *gin.Context) {
  110. form := request.Check(c, &struct {
  111. Gid string `form:"gid" json:"gid" binding:""`
  112. Pf string `form:"pf" json:"pf" binding:""`
  113. Offset int `form:"offset" json:"offset" binding:""`
  114. Limit int `form:"limit" json:"limit" binding:"required"`
  115. }{})
  116. //读取表
  117. userTotalKey := config.Get("app.user_total")
  118. if form.Gid != "" && form.Pf != "" {
  119. userTotalKey = config.Get("app.user_total") + ":" + form.Gid + ":" + form.Pf
  120. }
  121. data, err := global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)-1).Result()
  122. if err != nil {
  123. response.Fail(c, 1001, err.Error())
  124. return
  125. }
  126. count, err := global.App.Redis.ZCard(context.Background(), userTotalKey).Result()
  127. if err != nil {
  128. response.Fail(c, 1001, err.Error())
  129. return
  130. }
  131. //读取黑名单
  132. blackListKey := config.Get("app.black_list_table")
  133. black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
  134. if err != nil {
  135. response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
  136. return
  137. }
  138. var res []User
  139. for _, val := range data {
  140. userKey := val.Member.(string)
  141. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  142. if err != nil {
  143. global.App.Log.Error("GetUserData err")
  144. response.Fail(c, 1003, "GetUserData err"+err.Error())
  145. return
  146. }
  147. //查看是否在黑名单
  148. var inBlack bool
  149. if utils.InArray(userKey, black) {
  150. inBlack = true
  151. }
  152. userMsg := strings.Split(userKey, ":")
  153. openId := userMsg[len(userMsg)-1]
  154. gid := userMsg[0]
  155. pf := userMsg[1]
  156. //查看是否加了权限
  157. optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
  158. option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
  159. if err != nil {
  160. response.Fail(c, 1003, err.Error())
  161. return
  162. }
  163. res = append(res, User{
  164. UserId: userData["userId"],
  165. Gid: userData["gid"],
  166. Pf: userData["pf"],
  167. NickName: userData["nickName"],
  168. Head: userData["head"],
  169. OpenId: openId,
  170. InBlack: inBlack,
  171. Option: option["option"],
  172. })
  173. }
  174. response.Success(c, gin.H{
  175. "data": res,
  176. "count": count,
  177. })
  178. }
  179. func GetUserOnlineMsg(c *gin.Context) {
  180. form := request.Check(c, &struct {
  181. Gid string `form:"gid" json:"gid" binding:"required"`
  182. Pf string `form:"pf" json:"pf" binding:"required"`
  183. Date string `form:"date" json:"data" binding:""`
  184. StartTime string `form:"startTime" json:"startTime" binding:""`
  185. EndTime string `form:"endTime" json:"endTime" binding:""`
  186. }{})
  187. res, err := service.UserOnlineSummary(form.Gid, form.Pf, form.Date, form.StartTime, form.EndTime)
  188. if err != nil {
  189. response.Fail(c, 1001, err.Error())
  190. return
  191. }
  192. response.Success(c, gin.H{
  193. "data": res,
  194. })
  195. }