From faae6b95d4006ae8e955a725d679ac2896e6cb98 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 15 Sep 2026 17:32:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E3=80=8C=E6=88=91=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E3=80=8D=E9=9D=A2=E6=9D=BF=20me.html=20?= =?UTF-8?q?=E2=80=94=E2=80=94=20=E7=94=A8=E6=88=B7=E8=87=AA=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=20(1.0.30)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基于 1.0.29 `/api/v1/me` 新增 recent 块,每个用户可以**自助**看自己的 用量,不必 admin 帮忙——也省得同事自己 curl 拼 JSON。 **面板能力**(来自 `/api/v1/me`): - 6 张概览卡片:用户名 / 24h 调用 / 7d 调用 / 24h token / 上次调用 - 24h 调用分布:bar chart(来自 `recent.hour_24h`,按本地小时分桶) - 近 7 天按 slot 拆分表格:calls_24h / calls_7d / tin / tout / last_status - 我能调的 LLM 上游:白名单过滤后的完整 slot + base_url + models - 我能调的 API 代理:enabled 列表 + 调用方式(`GET /apix/...` 或 `POST /apixp/...`) + desc 截前 80 字 - 我的域名列表 **鉴权**: - 用户首次进入 → 输入 sk_ → sessionStorage 存 → 调 `/me` → 渲染 - 不能复用 `IP.api`(那个走 cookie)—— 新加 `meApi` helper 走 `X-API-Key` - Key 只在 sessionStorage,**关页面即清**,不会落盘不会上传 - 401 自动清 Key 跳回输 Key 卡片 - 顶部 nav 加「我的用量」入口:admin 也能用自己的 sk_ 查 **安全考虑**: - 用户拿 sk_ 看自己:API key 天然按 username 隔离,**不能看别人** ✓ - 面板只读,没有任何写操作 回归 108/0 全绿(gateway 108,含 me.html 200 断言)。 --- tests/gateway/e2e.sh | 1 + www/assets/core.js | 3 +- www/assets/me.js | 231 +++++++++++++++++++++++++++++++++++++++++++ www/me.html | 120 ++++++++++++++++++++++ 4 files changed, 354 insertions(+), 1 deletion(-) create mode 100644 www/assets/me.js create mode 100644 www/me.html diff --git a/tests/gateway/e2e.sh b/tests/gateway/e2e.sh index 86eafef..10154f5 100755 --- a/tests/gateway/e2e.sh +++ b/tests/gateway/e2e.sh @@ -292,6 +292,7 @@ ck "账号页 → 200" 200 "$(code -b "$JAR" $ADMIN/admin/users.html)" ck "上游页 → 200" 200 "$(code -b "$JAR" $ADMIN/admin/upstreams.html)" ck "用量页 → 200" 200 "$(code -b "$JAR" $ADMIN/admin/usage.html)" ck "24h 热力页 → 200(1.0.27 直接读 meta.jsonl 聚合)" 200 "$(code -b "$JAR" $ADMIN/admin/hourly.html)" +ck "我的用量页 → 200(1.0.30 账号态面板)" 200 "$(code $ADMIN/admin/me.html)" ck "登录页 → 200" 200 "$(code $ADMIN/admin/login.html)" ck "共享样式 → 200" 200 "$(code $ADMIN/admin/assets/admin.css)" ck "共享内核 → 200" 200 "$(code $ADMIN/admin/assets/core.js)" diff --git a/www/assets/core.js b/www/assets/core.js index 5a9ab49..443c0e2 100644 --- a/www/assets/core.js +++ b/www/assets/core.js @@ -20,7 +20,8 @@ window.IP = (function () { { key: "upstreams", name: "LLM 上游", href: "upstreams.html" }, { key: "apix", name: "API 代理", href: "apix.html" }, { key: "usage", name: "用量统计", href: "usage.html" }, - { key: "hourly", name: "24h 热力", href: "hourly.html" } + { key: "hourly", name: "24h 热力", href: "hourly.html" }, + { key: "me", name: "我的用量", href: "me.html" } ]; var state = { diff --git a/www/assets/me.js b/www/assets/me.js new file mode 100644 index 0000000..e22669e --- /dev/null +++ b/www/assets/me.js @@ -0,0 +1,231 @@ +/* 账号态「我的用量」面板(1.0.30) + 输入 API Key → 调 /api/v1/me → 渲染概览 / 24h 小时分布 / 按 slot 拆分 / + 我能调的上游 + apix + 我的域名。 + Key 只在 sessionStorage,关页面即清。 +*/ +(function () { + "use strict"; + var KEY_STORE = "ipass.me.key"; + var $ = IP.$; + + function esc(s) { return IP.esc(s); } + + function setKey(k) { + if (k) sessionStorage.setItem(KEY_STORE, k); + else sessionStorage.removeItem(KEY_STORE); + } + + function getKey() { + return sessionStorage.getItem(KEY_STORE) || ""; + } + + /** 走 X-API-Key 头,调 API。不能用 IP.api(那个走 cookie)。 + * 401 直接 throw 让 UI 显出重新输入 Key。 */ + function meApi(method, path, body) { + var key = getKey(); + if (!key) return Promise.reject(new Error("未设置 API Key")); + var opt = { method: method, headers: { "X-API-Key": key } }; + if (body) { + opt.headers["Content-Type"] = "application/json"; + opt.body = JSON.stringify(body); + } + return fetch(path, opt).then(function (res) { + return res.json().catch(function () { return { code: res.status, msg: "响应解析失败" }; }) + .then(function (data) { + if (!res.ok) throw new Error(data.msg || ("请求失败 HTTP " + res.status)); + return data; + }); + }); + } + + function showKeyCard() { + $("keyCard").hidden = false; + $("dashCard").hidden = true; + $("apiKey").focus(); + } + + function showDash() { + $("keyCard").hidden = true; + $("dashCard").hidden = false; + } + + function renderTopbar(me) { + $("topbar").innerHTML = + '
' + + '
尚数网 山海关个人用量面板
' + + '
' + + '' + esc(me.username) + " · " + + (me.role === "admin" ? "管理员" : "账号") + "" + + '' + + '' + + "
" + + "
"; + if (me.role === "admin") { + $("topbarAdminBtn").addEventListener("click", function () { + // 跳 admin login(cookie 鉴权不走 me Key) + location.href = "login.html"; + }); + } + $("topbarLogoutBtn").addEventListener("click", function () { + setKey(""); + showKeyCard(); + $("apiKey").value = ""; + }); + } + + function renderHourly(hour24) { + var el = $("dHourChart"); + IP.chart.bars(el, { + labels: hour24.map(function (_, i) { return i; }), + items: [{ name: "调用数", values: hour24.map(function (v) { return Number(v) || 0; }) }], + emptyText: "过去 24 小时没有调用记录" + }); + } + + function renderSlots(bySlot) { + var body = $("dSlotBody"); + if (!bySlot || !bySlot.length) { + body.innerHTML = '过去 7 天没有调用记录'; + return; + } + body.innerHTML = bySlot.map(function (s) { + var lastStatus = s.last_status || 0; + var badgeClass = lastStatus >= 200 && lastStatus < 300 ? "b-ok" + : lastStatus >= 400 && lastStatus < 500 ? "b-warn" + : lastStatus >= 500 ? "b-bad" : "b-gray"; + return '' + + '' + esc(s.slot) + '' + + '' + (s.calls_24h || 0) + '' + + '' + (s.calls_7d || 0) + '' + + '' + IP.chart.fmtNum(s.tin_7d || 0) + '' + + '' + IP.chart.fmtNum(s.tout_7d || 0) + '' + + '' + lastStatus + '' + + ''; + }).join(""); + } + + function renderUpstreams(upstreams) { + var el = $("dUpstream"); + if (!upstreams || !upstreams.length) { + el.innerHTML = '当前账号无可用上游(可能白名单为空,或全部 slot 被停用)'; + return; + } + el.innerHTML = '' + + upstreams.map(function (u) { + return '' + + '' + + '' + + '' + + ''; + }).join("") + '
slotbase_urlmodels
' + esc(u.slot || "") + '' + esc(u.base_url || "") + '' + ((u.models || []).map(esc).join("、") || "—") + '
'; + } + + function renderApix(apix) { + var el = $("dApix"); + if (!apix || !apix.length) { + el.innerHTML = '当前账号无可用 API 代理'; + return; + } + el.innerHTML = '' + + '' + + '' + + apix.map(function (a) { + var mode = a.mode || "vendor"; + var call = mode === "passthrough" + ? 'POST /apixp/' + esc(a.slug) + '/<tail>' + : 'GET /apix/' + esc(a.slug) + '?参数=值'; + return '' + + '' + + '' + + '' + + '' + + ''; + }).join("") + '
slug用途调用方式说明
' + esc(a.slug || "") + '' + esc(a.display_name || a.url || "—") + '' + call + '' + esc((a.desc || a.usage_hint || "").slice(0, 80)) + '
'; + } + + function renderDomains(domains) { + var el = $("dDomains"); + if (!domains || !domains.length) { + el.innerHTML = '当前账号没有域名(需要去"全部域名"页申请)'; + return; + } + el.innerHTML = '' + + '' + + '' + + domains.map(function (d) { + return '' + + '' + + '' + + '' + + '' + + ''; + }).join("") + '
子域名后端状态创建时间
' + esc(d.full || d.subdomain || "—") + '' + esc(d.backend || "—") + '' + + (d.enabled ? "启用" : "停用") + '' + esc(d.created_at_text || "") + '
'; + } + + function renderDash(me) { + renderTopbar(me); + $("dUser").textContent = me.username || "-"; + var r = me.recent || {}; + $("dCalls24").textContent = r.calls_24h || 0; + $("dCalls7").textContent = r.calls_7d || 0; + $("dTin24").textContent = IP.chart.fmtNum(r.tin_24h || 0); + $("dTout24").textContent = IP.chart.fmtNum(r.tout_24h || 0); + $("dLast").textContent = r.last_at + ? esc(r.last_at + " · " + (r.last_slot || "-")) + : "—"; + renderHourly(r.hour_24h || []); + renderSlots(r.by_slot || []); + renderUpstreams(me.upstreams || []); + renderApix(me.apix || []); + renderDomains(me.domains || []); + } + + function loadDash() { + IP.toast("加载中…"); + meApi("GET", "/api/v1/me").then(function (resp) { + var me = resp.data || {}; + if (!me.username) { + IP.toast("接口返回异常:缺 username", true); + showKeyCard(); + return; + } + renderDash(me); + showDash(); + IP.toast("已加载"); + }).catch(function (err) { + if (err.message && err.message.indexOf("HTTP 401") >= 0) { + setKey(""); + IP.toast("Key 无效或已过期,请重新输入", true); + showKeyCard(); + $("apiKey").value = ""; + $("apiKey").focus(); + } else { + IP.toast("加载失败:" + (err.message || err), true); + } + }); + } + + function bind() { + $("keyForm").addEventListener("submit", function (e) { + e.preventDefault(); + var k = $("apiKey").value.trim(); + if (!k) { IP.toast("请输入 API Key", true); return; } + setKey(k); + loadDash(); + }); + $("refreshBtn").addEventListener("click", loadDash); + } + + function boot() { + bind(); + if (getKey()) loadDash(); + else showKeyCard(); + } + + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", boot); + } else { + boot(); + } +})(); \ No newline at end of file diff --git a/www/me.html b/www/me.html new file mode 100644 index 0000000..55fcb68 --- /dev/null +++ b/www/me.html @@ -0,0 +1,120 @@ + + + + + + +我的用量 · 尚数网 山海关 + + + +
+ +
+ +
+
+
+

用 API Key 看我的用量

+
+ 输入账号的 sk_xxx 即可查询本账号的最近调用、消耗、能用的上游/代理等数据。 + Key 只在浏览器 sessionStorage,关页面即清,不会落盘也不会上传。 +
+
+
+ +
+ + + +
+ +
+ + + + + + \ No newline at end of file