2 lines
19 KiB
JavaScript
2 lines
19 KiB
JavaScript
import{a as r,j as e}from"./react-vendor-Bf9ELcll.js";import{r as M,c as R}from"./chat-metadata-74T0w35U.js";import{n as de,S as xe}from"./settings-D-mx9vFJ.js";import{h as ue,R as ae,ad as pe,v as me,J as D,N as O,ae as he,x as ge,b as re,af as le,ag as ye,s as fe}from"./icons-vendor-BSp7PTZg.js";import"./vendor-BE-_xUHH.js";const ne="https://dashscope.aliyuncs.com/compatible-mode/v1",be=[{value:"debug",label:"Debug"},{value:"info",label:"Info"},{value:"warn",label:"Warn"},{value:"error",label:"Error"}],we=new Set(["ai_api_key_configured","smtp_password_configured","qq_reply_secret_key_configured","captcha.remote_secret_key_configured","smtp_server","smtp_port","smtp_user","smtp_password","smtp_from","smtp_from_name","smtp_from_address","registration_enabled","show_default_login_info","login_captcha_enabled","item_sync_enabled","item_sync_interval","item_sync_max_pages","default_reply"]),je=s=>{const a={...s};for(const o of xe){const d=`${o}_configured`;d in a&&(a[d]=a[d]===!0||a[d]==="true")}if("renewal_log_retention_days"in a){const o=Number(a.renewal_log_retention_days);a.renewal_log_retention_days=Number.isFinite(o)?o:10}return a},ve=async(s,a)=>M(o=>R.PUT("/api/v1/session/credentials",{body:s,signal:o}),a),Ne=async s=>{const a=await M(o=>R.GET("/api/v1/settings/system",{signal:o}),s);return je(a)},_e=async(s,a)=>M(o=>R.PUT("/api/v1/settings/system",{body:de(s),signal:o}),a),Se=async(s,a="",o)=>{const d=await M(_=>R.POST("/api/v1/settings/ai-models",{body:{base_url:s,api_key:a},signal:_}),o);return Array.isArray(d.models)?d.models:[]},Ce=async s=>M(a=>R.GET("/api/v1/session",{signal:a}),s),ke=s=>Object.fromEntries(Object.entries(s).filter(([a,o])=>!we.has(a)&&o!==void 0&&o!==null)),Ee=s=>s.new_username.trim().length<3?"用户名至少需要 3 个字符":s.current_password?s.new_password&&s.new_password.length<8?"新密码至少需要 8 个字符":s.new_password!==s.confirm_password?"两次输入的新密码不一致":"":"请输入当前密码确认身份",y=(s,a,o)=>s===a&&!o.aborted,K=s=>s instanceof Error&&s.message==="请求已取消",U=(s,a)=>s instanceof Error?s.message:a,Ae=(s="")=>({new_username:s,current_password:"",new_password:"",confirm_password:""}),$=(s,a)=>({type:s,text:a}),Ie=()=>{const[s,a]=r.useState(null),[o,d]=r.useState(!1),[_,p]=r.useState(""),[S,A]=r.useState(!1),[L,C]=r.useState(""),[k,b]=r.useState([]),[w,I]=r.useState(!1),[P,x]=r.useState(""),[G,j]=r.useState(!1),[z,T]=r.useState(!1),[F,u]=r.useState(!1),[f,J]=r.useState(!1),[V,Y]=r.useState(!1),[W,v]=r.useState(!1),[q,g]=r.useState(null),[t,ee]=r.useState(()=>Ae()),[oe,B]=r.useState("idle"),se=r.useRef(null),te=r.useRef(null),m=r.useRef(0),H=r.useRef(null);te.current=s;const E=r.useCallback(()=>{var c;(c=H.current)==null||c.abort();const n=new AbortController;return H.current=n,m.current+=1,{controller:n,sequence:m.current}},[]),Q=r.useCallback(async(n,c=!1,l)=>{const i=n||te.current,h=(i==null?void 0:i.ai_api_url)||(i==null?void 0:i.ai_base_url)||ne;I(!0),x("");try{const N=await Se(h,(i==null?void 0:i.ai_api_key)||"",{signal:l});if(l!=null&&l.aborted)return;b(N),j(c&&N.length>0),!(i!=null&&i.ai_model)&&N.length>0&&a(Z=>Z&&{...Z,ai_model:N[0]})}catch(N){if(l!=null&&l.aborted||K(N))return;b([]),j(!1),x(U(N,"读取模型失败"))}finally{l!=null&&l.aborted||I(!1)}},[]),X=r.useCallback(()=>{const{controller:n,sequence:c}=E();d(!0),B("loading"),p(""),Promise.all([Ne({signal:n.signal}),Ce({signal:n.signal})]).then(([l,i])=>{y(m.current,c,n.signal)&&(a(l),i.username&&ee(h=>({...h,new_username:i.username||""})),Q(l,!1,n.signal),B("success"))}).catch(l=>{!y(m.current,c,n.signal)||K(l)||(a(null),p(U(l,"加载配置失败")),B("error"))}).finally(()=>{y(m.current,c,n.signal)&&d(!1)})},[E,Q]);r.useEffect(()=>(X(),(()=>{var n;return(n=H.current)==null?void 0:n.abort()})),[X]),r.useEffect(()=>{const n=c=>{var l;(l=se.current)!=null&&l.contains(c.target)||j(!1)};return document.addEventListener("mousedown",n),(()=>document.removeEventListener("mousedown",n))},[]);const ce=r.useCallback(async()=>{if(!s||S)return;const{controller:n,sequence:c}=E();A(!0),C("");try{if(await _e(ke(s),{signal:n.signal}),!y(m.current,c,n.signal))return;window.alert("系统配置已保存")}catch(l){if(!y(m.current,c,n.signal)||K(l))return;C(U(l,"保存配置失败"))}finally{y(m.current,c,n.signal)&&A(!1)}},[E,S,s]),ie=r.useCallback(async n=>{n.preventDefault(),g(null);const c=Ee(t);if(c){g($("error",c));return}const{controller:l,sequence:i}=E();v(!0);try{const h=await ve({current_password:t.current_password,new_username:t.new_username.trim(),new_password:t.new_password||void 0},{signal:l.signal});if(!y(m.current,i,l.signal))return;if(!h.success){g($("error",h.message||"登录凭据更新失败"));return}g($("success",h.message||"登录凭据已更新")),window.setTimeout(()=>window.location.reload(),1400)}catch(h){if(!y(m.current,i,l.signal)||K(h))return;g($("error",U(h,"登录凭据更新失败")))}finally{y(m.current,i,l.signal)&&v(!1)}},[E,t]);return{settings:s,loading:o,loadError:_,saving:S,saveError:L,aiModels:k,modelsLoading:w,modelError:P,modelDropdownOpen:G,showApiKey:z,showCaptchaSecret:F,showCurrentPassword:f,showNewPassword:V,credentialsSaving:W,credentialsMessage:q,credentials:t,requestStatus:oe,modelPickerRef:se,loadSettings:X,loadAIModels:((n,c)=>void Q(n,c)),handleSave:ce,handleCredentialsSave:ie,setSettings:a,setModelDropdownOpen:j,setShowApiKey:T,setShowCaptchaSecret:u,setShowCurrentPassword:J,setShowNewPassword:Y,setCredentials:ee,setCredentialsMessage:g}},qe=()=>{const{settings:s,loading:a,loadError:o,saving:d,saveError:_,aiModels:p,modelsLoading:S,modelError:A,modelDropdownOpen:L,showApiKey:C,showCaptchaSecret:k,showCurrentPassword:b,showNewPassword:w,credentialsSaving:I,credentialsMessage:P,credentials:x,modelPickerRef:G,loadSettings:j,loadAIModels:z,handleSave:T,handleCredentialsSave:F,setSettings:u,setModelDropdownOpen:f,setShowApiKey:J,setShowCaptchaSecret:V,setShowCurrentPassword:Y,setShowNewPassword:W,setCredentials:v}=Ie();if(!s)return e.jsxs("div",{className:"p-8 text-center text-gray-400 space-y-3",children:[o||(a?"加载配置中...":"暂无配置"),!a&&o&&e.jsx("div",{children:e.jsx("button",{type:"button",className:"ios-btn-primary px-4 py-2 rounded-xl",onClick:j,children:"重新加载"})})]});const q=s.ai_model||"",g=p;return e.jsxs("div",{className:"max-w-6xl mx-auto space-y-8 animate-fade-in pb-24",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"w-12 h-12 bg-gray-100 rounded-2xl flex items-center justify-center",children:e.jsx(ue,{className:"w-6 h-6 text-gray-600"})}),e.jsxs("div",{children:[e.jsx("h2",{className:"text-3xl font-extrabold text-gray-900",children:"系统设置"}),e.jsx("p",{className:"text-gray-500 mt-1 text-sm font-medium",children:"配置全局自动化规则与系统参数"})]})]}),e.jsxs("button",{onClick:j,className:"px-4 py-2 bg-gray-100 hover:bg-gray-200 rounded-xl font-bold text-gray-700 flex items-center gap-2 transition-colors",children:[e.jsx(ae,{className:"w-4 h-4"}),"刷新"]})]}),_&&e.jsxs("div",{className:"flex items-center justify-between gap-3 rounded-xl border border-red-100 bg-red-50 px-4 py-3 text-sm text-red-700",children:[e.jsx("span",{children:_}),e.jsx("button",{type:"button",className:"font-bold underline",onClick:(()=>{}),children:"重试保存"})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-8",children:[e.jsxs("div",{className:"space-y-8",children:[e.jsxs("section",{className:"space-y-4",children:[e.jsxs("h3",{className:"text-lg font-extrabold text-gray-800 flex items-center gap-2",children:[e.jsx("div",{className:"p-1.5 rounded-lg bg-gray-100 text-gray-600",children:e.jsx(pe,{className:"w-4 h-4"})}),"基础设置"]}),e.jsxs("div",{className:"ios-card rounded-xl p-6 bg-white space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"日志输出等级"}),e.jsx("select",{value:s.log_level||"info",onChange:(t=>u({...s,log_level:t.target.value})),className:"w-full ios-input px-4 py-3 rounded-xl",children:be.map(t=>e.jsx("option",{value:t.value,children:t.label},t.value))}),e.jsx("p",{className:"text-xs text-gray-500",children:"等级越低输出越详细,Debug 适合排查问题"})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"日志输出格式"}),e.jsxs("select",{value:s.log_format||"text",onChange:(t=>u({...s,log_format:t.target.value})),className:"w-full ios-input px-4 py-3 rounded-xl",children:[e.jsx("option",{value:"text",children:"Text"}),e.jsx("option",{value:"json",children:"JSON"})]}),e.jsx("p",{className:"text-xs text-gray-500",children:"JSON 适合接入集中式日志系统,保存后需重启服务生效"})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"续期日志保留天数"}),e.jsx("input",{type:"number",value:s.renewal_log_retention_days??10,onChange:(t=>u({...s,renewal_log_retention_days:parseInt(t.target.value)||0})),className:"w-full ios-input px-4 py-3 rounded-xl",min:"0",max:"365"}),e.jsx("p",{className:"text-xs text-gray-500",children:"0 表示不自动清理续期日志"})]})]})]}),e.jsxs("section",{className:"space-y-4",children:[e.jsxs("h3",{className:"text-lg font-extrabold text-gray-800 flex items-center gap-2",children:[e.jsx("div",{className:"p-1.5 rounded-lg bg-brand text-white",children:e.jsx(me,{className:"w-4 h-4"})}),"AI 智能回复配置"]}),e.jsxs("div",{className:"ios-card rounded-xl p-6 bg-white space-y-6",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"API 地址"}),e.jsx("input",{type:"text",value:s.ai_api_url||ne,onChange:(t=>u({...s,ai_api_url:t.target.value})),className:"w-full ios-input px-4 py-3 rounded-xl text-sm",placeholder:"https://api.openai.com/v1"}),e.jsx("p",{className:"text-xs text-gray-500",children:"无需补全 /chat/completions"})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"API Key"}),e.jsxs("div",{className:"relative",children:[e.jsx("input",{type:C?"text":"password",value:s.ai_api_key||"",onChange:(t=>u({...s,ai_api_key:t.target.value})),className:"w-full ios-input px-4 py-3 pr-12 rounded-xl font-mono text-sm",placeholder:s.ai_api_key_configured?"已配置,如需替换请输入新密钥":"sk-..."}),e.jsx("button",{type:"button",onClick:(()=>J(!C)),className:"absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600 transition-colors",children:C?e.jsx(D,{className:"w-4 h-4"}):e.jsx(O,{className:"w-4 h-4"})})]})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"模型"}),e.jsxs("div",{ref:G,className:"relative flex flex-col sm:flex-row gap-2",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx("input",{value:q,onFocus:(()=>p.length>0&&f(!0)),onChange:(t=>{u({...s,ai_model:t.target.value}),p.length>0&&f(!0)}),onKeyDown:(t=>{t.key==="Escape"&&f(!1),t.key==="ArrowDown"&&p.length>0&&f(!0)}),className:"w-full ios-input px-4 py-3 pr-10 rounded-xl",placeholder:"从接口读取或手动输入模型名"}),e.jsx("button",{type:"button",onClick:(()=>p.length>0&&f(t=>!t)),disabled:p.length===0,className:"absolute right-2 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600 disabled:opacity-30","aria-label":"展开模型列表",children:e.jsx(he,{className:`w-4 h-4 transition-transform ${L?"rotate-180":""}`})}),L&&e.jsx("div",{className:"absolute left-0 right-0 top-[calc(100%+6px)] z-40 max-h-64 overflow-y-auto rounded-xl border border-gray-200 bg-white shadow-xl shadow-gray-200/70 py-1",children:g.length>0?g.map(t=>e.jsxs("button",{type:"button",onClick:(()=>{u({...s,ai_model:t}),f(!1)}),className:"w-full px-4 py-2.5 text-left text-sm text-gray-700 hover:bg-blue-50 hover:text-brand flex items-center justify-between gap-3",children:[e.jsx("span",{className:"truncate",children:t}),t===q&&e.jsx(ge,{className:"w-4 h-4 shrink-0 text-brand"})]},t)):e.jsx("div",{className:"px-4 py-3 text-sm text-gray-400",children:"没有匹配的模型"})})]}),e.jsxs("button",{type:"button",onClick:(()=>z(void 0,!0)),disabled:S,className:"px-4 py-3 rounded-xl bg-gray-100 text-gray-700 hover:bg-gray-200 disabled:opacity-60 font-bold flex items-center justify-center gap-2 whitespace-nowrap",children:[e.jsx(ae,{className:`w-4 h-4 ${S?"animate-spin":""}`}),"读取模型"]})]}),A?e.jsx("p",{className:"text-xs text-red-500",children:A}):e.jsx("p",{className:"text-xs text-gray-500",children:p.length>0?`已从当前 API 地址读取到 ${p.length} 个模型`:"模型列表从当前 API 地址读取,也可以手动输入模型名"})]}),e.jsxs("div",{className:"p-3 bg-blue-50 rounded-xl text-xs text-blue-700",children:[e.jsx("strong",{children:"常见 AI 服务:"}),e.jsxs("ul",{className:"list-disc list-inside mt-1 space-y-0.5",children:[e.jsx("li",{children:"阿里云通义千问: https://dashscope.aliyuncs.com/compatible-mode/v1"}),e.jsx("li",{children:"OpenAI: https://api.openai.com/v1"})]})]})]})]})]}),e.jsxs("div",{className:"space-y-8",children:[e.jsxs("section",{className:"space-y-4",children:[e.jsxs("h3",{className:"text-lg font-extrabold text-gray-800 flex items-center gap-2",children:[e.jsx("div",{className:"p-1.5 rounded-lg bg-amber-500 text-white",children:e.jsx(re,{className:"w-4 h-4"})}),"远程过滑块配置"]}),e.jsxs("div",{className:"ios-card rounded-xl p-6 bg-white space-y-5",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"服务地址"}),e.jsx("input",{type:"url",value:s["captcha.remote_service_url"]||"",onChange:(t=>u({...s,"captcha.remote_service_url":t.target.value})),className:"w-full ios-input px-4 py-3 rounded-xl text-sm",placeholder:"https://example.com/internal/captcha/solve"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"服务秘钥"}),e.jsxs("div",{className:"relative",children:[e.jsx("input",{type:k?"text":"password",value:s["captcha.remote_secret_key"]||"",onChange:(t=>u({...s,"captcha.remote_secret_key":t.target.value})),className:"w-full ios-input px-4 py-3 pr-12 rounded-xl font-mono text-sm",autoComplete:"off",placeholder:s["captcha.remote_secret_key_configured"]?"已配置,如需替换请输入新密钥":"请输入服务秘钥"}),e.jsx("button",{type:"button",onClick:(()=>V(!k)),className:"absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600",title:k?"隐藏秘钥":"显示秘钥",children:k?e.jsx(D,{className:"w-4 h-4"}):e.jsx(O,{className:"w-4 h-4"})})]})]}),e.jsxs("label",{className:"flex items-start gap-3 rounded-xl bg-amber-50 p-4 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:String(s["captcha.remote_pass_cookies"]||"").toLowerCase()==="true",onChange:(t=>u({...s,"captcha.remote_pass_cookies":t.target.checked})),className:"mt-0.5 w-4 h-4 rounded border-gray-300"}),e.jsxs("span",{children:[e.jsx("span",{className:"block text-sm font-bold text-amber-900",children:"允许向远程服务传递账号 Cookie"}),e.jsx("span",{className:"block mt-1 text-xs text-amber-700",children:"默认关闭。仅在信任远程服务且需要由其自动重取过期验证链接时开启。"})]})]}),e.jsx("p",{className:"text-xs text-gray-500",children:"配置地址和秘钥后优先调用远程服务;只有网络不可用或超时才回退本机引擎,远程明确返回失败时不会重复触发本机验证。"})]})]}),e.jsxs("section",{className:"space-y-4",children:[e.jsxs("h3",{className:"text-lg font-extrabold text-gray-800 flex items-center gap-2",children:[e.jsx("div",{className:"p-1.5 rounded-lg bg-gray-900 text-white",children:e.jsx(le,{className:"w-4 h-4"})}),"登录凭据"]}),e.jsxs("form",{onSubmit:F,className:"ios-card rounded-xl p-6 bg-white space-y-5",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"登录用户名"}),e.jsxs("div",{className:"relative",children:[e.jsx(ye,{className:"absolute left-4 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400"}),e.jsx("input",{type:"text",value:x.new_username,onChange:(t=>v({...x,new_username:t.target.value})),className:"w-full ios-input pl-11 pr-4 py-3 rounded-xl text-sm",autoComplete:"username"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"当前密码"}),e.jsxs("div",{className:"relative",children:[e.jsx("input",{type:b?"text":"password",value:x.current_password,onChange:(t=>v({...x,current_password:t.target.value})),className:"w-full ios-input px-4 py-3 pr-12 rounded-xl text-sm",placeholder:"用于确认当前身份",autoComplete:"current-password"}),e.jsx("button",{type:"button",onClick:(()=>Y(!b)),className:"absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600",title:b?"隐藏密码":"显示密码",children:b?e.jsx(D,{className:"w-4 h-4"}):e.jsx(O,{className:"w-4 h-4"})})]})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"新密码"}),e.jsxs("div",{className:"relative",children:[e.jsx("input",{type:w?"text":"password",value:x.new_password,onChange:(t=>v({...x,new_password:t.target.value})),className:"w-full ios-input px-4 py-3 pr-11 rounded-xl text-sm",placeholder:"不修改则留空",autoComplete:"new-password"}),e.jsx("button",{type:"button",onClick:(()=>W(!w)),className:"absolute right-2 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600",title:w?"隐藏密码":"显示密码",children:w?e.jsx(D,{className:"w-4 h-4"}):e.jsx(O,{className:"w-4 h-4"})})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"block text-sm font-bold text-gray-800",children:"确认新密码"}),e.jsx("input",{type:w?"text":"password",value:x.confirm_password,onChange:(t=>v({...x,confirm_password:t.target.value})),className:"w-full ios-input px-4 py-3 rounded-xl text-sm",placeholder:"再次输入新密码",autoComplete:"new-password"})]})]}),P&&e.jsxs("div",{className:`flex items-start gap-2 rounded-xl px-3 py-2.5 text-sm font-medium ${P.type==="success"?"bg-green-50 text-green-700":"bg-red-50 text-red-700"}`,children:[e.jsx(re,{className:"w-4 h-4 mt-0.5 flex-shrink-0"}),e.jsx("span",{children:P.text})]}),e.jsxs("button",{type:"submit",disabled:I||!x.new_username||!x.current_password,className:"w-full bg-gray-900 hover:bg-black text-white px-5 py-3 rounded-xl font-bold text-sm flex items-center justify-center gap-2 transition-colors disabled:opacity-40",children:[e.jsx(le,{className:"w-4 h-4"}),I?"正在更新...":"更新登录凭据"]})]})]})]})]}),e.jsx("div",{className:"fixed bottom-10 right-10 z-30",children:e.jsxs("button",{onClick:T,disabled:d,className:"ios-btn-primary px-10 py-5 rounded-xl text-lg shadow-2xl shadow-blue-200 flex items-center gap-3 transform hover:scale-105 active:scale-95 transition-all disabled:opacity-70",children:[e.jsx(fe,{className:"w-6 h-6"}),d?"保存中...":"保存所有配置"]})})]})};export{qe as default};
|