1 line
13 KiB
JavaScript
1 line
13 KiB
JavaScript
(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[177],{1290:()=>{},2902:(e,t,a)=>{"use strict";a.d(t,{Rx:()=>n,mC:()=>r});let n=[{value:"today",label:"今天"},{value:"7d",label:"近 7 天"},{value:"30d",label:"近 30 天"},{value:"month",label:"本月"}];function r(e){var t;let a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new Date,r=function(e){let t=Object.fromEntries(new Intl.DateTimeFormat("en-CA",{timeZone:"Asia/Shanghai",year:"numeric",month:"2-digit",day:"2-digit"}).formatToParts(e).map(e=>[e.type,e.value]));return"".concat(t.year,"-").concat(t.month,"-").concat(t.day)}(a),l=r;return"7d"===e&&(l=s(r,-6)),"30d"===e&&(l=s(r,-29)),"month"===e&&(l="".concat(r.slice(0,7),"-01")),function(e,t,a){var n,r;if(!i(e)||!i(t))throw o("日期格式必须为 YYYY-MM-DD。");if(e>t)throw o("开始日期不能晚于结束日期。");let l=(n=e,r=t,Math.round((Date.parse("".concat(r,"T00:00:00Z"))-Date.parse("".concat(n,"T00:00:00Z")))/864e5)+1);if(l>3660)throw o("单次查询最多支持 10 年。");return{from:"".concat(e,"T00:00:00+08:00"),to:"".concat(s(t,1),"T00:00:00+08:00"),startDate:e,endDate:t,label:a||(e===t?e:"".concat(e," 至 ").concat(t)),dayCount:l}}(l,r,null==(t=n.find(t=>t.value===e))?void 0:t.label)}function s(e,t){let a=new Date("".concat(e,"T00:00:00Z"));return a.setUTCDate(a.getUTCDate()+t),a.toISOString().slice(0,10)}function i(e){return!!/^\d{4}-\d{2}-\d{2}$/.test(e)&&new Date("".concat(e,"T00:00:00Z")).toISOString().slice(0,10)===e}function o(e){return Object.assign(Error(e),{status:400})}},4577:(e,t,a)=>{"use strict";a.d(t,{BrowserAuthGuard:()=>u,BrowserAuthProvider:()=>l,ClientRedirect:()=>d,a:()=>c});var n=a(5155),r=a(2115),s=a(63),i=a(6660);let o=(0,r.createContext)(null);function l(e){let{children:t}=e,[a,s]=(0,r.useState)(null),[l,c]=(0,r.useState)("loading"),[u,d]=(0,r.useState)(null),h=(0,r.useCallback)(async e=>{try{let t=await (0,i.uK)(e);s(t),c("ready"),d(null)}catch(e){if(e instanceof DOMException&&"AbortError"===e.name)return;c("error"),d(e instanceof Error?e.message:"无法读取认证状态")}},[]);(0,r.useEffect)(()=>{let e=new AbortController;return h(e.signal),()=>e.abort()},[h]);let m=(0,r.useMemo)(()=>{var e,t;let n=(null==a?void 0:a.user)||null,r=null==(e=null==a?void 0:a.authRequired)||e,s=!r||(null==n?void 0:n.role)==="super_admin",i=s||(null==n?void 0:n.role)==="organization_admin";return{status:l,user:n,authMode:(null==a?void 0:a.authMode)||null,authRequired:r,authConfigured:null!=(t=null==a?void 0:a.authConfigured)&&t,isAdmin:i,isSuperAdmin:s,error:u,refresh:()=>h()}},[u,h,a,l]);return(0,n.jsx)(o.Provider,{value:m,children:t})}function c(){let e=(0,r.useContext)(o);if(!e)throw Error("useBrowserAuth must be used inside BrowserAuthProvider");return e}function u(e){let{children:t,role:a="user"}=e,o=c(),l=(0,s.usePathname)(),u=(0,s.useRouter)(),d="ready"===o.status&&o.authRequired&&!o.user;return((0,r.useEffect)(()=>{if(d){let e=(0,i.M6)(window.location,l||"/create");u.replace("/auth/login?next=".concat(encodeURIComponent(e)))}},[d,l,u]),"loading"===o.status||d)?(0,n.jsx)(h,{message:"正在确认登录状态…"}):"error"===o.status?(0,n.jsx)(h,{message:o.error||"无法读取认证状态",actionLabel:"重试",onAction:()=>void o.refresh()}):("admin"!==a||o.isAdmin)&&("super"!==a||o.isSuperAdmin)?(0,n.jsx)(n.Fragment,{children:t}):(0,n.jsx)(h,{message:"当前账号无权访问此页面。"})}function d(e){let{to:t,forwardNext:a=!1}=e;return(0,r.useEffect)(()=>{let e=a?new URLSearchParams(window.location.search).get("next"):null,n=e?"".concat(t,"?next=").concat(encodeURIComponent(e)):t;window.location.replace(n)},[a,t]),(0,n.jsx)(h,{message:"正在跳转…"})}function h(e){let{message:t,actionLabel:a,onAction:r}=e;return(0,n.jsxs)("section",{className:"panel",children:[(0,n.jsx)("p",{className:"muted",children:t}),a&&r?(0,n.jsx)("button",{className:"button",type:"button",onClick:r,children:a}):null]})}},4662:(e,t,a)=>{"use strict";a.d(t,{AppShell:()=>R});var n=a(5155),r=a(2115),s=a(5239),i=a(2619),o=a.n(i),l=a(63),c=a(5740),u=a(4114),d=a(5870),h=a(6907),m=a(534),f=a(5468),p=a(4926),g=a(9865),v=a(2821),x=a(7612),b=a(69),j=a(4033),y=a(5299),w=a(9051),A=a(2902);function N(e){let{user:t}=e,[a,s]=(0,r.useState)(!1),[i,o]=(0,r.useState)("month"),[l,c]=(0,r.useState)(null),[u,d]=(0,r.useState)(!1),[h,m]=(0,r.useState)(null),f=(0,r.useRef)(null);return(0,r.useEffect)(()=>{if(!a)return;let e=e=>{var t;(null==(t=f.current)?void 0:t.contains(e.target))||s(!1)},t=e=>{"Escape"===e.key&&s(!1)};return document.addEventListener("pointerdown",e),document.addEventListener("keydown",t),()=>{document.removeEventListener("pointerdown",e),document.removeEventListener("keydown",t)}},[a]),(0,r.useEffect)(()=>{if(!a)return;let e=new AbortController;return d(!0),m(null),fetch("/api/usage?preset=".concat(i),{cache:"no-store",signal:e.signal}).then(async e=>{let t=await e.json();if(!e.ok)throw Error(t.error||"读取用量失败");c(t)}).catch(e=>{e instanceof DOMException&&"AbortError"===e.name||m(e instanceof Error?e.message:String(e))}).finally(()=>{e.signal.aborted||d(!1)}),()=>e.abort()},[a,i]),(0,n.jsxs)("div",{className:"account-usage-menu",ref:f,children:[(0,n.jsxs)("button",{className:"account-chip account-chip-button",type:"button","aria-haspopup":"dialog","aria-expanded":a,"aria-controls":"account-usage-popover",title:"".concat(t.username||t.subject," \xb7 查看用量"),onClick:()=>s(e=>!e),children:[(0,n.jsx)(b.A,{"aria-hidden":"true"}),(0,n.jsx)("span",{children:t.displayName}),(0,n.jsx)(j.A,{className:a?"account-chevron open":"account-chevron","aria-hidden":"true"})]}),a?(0,n.jsxs)("section",{className:"account-usage-popover",id:"account-usage-popover",role:"dialog","aria-label":"我的用量",children:[(0,n.jsxs)("header",{className:"account-usage-head",children:[(0,n.jsxs)("div",{children:[(0,n.jsx)("span",{children:"我的用量"}),(0,n.jsx)("strong",{children:t.displayName})]}),(0,n.jsx)("small",{children:t.username||t.subject})]}),(0,n.jsx)("div",{className:"segmented account-usage-tabs","aria-label":"用量周期",children:A.Rx.map(e=>(0,n.jsx)("button",{className:i===e.value?"active":"",type:"button","aria-pressed":i===e.value,onClick:()=>o(e.value),children:e.label},e.value))}),h?(0,n.jsx)("div",{className:"account-usage-error",role:"alert",children:h}):null,u&&!l?(0,n.jsxs)("div",{className:"account-usage-loading",children:[(0,n.jsx)(y.A,{className:"spin"}),"正在读取用量"]}):l?(0,n.jsxs)("div",{className:u?"account-usage-content refreshing":"account-usage-content",children:[(0,n.jsxs)("div",{className:"account-usage-total",children:[(0,n.jsx)("span",{children:l.range.label}),(0,n.jsx)("strong",{children:l.total}),(0,n.jsx)("small",{children:"次成功任务"})]}),(0,n.jsx)("div",{className:"account-usage-breakdown","aria-label":"按功能分类",children:l.byCapability.map(e=>(0,n.jsxs)("div",{children:[(0,n.jsx)("span",{children:e.label}),(0,n.jsx)("strong",{children:e.count})]},e.key))}),(0,n.jsxs)("div",{className:"account-usage-recent",children:[(0,n.jsxs)("div",{className:"account-usage-section-title",children:[(0,n.jsx)(w.A,{"aria-hidden":"true"}),(0,n.jsx)("strong",{children:"最近记录"})]}),l.recent.length?l.recent.map(e=>{var t;return(0,n.jsxs)("div",{className:"account-usage-record",children:[(0,n.jsxs)("div",{children:[(0,n.jsx)("strong",{children:e.capabilityLabel}),(0,n.jsxs)("small",{children:[e.providerLabel,e.reqKey?" \xb7 ".concat(e.reqKey):""]})]}),(0,n.jsx)("time",{dateTime:e.createdAt,children:(t=e.createdAt,new Intl.DateTimeFormat("zh-CN",{timeZone:"Asia/Shanghai",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",hour12:!1}).format(new Date(t)))})]},e.id)}):(0,n.jsx)("div",{className:"account-usage-empty",children:"当前周期暂无真实任务用量"})]})]}):null]}):null]})}var E=a(4577);let C=[{href:"/create",label:"创作",icon:c.A},{href:"/logs",label:"日志",icon:u.A,requiredRole:"super"},{href:"/settings",label:"设置",icon:d.A,requiredRole:"super"},{href:"/accounts",label:"账号",icon:h.A},{href:"/usage",label:"用量",icon:m.A,requiredRole:"admin"},{href:"/billing",label:"计费",icon:f.A}];function R(e){let{children:t}=e,{user:a,authRequired:i,isAdmin:c,isSuperAdmin:u}=(0,E.a)(),d=(0,l.usePathname)(),h=(0,r.useRef)(null),m=d.startsWith("/auth"),f="/create"===d;return(0,r.useEffect)(()=>(0,x.It)(h,e=>(0,x.xr)(e,"[data-shell-animate]")),[]),(0,n.jsxs)("div",{className:(0,v.A)("app-shell",m&&"auth-shell",f&&"create-shell"),ref:h,children:[m?null:(0,n.jsx)("a",{className:"skip-link",href:"#main-content",children:"跳到主要内容"}),m?null:(0,n.jsxs)("header",{className:"topbar","data-shell-animate":!0,children:[(0,n.jsxs)(o(),{className:"brand",href:"/create","aria-label":"智念AIGC平台",children:[(0,n.jsx)("span",{className:"brand-mark","aria-hidden":"true",children:(0,n.jsx)(s.default,{className:"brand-logo-img",src:"/logo/zhinian-logo.png",alt:"",width:124,height:28,priority:!0})}),(0,n.jsx)("div",{children:(0,n.jsx)("div",{className:"brand-title",children:"智念AIGC平台"})})]}),(0,n.jsx)("nav",{className:"nav top-nav","aria-label":"主导航",children:C.filter(e=>!e.requiredRole||"super"===e.requiredRole&&u||"admin"===e.requiredRole&&c).map(e=>{let t=e.icon,a="/"===e.href?"/"===d:d.startsWith(e.href);return(0,n.jsxs)(o(),{href:e.href,className:(0,v.A)("nav-link",a&&"active"),"aria-current":a?"page":void 0,children:[(0,n.jsx)(t,{"aria-hidden":"true"}),(0,n.jsx)("span",{children:e.label})]},e.href)})}),(0,n.jsx)("div",{className:"topbar-account",children:a?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(N,{user:a}),(0,n.jsx)("form",{action:"/api/auth/logout",method:"post",children:(0,n.jsx)("button",{className:"icon-button logout-button",type:"submit","aria-label":"退出登录",title:"退出登录",children:(0,n.jsx)(p.A,{"aria-hidden":"true"})})})]}):i?(0,n.jsxs)(o(),{className:"nav-link login-link",href:"/auth/login",children:[(0,n.jsx)(g.A,{"aria-hidden":"true"}),(0,n.jsx)("span",{children:"登录"})]}):null})]}),(0,n.jsx)("main",{className:(0,v.A)("main",m&&"auth-main",f&&"create-main"),id:"main-content",tabIndex:-1,children:t})]})}},6645:(e,t,a)=>{Promise.resolve().then(a.t.bind(a,1290,23)),Promise.resolve().then(a.bind(a,4662)),Promise.resolve().then(a.bind(a,4577)),Promise.resolve().then(a.t.bind(a,1402,23))},6660:(e,t,a)=>{"use strict";async function n(e){let t=await fetch("/api/auth/me",{method:"GET",cache:"no-store",credentials:"same-origin",headers:{Accept:"application/json"},signal:e});if(!t.ok)throw Error("认证状态请求失败(".concat(t.status,")"));var a,n=await t.json();if(!i(n)||"boolean"!=typeof n.authenticated||"boolean"!=typeof n.authRequired||"boolean"!=typeof n.authConfigured)throw Error("认证状态响应格式无效");if(!n.authenticated){if(null!==n.authMode||null!==n.user)throw Error("认证状态响应格式无效");return{authenticated:!1,authRequired:n.authRequired,authConfigured:n.authConfigured,authMode:null,user:null}}if(!n.authConfigured||"user"!==n.authMode&&"admin"!==n.authMode||!(i(a=n.user)&&"string"==typeof a.id&&a.id.length>0&&"string"==typeof a.subject&&a.subject===a.id&&"string"==typeof a.displayName&&"string"==typeof a.clientId&&("user"===a.role||"organization_admin"===a.role||"super_admin"===a.role)&&"active"===a.status&&("super_admin"===a.role||"string"==typeof a.organizationId&&a.organizationId.length>0)&&Array.isArray(a.authorities)&&a.authorities.every(e=>"string"==typeof e)&&Array.isArray(a.scope)&&a.scope.every(e=>"string"==typeof e)))throw Error("认证用户响应格式无效");let r="user"===n.user.role?"user":"admin";if(n.authMode!==r)throw Error("认证用户响应格式无效");return{authenticated:!0,authRequired:n.authRequired,authConfigured:n.authConfigured,authMode:n.authMode,user:n.user}}function r(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"/create";if(!e||!e.startsWith("/")||e.startsWith("//")||e.includes("\\"))return t;try{let a="http://zhinian.local",n=new URL(e,a);if(n.origin!==a)return t;let r="".concat(n.pathname).concat(n.search).concat(n.hash);if(n.pathname.startsWith("/api/auth")||n.pathname.startsWith("/auth/login")||n.pathname.startsWith("/auth/admin-login"))return t;return r}catch(e){return t}}function s(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"/create";return r("".concat(e.pathname).concat(e.search).concat(e.hash),t)}function i(e){return"object"==typeof e&&null!==e}a.d(t,{M6:()=>s,uK:()=>n,z3:()=>r})},7612:(e,t,a)=>{"use strict";a.d(t,{It:()=>i,Js:()=>c,c8:()=>l,xr:()=>o});var n=a(7605);let r="power2.out";function s(){return window.matchMedia("(prefers-reduced-motion: reduce)").matches}function i(e,t){let a=e.current;if(!a||s())return;let r=n.os.context(()=>t(a),a);return()=>r.revert()}function o(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"[data-animate]";if(s())return;let a=n.os.utils.toArray(t,e);a.length&&n.os.fromTo(a,{autoAlpha:0,y:14},{autoAlpha:1,y:0,duration:.28,ease:r,stagger:.045,clearProps:"transform,opacity,visibility"})}function l(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10;!e||s()||n.os.fromTo(e,{autoAlpha:0,y:t},{autoAlpha:1,y:0,duration:.28,ease:r,clearProps:"transform,opacity,visibility"})}function c(e){!e||s()||n.os.fromTo(e,{scale:.985},{scale:1,duration:.22,ease:r,clearProps:"transform"})}}},e=>{e.O(0,[741,592,274,271,38,441,255,358],()=>e(e.s=6645)),_N_E=e.O()}]); |