user.go 7.8 KB

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