log.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "one-api/common"
  5. "one-api/model"
  6. "strconv"
  7. )
  8. func GetAllLogs(c *gin.Context) {
  9. p, _ := strconv.Atoi(c.Query("p"))
  10. if p < 0 {
  11. p = 0
  12. }
  13. logType, _ := strconv.Atoi(c.Query("type"))
  14. logs, err := model.GetAllLogs(logType, p*common.ItemsPerPage, common.ItemsPerPage)
  15. if err != nil {
  16. c.JSON(200, gin.H{
  17. "success": false,
  18. "message": err.Error(),
  19. })
  20. return
  21. }
  22. c.JSON(200, gin.H{
  23. "success": true,
  24. "message": "",
  25. "data": logs,
  26. })
  27. }
  28. func GetUserLogs(c *gin.Context) {
  29. p, _ := strconv.Atoi(c.Query("p"))
  30. if p < 0 {
  31. p = 0
  32. }
  33. userId := c.GetInt("id")
  34. logType, _ := strconv.Atoi(c.Query("type"))
  35. logs, err := model.GetUserLogs(userId, logType, p*common.ItemsPerPage, common.ItemsPerPage)
  36. if err != nil {
  37. c.JSON(200, gin.H{
  38. "success": false,
  39. "message": err.Error(),
  40. })
  41. return
  42. }
  43. c.JSON(200, gin.H{
  44. "success": true,
  45. "message": "",
  46. "data": logs,
  47. })
  48. }
  49. func SearchAllLogs(c *gin.Context) {
  50. keyword := c.Query("keyword")
  51. logs, err := model.SearchAllLogs(keyword)
  52. if err != nil {
  53. c.JSON(200, gin.H{
  54. "success": false,
  55. "message": err.Error(),
  56. })
  57. return
  58. }
  59. c.JSON(200, gin.H{
  60. "success": true,
  61. "message": "",
  62. "data": logs,
  63. })
  64. }
  65. func SearchUserLogs(c *gin.Context) {
  66. keyword := c.Query("keyword")
  67. userId := c.GetInt("id")
  68. logs, err := model.SearchUserLogs(userId, keyword)
  69. if err != nil {
  70. c.JSON(200, gin.H{
  71. "success": false,
  72. "message": err.Error(),
  73. })
  74. return
  75. }
  76. c.JSON(200, gin.H{
  77. "success": true,
  78. "message": "",
  79. "data": logs,
  80. })
  81. }