usedata.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/model"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func GetAllQuotaDates(c *gin.Context) {
  10. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  11. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  12. username := c.Query("username")
  13. dates, err := model.GetAllQuotaDates(startTimestamp, endTimestamp, username)
  14. if err != nil {
  15. common.ApiError(c, err)
  16. return
  17. }
  18. c.JSON(http.StatusOK, gin.H{
  19. "success": true,
  20. "message": "",
  21. "data": dates,
  22. })
  23. return
  24. }
  25. func GetQuotaDatesByUser(c *gin.Context) {
  26. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  27. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  28. dates, err := model.GetQuotaDataGroupByUser(startTimestamp, endTimestamp)
  29. if err != nil {
  30. common.ApiError(c, err)
  31. return
  32. }
  33. c.JSON(http.StatusOK, gin.H{
  34. "success": true,
  35. "message": "",
  36. "data": dates,
  37. })
  38. }
  39. func GetUserQuotaDates(c *gin.Context) {
  40. userId := c.GetInt("id")
  41. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  42. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  43. // 判断时间跨度是否超过 1 个月
  44. if endTimestamp-startTimestamp > 2592000 {
  45. c.JSON(http.StatusOK, gin.H{
  46. "success": false,
  47. "message": "时间跨度不能超过 1 个月",
  48. })
  49. return
  50. }
  51. dates, err := model.GetQuotaDataByUserId(userId, startTimestamp, endTimestamp)
  52. if err != nil {
  53. common.ApiError(c, err)
  54. return
  55. }
  56. c.JSON(http.StatusOK, gin.H{
  57. "success": true,
  58. "message": "",
  59. "data": dates,
  60. })
  61. return
  62. }