@@ -24,51 +24,55 @@ GeneralPart.propTypes = {
24
24
}
25
25
26
26
function formatDate ( date ) {
27
- const year = date . getFullYear ( ) ;
28
- const month = ( date . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' ) ;
29
- const day = date . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) ;
27
+ const year = date . getFullYear ( )
28
+ const month = ( date . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' )
29
+ const day = date . getDate ( ) . toString ( ) . padStart ( 2 , '0' )
30
30
31
- return `${ year } -${ month } -${ day } ` ;
31
+ return `${ year } -${ month } -${ day } `
32
32
}
33
33
34
34
async function checkBilling ( apiKey , apiUrl ) {
35
- const now = new Date ( ) ;
36
- let startDate = new Date ( now - 90 * 24 * 60 * 60 * 1000 ) ;
37
- const endDate = new Date ( now . getTime ( ) + 24 * 60 * 60 * 1000 ) ;
38
- const subDate = new Date ( now ) ;
35
+ const now = new Date ( )
36
+ let startDate = new Date ( now - 90 * 24 * 60 * 60 * 1000 )
37
+ const endDate = new Date ( now . getTime ( ) + 24 * 60 * 60 * 1000 )
38
+ const subDate = new Date ( now )
39
39
subDate . setDate ( 1 )
40
40
41
- const urlSubscription = `${ apiUrl } /v1/dashboard/billing/subscription` ;
42
- let urlUsage = `${ apiUrl } /v1/dashboard/billing/usage?start_date=${ formatDate ( startDate ) } &end_date=${ formatDate ( endDate ) } ` ; // 查使用量
41
+ const urlSubscription = `${ apiUrl } /v1/dashboard/billing/subscription`
42
+ let urlUsage = `${ apiUrl } /v1/dashboard/billing/usage?start_date=${ formatDate (
43
+ startDate ,
44
+ ) } &end_date=${ formatDate ( endDate ) } `
43
45
const headers = {
44
- " Authorization" : " Bearer " + apiKey ,
45
- " Content-Type" : " application/json"
46
- } ;
46
+ Authorization : ' Bearer ' + apiKey ,
47
+ ' Content-Type' : ' application/json' ,
48
+ }
47
49
48
50
try {
49
- let response = await fetch ( urlSubscription , { headers } ) ;
51
+ let response = await fetch ( urlSubscription , { headers } )
50
52
if ( ! response . ok ) {
51
- console . log ( " Your account has been suspended. Please log in to OpenAI to check." ) ;
52
- return ;
53
+ console . log ( ' Your account has been suspended. Please log in to OpenAI to check.' )
54
+ return [ null , null , null ]
53
55
}
54
- const subscriptionData = await response . json ( ) ;
55
- const totalAmount = subscriptionData . hard_limit_usd ;
56
+ const subscriptionData = await response . json ( )
57
+ const totalAmount = subscriptionData . hard_limit_usd
56
58
57
59
if ( totalAmount > 20 ) {
58
- startDate = subDate ;
60
+ startDate = subDate
59
61
}
60
62
61
- urlUsage = `${ apiUrl } /v1/dashboard/billing/usage?start_date=${ formatDate ( startDate ) } &end_date=${ formatDate ( endDate ) } ` ;
63
+ urlUsage = `${ apiUrl } /v1/dashboard/billing/usage?start_date=${ formatDate (
64
+ startDate ,
65
+ ) } &end_date=${ formatDate ( endDate ) } `
62
66
63
- response = await fetch ( urlUsage , { headers } ) ;
64
- const usageData = await response . json ( ) ;
65
- const totalUsage = usageData . total_usage / 100 ;
66
- const remaining = totalAmount - totalUsage ;
67
+ response = await fetch ( urlUsage , { headers } )
68
+ const usageData = await response . json ( )
69
+ const totalUsage = usageData . total_usage / 100
70
+ const remaining = totalAmount - totalUsage
67
71
68
- return [ totalAmount , totalUsage , remaining ] ;
72
+ return [ totalAmount , totalUsage , remaining ]
69
73
} catch ( error ) {
70
- console . error ( error ) ;
71
- return [ null , null , null ] ;
74
+ console . error ( error )
75
+ return [ null , null , null ]
72
76
}
73
77
}
74
78
@@ -78,7 +82,7 @@ export function GeneralPart({ config, updateConfig }) {
78
82
79
83
const getBalance = async ( ) => {
80
84
const billing = await checkBilling ( config . apiKey , config . customOpenAiApiUrl )
81
- if ( billing [ 2 ] ) setBalance ( `${ billing [ 2 ] . toFixed ( 2 ) } ` )
85
+ if ( billing && billing . length > 2 && billing [ 2 ] ) setBalance ( `${ billing [ 2 ] . toFixed ( 2 ) } ` )
82
86
else openUrl ( 'https://platform.openai.com/account/usage' )
83
87
}
84
88
0 commit comments