sys_opera_log.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package model
  2. import (
  3. model2 "Medical_OAuth/common/model"
  4. "encoding/json"
  5. "errors"
  6. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  7. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  8. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  9. "gogs.baozhida.cn/zoie/OAuth-core/storage"
  10. "time"
  11. )
  12. type SysOperaLog struct {
  13. model2.Model
  14. Title string `json:"title" gorm:"size:255;comment:操作模块"`
  15. BusinessType string `json:"businessType" gorm:"size:128;comment:操作类型"`
  16. BusinessTypes string `json:"businessTypes" gorm:"size:128;comment:BusinessTypes"`
  17. Method string `json:"method" gorm:"size:128;comment:函数"`
  18. RequestMethod string `json:"requestMethod" gorm:"size:128;comment:请求方式 GET POST PUT DELETE"`
  19. OperatorType string `json:"operatorType" gorm:"size:128;comment:操作类型"`
  20. OperaName string `json:"operaName" gorm:"size:128;comment:操作者"`
  21. DeptName string `json:"deptName" gorm:"size:128;comment:机构名称"`
  22. OperaUrl string `json:"operaUrl" gorm:"size:255;comment:访问地址"`
  23. OperaIp string `json:"operaIp" gorm:"size:128;comment:客户端ip"`
  24. OperaLocation string `json:"operaLocation" gorm:"size:128;comment:访问位置"`
  25. OperaParam string `json:"operaParam" gorm:"text;comment:请求参数"`
  26. Status string `json:"status" gorm:"size:4;comment:操作状态 1:正常 2:关闭"`
  27. OperaTime time.Time `json:"operaTime" gorm:"comment:操作时间"`
  28. JsonResult string `json:"jsonResult" gorm:"text;comment:返回数据"`
  29. Remark string `json:"remark" gorm:"size:255;comment:备注"`
  30. LatencyTime string `json:"latencyTime" gorm:"size:128;comment:耗时"`
  31. UserAgent string `json:"userAgent" gorm:"size:255;comment:ua"`
  32. CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"`
  33. UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"`
  34. model2.ControlBy
  35. }
  36. func (SysOperaLog) TableName() string {
  37. return "sys_opera_log"
  38. }
  39. func (e *SysOperaLog) Generate() model2.ActiveRecord {
  40. o := *e
  41. return &o
  42. }
  43. func (e *SysOperaLog) GetId() interface{} {
  44. return e.Id
  45. }
  46. // SaveOperaLog 从队列中获取操作日志
  47. func SaveOperaLog(message storage.Messager) (err error) {
  48. //准备db
  49. db := sdk.Runtime.GetDbByKey(config.ApplicationConfig.Host)
  50. if db == nil {
  51. err = errors.New("db not exist")
  52. log.Errorf("host[%s]'s %s", config.ApplicationConfig.Host, err.Error())
  53. // Log writing to the database ignores error
  54. return nil
  55. }
  56. var rb []byte
  57. rb, err = json.Marshal(message.GetValues())
  58. if err != nil {
  59. log.Errorf("json Marshal error, %s", err.Error())
  60. // Log writing to the database ignores error
  61. return nil
  62. }
  63. var l SysOperaLog
  64. err = json.Unmarshal(rb, &l)
  65. if err != nil {
  66. log.Errorf("json Unmarshal error, %s", err.Error())
  67. // Log writing to the database ignores error
  68. return nil
  69. }
  70. // 外部调用获取身份信息,操作频繁,不做日志记录
  71. if l.OperaUrl == "/api/service/userinfo" {
  72. return nil
  73. }
  74. // 超出100个字符返回值截断
  75. if len(l.JsonResult) > 500 {
  76. l.JsonResult = l.JsonResult[:500]
  77. }
  78. l.OperaTime = time.Now()
  79. err = db.Create(&l).Error
  80. if err != nil {
  81. log.Errorf("db create error, %s", err.Error())
  82. // Log writing to the database ignores error
  83. return nil
  84. }
  85. return nil
  86. }