@@ -497,9 +497,7 @@ async function generatePlusResponse(message,adminuserName) {
const replacements = [
{ target: '', replacement: errorHtml + '' },
- // 在这里添加更多的替换规则
{ target: '', replacement: `` },
- // 继续添加替换规则
];
const html = await getPlusHTML();
@@ -513,14 +511,14 @@ async function generatePlusResponse(message,adminuserName) {
}
async function getPlusHTML() {
- const WorkerURL=await KV.get('WorkerURL');
- const turnstileSiteKey=await KV.get('TurnstileSiteKey');
+ const WorkerURL = await KV.get('WorkerURL');
+ const turnstileSiteKey = await KV.get('TurnstileSiteKey');
return `
-
Token Management
+ Token Management
@@ -626,15 +624,15 @@ async function getPlusHTML() {
+ });
+
@@ -643,6 +641,7 @@ document.getElementById('managePlus').addEventListener('submit', function(event)
+
//admin页面
async function handleAdminPostRequest(request) {
const formData = await request.formData();
@@ -1076,27 +1075,58 @@ async function handleQueryRequest(accessToken, shareToken) {
return `Usage: GPT-4: ${limits.gpt4Limit}, GPT-3.5: ${limits.gpt35Limit}`;
}
+
+
async function queryLimits(accessToken, shareToken) {
+ const CACHE_TTL = 60; // 缓存时间,单位为秒
+ const MAX_RETRIES = 3; // 最大重试次数
+ const cacheKey = `limits_${shareToken}`;
+ const cachedData = await KV.get(cacheKey);
+ if (cachedData) {
+ return JSON.parse(cachedData);
+ }
+
const url = `https://chat.oaifree.com/token/info/${shareToken}`;
- const response = await fetch(url, {
- method: 'GET',
- headers: {
- 'Authorization': `Bearer ${accessToken}`,
- 'Content-Type': 'application/json'
- }
- });
+ let attempt = 0;
- if (!response.ok) {
- throw new Error('Failed to fetch limits');
- }
+ while (attempt < MAX_RETRIES) {
+ try {
+ const response = await fetch(url, {
+ method: 'GET',
+ headers: {
+ 'Authorization': `Bearer ${accessToken}`,
+ 'Content-Type': 'application/json'
+ }
+ });
- const result = await response.json();
- return {
- gpt4Limit: result.gpt4_limit,
- gpt35Limit: result.gpt35_limit
- };
+ if (!response.ok) {
+ throw new Error(`Failed to fetch limits (status: ${response.status})`);
+ }
+
+ const result = await response.json();
+ const data = {
+ gpt4Limit: result.gpt4_limit,
+ gpt35Limit: result.gpt35_limit
+ };
+
+ await KV.put(cacheKey, JSON.stringify(data), { expirationTtl: CACHE_TTL }); // 缓存数据
+ return data;
+
+ } catch (error) {
+ console.error(`Attempt ${attempt + 1} failed:`, error);
+ attempt += 1;
+
+ if (attempt >= MAX_RETRIES) {
+ throw new Error('Failed to fetch limits after multiple attempts');
+ }
+
+ // 可选:在重试前等待一段时间
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ }
+ }
}
+
async function getUserHTML() {
const turnstileSiteKey=await KV.get('TurnstileSiteKey');
return `
@@ -1748,9 +1778,10 @@ async function getRegisterHTML() {
-//Usage查询功能
-const MAX_USERS_PER_BATCH = 10;
+
+//Usage查询功能
+const MAX_USERS_PER_BATCH = 5;
async function handleUsageRequest(request) {
if (request.method === 'POST') {
const url = new URL(request.url);
@@ -1795,7 +1826,7 @@ async function handleUsageRequest(request) {
const htmlResponse = await generateTableHTML(usersData, queryType);
return new Response(htmlResponse, { headers: { 'Content-Type': 'text/html' } });
} else {
- const accountNumber = await getToCheckAccountNumber(adminUsername,'Plus');
+ const accountNumber = await getTableToCheckAccountNumber(adminUsername);
const accessToken = await KV.get(`at_${accountNumber}`) || '1';
const shareToken = await getToCheckShareToken(adminUsername,accessToken);
const queryLimit = await handleQueryRequest(accessToken,shareToken);
@@ -1808,23 +1839,35 @@ async function handleUsageRequest(request) {
}
async function processBatchUsers(users, queryType) {
- let usersData = [];
- for (const user of users) {
- const accountNumber = await getTableToCheckAccountNumber(user);
- const accessToken = await KV.get(`at_${accountNumber}`) || '1';
- const shareToken = await getToCheckShareToken(user, accessToken);
- const usage = await queryLimits(accessToken, shareToken);
-
- usersData.push({
+ const usersData = await Promise.all(users.map(user => processSingleUser(user, queryType).catch(error => {
+ console.error(`Error processing user ${user}:`, error);
+ return {
user,
- accountNumber,
+ accountNumber: 'Unknown',
queryType,
- ...parseUsage(usage)
- });
- }
+ gpt4: 'Error',
+ gpt35: 'Error'
+ };
+ })));
return usersData;
}
+async function processSingleUser(user, queryType) {
+ const accountNumber = await getTableToCheckAccountNumber(user);
+ const accessToken = await KV.get(`at_${accountNumber}`) || '1';
+ const shareToken = await getToCheckShareToken(user, accessToken);
+ const usage = await queryLimits(accessToken, shareToken);
+
+ return {
+ user,
+ accountNumber,
+ queryType,
+ ...parseUsage(usage)
+ };
+}
+
+
+
function parseUsage(usage) {
return {
gpt4: usage.gpt4Limit !== undefined ? usage.gpt4Limit : 'N/A',
@@ -1994,7 +2037,7 @@ async function generateUsageResponse(message) {
async function generateTableHTML(usersData, queryType) {
const logourl = await KV.get('LogoURL');
- const pageTitle = "Usage Chart";
+ const pageTitle = "Usage Chart";
const historyData = await getHistoryData(queryType);
let combinedData = combineData(usersData, historyData);
@@ -2288,7 +2331,7 @@ if (turnstileResponse !== 'do not need Turnstle' && (!turnstileResponse || !awai
const proxiedDomain = await KV.get('WorkerURL');
const status = await KV.get('Status');
const GPTState = await getGPTStatus();
-if ((GPTState !== 'operational')&&(!status)){
+if ((GPTState == 'major_performance')&&(!status)){
await loginlog(userName, 'Bad_OAIStatus','Error');
return generateLoginResponse(`OpenAI service is under maintenance.
Official status: ${GPTState}
More details: https://status.openai.com`);
}