user.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package controller
  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/model"
  10. "designs/utils"
  11. "os"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/gin-gonic/gin"
  16. )
  17. type GameConfig struct {
  18. Gid string `form:"gid" json:"gid" binding:"required"` //游戏id
  19. Pid string `form:"pid" json:"pid" binding:""` //游戏pid
  20. GameName string `form:"gameName" json:"gameName" binding:"required"` //游戏名称
  21. WxAppid string `form:"wxAppid" json:"wxAppid" binding:"required"` //微信appid
  22. WxSecret string `form:"wxSecret" json:"wxSecret" binding:"required"` //微信secret
  23. TtAppid string `form:"ttAppid" json:"ttAppid" binding:"required"` //抖音appid
  24. TtSecret string `form:"ttSecret" json:"ttSecret" binding:"required"` //抖音secret
  25. AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
  26. TtTplId string `form:"ttTplId" json:"ttTplId" binding:""`
  27. WxTplId string `form:"wxTplId" json:"wxTplId" binding:""`
  28. }
  29. type GetGameCfg struct {
  30. AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
  31. }
  32. /* code 结构体 */
  33. type CodeData struct {
  34. Code string `form:"code" json:"code" binding:"required"`
  35. Gid string `form:"gid" json:"gid" binding:"required"`
  36. Pf string `form:"pf" json:"pf" binding:"required"`
  37. }
  38. /* 保存数据结构体 */
  39. type SaveData struct {
  40. Extend string `form:"extend" json:"extend" binding:"required"`
  41. Data string `form:"data" json:"data" binding:"required"`
  42. Secret string `form:"secret" json:"secret" binding:""`
  43. }
  44. /* 获取结构体 */
  45. type GetSaveData struct {
  46. Extend string `form:"extend" json:"extend" binding:"required"`
  47. }
  48. /*
  49. *登陆
  50. */
  51. func Login(c *gin.Context) {
  52. //http://127.0.0.1:8787/v1/user/login
  53. // 使用 ShouldBind 方法自动绑定:
  54. form := request.Check(c, &struct {
  55. UserName string `form:"userName" json:"userName" binding:"required"`
  56. Password string `form:"password" json:"password" binding:"required"`
  57. }{})
  58. //验证
  59. var admin model.AdminUser
  60. global.App.DB.Table(model.TableAdminUser).Where("account", form.UserName).First(&admin)
  61. if admin.Password != form.Password {
  62. response.Fail(c, 1003, "账户密码错误")
  63. return
  64. }
  65. //获取权限
  66. var identity model.AdminIdentity
  67. global.App.DB.Table(model.TableAdminIdentity).Where("id", admin.IdentityId).First(&identity)
  68. var permission []string
  69. per := strings.Split(identity.Permissions, ",")
  70. for _, v := range per {
  71. into, _ := strconv.Atoi(v)
  72. permission = append(permission, permissions[into])
  73. }
  74. //获取token
  75. token, err := common.GetJwtToken(form.UserName, admin.ID, 0)
  76. //刷新refreshtoken
  77. refresh, _ := common.GetJwtToken(form.UserName, admin.ID, 1)
  78. if err != nil {
  79. response.Fail(c, 1003, err.Error())
  80. return
  81. }
  82. //回传
  83. response.Success(c, gin.H{
  84. "token": token,
  85. "refreshToken": refresh,
  86. "loginTime": time.Now().Unix(),
  87. "ip": c.ClientIP(),
  88. "name": admin.Name,
  89. "permission": permission,
  90. })
  91. }
  92. /* 刷新token */
  93. func RefreshToken(c *gin.Context) {
  94. userName := c.GetString("userName")
  95. userId := c.GetInt("userId")
  96. //获取token
  97. token, err0 := common.GetJwtToken(userName, userId, 0)
  98. if err0 != nil {
  99. global.App.Log.Info("RefreshToken err")
  100. response.Fail(c, 1003, "RefreshToken err")
  101. return
  102. }
  103. data := map[string]interface{}{
  104. "token": token,
  105. }
  106. //返回数据
  107. response.Success(c, gin.H{
  108. "data": data,
  109. })
  110. }
  111. /* 获取时间 */
  112. func GetSysTime(c *gin.Context) {
  113. time := time.Now().Unix()
  114. common.RetJson(0, "获取系统时间", time, c)
  115. }
  116. type User struct {
  117. UserId string `json:"userId"`
  118. Gid string `json:"gid"`
  119. Pf string `json:"pf"`
  120. NickName string `json:"nickName"`
  121. Head string `json:"head"`
  122. OpenId string `json:"openId"`
  123. InBlack bool `json:"inBlack"`
  124. Option string `json:"option"`
  125. }
  126. func UserList(c *gin.Context) {
  127. form := request.Check(c, &struct {
  128. Gid string `form:"gid" json:"gid" binding:""`
  129. Pf string `form:"pf" json:"pf" binding:""`
  130. Offset int `form:"offset" json:"offset" binding:""`
  131. Limit int `form:"limit" json:"limit" binding:"required"`
  132. }{})
  133. //读取表
  134. userTotalKey := config.Get("app.user_total")
  135. if form.Gid != "" && form.Pf != "" {
  136. userTotalKey = config.Get("app.user_total") + ":" + form.Gid + ":" + form.Pf
  137. }
  138. data, err := global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)-1).Result()
  139. if err != nil {
  140. response.Fail(c, 1001, err.Error())
  141. return
  142. }
  143. count, err := global.App.Redis.ZCard(context.Background(), userTotalKey).Result()
  144. if err != nil {
  145. response.Fail(c, 1001, err.Error())
  146. return
  147. }
  148. //读取黑名单
  149. blackListKey := config.Get("app.black_list_table")
  150. black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
  151. if err != nil {
  152. response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
  153. return
  154. }
  155. var res []User
  156. for _, val := range data {
  157. userKey := val.Member.(string)
  158. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  159. if err != nil {
  160. global.App.Log.Error("GetUserData err")
  161. response.Fail(c, 1003, "GetUserData err"+err.Error())
  162. return
  163. }
  164. //查看是否在黑名单
  165. var inBlack bool
  166. if utils.InArray(userKey, black) {
  167. inBlack = true
  168. }
  169. userMsg := strings.Split(userKey, ":")
  170. openId := userMsg[len(userMsg)-1]
  171. gid := userMsg[0]
  172. pf := userMsg[1]
  173. //查看是否加了权限
  174. optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
  175. option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
  176. if err != nil {
  177. response.Fail(c, 1003, err.Error())
  178. return
  179. }
  180. res = append(res, User{
  181. UserId: userData["userId"],
  182. Gid: userData["gid"],
  183. Pf: userData["pf"],
  184. NickName: userData["nickName"],
  185. Head: userData["head"],
  186. OpenId: openId,
  187. InBlack: inBlack,
  188. Option: option["option"],
  189. })
  190. }
  191. response.Success(c, gin.H{
  192. "data": res,
  193. "count": count,
  194. })
  195. }
  196. func DownloadFile(c *gin.Context) {
  197. filename := c.Query("filename")
  198. // 验证文件是否存在
  199. if _, err := os.Stat(filename); os.IsNotExist(err) {
  200. c.JSON(404, gin.H{"error": "文件不存在"})
  201. return
  202. }
  203. // 设置响应头,触发浏览器下载行为
  204. c.Header("Content-Disposition", "attachment; filename="+filename)
  205. //c.Header("Content-Disposition", "inline; filename="+filename) // 或直接省略此行
  206. c.Header("Content-Type", "application/octet-stream")
  207. c.File(filename)
  208. }