@@ -9,7 +9,7 @@ import { getCacheConfig, getOriginConfig } from '../storage/config'
9
9
import { sendResponse } from '../utils'
10
10
import { isNotEmptyString } from '../utils/is'
11
11
import type { ApiModel , ChatContext , ChatGPTUnofficialProxyAPIOptions , ModelConfig } from '../types'
12
- import type { BalanceResponse , RequestOptions } from './types'
12
+ import type { RequestOptions } from './types'
13
13
14
14
const { HttpsProxyAgent } = httpsProxyAgent
15
15
@@ -126,6 +126,10 @@ async function chatReplyProcess(options: RequestOptions) {
126
126
127
127
async function fetchBalance ( ) {
128
128
// 计算起始日期和结束日期
129
+ const now = new Date ( ) . getTime ( )
130
+ const startDate = new Date ( now - 90 * 24 * 60 * 60 * 1000 )
131
+ const endDate = new Date ( now + 24 * 60 * 60 * 1000 )
132
+
129
133
const config = await getCacheConfig ( )
130
134
const OPENAI_API_KEY = config . apiKey
131
135
const OPENAI_API_BASE_URL = config . apiBaseUrl
@@ -137,36 +141,49 @@ async function fetchBalance() {
137
141
? OPENAI_API_BASE_URL
138
142
: 'https://api.openai.com'
139
143
140
- const [ startDate , endDate ] = formatDate ( )
141
-
142
- // 每月使用量
143
- const urlUsage = `${ API_BASE_URL } /v1/dashboard/billing/usage?start_date=${ startDate } &end_date=${ endDate } `
144
+ // 查是否订阅
145
+ const urlSubscription = `${ API_BASE_URL } /v1/dashboard/billing/subscription`
146
+ // 查普通账单
147
+ // const urlBalance = `${API_BASE_URL}/dashboard/billing/credit_grants`
148
+ // 查使用量
149
+ const urlUsage = `${ API_BASE_URL } /v1/dashboard/billing/usage?start_date=${ formatDate ( startDate ) } &end_date=${ formatDate ( endDate ) } `
144
150
145
151
const headers = {
146
152
'Authorization' : `Bearer ${ OPENAI_API_KEY } ` ,
147
153
'Content-Type' : 'application/json' ,
148
154
}
149
155
150
156
try {
157
+ // 获取API限额
158
+ let response = await fetch ( urlSubscription , { headers } )
159
+ if ( ! response . ok ) {
160
+ console . error ( '您的账户已被封禁,请登录OpenAI进行查看。' )
161
+ return
162
+ }
163
+ const subscriptionData = await response . json ( )
164
+ const totalAmount = subscriptionData . hard_limit_usd
165
+
151
166
// 获取已使用量
152
- const useResponse = await fetch ( urlUsage , { headers } )
153
- const usageData = await useResponse . json ( ) as BalanceResponse
154
- const usage = Math . round ( usageData . total_usage ) / 100
155
- return Promise . resolve ( usage ? `$${ usage } ` : '-' )
167
+ response = await fetch ( urlUsage , { headers } )
168
+ const usageData = await response . json ( )
169
+ const totalUsage = usageData . total_usage / 100
170
+
171
+ // 计算剩余额度
172
+ const balance = totalAmount - totalUsage
173
+
174
+ return Promise . resolve ( balance . toFixed ( 3 ) )
156
175
}
157
176
catch {
158
177
return Promise . resolve ( '-' )
159
178
}
160
179
}
161
180
162
- function formatDate ( ) : string [ ] {
163
- const today = new Date ( )
164
- const year = today . getFullYear ( )
165
- const month = today . getMonth ( ) + 1
166
- const lastDay = new Date ( year , month , 0 )
167
- const formattedFirstDay = `${ year } -${ month . toString ( ) . padStart ( 2 , '0' ) } -01`
168
- const formattedLastDay = `${ year } -${ month . toString ( ) . padStart ( 2 , '0' ) } -${ lastDay . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) } `
169
- return [ formattedFirstDay , formattedLastDay ]
181
+ function formatDate ( date ) {
182
+ const year = date . getFullYear ( )
183
+ const month = ( date . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' )
184
+ const day = date . getDate ( ) . toString ( ) . padStart ( 2 , '0' )
185
+
186
+ return `${ year } -${ month } -${ day } `
170
187
}
171
188
172
189
async function chatConfig ( ) {
0 commit comments