From 7adea7d162bffa25f85c85230720436d14d06abd Mon Sep 17 00:00:00 2001
From: TimeTrapzz <55572425+TimeTrapzz@users.noreply.github.com>
Date: Sun, 22 Sep 2024 16:31:26 +0800
Subject: [PATCH] feat: add siliconflow usage (#1798)
---
controller/channel-billing.go | 44 +++++++++++++++++++
.../src/views/Channel/component/TableRow.js | 2 +
web/default/src/components/ChannelsTable.js | 2 +
3 files changed, 48 insertions(+)
diff --git a/controller/channel-billing.go b/controller/channel-billing.go
index 535927444e..a6ffaafe7c 100644
--- a/controller/channel-billing.go
+++ b/controller/channel-billing.go
@@ -81,6 +81,26 @@ type APGC2DGPTUsageResponse struct {
TotalUsed float64 `json:"total_used"`
}
+type SiliconFlowUsageResponse struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Status bool `json:"status"`
+ Data struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ Image string `json:"image"`
+ Email string `json:"email"`
+ IsAdmin bool `json:"isAdmin"`
+ Balance string `json:"balance"`
+ Status string `json:"status"`
+ Introduction string `json:"introduction"`
+ Role string `json:"role"`
+ ChargeBalance string `json:"chargeBalance"`
+ TotalBalance string `json:"totalBalance"`
+ Category string `json:"category"`
+ } `json:"data"`
+}
+
// GetAuthHeader get auth header
func GetAuthHeader(token string) http.Header {
h := http.Header{}
@@ -203,6 +223,28 @@ func updateChannelAIGC2DBalance(channel *model.Channel) (float64, error) {
return response.TotalAvailable, nil
}
+func updateChannelSiliconFlowBalance(channel *model.Channel) (float64, error) {
+ url := "https://api.siliconflow.cn/v1/user/info"
+ body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
+ if err != nil {
+ return 0, err
+ }
+ response := SiliconFlowUsageResponse{}
+ err = json.Unmarshal(body, &response)
+ if err != nil {
+ return 0, err
+ }
+ if response.Code != 20000 {
+ return 0, fmt.Errorf("code: %d, message: %s", response.Code, response.Message)
+ }
+ balance, err := strconv.ParseFloat(response.Data.Balance, 64)
+ if err != nil {
+ return 0, err
+ }
+ channel.UpdateBalance(balance)
+ return balance, nil
+}
+
func updateChannelBalance(channel *model.Channel) (float64, error) {
baseURL := channeltype.ChannelBaseURLs[channel.Type]
if channel.GetBaseURL() == "" {
@@ -227,6 +269,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
return updateChannelAPI2GPTBalance(channel)
case channeltype.AIGC2D:
return updateChannelAIGC2DBalance(channel)
+ case channeltype.SiliconFlow:
+ return updateChannelSiliconFlowBalance(channel)
default:
return 0, errors.New("尚未实现")
}
diff --git a/web/berry/src/views/Channel/component/TableRow.js b/web/berry/src/views/Channel/component/TableRow.js
index 2a7b9c7faa..3114479d9c 100644
--- a/web/berry/src/views/Channel/component/TableRow.js
+++ b/web/berry/src/views/Channel/component/TableRow.js
@@ -268,6 +268,8 @@ function renderBalance(type, balance) {
return ¥{balance.toFixed(2)};
case 13: // AIGC2D
return {renderNumber(balance)};
+ case 44: // SiliconFlow
+ return ¥{balance.toFixed(2)};
default:
return 不支持;
}
diff --git a/web/default/src/components/ChannelsTable.js b/web/default/src/components/ChannelsTable.js
index 6025b7d9a6..48f476a7b8 100644
--- a/web/default/src/components/ChannelsTable.js
+++ b/web/default/src/components/ChannelsTable.js
@@ -52,6 +52,8 @@ function renderBalance(type, balance) {
return ¥{balance.toFixed(2)};
case 13: // AIGC2D
return {renderNumber(balance)};
+ case 44: // SiliconFlow
+ return ¥{balance.toFixed(2)};
default:
return 不支持;
}