user.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/model"
  10. "designs/service"
  11. "designs/utils"
  12. "fmt"
  13. "go.mongodb.org/mongo-driver/v2/bson"
  14. "strings"
  15. "time"
  16. "github.com/gin-gonic/gin"
  17. )
  18. type GameConfig struct {
  19. Gid string `form:"gid" json:"gid" binding:"required"` //游戏id
  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.Admin
  60. global.App.DB.Table("admin").Where("account", form.UserName).First(&admin)
  61. if admin.Password != "" && admin.Password != form.Password {
  62. response.Fail(c, 1003, "账户密码错误")
  63. return
  64. }
  65. //获取token
  66. token, err := common.GetJwtToken(form.UserName, 0)
  67. //刷新refreshtoken
  68. refresh, _ := common.GetJwtToken(form.UserName, 1)
  69. if err != nil {
  70. response.Fail(c, 1003, err.Error())
  71. return
  72. }
  73. //回传
  74. response.Success(c, gin.H{
  75. "token": token,
  76. "refreshToken": refresh,
  77. "loginTime": time.Now().Unix(),
  78. "ip": c.ClientIP(),
  79. })
  80. }
  81. /* 刷新token */
  82. func RefreshToken(c *gin.Context) {
  83. userName := c.GetString("userName")
  84. //获取token
  85. token, err0 := common.GetJwtToken(userName, 0)
  86. if err0 != nil {
  87. global.App.Log.Info("RefreshToken err")
  88. response.Fail(c, 1003, "RefreshToken err")
  89. return
  90. }
  91. data := map[string]interface{}{
  92. "token": token,
  93. }
  94. //返回数据
  95. response.Success(c, gin.H{
  96. "data": data,
  97. })
  98. }
  99. /* 获取时间 */
  100. func GetSysTime(c *gin.Context) {
  101. time := time.Now().Unix()
  102. common.RetJson(0, "获取系统时间", time, c)
  103. }
  104. type User struct {
  105. UserId string `json:"userId"`
  106. Gid string `json:"gid"`
  107. Pf string `json:"pf"`
  108. NickName string `json:"nickName"`
  109. Head string `json:"head"`
  110. OpenId string `json:"openId"`
  111. InBlack bool `json:"inBlack"`
  112. Option string `json:"option"`
  113. }
  114. func UserList(c *gin.Context) {
  115. form := request.Check(c, &struct {
  116. Gid string `form:"gid" json:"gid" binding:""`
  117. Pf string `form:"pf" json:"pf" binding:""`
  118. Offset int `form:"offset" json:"offset" binding:""`
  119. Limit int `form:"limit" json:"limit" binding:"required"`
  120. }{})
  121. //读取表
  122. userTotalKey := config.Get("app.user_total")
  123. if form.Gid != "" && form.Pf != "" {
  124. userTotalKey = config.Get("app.user_total") + ":" + form.Gid + ":" + form.Pf
  125. }
  126. data, err := global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)-1).Result()
  127. if err != nil {
  128. response.Fail(c, 1001, err.Error())
  129. return
  130. }
  131. count, err := global.App.Redis.ZCard(context.Background(), userTotalKey).Result()
  132. if err != nil {
  133. response.Fail(c, 1001, err.Error())
  134. return
  135. }
  136. //读取黑名单
  137. blackListKey := config.Get("app.black_list_table")
  138. black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
  139. if err != nil {
  140. response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
  141. return
  142. }
  143. var res []User
  144. for _, val := range data {
  145. userKey := val.Member.(string)
  146. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  147. if err != nil {
  148. global.App.Log.Error("GetUserData err")
  149. response.Fail(c, 1003, "GetUserData err"+err.Error())
  150. return
  151. }
  152. //查看是否在黑名单
  153. var inBlack bool
  154. if utils.InArray(userKey, black) {
  155. inBlack = true
  156. }
  157. userMsg := strings.Split(userKey, ":")
  158. openId := userMsg[len(userMsg)-1]
  159. gid := userMsg[0]
  160. pf := userMsg[1]
  161. //查看是否加了权限
  162. optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
  163. option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
  164. if err != nil {
  165. response.Fail(c, 1003, err.Error())
  166. return
  167. }
  168. res = append(res, User{
  169. UserId: userData["userId"],
  170. Gid: userData["gid"],
  171. Pf: userData["pf"],
  172. NickName: userData["nickName"],
  173. Head: userData["head"],
  174. OpenId: openId,
  175. InBlack: inBlack,
  176. Option: option["option"],
  177. })
  178. }
  179. response.Success(c, gin.H{
  180. "data": res,
  181. "count": count,
  182. })
  183. }
  184. func GetUserOnlineMsg(c *gin.Context) {
  185. form := request.Check(c, &struct {
  186. Gid string `form:"gid" json:"gid" binding:"required"`
  187. Pf string `form:"pf" json:"pf" binding:"required"`
  188. Date string `form:"date" json:"data" binding:""`
  189. StartTime string `form:"startTime" json:"startTime" binding:""`
  190. EndTime string `form:"endTime" json:"endTime" binding:""`
  191. }{})
  192. res, err := service.UserOnlineSummary(form.Gid, form.Pf, form.Date, form.StartTime, form.EndTime)
  193. if err != nil {
  194. response.Fail(c, 1001, err.Error())
  195. return
  196. }
  197. response.Success(c, gin.H{
  198. "data": res,
  199. })
  200. }
  201. func UserBehaviorList(c *gin.Context) {
  202. //form := request.Check(c, &struct {
  203. // Gid string `form:"gid" json:"gid" binding:"required"`
  204. // Pf string `form:"pf" json:"pf" binding:"required"`
  205. // Cid string `form:"cid" json:"cid" binding:""`
  206. // Order string `form:"order" json:"order" binding:""`
  207. // Search string `form:"search" json:"search" binding:""`
  208. //}{})
  209. collection := global.App.MongoDB.Database("testing").Collection("numbers")
  210. res, _ := collection.InsertOne(context.Background(), bson.D{{"name", "pi"}, {"value", 3.14159}})
  211. fmt.Println(res)
  212. }