balance.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package public
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/go-resty/resty/v2"
  7. )
  8. func InitAiCli() *resty.Client {
  9. if Config.HttpProxy != "" {
  10. return resty.New().SetTimeout(10*time.Second).SetHeader("Authorization", fmt.Sprintf("Bearer %s", Config.ApiKey)).SetProxy(Config.HttpProxy).SetRetryCount(3).SetRetryWaitTime(2 * time.Second)
  11. }
  12. return resty.New().SetTimeout(10*time.Second).SetHeader("Authorization", fmt.Sprintf("Bearer %s", Config.ApiKey)).SetRetryCount(3).SetRetryWaitTime(2 * time.Second)
  13. }
  14. type Bill struct {
  15. Object string `json:"object"`
  16. DailyCosts []DailyCost `json:"daily_costs"`
  17. TotalUsage float64 `json:"total_usage"`
  18. }
  19. type DailyCost struct {
  20. Timestamp float64 `json:"timestamp"`
  21. LineItems []LineItem `json:"line_items"`
  22. }
  23. type LineItem struct {
  24. Name string `json:"name"`
  25. Cost float64 `json:"cost"`
  26. }
  27. // GetBalance 获取账号余额
  28. func GetBalance() (string, error) {
  29. var data Bill
  30. path := "/v1/dashboard/billing/usage"
  31. var url string = "https://api.openai.com" + path
  32. if Config.BaseURL != "" {
  33. url = Config.BaseURL + path
  34. }
  35. d, _ := time.ParseDuration("-24h")
  36. resp, err := InitAiCli().R().SetQueryParams(map[string]string{
  37. "start_date": time.Now().Add(d * 90).Format("2006-01-02"),
  38. "end_date": time.Now().Format("2006-01-02"),
  39. }).Get(url)
  40. if err != nil {
  41. return "", err
  42. }
  43. err = json.Unmarshal(resp.Body(), &data)
  44. if err != nil {
  45. return "", err
  46. }
  47. sub, err := GetSub()
  48. if err != nil {
  49. return "", err
  50. }
  51. expireDate := time.Unix(sub.AccessUntil, 0).Format("2006-01-02 15:04:05")
  52. used := data.TotalUsage / 100
  53. totalAvailable := sub.HardLimitUsd - used
  54. msg := fmt.Sprintf("💵 已用: 💲%v\n💵 剩余: 💲%v\n🕰 到期时间: %v", fmt.Sprintf("%.2f", used), fmt.Sprintf("%.2f", totalAvailable), expireDate)
  55. // 放入缓存
  56. UserService.SetUserMode("system_balance", msg)
  57. return msg, nil
  58. }
  59. type Subscription struct {
  60. Object string `json:"object"`
  61. HasPaymentMethod bool `json:"has_payment_method"`
  62. Canceled bool `json:"canceled"`
  63. CanceledAt interface{} `json:"canceled_at"`
  64. Delinquent interface{} `json:"delinquent"`
  65. AccessUntil int64 `json:"access_until"`
  66. SoftLimit int64 `json:"soft_limit"`
  67. HardLimit int64 `json:"hard_limit"`
  68. SystemHardLimit int64 `json:"system_hard_limit"`
  69. SoftLimitUsd float64 `json:"soft_limit_usd"`
  70. HardLimitUsd float64 `json:"hard_limit_usd"`
  71. SystemHardLimitUsd float64 `json:"system_hard_limit_usd"`
  72. Plan Plan `json:"plan"`
  73. AccountName string `json:"account_name"`
  74. PoNumber interface{} `json:"po_number"`
  75. BillingEmail interface{} `json:"billing_email"`
  76. TaxIDS interface{} `json:"tax_ids"`
  77. BillingAddress interface{} `json:"billing_address"`
  78. BusinessAddress interface{} `json:"business_address"`
  79. }
  80. type Plan struct {
  81. Title string `json:"title"`
  82. ID string `json:"id"`
  83. }
  84. func GetSub() (Subscription, error) {
  85. var data Subscription
  86. path := "/v1/dashboard/billing/subscription"
  87. var url string = "https://api.openai.com" + path
  88. if Config.BaseURL != "" {
  89. url = Config.BaseURL + path
  90. }
  91. resp, err := InitAiCli().R().Get(url)
  92. if err != nil {
  93. return data, err
  94. }
  95. err = json.Unmarshal(resp.Body(), &data)
  96. if err != nil {
  97. return data, err
  98. }
  99. return data, nil
  100. }