1
0

usedata.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package controller
  2. import (
  3. "net/http"
  4. "one-api/common"
  5. "one-api/model"
  6. "strconv"
  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 GetUserQuotaDates(c *gin.Context) {
  26. userId := c.GetInt("id")
  27. startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
  28. endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
  29. // 判断时间跨度是否超过 1 个月
  30. if endTimestamp-startTimestamp > 2592000 {
  31. c.JSON(http.StatusOK, gin.H{
  32. "success": false,
  33. "message": "时间跨度不能超过 1 个月",
  34. })
  35. return
  36. }
  37. dates, err := model.GetQuotaDataByUserId(userId, startTimestamp, endTimestamp)
  38. if err != nil {
  39. common.ApiError(c, err)
  40. return
  41. }
  42. c.JSON(http.StatusOK, gin.H{
  43. "success": true,
  44. "message": "",
  45. "data": dates,
  46. })
  47. return
  48. }