log_middle.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package middleware
  2. import (
  3. db "Medical_ERP/common/initialize"
  4. "Medical_ERP/models"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/go-basic/uuid"
  8. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/beegouser"
  9. "strconv"
  10. "strings"
  11. "time"
  12. adapter "github.com/beego/beego/v2/adapter"
  13. "github.com/beego/beego/v2/adapter/context"
  14. context2 "github.com/beego/beego/v2/server/web/context"
  15. )
  16. type OperationLogDetailed struct {
  17. Input string `json:"input"`
  18. Data string `json:"data"`
  19. Path string `json:"path"`
  20. Method string `json:"method"`
  21. Ip string `json:"ip"`
  22. Username string `json:"username"`
  23. RequestTime string `json:"request_time"`
  24. }
  25. // LogMiddle 中间件
  26. var filterLog = func(ctx *context.Context) {
  27. if ctx.Input.URL() != "/api/home/chart" && ctx.Input.URL() != "/api/home/list" {
  28. var name, username string
  29. //非登录接口从token中获取用户name
  30. if len(ctx.Request.Header["Authorization"]) != 0 {
  31. username = beegouser.GetUserName((*context2.Context)(ctx))
  32. }
  33. //获取url类型
  34. urlKey := strings.Replace(ctx.Input.URL(), "/", "_", -1)
  35. //传递name
  36. detailedStruct := getRequestDetailed(ctx)
  37. detailedStruct.Username = username
  38. detailedJsonByte, err := json.Marshal(detailedStruct) //转换成JSON返回的是byte[]
  39. if err != nil {
  40. fmt.Println(err.Error())
  41. }
  42. //组合描述
  43. describe := name + "-send:" + detailedStruct.Path
  44. var uuid = uuid.New()
  45. logData := models.OperationLog{
  46. ID: uuid,
  47. UrlKey: urlKey,
  48. Describe: describe,
  49. DataID: "",
  50. Detailed: string(detailedJsonByte),
  51. }
  52. if err := db.DB.Create(&logData).Error; err != nil {
  53. fmt.Println("log insert fail")
  54. }
  55. }
  56. }
  57. // 获取请求详细数据
  58. func getRequestDetailed(ctx *context.Context) *OperationLogDetailed {
  59. var jsonDataMap map[string]interface{}
  60. _ = json.Unmarshal(ctx.Input.RequestBody, &jsonDataMap)
  61. // 将 JSON 对象压缩为字节切片
  62. compressedData, _ := json.Marshal(jsonDataMap)
  63. //获取起始时间
  64. startTime := ctx.Request.Header.Get("req_start_time")
  65. //string转int
  66. startTimeInt, _ := strconv.Atoi(startTime)
  67. reqTimeInt := int(time.Now().UnixNano()/1e6) - startTimeInt
  68. var detailedStruct OperationLogDetailed
  69. detailedStruct.Input = string(compressedData)
  70. detailedStruct.Path = ctx.Input.URL()
  71. detailedStruct.Method = ctx.Request.Method
  72. detailedStruct.Ip = ctx.Input.IP()
  73. detailedStruct.RequestTime = strconv.Itoa(reqTimeInt)
  74. return &detailedStruct
  75. }
  76. // 将开始时间放入cookie
  77. var getStartTime = func(ctx *context.Context) {
  78. startTime := time.Now().UnixNano() / 1e6
  79. startTimeStr := strconv.FormatInt(startTime, 10)
  80. ctx.Request.Header.Set("req_start_time", startTimeStr)
  81. }
  82. func LogMiddle() {
  83. adapter.InsertFilter("/*", adapter.FinishRouter, filterLog, false)
  84. adapter.InsertFilter("/*", adapter.BeforeRouter, getStartTime, false)
  85. }