2023-05-22 09:10:31 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-06-20 12:09:17 +00:00
|
|
|
"one-api/common"
|
2023-05-22 09:10:31 +00:00
|
|
|
"one-api/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetSubscription(c *gin.Context) {
|
|
|
|
userId := c.GetInt("id")
|
|
|
|
quota, err := model.GetUserQuota(userId)
|
|
|
|
if err != nil {
|
|
|
|
openAIError := OpenAIError{
|
|
|
|
Message: err.Error(),
|
|
|
|
Type: "one_api_error",
|
|
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"error": openAIError,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2023-06-20 12:09:17 +00:00
|
|
|
amount := float64(quota)
|
|
|
|
if common.DisplayInCurrencyEnabled {
|
|
|
|
amount /= common.QuotaPerUnit
|
|
|
|
}
|
2023-05-22 09:10:31 +00:00
|
|
|
subscription := OpenAISubscriptionResponse{
|
|
|
|
Object: "billing_subscription",
|
|
|
|
HasPaymentMethod: true,
|
2023-06-20 12:09:17 +00:00
|
|
|
SoftLimitUSD: amount,
|
|
|
|
HardLimitUSD: amount,
|
|
|
|
SystemHardLimitUSD: amount,
|
2023-05-22 09:10:31 +00:00
|
|
|
}
|
|
|
|
c.JSON(200, subscription)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUsage(c *gin.Context) {
|
2023-06-20 12:09:17 +00:00
|
|
|
userId := c.GetInt("id")
|
|
|
|
quota, err := model.GetUserUsedQuota(userId)
|
|
|
|
if err != nil {
|
|
|
|
openAIError := OpenAIError{
|
|
|
|
Message: err.Error(),
|
|
|
|
Type: "one_api_error",
|
|
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"error": openAIError,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
amount := float64(quota)
|
2023-06-20 13:05:07 +00:00
|
|
|
if common.DisplayInCurrencyEnabled {
|
|
|
|
amount /= common.QuotaPerUnit
|
|
|
|
}
|
2023-05-22 09:10:31 +00:00
|
|
|
usage := OpenAIUsageResponse{
|
|
|
|
Object: "list",
|
2023-06-20 12:09:17 +00:00
|
|
|
TotalUsage: amount,
|
2023-05-22 09:10:31 +00:00
|
|
|
}
|
|
|
|
c.JSON(200, usage)
|
|
|
|
return
|
|
|
|
}
|