billing.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "one-api/common"
  5. "one-api/model"
  6. )
  7. func GetSubscription(c *gin.Context) {
  8. userId := c.GetInt("id")
  9. quota, err := model.GetUserQuota(userId)
  10. if err != nil {
  11. openAIError := OpenAIError{
  12. Message: err.Error(),
  13. Type: "one_api_error",
  14. }
  15. c.JSON(200, gin.H{
  16. "error": openAIError,
  17. })
  18. return
  19. }
  20. amount := float64(quota)
  21. if common.DisplayInCurrencyEnabled {
  22. amount /= common.QuotaPerUnit
  23. }
  24. subscription := OpenAISubscriptionResponse{
  25. Object: "billing_subscription",
  26. HasPaymentMethod: true,
  27. SoftLimitUSD: amount,
  28. HardLimitUSD: amount,
  29. SystemHardLimitUSD: amount,
  30. }
  31. c.JSON(200, subscription)
  32. return
  33. }
  34. func GetUsage(c *gin.Context) {
  35. userId := c.GetInt("id")
  36. quota, err := model.GetUserUsedQuota(userId)
  37. if err != nil {
  38. openAIError := OpenAIError{
  39. Message: err.Error(),
  40. Type: "one_api_error",
  41. }
  42. c.JSON(200, gin.H{
  43. "error": openAIError,
  44. })
  45. return
  46. }
  47. amount := float64(quota)
  48. usage := OpenAIUsageResponse{
  49. Object: "list",
  50. TotalUsage: amount,
  51. }
  52. c.JSON(200, usage)
  53. return
  54. }