return.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. #*****************************************************************************
  3. # @file return.go
  4. # @author MakerYang(https://www.makeryang.com)
  5. # @statement 免费课程配套开源项目,任何形式收费均为盗版
  6. #*****************************************************************************
  7. */
  8. package Utils
  9. import (
  10. "encoding/json"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "log"
  14. "math"
  15. "net/http"
  16. "os"
  17. "strconv"
  18. "time"
  19. )
  20. type logData struct {
  21. ClientIp string `json:"client_ip"`
  22. ClientMethod string `json:"client_method"`
  23. ClientUrl string `json:"client_url"`
  24. ClientSign string `json:"client_sign"`
  25. ClientToken string `json:"client_token"`
  26. ClientReferer string `json:"client_referer"`
  27. ClientParameter clientParameter `json:"client_parameter"`
  28. ServerParameter string `json:"server_parameter"`
  29. Date string `json:"date"`
  30. Time int64 `json:"time"`
  31. Server string `json:"server"`
  32. Length string `json:"length"`
  33. }
  34. type clientParameter struct {
  35. Get interface{} `json:"get"`
  36. Post interface{} `json:"post"`
  37. }
  38. func requestLog(c *gin.Context, serverParameter string) {
  39. data := &logData{}
  40. data.Time = time.Now().Unix()
  41. data.Date = time.Now().Format("2006-01-02 15:04:05")
  42. data.ClientMethod = c.Request.Method
  43. data.ClientIp = c.ClientIP()
  44. data.ClientSign = c.Request.Header.Get("Client-Sign")
  45. data.ClientToken = c.Request.Header.Get("Client-Token")
  46. data.ClientReferer = c.Request.Header.Get("Client-Referer")
  47. if data.ClientMethod == "GET" {
  48. data.ClientParameter.Get = c.Request.URL.Query()
  49. }
  50. if data.ClientMethod == "POST" {
  51. data.ClientParameter.Post = c.Request.PostForm
  52. }
  53. scheme := "http://"
  54. if c.Request.TLS != nil {
  55. scheme = "https://"
  56. }
  57. serverUrl := scheme + c.Request.Host + c.Request.URL.Path
  58. data.ClientUrl = serverUrl
  59. serverName, _ := os.Hostname()
  60. data.Server = serverName
  61. data.ServerParameter = serverParameter
  62. clientTimeStr := c.Request.Header.Get("Client-Time")
  63. if clientTimeStr != "" {
  64. clientTimeMs, _ := strconv.ParseInt(clientTimeStr, 10, 64)
  65. serverTimeMs := time.Now().UnixNano() / 1e6
  66. processingTimeMs := serverTimeMs - clientTimeMs
  67. data.Length = fmt.Sprintf("%.3f", math.Abs(float64(processingTimeMs)/1000.0))
  68. }
  69. dataString, _ := json.Marshal(data)
  70. log.Println("[request]", string(dataString))
  71. }
  72. func Success(c *gin.Context, data interface{}) {
  73. c.JSON(http.StatusOK, gin.H{
  74. "code": 0,
  75. "msg": "success",
  76. "data": data,
  77. })
  78. logJson, _ := json.Marshal(gin.H{"code": 0, "msg": "success", "data": data})
  79. requestLog(c, string(logJson))
  80. }
  81. func Error(c *gin.Context, data interface{}) {
  82. c.JSON(http.StatusOK, gin.H{
  83. "code": 10000,
  84. "msg": "error",
  85. "data": data,
  86. })
  87. logJson, _ := json.Marshal(gin.H{"code": 10000, "msg": "error", "data": data})
  88. requestLog(c, string(logJson))
  89. }
  90. func Warning(c *gin.Context, code int, msg string, data interface{}) {
  91. c.JSON(http.StatusOK, gin.H{
  92. "code": code,
  93. "msg": msg,
  94. "data": data,
  95. })
  96. logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "data": data})
  97. requestLog(c, string(logJson))
  98. }
  99. func AuthError(c *gin.Context, code int, msg string, data interface{}) {
  100. c.JSON(http.StatusUnauthorized, gin.H{
  101. "code": code,
  102. "msg": msg,
  103. "data": data,
  104. })
  105. logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "data": data})
  106. requestLog(c, string(logJson))
  107. }