usedata.go 1.3 KB

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