From 172e8c823521dcebea8e33d0c7ce2d50277079ad Mon Sep 17 00:00:00 2001 From: wood Date: Sat, 18 Nov 2023 05:31:56 +0800 Subject: [PATCH] 1 --- README.md | 3 +- controller/model.go | 9 ---- web/src/components/ChannelsTable.js | 69 ++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2c251037..c6aecbb0 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,13 @@ - [x] 当用户充值达到5刀时,自动提升为vip分组; - [x] 修改颜色 -- [x] 日志页面新增快速筛选日期、修复渠道ID查询、自动更新消耗额度、修改日志详情; +- [x] 日志页面新增快速筛选日期、修复渠道ID查询、自动更新消耗额度、修改日志详情(改为单价); - [x] 用户管理界面,支持快速设置用户组,在`web\src\components\UsersTable.js`处修改相关值; - [x] Key界面,删除无用的多种复制、聊天等按钮; - [x] 删除系统访问Key; - [x] 在个人设置页面显示分组,使用信息等; - [x] 前端全部以美金展示; +- [x] 渠道表,增加 本月已用、总已用、代理url、模型; ---

中文 | English | 日本語 diff --git a/controller/model.go b/controller/model.go index 675b8f87..59ea22e8 100644 --- a/controller/model.go +++ b/controller/model.go @@ -72,15 +72,6 @@ func init() { Root: "dall-e-3", Parent: nil, }, - { - Id: "dall-e-3", - Object: "model", - Created: 1677649963, - OwnedBy: "openai", - Permission: permission, - Root: "dall-e-3", - Parent: nil, - }, { Id: "whisper-1", Object: "model", diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index a46db717..3ccf7962 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -57,18 +57,61 @@ const ChannelsTable = () => { const [searching, setSearching] = useState(false); const [updatingBalance, setUpdatingBalance] = useState(false); const [showPrompt, setShowPrompt] = useState(shouldShowPrompt("channel-test")); + const [monthlyQuotas, setMonthlyQuotas] = useState({}); + // 获取本月的开始和结束时间戳 + function getMonthStartAndEndTimestamps() { + const now = new Date(); + const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1); + const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999); // 设置到月末的最后一刻 + + // 将日期转换为UNIX时间戳(秒数) + const startTimestamp = Math.floor(startOfMonth.getTime() / 1000); + const endTimestamp = Math.floor(endOfMonth.getTime() / 1000); + + return { startTimestamp, endTimestamp }; + } + + // 获取本月的配额 + const fetchMonthlyQuotasAndChannels = async (fetchedChannels) => { + const { startTimestamp, endTimestamp } = getMonthStartAndEndTimestamps(); + + const quotaRequests = fetchedChannels.map(channel => ( + API.get(`/api/log/stat?type=0&start_timestamp=${startTimestamp}&end_timestamp=${endTimestamp}&channel=${channel.id}`) + )); + + try { + const quotaResponses = await Promise.all(quotaRequests); + const quotaPerUnit = localStorage.getItem('quota_per_unit') || 500000; + const newMonthlyQuotas = quotaResponses.reduce((acc, response, index) => { + const quota = (response.data.data.quota / quotaPerUnit).toFixed(3); + const channelId = fetchedChannels[index].id; + acc[channelId] = parseFloat(quota); + return acc; + }, {}); + + setMonthlyQuotas(newMonthlyQuotas); + } catch (error) { + console.error('获取月度配额失败:', error); + } + }; + + // 加载频道列表 const loadChannels = async (startIdx) => { const res = await API.get(`/api/channel/?p=${startIdx}`); const { success, message, data } = res.data; if (success) { + const fetchedChannels = data; + if (startIdx === 0) { - setChannels(data); + setChannels(fetchedChannels); } else { let newChannels = [...channels]; - newChannels.splice(startIdx * ITEMS_PER_PAGE, data.length, ...data); + newChannels.splice(startIdx * ITEMS_PER_PAGE, fetchedChannels.length, ...fetchedChannels); setChannels(newChannels); } + + fetchMonthlyQuotasAndChannels(fetchedChannels); // 在这里调用函数 } else { showError(message); } @@ -97,6 +140,9 @@ const ChannelsTable = () => { showError(reason); }); }, []); + + + const manageChannel = async (id, action, idx, value) => { let data = { id }; @@ -304,17 +350,22 @@ const ChannelsTable = () => { setLoading(false); }; + // Truncate string function truncateString(str, num) { if (str.length <= num) return str; return str.slice(0, num) + "..."; } - + // 总已用额度 function formatUsedQuota(usedQuota) { const quotaPerUnit = localStorage.getItem('quota_per_unit') || 500000; // 如果未设置,则使用 1 作为默认值 - return `$${(usedQuota / quotaPerUnit).toFixed(2)}`; + return `$${(usedQuota / quotaPerUnit).toFixed(3)}`; } + + + + return ( <>

@@ -401,7 +452,7 @@ const ChannelsTable = () => { sortChannel('used_quota'); }} > - 已用额度 + 本月已用额度 { basic /> - {formatUsedQuota(channel.used_quota)} + + ${monthlyQuotas[channel.id]}} + content={`总: ${formatUsedQuota(channel.used_quota)}`} + basic + /> + {