Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4e9d8f1c
authored
Jun 21, 2025
by
Calcium-Ion
Committed by
GitHub
Jun 21, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1160 from feitianbubu/add-moonshot-kimi-update-balance
feat: add moonshot(kimi) update balance
parents
3170f26a
3e3b1d0a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
0 deletions
+38
-0
controller/channel-billing.go
+38
-0
No files found.
controller/channel-billing.go
View file @
4e9d8f1c
...
...
@@ -4,11 +4,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/shopspring/decimal"
"io"
"net/http"
"one-api/common"
"one-api/model"
"one-api/service"
"one-api/setting"
"strconv"
"time"
...
...
@@ -304,6 +306,40 @@ func updateChannelOpenRouterBalance(channel *model.Channel) (float64, error) {
return
balance
,
nil
}
func
updateChannelMoonshotBalance
(
channel
*
model
.
Channel
)
(
float64
,
error
)
{
url
:=
"https://api.moonshot.cn/v1/users/me/balance"
body
,
err
:=
GetResponseBody
(
"GET"
,
url
,
channel
,
GetAuthHeader
(
channel
.
Key
))
if
err
!=
nil
{
return
0
,
err
}
type
MoonshotBalanceData
struct
{
AvailableBalance
float64
`json:"available_balance"`
VoucherBalance
float64
`json:"voucher_balance"`
CashBalance
float64
`json:"cash_balance"`
}
type
MoonshotBalanceResponse
struct
{
Code
int
`json:"code"`
Data
MoonshotBalanceData
`json:"data"`
Scode
string
`json:"scode"`
Status
bool
`json:"status"`
}
response
:=
MoonshotBalanceResponse
{}
err
=
json
.
Unmarshal
(
body
,
&
response
)
if
err
!=
nil
{
return
0
,
err
}
if
!
response
.
Status
||
response
.
Code
!=
0
{
return
0
,
fmt
.
Errorf
(
"failed to update moonshot balance, status: %v, code: %d, scode: %s"
,
response
.
Status
,
response
.
Code
,
response
.
Scode
)
}
availableBalanceCny
:=
response
.
Data
.
AvailableBalance
availableBalanceUsd
:=
decimal
.
NewFromFloat
(
availableBalanceCny
)
.
Div
(
decimal
.
NewFromFloat
(
setting
.
Price
))
.
InexactFloat64
()
channel
.
UpdateBalance
(
availableBalanceUsd
)
return
availableBalanceUsd
,
nil
}
func
updateChannelBalance
(
channel
*
model
.
Channel
)
(
float64
,
error
)
{
baseURL
:=
common
.
ChannelBaseURLs
[
channel
.
Type
]
if
channel
.
GetBaseURL
()
==
""
{
...
...
@@ -332,6 +368,8 @@ func updateChannelBalance(channel *model.Channel) (float64, error) {
return
updateChannelDeepSeekBalance
(
channel
)
case
common
.
ChannelTypeOpenRouter
:
return
updateChannelOpenRouterBalance
(
channel
)
case
common
.
ChannelTypeMoonshot
:
return
updateChannelMoonshotBalance
(
channel
)
default
:
return
0
,
errors
.
New
(
"尚未实现"
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment