log_ip.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package log
  2. import (
  3. "go.uber.org/zap"
  4. "log-server/global"
  5. "log-server/model/log"
  6. "log-server/model/log/request"
  7. "time"
  8. )
  9. type ServiceIpLog struct {
  10. }
  11. func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  12. limit := info.PageSize
  13. offset := info.PageSize * (info.Page - 1)
  14. db := global.GVA_DB.Model(&log.IpLog{})
  15. var apiList []log.IpLogResponse
  16. startDate := time.Now().Format("2006-01-02")
  17. endDate := time.Now().Format("2006-01-02")
  18. if len(api.Date) == 2 {
  19. startDate = api.Date[0]
  20. endDate = api.Date[1]
  21. }
  22. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  23. if api.GameId != 0 {
  24. db = db.Where("game_id = ?", api.GameId)
  25. }
  26. if api.PcCode != "" {
  27. db = db.Where("pc_code = ?", api.PcCode)
  28. }
  29. if api.Ip != "" {
  30. db = db.Where("ip = ?", api.Ip)
  31. }
  32. db = db.Select("create_date,ip,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
  33. db = db.Group("pc_code, game_id, create_date")
  34. //db.Select("count(*)").Count(&countTotal)
  35. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  36. err = db.Count(&total).Error
  37. if err != nil {
  38. return apiList, total, err
  39. } else {
  40. db = db.Limit(limit).Offset(offset)
  41. if order != "" {
  42. var OrderStr string
  43. // 设置有效排序key 防止sql注入
  44. // 感谢 Tom4t0 提交漏洞信息
  45. orderMap := make(map[string]bool, 1)
  46. orderMap["game_id"] = true
  47. if orderMap[order] {
  48. if desc {
  49. OrderStr = order + " desc"
  50. } else {
  51. OrderStr = order
  52. }
  53. } else { // didn't matched any order key in `orderMap`
  54. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  55. return apiList, total, err
  56. }
  57. err = db.Order(OrderStr).Find(&apiList).Error
  58. } else {
  59. err = db.Order("id").Find(&apiList).Error
  60. }
  61. }
  62. //遍历更改日期格式
  63. for i, _ := range apiList {
  64. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  65. }
  66. return apiList, total, err
  67. }