Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1187d88

Browse files
fix: 修复API余额查询 (Chanzhaoyu#1174)
* fix: 使API余额查询可用* chore: 调整计算方式* perf: 余额描述变更---------Co-authored-by: ChenZhaoYu <790348264@qq.com>
1 parentb07f01b commit1187d88

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

‎service/src/chatgpt/index.ts‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
55
import{SocksProxyAgent}from'socks-proxy-agent'
66
importhttpsProxyAgentfrom'https-proxy-agent'
77
importfetchfrom'node-fetch'
8-
importaxiosfrom'axios'
98
import{sendResponse}from'../utils'
109
import{isNotEmptyString}from'../utils/is'
1110
importtype{ApiModel,ChatContext,ChatGPTUnofficialProxyAPIOptions,ModelConfig}from'../types'
12-
importtype{RequestOptions}from'./types'
11+
importtype{BalanceResponse,RequestOptions}from'./types'
1312

1413
const{ HttpsProxyAgent}=httpsProxyAgent
1514

@@ -126,6 +125,8 @@ async function chatReplyProcess(options: RequestOptions) {
126125
}
127126

128127
asyncfunctionfetchBalance(){
128+
// 计算起始日期和结束日期
129+
129130
constOPENAI_API_KEY=process.env.OPENAI_API_KEY
130131
constOPENAI_API_BASE_URL=process.env.OPENAI_API_BASE_URL
131132

@@ -136,17 +137,38 @@ async function fetchBalance() {
136137
?OPENAI_API_BASE_URL
137138
:'https://api.openai.com'
138139

140+
const[startDate,endDate]=formatDate()
141+
142+
// 每月使用量
143+
consturlUsage=`${API_BASE_URL}/v1/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`
144+
145+
constheaders={
146+
'Authorization':`Bearer${OPENAI_API_KEY}`,
147+
'Content-Type':'application/json',
148+
}
149+
139150
try{
140-
constheaders={'Content-Type':'application/json','Authorization':`Bearer${OPENAI_API_KEY}`}
141-
constresponse=awaitaxios.get(`${API_BASE_URL}/dashboard/billing/credit_grants`,{ headers})
142-
constbalance=response.data.total_available??0
143-
returnPromise.resolve(balance.toFixed(3))
151+
// 获取已使用量
152+
constuseResponse=awaitfetch(urlUsage,{ headers})
153+
constusageData=awaituseResponse.json()asBalanceResponse
154+
constusage=Math.round(usageData.total_usage)/100
155+
returnPromise.resolve(usage ?`$${usage}` :'-')
144156
}
145157
catch{
146158
returnPromise.resolve('-')
147159
}
148160
}
149161

162+
functionformatDate():string[]{
163+
consttoday=newDate()
164+
constyear=today.getFullYear()
165+
constmonth=today.getMonth()+1
166+
constlastDay=newDate(year,month,0)
167+
constformattedFirstDay=`${year}-${month.toString().padStart(2,'0')}-01`
168+
constformattedLastDay=`${year}-${month.toString().padStart(2,'0')}-${lastDay.getDate().toString().padStart(2,'0')}`
169+
return[formattedFirstDay,formattedLastDay]
170+
}
171+
150172
asyncfunctionchatConfig(){
151173
constbalance=awaitfetchBalance()
152174
constreverseProxy=process.env.API_REVERSE_PROXY??'-'

‎service/src/chatgpt/types.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ export interface RequestOptions {
66
process?:(chat:ChatMessage)=>void
77
systemMessage?:string
88
}
9+
10+
exportinterfaceBalanceResponse{
11+
total_usage:number
12+
}

‎src/components/common/Setting/About.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ onMounted(() => {
6363
<p>{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}</p>
6464
<pv-if="isChatGPTAPI">
6565
{{ $t("setting.balance") }}:{{ config?.balance ?? '-' }}
66+
<spanclass="text-xs text-neutral-400">({{ $t('setting.monthlyUsage') }})</span>
6667
</p>
6768
<pv-if="!isChatGPTAPI">
6869
{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }}

‎src/locales/en-US.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default {
6969
socks:'Socks',
7070
httpsProxy:'HTTPS Proxy',
7171
balance:'API Balance',
72+
monthlyUsage:'Monthly Usage',
7273
},
7374
store:{
7475
siderButton:'Prompt Store',

‎src/locales/zh-CN.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default {
6969
socks:'Socks',
7070
httpsProxy:'HTTPS Proxy',
7171
balance:'API余额',
72+
monthlyUsage:'本月使用量',
7273
},
7374
store:{
7475
siderButton:'提示词商店',

‎src/locales/zh-TW.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export default {
6969
socks:'Socks',
7070
httpsProxy:'HTTPS Proxy',
7171
balance:'API余額',
72+
monthlyUsage:'本月使用量',
7273
},
7374
store:{
7475
siderButton:'提示詞商店',

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp