return.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package Utils
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "log"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "time"
  10. )
  11. type logData struct {
  12. Timestamp int64 `json:"timestamp"`
  13. TimestampFormat string `json:"timestamp_format"`
  14. ClientMethod string `json:"client_method"`
  15. ClientIp string `json:"client_ip"`
  16. ClientParameter string `json:"client_parameter"`
  17. ServerParameter string `json:"server_parameter"`
  18. ServerUrl string `json:"server_url"`
  19. ServerName string `json:"server_name"`
  20. ServerYear string `json:"server_year"`
  21. ServerMonth string `json:"server_month"`
  22. ServerDay string `json:"server_day"`
  23. ServerTime string `json:"server_time"`
  24. TimeLength string `json:"time_length"`
  25. }
  26. func recordLog(c *gin.Context, serverParameter string) {
  27. data := &logData{}
  28. data.Timestamp = time.Now().Unix()
  29. data.TimestampFormat = time.Now().Format("2006-01-02 15:04:05")
  30. data.ClientMethod = c.Request.Method
  31. data.ClientIp = c.ClientIP()
  32. if data.ClientMethod == "GET" {
  33. data.ClientParameter = c.Request.RequestURI
  34. }
  35. if data.ClientMethod == "POST" {
  36. clientParam, err := json.Marshal(c.Request.PostForm)
  37. if err != nil {
  38. data.ClientParameter = ""
  39. }
  40. if err == nil {
  41. data.ClientParameter = string(clientParam)
  42. }
  43. }
  44. scheme := "http://"
  45. if c.Request.TLS != nil {
  46. scheme = "https://"
  47. }
  48. serverUrl := scheme + c.Request.Host + c.Request.URL.Path
  49. serverName, _ := os.Hostname()
  50. data.ServerUrl = serverUrl
  51. data.ServerName = serverName
  52. data.ClientParameter = c.GetString("client_parameter")
  53. data.ServerParameter = serverParameter
  54. data.ServerYear = time.Now().Format("2006")
  55. data.ServerMonth = time.Now().Format("01")
  56. data.ServerDay = time.Now().Format("02")
  57. data.ServerTime = time.Now().Format("15:04:05")
  58. data.TimeLength = strconv.FormatFloat(float64(time.Now().UnixNano())/1000000-c.GetFloat64("start_time"), 'f', 2, 64)
  59. dataString, _ := json.Marshal(data)
  60. log.Println("[Log]", string(dataString))
  61. }
  62. func Success(c *gin.Context, data interface{}) {
  63. c.JSON(http.StatusOK, gin.H{
  64. "code": 0,
  65. "msg": "success",
  66. "data": data,
  67. })
  68. logJson, _ := json.Marshal(gin.H{"code": 0, "msg": "success", "data": data})
  69. recordLog(c, string(logJson))
  70. }
  71. func Error(c *gin.Context, data interface{}) {
  72. c.JSON(http.StatusOK, gin.H{
  73. "code": 10000,
  74. "msg": "error",
  75. "data": data,
  76. })
  77. logJson, _ := json.Marshal(gin.H{"code": 10000, "msg": "error", "data": data})
  78. recordLog(c, string(logJson))
  79. }
  80. func Warning(c *gin.Context, code int, msg string, data interface{}) {
  81. c.JSON(http.StatusOK, gin.H{
  82. "code": code,
  83. "msg": msg,
  84. "data": data,
  85. })
  86. logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "data": data})
  87. recordLog(c, string(logJson))
  88. }
  89. func AuthError(c *gin.Context, code int, msg string, data interface{}) {
  90. c.JSON(http.StatusUnauthorized, gin.H{
  91. "code": code,
  92. "msg": msg,
  93. "data": data,
  94. })
  95. logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "data": data})
  96. recordLog(c, string(logJson))
  97. }