user.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. "os"
  15. "path/filepath"
  16. "strings"
  17. "time"
  18. "github.com/gin-gonic/gin"
  19. )
  20. type GameConfig struct {
  21. Gid string `form:"gid" json:"gid" binding:"required"` //游戏id
  22. Pid string `form:"pid" json:"pid" binding:""` //游戏pid
  23. GameName string `form:"gameName" json:"gameName" binding:"required"` //游戏名称
  24. WxAppid string `form:"wxAppid" json:"wxAppid" binding:"required"` //微信appid
  25. WxSecret string `form:"wxSecret" json:"wxSecret" binding:"required"` //微信secret
  26. TtAppid string `form:"ttAppid" json:"ttAppid" binding:"required"` //抖音appid
  27. TtSecret string `form:"ttSecret" json:"ttSecret" binding:"required"` //抖音secret
  28. AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
  29. TtTplId string `form:"ttTplId" json:"ttTplId" binding:""`
  30. WxTplId string `form:"wxTplId" json:"wxTplId" binding:""`
  31. ConfigPath string `form:"configPath" json:"configPath" binding:""`
  32. }
  33. type GetGameCfg struct {
  34. AppSecret string `form:"appSecret" json:"appSecret" binding:"required"` //游戏配置密钥
  35. }
  36. /* code 结构体 */
  37. type CodeData struct {
  38. Code string `form:"code" json:"code" binding:"required"`
  39. Gid string `form:"gid" json:"gid" binding:"required"`
  40. Pf string `form:"pf" json:"pf" binding:"required"`
  41. }
  42. /* 保存数据结构体 */
  43. type SaveData struct {
  44. Extend string `form:"extend" json:"extend" binding:"required"`
  45. Data string `form:"data" json:"data" binding:"required"`
  46. Secret string `form:"secret" json:"secret" binding:""`
  47. }
  48. /* 获取结构体 */
  49. type GetSaveData struct {
  50. Extend string `form:"extend" json:"extend" binding:"required"`
  51. }
  52. /*
  53. *登陆
  54. */
  55. func Login(c *gin.Context) {
  56. //http://127.0.0.1:8787/v1/user/login
  57. // 使用 ShouldBind 方法自动绑定:
  58. form := request.Check(c, &struct {
  59. UserName string `form:"userName" json:"userName" binding:"required"`
  60. Password string `form:"password" json:"password" binding:"required"`
  61. }{})
  62. //验证
  63. var admin model.Admin
  64. global.App.DB.Table("admin").Where("account", form.UserName).First(&admin)
  65. if admin.Password != "" && admin.Password != form.Password {
  66. response.Fail(c, 1003, "账户密码错误")
  67. return
  68. }
  69. //获取token
  70. token, err := common.GetJwtToken(form.UserName, 0)
  71. //刷新refreshtoken
  72. refresh, _ := common.GetJwtToken(form.UserName, 1)
  73. if err != nil {
  74. response.Fail(c, 1003, err.Error())
  75. return
  76. }
  77. //回传
  78. response.Success(c, gin.H{
  79. "token": token,
  80. "refreshToken": refresh,
  81. "loginTime": time.Now().Unix(),
  82. "ip": c.ClientIP(),
  83. })
  84. }
  85. /* 刷新token */
  86. func RefreshToken(c *gin.Context) {
  87. userName := c.GetString("userName")
  88. //获取token
  89. token, err0 := common.GetJwtToken(userName, 0)
  90. if err0 != nil {
  91. global.App.Log.Info("RefreshToken err")
  92. response.Fail(c, 1003, "RefreshToken err")
  93. return
  94. }
  95. data := map[string]interface{}{
  96. "token": token,
  97. }
  98. //返回数据
  99. response.Success(c, gin.H{
  100. "data": data,
  101. })
  102. }
  103. /* 获取时间 */
  104. func GetSysTime(c *gin.Context) {
  105. time := time.Now().Unix()
  106. common.RetJson(0, "获取系统时间", time, c)
  107. }
  108. type User struct {
  109. UserId string `json:"userId"`
  110. Gid string `json:"gid"`
  111. Pf string `json:"pf"`
  112. NickName string `json:"nickName"`
  113. Head string `json:"head"`
  114. OpenId string `json:"openId"`
  115. InBlack bool `json:"inBlack"`
  116. Option string `json:"option"`
  117. }
  118. func UserList(c *gin.Context) {
  119. form := request.Check(c, &struct {
  120. Gid string `form:"gid" json:"gid" binding:""`
  121. Pf string `form:"pf" json:"pf" binding:""`
  122. Offset int `form:"offset" json:"offset" binding:""`
  123. Limit int `form:"limit" json:"limit" binding:"required"`
  124. }{})
  125. //读取表
  126. userTotalKey := config.Get("app.user_total")
  127. if form.Gid != "" && form.Pf != "" {
  128. userTotalKey = config.Get("app.user_total") + ":" + form.Gid + ":" + form.Pf
  129. }
  130. data, err := global.App.Redis.ZRevRangeWithScores(context.Background(), userTotalKey, int64(form.Offset), int64(form.Offset+form.Limit)-1).Result()
  131. if err != nil {
  132. response.Fail(c, 1001, err.Error())
  133. return
  134. }
  135. count, err := global.App.Redis.ZCard(context.Background(), userTotalKey).Result()
  136. if err != nil {
  137. response.Fail(c, 1001, err.Error())
  138. return
  139. }
  140. //读取黑名单
  141. blackListKey := config.Get("app.black_list_table")
  142. black, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
  143. if err != nil {
  144. response.Fail(c, 1003, "读取黑名单列表失败"+err.Error())
  145. return
  146. }
  147. var res []User
  148. for _, val := range data {
  149. userKey := val.Member.(string)
  150. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  151. if err != nil {
  152. global.App.Log.Error("GetUserData err")
  153. response.Fail(c, 1003, "GetUserData err"+err.Error())
  154. return
  155. }
  156. //查看是否在黑名单
  157. var inBlack bool
  158. if utils.InArray(userKey, black) {
  159. inBlack = true
  160. }
  161. userMsg := strings.Split(userKey, ":")
  162. openId := userMsg[len(userMsg)-1]
  163. gid := userMsg[0]
  164. pf := userMsg[1]
  165. //查看是否加了权限
  166. optionKey := config.Get("app.option_key") + gid + ":" + pf + ":" + openId
  167. option, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
  168. if err != nil {
  169. response.Fail(c, 1003, err.Error())
  170. return
  171. }
  172. res = append(res, User{
  173. UserId: userData["userId"],
  174. Gid: userData["gid"],
  175. Pf: userData["pf"],
  176. NickName: userData["nickName"],
  177. Head: userData["head"],
  178. OpenId: openId,
  179. InBlack: inBlack,
  180. Option: option["option"],
  181. })
  182. }
  183. response.Success(c, gin.H{
  184. "data": res,
  185. "count": count,
  186. })
  187. }
  188. func GetUserOnlineMsg(c *gin.Context) {
  189. form := request.Check(c, &struct {
  190. Gid string `form:"gid" json:"gid" binding:"required"`
  191. Pf string `form:"pf" json:"pf" binding:"required"`
  192. Date string `form:"date" json:"data" binding:""`
  193. StartTime string `form:"startTime" json:"startTime" binding:""`
  194. EndTime string `form:"endTime" json:"endTime" binding:""`
  195. }{})
  196. res, err := service.UserOnlineSummary(form.Gid, form.Pf, form.Date, form.StartTime, form.EndTime)
  197. if err != nil {
  198. response.Fail(c, 1001, err.Error())
  199. return
  200. }
  201. response.Success(c, gin.H{
  202. "data": res,
  203. })
  204. }
  205. func UserBehaviorList(c *gin.Context) {
  206. //form := request.Check(c, &struct {
  207. // Gid string `form:"gid" json:"gid" binding:"required"`
  208. // Pf string `form:"pf" json:"pf" binding:"required"`
  209. // Cid string `form:"cid" json:"cid" binding:""`
  210. // Order string `form:"order" json:"order" binding:""`
  211. // Search string `form:"search" json:"search" binding:""`
  212. //}{})
  213. collection := global.App.MongoDB.Database("testing").Collection("numbers")
  214. res, _ := collection.InsertOne(context.Background(), bson.D{{"name", "pi"}, {"value", 3.14159}})
  215. fmt.Println(res)
  216. }
  217. func DownloadFile(c *gin.Context) {
  218. filename := c.Param("filename")
  219. // 验证文件是否存在
  220. if _, err := os.Stat(filename); os.IsNotExist(err) {
  221. c.JSON(404, gin.H{"error": "文件不存在"})
  222. return
  223. }
  224. // 设置响应头,触发浏览器下载行为
  225. c.Header("Content-Disposition", "attachment; filename="+filename)
  226. c.Header("Content-Type", "application/octet-stream")
  227. c.File(filename)
  228. }
  229. // 上传图片到服务器
  230. func Upload(c *gin.Context) {
  231. // 获取文件
  232. file, err := c.FormFile("file")
  233. if err != nil {
  234. c.JSON(400, gin.H{"error": "未选择文件"})
  235. return
  236. }
  237. // 存储路径生成
  238. saveDir := fmt.Sprintf("uploads/%s", time.Now().Format("2006-01-02"))
  239. os.MkdirAll(saveDir, 0755)
  240. savePath := filepath.Join(saveDir, file.Filename)
  241. // 保存文件
  242. if err := c.SaveUploadedFile(file, savePath); err != nil {
  243. c.JSON(500, gin.H{"error": "文件保存失败"})
  244. return
  245. }
  246. fmt.Println(savePath)
  247. response.Success(c, gin.H{
  248. "path": savePath,
  249. })
  250. }