user.go 5.0 KB

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