user.go 9.3 KB

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