Skip to content

Commit e100826

Browse files
committed
fix: get balance (#370) @FSpark
1 parent 8b39926 commit e100826

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/popup/sections/GeneralPart.jsx

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,55 @@ GeneralPart.propTypes = {
2424
}
2525

2626
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')
3030

31-
return `${year}-${month}-${day}`;
31+
return `${year}-${month}-${day}`
3232
}
3333

3434
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)
3939
subDate.setDate(1)
4040

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)}`
4345
const headers = {
44-
"Authorization": "Bearer " + apiKey,
45-
"Content-Type": "application/json"
46-
};
46+
Authorization: 'Bearer ' + apiKey,
47+
'Content-Type': 'application/json',
48+
}
4749

4850
try {
49-
let response = await fetch(urlSubscription, { headers });
51+
let response = await fetch(urlSubscription, { headers })
5052
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]
5355
}
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
5658

5759
if (totalAmount > 20) {
58-
startDate = subDate;
60+
startDate = subDate
5961
}
6062

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)}`
6266

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
6771

68-
return [totalAmount, totalUsage, remaining];
72+
return [totalAmount, totalUsage, remaining]
6973
} catch (error) {
70-
console.error(error);
71-
return [null, null, null];
74+
console.error(error)
75+
return [null, null, null]
7276
}
7377
}
7478

@@ -78,7 +82,7 @@ export function GeneralPart({ config, updateConfig }) {
7882

7983
const getBalance = async () => {
8084
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)}`)
8286
else openUrl('https://platform.openai.com/account/usage')
8387
}
8488

0 commit comments

Comments
 (0)