Files
Sub-Router/dist/assets/Tokens-CVIOu66Q.js
T
abingyydsandClaude Sonnet 4.6 782439995f feat: 分站 API Key 支持随时查看复制 + 配置文件导出器
- 后端:DistTokenList 去掉 Clean(),返回完整 key 供前端显示
- 前端:Token 列表每行显示完整 key 和复制按钮
- 新增 ConfigExporter 组件,支持 7 种工具配置生成
  (Claude Code / OpenClaw / OpenCode / Cursor / cURL / Python / Anthropic SDK)
- 自动使用分站域名作为 API Base URL
- 中英文 i18n 翻译

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 22:54:52 +09:00

74 lines
16 KiB
JavaScript

import{r as o,e as K,f as E,j as e,z as k,h as I,i as z,k as B}from"./index-DXlTUvPt.js";import{u as D}from"./useTranslation-nOWDYxQJ.js";const P=()=>{var L;const{t}=D(),[b,H]=o.useState([]),[d,y]=o.useState(null),[$,m]=o.useState([]),[l,j]=o.useState(""),[x,S]=o.useState("claudecode"),[T,N]=o.useState(!0),[v,w]=o.useState(!1),c=window.location.origin,u=[{id:"claudecode",name:"Claude Code",path:"~/.claude/settings.json"},{id:"openclaw",name:"OpenClaw",path:"~/.openclaw/openclaw.json"},{id:"opencode",name:"OpenCode",path:"~/.config/opencode/opencode.json"},{id:"cursor",name:"Cursor",path:"Settings → Models → OpenAI API Key"},{id:"curl",name:"cURL",path:"Terminal"},{id:"python",name:"Python SDK",path:"main.py"},{id:"anthropic",name:"Anthropic SDK",path:"main.py"}];o.useEffect(()=>{h()},[]),o.useEffect(()=>{d&&M(d)},[d]);const h=async()=>{N(!0);try{const s=await K();if(s.data.success){const n=s.data.data||[];H(n),n.length>0&&y(n[0])}}catch{}N(!1)},M=async s=>{try{if(s.model_limits_enabled&&s.model_limits){const r=s.model_limits.split(",").filter(p=>p.trim());m(r),r.length>0&&j(r[0]);return}const n=await E();if(n.data.success&&n.data.data){const p=(Array.isArray(n.data.data)?n.data.data:n.data.data.items||[]).map(f=>f.model_name||f.id||f);m(p),p.length>0&&j(p[0])}}catch{}},_=s=>{const n=s.toLowerCase();return n.includes("claude")?{provider:"anthropic",api:"anthropic-messages"}:n.includes("gpt")||n.includes("o1")||n.includes("o3")?{provider:"openai",api:"openai-chat"}:n.includes("gemini")?{provider:"google",api:"google-chat"}:{provider:"openai",api:"openai-chat"}},C=()=>{if(!d||!l)return"";const s="sk-"+d.key;switch(x){case"claudecode":return`{
"env": {
"ANTHROPIC_API_KEY": "${s}",
"ANTHROPIC_BASE_URL": "${c}/",
"ANTHROPIC_MODEL": "${l}"
}
}`;case"openclaw":{const n=_(l);return`{
"provider": "${n.provider}",
"base_url": "${c}/",
"api": "${n.api}",
"api_key": "${s}",
"model": {
"id": "${l}",
"name": "${l}"
}
}`}case"opencode":return`{
"provider": {
"openai": {
"options": {
"baseURL": "${c}/v1",
"apiKey": "${s}"
},
"models": {
"${l}": {
"name": "${l}",
"options": {
"store": false
}
}
}
}
},
"$schema": "https://opencode.ai/config.json"
}`;case"cursor":return`API Key: ${s}
Base URL: ${c}/v1
Model: ${l}`;case"curl":return`curl ${c}/v1/chat/completions \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer ${s}" \\
-d '{
"model": "${l}",
"messages": [
{"role": "user", "content": "Hello!"}
]
}'`;case"python":return`from openai import OpenAI
client = OpenAI(
api_key="${s}",
base_url="${c}/v1"
)
response = client.chat.completions.create(
model="${l}",
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response.choices[0].message.content)`;case"anthropic":return`import anthropic
client = anthropic.Anthropic(
api_key="${s}",
base_url="${c}/"
)
message = client.messages.create(
model="${l}",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(message.content[0].text)`;default:return""}},A=()=>{var s;switch(x){case"curl":return"api-call.sh";case"python":return"main.py";case"anthropic":return"main.py";case"cursor":return"cursor-config.txt";default:return((s=u.find(n=>n.id===x))==null?void 0:s.path.split("/").pop())||"config.json"}},a=async()=>{const s=C();try{await navigator.clipboard.writeText(s)}catch{const r=document.createElement("textarea");r.value=s,r.style.position="fixed",r.style.opacity="0",document.body.appendChild(r),r.select(),document.execCommand("copy"),document.body.removeChild(r)}w(!0),k.success(t("config.copied")),setTimeout(()=>w(!1),2e3)},g=()=>{const s=C(),n=A(),r=new Blob([s],{type:"text/plain"}),p=URL.createObjectURL(r),f=document.createElement("a");f.href=p,f.download=n,f.click(),URL.revokeObjectURL(p),k.success(t("config.downloaded"))};if(T)return e.jsxs("div",{className:"glass rounded-2xl p-6 text-center",children:[e.jsx("div",{className:"w-8 h-8 border-2 border-brand-500/30 border-t-brand-500 rounded-full animate-spin mx-auto mb-2"}),e.jsx("p",{className:"text-sm text-page-secondary",children:t("config.loading")})]});if(b.length===0)return e.jsxs("div",{className:"glass rounded-2xl p-6 text-center",children:[e.jsx("svg",{className:"w-8 h-8 mx-auto mb-3 text-page-muted",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"})}),e.jsx("p",{className:"text-sm text-page-secondary mb-1",children:t("config.loginPrompt")}),e.jsx("p",{className:"text-xs text-page-muted",children:t("config.loginDesc")})]});const i=C();return e.jsxs("div",{className:"glass rounded-2xl overflow-hidden",children:[e.jsxs("div",{className:"px-5 py-4 border-b border-white/5",children:[e.jsxs("h4",{className:"font-semibold text-sm text-page flex items-center gap-2",children:[e.jsx("svg",{className:"w-4 h-4 text-brand-400",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"})}),t("config.title")]}),e.jsx("p",{className:"text-xs text-page-muted mt-1",children:t("config.subtitle")})]}),e.jsxs("div",{className:"p-5 space-y-4",children:[e.jsxs("div",{children:[e.jsxs("label",{className:"flex items-center gap-2 text-xs font-medium text-page-label mb-2",children:[e.jsx("svg",{className:"w-3 h-3",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"})}),t("config.selectKey")]}),e.jsx("select",{value:(d==null?void 0:d.id)||"",onChange:s=>{const n=b.find(r=>r.id===parseInt(s.target.value));y(n)},className:"input",children:b.map(s=>e.jsxs("option",{value:s.id,children:[s.name," (sk-",s.key.substring(0,16),"...)"]},s.id))})]}),$.length>0&&e.jsxs("div",{children:[e.jsxs("label",{className:"flex items-center gap-2 text-xs font-medium text-page-label mb-2",children:[e.jsx("svg",{className:"w-3 h-3",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"})}),t("config.selectModel")]}),e.jsx("select",{value:l,onChange:s=>j(s.target.value),className:"input",children:$.map(s=>e.jsx("option",{value:s,children:s},s))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-medium text-page-label mb-2 block",children:t("config.selectTool")}),e.jsx("div",{className:"flex flex-wrap gap-2",children:u.map(s=>e.jsx("button",{onClick:()=>S(s.id),className:`px-3 py-1.5 rounded-lg text-xs font-medium transition-all ${x===s.id?"bg-brand-500 text-white shadow-sm":"bg-white/5 text-page-secondary hover:bg-white/10"}`,children:s.name},s.id))})]})]}),e.jsxs("div",{className:"border-t border-white/5",children:[e.jsxs("div",{className:"flex items-center justify-between px-4 py-2.5 bg-black/20",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex gap-1.5",children:[e.jsx("span",{className:"w-2.5 h-2.5 rounded-full bg-red-400"}),e.jsx("span",{className:"w-2.5 h-2.5 rounded-full bg-yellow-400"}),e.jsx("span",{className:"w-2.5 h-2.5 rounded-full bg-green-400"})]}),e.jsx("span",{className:"text-xs text-page-muted ml-1",children:(L=u.find(s=>s.id===x))==null?void 0:L.path})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{onClick:a,className:"p-1.5 rounded-md hover:bg-white/10 transition-colors text-page-muted hover:text-page",title:t("config.copy"),children:v?e.jsx("svg",{className:"w-3.5 h-3.5 text-green-400",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M5 13l4 4L19 7"})}):e.jsx("svg",{className:"w-3.5 h-3.5",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"})})}),e.jsx("button",{onClick:g,className:"p-1.5 rounded-md hover:bg-white/10 transition-colors text-page-muted hover:text-page",title:t("config.download"),children:e.jsx("svg",{className:"w-3.5 h-3.5",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:2,d:"M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"})})})]})]}),e.jsx("pre",{className:"p-4 text-xs leading-relaxed overflow-x-auto max-h-64 font-mono text-green-400",children:e.jsx("code",{children:i})})]})]})};function W(){const{t}=D(),[b,H]=o.useState([]),[d,y]=o.useState(!0),[$,m]=o.useState(!1),[l,j]=o.useState(""),[x,S]=o.useState(!1),[T,N]=o.useState(null),[v,w]=o.useState(null),[c,u]=o.useState(null),h=o.useCallback(async()=>{y(!0);try{const a=await K();a.data.success&&H(a.data.data||[])}catch{}y(!1)},[]);o.useEffect(()=>{h()},[h]);const M=async a=>{var g;if(a.preventDefault(),!l.trim()){k.error(t("tokens.enterName"));return}S(!0);try{const i=await I({name:l.trim()});if(i.data.success){j(""),m(!1);const L=(g=i.data.data)==null?void 0:g.key;L&&w(L),await h()}}catch{}S(!1)},_=async a=>{try{(await z(a.id,{status:a.status===1?2:1})).data.success&&(k.success(a.status===1?t("tokens.tokenDisabled"):t("tokens.tokenEnabled")),await h())}catch{}},C=async()=>{if(c)try{(await B(c.id)).data.success&&(k.success(t("tokens.tokenDeleted")),u(null),await h())}catch{}},A=async a=>{try{await navigator.clipboard.writeText(a)}catch{const i=document.createElement("textarea");i.value=a,i.style.position="fixed",i.style.opacity="0",document.body.appendChild(i),i.select(),document.execCommand("copy"),document.body.removeChild(i)}N(a),k.success(t("tokens.copiedToClipboard")),setTimeout(()=>N(null),2e3)};return d?e.jsx("div",{className:"flex items-center justify-center min-h-[50vh]",children:e.jsx("div",{className:"w-8 h-8 border-2 border-brand-500/30 border-t-brand-500 rounded-full animate-spin"})}):e.jsxs("div",{className:"max-w-5xl mx-auto px-6 py-10",children:[e.jsxs("div",{className:"flex items-center justify-between mb-8",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-heading font-bold text-page",children:t("tokens.title")}),e.jsx("p",{className:"text-sm text-page-secondary mt-1",children:t("tokens.subtitle")})]}),e.jsx("button",{onClick:()=>m(!0),className:"btn-primary",children:t("tokens.newKey")})]}),$&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm",onClick:()=>m(!1),children:e.jsxs("div",{className:"glass rounded-2xl p-6 w-full max-w-md",onClick:a=>a.stopPropagation(),children:[e.jsx("h2",{className:"text-lg font-semibold text-page mb-4",children:t("tokens.createApiKey")}),e.jsxs("form",{onSubmit:M,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-page-label mb-1.5",children:t("tokens.name")}),e.jsx("input",{type:"text",value:l,onChange:a=>j(a.target.value),className:"input",placeholder:t("tokens.namePlaceholder"),autoFocus:!0,required:!0})]}),e.jsxs("div",{className:"flex justify-end gap-3",children:[e.jsx("button",{type:"button",onClick:()=>m(!1),className:"btn-secondary",children:t("tokens.cancel")}),e.jsx("button",{type:"submit",disabled:x,className:"btn-primary",children:t(x?"tokens.creating":"tokens.create")})]})]})]})}),v&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm",children:e.jsxs("div",{className:"glass rounded-2xl p-6 w-full max-w-lg",children:[e.jsx("h2",{className:"text-lg font-semibold text-page mb-2",children:t("tokens.newApiKey")}),e.jsx("div",{className:"bg-yellow-500/10 border border-yellow-500/20 rounded-xl p-3 mb-4",children:e.jsx("p",{className:"text-sm text-yellow-300",children:t("tokens.keyWarning")})}),e.jsxs("div",{className:"bg-black/40 rounded-xl p-4 flex items-center gap-3",children:[e.jsx("code",{className:"text-sm font-mono text-green-400 flex-1 break-all select-all",children:v}),e.jsx("button",{onClick:()=>A(v),className:"btn-primary !px-4 !py-1.5 flex-shrink-0",children:t(T===v?"tokens.copied":"tokens.copy")})]}),e.jsx("div",{className:"flex justify-end mt-4",children:e.jsx("button",{onClick:()=>w(null),className:"btn-secondary",children:t("tokens.savedKey")})})]})}),c&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm",onClick:()=>u(null),children:e.jsxs("div",{className:"glass rounded-2xl p-6 w-full max-w-sm",onClick:a=>a.stopPropagation(),children:[e.jsx("h2",{className:"text-lg font-semibold text-page mb-3",children:t("tokens.deleteToken")}),e.jsx("p",{className:"text-sm text-page-secondary mb-4",children:t("tokens.deleteConfirm",{name:c.name})}),e.jsxs("div",{className:"flex justify-end gap-3",children:[e.jsx("button",{onClick:()=>u(null),className:"btn-secondary",children:t("tokens.cancel")}),e.jsx("button",{onClick:C,className:"px-6 py-2.5 rounded-xl bg-red-600 text-white text-sm font-medium hover:bg-red-500 transition-colors",children:t("tokens.delete")})]})]})}),b.length===0?e.jsxs("div",{className:"glass rounded-2xl p-12 text-center",children:[e.jsx("div",{className:"w-16 h-16 mx-auto mb-4 rounded-2xl bg-neutral-800/50 flex items-center justify-center",children:e.jsx("svg",{className:"w-8 h-8 text-neutral-600",fill:"none",viewBox:"0 0 24 24",stroke:"currentColor",children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"})})}),e.jsx("p",{className:"text-page-secondary mb-4",children:t("tokens.noKeys")}),e.jsx("button",{onClick:()=>m(!0),className:"btn-primary",children:t("tokens.createFirst")})]}):e.jsx("div",{className:"space-y-3",children:b.map(a=>e.jsxs("div",{className:"glass-sm rounded-xl p-5",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:`w-2.5 h-2.5 rounded-full flex-shrink-0 ${a.status===1?"bg-green-500":"bg-neutral-600"}`}),e.jsx("div",{className:"flex-1 min-w-0",children:e.jsx("p",{className:"text-sm font-medium text-page",children:a.name})}),e.jsx("span",{className:"text-xs text-page-muted hidden md:block",children:a.created_time?new Date(a.created_time*1e3).toLocaleDateString():""}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("button",{onClick:()=>_(a),className:`px-3 py-1 text-xs rounded-lg border transition-colors ${a.status===1?"border-green-500/30 text-green-400 hover:bg-green-500/10":"border-neutral-600 text-page-secondary hover:bg-neutral-800"}`,children:a.status===1?t("tokens.enabled"):t("tokens.disabled")}),e.jsx("button",{onClick:()=>u(a),className:"px-3 py-1 text-xs rounded-lg border border-red-500/20 text-red-400 hover:bg-red-500/10 transition-colors",children:t("tokens.delete")})]})]}),a.key&&e.jsxs("div",{className:"mt-3 flex items-center gap-2 bg-black/30 rounded-lg px-3 py-2",children:[e.jsxs("code",{className:"text-xs font-mono text-page-muted flex-1 break-all select-all",children:["sk-",a.key]}),e.jsx("button",{onClick:()=>A("sk-"+a.key),className:"flex-shrink-0 px-2.5 py-1 text-xs rounded-md bg-white/10 text-page-secondary hover:bg-white/20 hover:text-page transition-colors",children:T==="sk-"+a.key?t("tokens.copied"):t("tokens.copy")})]})]},a.id))}),e.jsx("div",{className:"mt-8",children:e.jsx(P,{})})]})}export{W as default};