sys_opera_log.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "time"
  6. log "github.com/go-admin-team/go-admin-core/logger"
  7. "github.com/go-admin-team/go-admin-core/sdk"
  8. "github.com/go-admin-team/go-admin-core/storage"
  9. "go-admin/common/models"
  10. )
  11. type SysOperaLog struct {
  12. models.Model
  13. Title string `json:"title" gorm:"size:255;comment:操作模块"`
  14. BusinessType string `json:"businessType" gorm:"size:128;comment:操作类型"`
  15. BusinessTypes string `json:"businessTypes" gorm:"size:128;comment:BusinessTypes"`
  16. Method string `json:"method" gorm:"size:128;comment:函数"`
  17. RequestMethod string `json:"requestMethod" gorm:"size:128;comment:请求方式 GET POST PUT DELETE"`
  18. OperatorType string `json:"operatorType" gorm:"size:128;comment:操作类型"`
  19. OperName string `json:"operName" gorm:"size:128;comment:操作者"`
  20. DeptName string `json:"deptName" gorm:"size:128;comment:部门名称"`
  21. OperUrl string `json:"operUrl" gorm:"size:255;comment:访问地址"`
  22. OperIp string `json:"operIp" gorm:"size:128;comment:客户端ip"`
  23. OperLocation string `json:"operLocation" gorm:"size:128;comment:访问位置"`
  24. OperParam string `json:"operParam" gorm:"text;comment:请求参数"`
  25. Status string `json:"status" gorm:"size:4;comment:操作状态 1:正常 2:关闭"`
  26. OperTime time.Time `json:"operTime" gorm:"comment:操作时间"`
  27. JsonResult string `json:"jsonResult" gorm:"size:255;comment:返回数据"`
  28. Remark string `json:"remark" gorm:"size:255;comment:备注"`
  29. LatencyTime string `json:"latencyTime" gorm:"size:128;comment:耗时"`
  30. UserAgent string `json:"userAgent" gorm:"size:255;comment:ua"`
  31. CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"`
  32. UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"`
  33. models.ControlBy
  34. }
  35. func (SysOperaLog) TableName() string {
  36. return "sys_opera_log"
  37. }
  38. func (e *SysOperaLog) Generate() models.ActiveRecord {
  39. o := *e
  40. return &o
  41. }
  42. func (e *SysOperaLog) GetId() interface{} {
  43. return e.Id
  44. }
  45. // SaveOperaLog 从队列中获取操作日志
  46. func SaveOperaLog(message storage.Messager) (err error) {
  47. //准备db
  48. db := sdk.Runtime.GetDbByKey(message.GetPrefix())
  49. if db == nil {
  50. err = errors.New("db not exist")
  51. log.Errorf("host[%s]'s %s", message.GetPrefix(), err.Error())
  52. // Log writing to the database ignores error
  53. return nil
  54. }
  55. var rb []byte
  56. rb, err = json.Marshal(message.GetValues())
  57. if err != nil {
  58. log.Errorf("json Marshal error, %s", err.Error())
  59. // Log writing to the database ignores error
  60. return nil
  61. }
  62. var l SysOperaLog
  63. err = json.Unmarshal(rb, &l)
  64. if err != nil {
  65. log.Errorf("json Unmarshal error, %s", err.Error())
  66. // Log writing to the database ignores error
  67. return nil
  68. }
  69. // 超出100个字符返回值截断
  70. if len(l.JsonResult) > 100 {
  71. l.JsonResult = l.JsonResult[:100]
  72. }
  73. err = db.Create(&l).Error
  74. if err != nil {
  75. log.Errorf("db create error, %s", err.Error())
  76. // Log writing to the database ignores error
  77. return nil
  78. }
  79. return nil
  80. }