user.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. Gid string `form:"gid" json:"gid" binding:"required"`
  111. Pf string `form:"pf" json:"pf" binding:"required"`
  112. Offset int `form:"offset" json:"offset" binding:""`
  113. Limit int `form:"limit" json:"limit" binding:"required"`
  114. }{})
  115. //读取表
  116. userTotalKey := config.Get("app.user_total")
  117. if form.Gid != "" && form.Pf != "" {
  118. userTotalKey = config.Get("app.user_total") + ":" + form.Gid + ":" + form.Pf
  119. }
  120. data, err := global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)-1).Result()
  121. if err != nil {
  122. response.Fail(c, 1001, err.Error())
  123. return
  124. }
  125. count, err := global.App.Redis.ZCard(context.Background(), userTotalKey).Result()
  126. if err != nil {
  127. response.Fail(c, 1001, err.Error())
  128. return
  129. }
  130. //读取黑名单
  131. blackListKey := config.Get("app.black_list_table")
  132. black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
  133. if err != nil {
  134. response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
  135. return
  136. }
  137. var res []User
  138. for _, val := range data {
  139. userKey := val.Member.(string)
  140. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  141. if err != nil {
  142. global.App.Log.Error("GetUserData err")
  143. response.Fail(c, 1003, "GetUserData err"+err.Error())
  144. return
  145. }
  146. //查看是否在黑名单
  147. var inBlack bool
  148. if utils.InArray(userKey, black) {
  149. inBlack = true
  150. }
  151. userMsg := strings.Split(userKey, ":")
  152. openId := userMsg[len(userMsg)-1]
  153. gid := userMsg[0]
  154. pf := userMsg[1]
  155. //查看是否加了权限
  156. optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
  157. option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
  158. if err != nil {
  159. response.Fail(c, 1003, err.Error())
  160. return
  161. }
  162. res = append(res, User{
  163. UserId: userData["userId"],
  164. Gid: userData["gid"],
  165. Pf: userData["pf"],
  166. NickName: userData["nickName"],
  167. Head: userData["head"],
  168. OpenId: openId,
  169. InBlack: inBlack,
  170. Option: option["option"],
  171. })
  172. }
  173. response.Success(c, gin.H{
  174. "data": res,
  175. "count": count,
  176. })
  177. }