sys_oper_log.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package sys_oper_log
  2. import (
  3. "gfast/app/model/admin/auth_rule"
  4. "gfast/app/model/admin/user"
  5. "gfast/library/service"
  6. "gfast/library/utils"
  7. "github.com/gogf/gf/encoding/gjson"
  8. "github.com/gogf/gf/errors/gerror"
  9. "github.com/gogf/gf/frame/g"
  10. "github.com/gogf/gf/os/gtime"
  11. "github.com/gogf/gf/util/gconv"
  12. "net/url"
  13. )
  14. // Fill with you ideas below.
  15. //查询列表请求参数
  16. type SelectPageReq struct {
  17. Title string `p:"title"` //系统模块
  18. OperName string `p:"operName"` //操作人员
  19. Status string `p:"status"` //操作状态
  20. BeginTime string `p:"beginTime"` //数据范围
  21. EndTime string `p:"endTime"` //开始时间
  22. PageNum int `p:"pageNum"` //当前页码
  23. PageSize int `p:"pageSize"` //每页数
  24. SortName string `p:"orderByColumn"` //排序字段
  25. SortOrder string `p:"isAsc"` //排序方式
  26. }
  27. //新增操作日志记录
  28. func Add(user *user.Entity, menu *auth_rule.Entity, url *url.URL,
  29. param g.Map, method, clientIp string) {
  30. var operLog Entity
  31. if menu != nil {
  32. operLog.Title = menu.Title
  33. }
  34. operLog.Method = url.Path
  35. operLog.RequestMethod = method
  36. operLog.OperatorType = 1
  37. operLog.OperName = user.UserName
  38. rawQuery := url.RawQuery
  39. if rawQuery != "" {
  40. rawQuery = "?" + rawQuery
  41. }
  42. operLog.OperUrl = url.Path + rawQuery
  43. operLog.OperIp = clientIp
  44. operLog.OperLocation = utils.GetCityByIp(operLog.OperIp)
  45. operLog.OperTime = gtime.Timestamp()
  46. if param != nil {
  47. if v, ok := param["apiReturnRes"]; ok {
  48. res := gconv.Map(v)
  49. if gconv.Int(res["code"]) == 0 {
  50. operLog.Status = 1
  51. } else {
  52. operLog.Status = 0
  53. }
  54. if _, ok = res["data"]; ok {
  55. delete(res, "data")
  56. }
  57. b, _ := gjson.Encode(res)
  58. if len(b) > 0 {
  59. operLog.JsonResult = string(b)
  60. }
  61. delete(param, "apiReturnRes")
  62. }
  63. b, _ := gjson.Encode(param)
  64. if len(b) > 0 {
  65. operLog.OperParam = string(b)
  66. }
  67. }
  68. operLog.Save()
  69. }
  70. //操作日志列表
  71. func ListByPage(req *SelectPageReq) (total, page int, list []*Entity, err error) {
  72. model := Model
  73. order := "oper_time DESC"
  74. if req != nil {
  75. if req.OperName != "" {
  76. model = model.Where("oper_name like ?", "%"+req.OperName+"%")
  77. }
  78. if req.Title != "" {
  79. model = model.Where("title like ?", "%"+req.Title+"%")
  80. }
  81. if req.Status != "" {
  82. model = model.Where("status", gconv.Int(req.Status))
  83. }
  84. if req.BeginTime != "" {
  85. model = model.Where("oper_time >=", utils.StrToTimestamp(req.BeginTime))
  86. }
  87. if req.EndTime != "" {
  88. model = model.Where("oper_time <=", utils.StrToTimestamp(req.EndTime))
  89. }
  90. if req.SortName != "" {
  91. if req.SortOrder != "" {
  92. order = req.SortName + " " + req.SortOrder
  93. } else {
  94. order = req.SortName + " DESC"
  95. }
  96. }
  97. }
  98. total, err = model.Count()
  99. if err != nil {
  100. g.Log().Error(err)
  101. err = gerror.New("获取总行数失败")
  102. return
  103. }
  104. if req.PageNum == 0 {
  105. req.PageNum = 1
  106. }
  107. page = req.PageNum
  108. if req.PageSize == 0 {
  109. req.PageSize = service.AdminPageNum
  110. }
  111. list, err = model.Page(page, req.PageSize).Order(order).All()
  112. if err != nil {
  113. g.Log().Error(err)
  114. err = gerror.New("获取数据失败")
  115. return
  116. }
  117. return
  118. }
  119. //通过id获取操作日志
  120. func GetById(id int64) (log *Entity, err error) {
  121. if id == 0 {
  122. err = gerror.New("参数错误")
  123. return
  124. }
  125. log, err = Model.FindOne("oper_id", id)
  126. if err != nil {
  127. g.Log().Error(err)
  128. }
  129. if err != nil || log == nil {
  130. err = gerror.New("获取操作日志失败")
  131. }
  132. return
  133. }
  134. //删除
  135. func DeleteByIds(ids []int) (err error) {
  136. if len(ids) == 0 {
  137. err = gerror.New("参数错误")
  138. return
  139. }
  140. _, err = Model.Delete("oper_id in (?)", ids)
  141. if err != nil {
  142. g.Log().Error(err)
  143. err = gerror.New("删除失败")
  144. }
  145. return
  146. }
  147. //清空
  148. func ClearLog() (err error) {
  149. _, err = g.DB().Exec("truncate " + Table)
  150. if err != nil {
  151. g.Log().Error(err)
  152. err = gerror.New("清除失败")
  153. }
  154. return
  155. }