2 lines
14 KiB
JavaScript
2 lines
14 KiB
JavaScript
import{u as Te,a as Oe,d as Ee,r as l,g as $e,w as Le,j as e,Q as re,f as Ue,z as d,x as He,y as Re,A as De,B as Fe,C as Ve,D as ze,E as Be}from"./index-5ebNoZ4G.js";import{C as L}from"./CountUp-EtJN562p.js";import{u as Qe}from"./useTranslation-DtgXIWa1.js";function Xe(){var te;const{t:s}=Qe(),{user:i,refreshUser:U}=Te(),{site:o}=Oe(),{symbol:k,rate:m}=Ee(),[p,ne]=l.useState(null),[r,le]=l.useState(null),[oe,H]=l.useState(!0),[O,R]=l.useState(""),[D,F]=l.useState(!1),[b,V]=l.useState(""),[ce,z]=l.useState(null),[B,Q]=l.useState(null),[de,W]=l.useState(!1),[y,f]=l.useState(!1),[E,j]=l.useState(""),[_,P]=l.useState(null),[ie,N]=l.useState(!1),[u,J]=l.useState(""),[A,me]=l.useState("usdt"),[v,pe]=l.useState(null),[M,ue]=l.useState(!1),[G,xe]=l.useState([]),[ge,K]=l.useState(!1);o!=null&&o.enable_topup,o==null||o.topup_config;const q=l.useCallback(async()=>{var t;H(!0);try{const[a,n]=await Promise.all([$e(),o!=null&&o.enable_topup?Le().catch(()=>null):Promise.resolve(null)]);a.data.success&&ne(a.data.data),(t=n==null?void 0:n.data)!=null&&t.data&&le(n.data.data)}catch{}H(!1)},[o==null?void 0:o.enable_topup]);l.useEffect(()=>{q()},[q]);const X=(p==null?void 0:p.quota)??(i==null?void 0:i.quota)??0,he=(p==null?void 0:p.used_quota)??(i==null?void 0:i.used_quota)??0,be=(p==null?void 0:p.request_count)??(i==null?void 0:i.request_count)??0,ye=X/re*m,fe=async t=>{if(t.preventDefault(),!!O.trim()){F(!0);try{(await Ue(O.trim())).data.success&&(d.success(s("topup.redeemSuccess")),R(""),await Promise.all([q(),U()]))}catch{}F(!1)}},Y=async t=>{if(!t||t<=0){Q(null);return}W(!0);try{const a=await He({amount:parseInt(t)});a.data.message==="success"&&Q(a.data.data)}catch{}W(!1)},je=t=>{z(t),V(t),Y(t)},Ne=t=>["stripe","alipay","wxpay"].includes(t)&&!t.startsWith("epay_"),ve=async t=>{var n;const a=parseInt(b);if(!a||a<=0){d.error(s("topup.enterAmount"));return}f(!0),j(t);try{const x=window.location.origin+"/topup",S={amount:a,payment_method:t,return_url:x};if(Ne(t)){const c=await Re(S);if(c.data.message==="success"&&((n=c.data.data)!=null&&n.pay_link))window.open(c.data.data.pay_link,"_blank");else if(c.data.message!=="success"){const g=typeof c.data.data=="string"?c.data.data:c.data.message;d.error(g||s("common.requestFailed"))}}else{const c=await De(S);if(c.data.message==="success"){const g=c.data.data,se=c.data.url;if(se&&g){const h=document.createElement("form");h.action=se,h.method="POST",navigator.userAgent.indexOf("Safari")>-1&&navigator.userAgent.indexOf("Chrome")<1||(h.target="_blank");for(const ae in g){const T=document.createElement("input");T.type="hidden",T.name=ae,T.value=g[ae],h.appendChild(T)}document.body.appendChild(h),h.submit(),document.body.removeChild(h)}}else{const g=typeof c.data.data=="string"?c.data.data:c.data.message;d.error(g||s("common.requestFailed"))}}}catch{}f(!1),j("")},we=async t=>{var a;f(!0),j("creem");try{const n=await Fe({product_id:t.productId,payment_method:"creem"});if(n.data.message==="success"&&((a=n.data.data)!=null&&a.checkout_url))window.open(n.data.data.checkout_url,"_blank");else if(n.data.message!=="success"){const x=typeof n.data.data=="string"?n.data.data:n.data.message;d.error(x||s("common.requestFailed"))}}catch{}f(!1),j("")},Ce=async()=>{const t=parseInt(b);if(!t||t<=0){d.error(s("topup.enterAmount"));return}if(!u){d.error(s("topup.selectChain"));return}if(!A){d.error(s("topup.selectToken"));return}f(!0),j("crypto");try{const a=await Ve({amount:t,chain:u,token:A});if(a.data.message==="success"&&a.data.data)P(a.data.data),_e(a.data.data.trade_no);else if(a.data.message!=="success"){const n=typeof a.data.data=="string"?a.data.data:a.data.message;d.error(n||s("common.requestFailed"))}}catch{}f(!1),j("")},_e=t=>{N(!0);const a=setInterval(async()=>{var x,S;try{const c=await ze(t);((x=c.data.data)==null?void 0:x.status)==="success"?(clearInterval(a),N(!1),P(null),d.success(s("topup.paymentSuccess")),await Promise.all([q(),U()])):((S=c.data.data)==null?void 0:S.status)==="expired"&&(clearInterval(a),N(!1),d.error(s("topup.orderExpired")))}catch{clearInterval(a),N(!1)}},5e3),n=((r==null?void 0:r.crypto_expiry_minutes)||30)*60*1e3;setTimeout(()=>{clearInterval(a),N(!1)},n)},w=(r==null?void 0:r.crypto_wallets)||{},C=l.useMemo(()=>{const t=[];return w.tron&&t.push({key:"tron",label:"TRON (TRC20)"}),w.eth&&t.push({key:"eth",label:"Ethereum (ERC20)"}),w.bsc&&t.push({key:"bsc",label:"BSC (BEP20)"}),t},[w.tron,w.eth,w.bsc]);l.useEffect(()=>{C.length>0&&!u&&J(C[0].key)},[C,u]);const Z=l.useMemo(()=>{if(!(r!=null&&r.creem_products))return[];try{const t=typeof r.creem_products=="string"?JSON.parse(r.creem_products):r.creem_products;return Array.isArray(t)?t:[]}catch{return[]}},[r==null?void 0:r.creem_products]),Se=async()=>{var t;K(!0);try{const a=await Be({page:1,page_size:20});(t=a.data.data)!=null&&t.items&&xe(a.data.data.items)}catch{}K(!1)},ke=(r==null?void 0:r.amount_options)||[1,5,10,20,50,100],I=(r==null?void 0:r.min_topup)||1,Pe=(r==null?void 0:r.pay_methods)||[],Ae=r==null?void 0:r.enable_online_topup,Me=r==null?void 0:r.enable_stripe_topup,qe=r==null?void 0:r.enable_creem_topup,$=r==null?void 0:r.enable_crypto_topup,ee=Pe.filter(t=>t.type!=="creem"&&t.type!=="crypto");return oe?e.jsx("div",{className:"flex items-center justify-center min-h-[60vh]",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-4xl mx-auto px-6 py-10",children:[e.jsxs("div",{className:"mb-8",children:[e.jsx("h1",{className:"text-2xl font-heading font-bold text-page mb-1",children:s("topup.title")}),e.jsx("p",{className:"text-sm text-page-secondary",children:s("topup.subtitle")})]}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-6 mb-10",children:[e.jsxs("div",{className:"glass rounded-2xl p-6",children:[e.jsx("p",{className:"text-sm text-page-secondary mb-2",children:s("dashboard.balance")}),e.jsxs("div",{className:"text-3xl font-bold text-page",children:[k,e.jsx(L,{from:0,to:ye,duration:1.5})]}),e.jsx("p",{className:"text-xs text-page-muted mt-1",children:s("dashboard.quotaUnits",{count:X.toLocaleString()})})]}),e.jsxs("div",{className:"glass rounded-2xl p-6",children:[e.jsx("p",{className:"text-sm text-page-secondary mb-2",children:s("dashboard.used")}),e.jsxs("div",{className:"text-3xl font-bold text-page",children:[k,e.jsx(L,{from:0,to:he/re*m,duration:1.5})]})]}),e.jsxs("div",{className:"glass rounded-2xl p-6",children:[e.jsx("p",{className:"text-sm text-page-secondary mb-2",children:s("dashboard.totalRequests")}),e.jsx("div",{className:"text-3xl font-bold text-page",children:e.jsx(L,{from:0,to:be,duration:1.5})})]})]}),(o==null?void 0:o.enable_topup)&&(Ae||Me||$)&&(ee.length>0||$)&&e.jsxs("div",{className:"glass rounded-2xl p-6 mb-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-6",children:[e.jsx("h2",{className:"text-lg font-semibold text-page",children:s("topup.onlineTopup")}),e.jsx("button",{onClick:()=>{ue(!M),M||Se()},className:"text-sm text-page-secondary hover:text-page transition-colors",children:s(M?"topup.hideHistory":"topup.viewHistory")})]}),e.jsxs("div",{className:"mb-6",children:[e.jsx("label",{className:"block text-sm font-medium text-page-label mb-3",children:s("topup.selectAmount")}),e.jsx("div",{className:"grid grid-cols-3 sm:grid-cols-6 gap-2",children:ke.map(t=>e.jsxs("button",{onClick:()=>je(t),className:`py-2.5 px-3 rounded-xl text-sm font-medium transition-all ${ce===t?"bg-brand-500 text-white shadow-lg shadow-brand-500/25":"glass-sm text-page-label hover:text-page hover:bg-page-surface-hover"}`,children:[k,Math.round(t*m)]},t))})]}),e.jsxs("div",{className:"mb-6",children:[e.jsx("label",{className:"block text-sm font-medium text-page-label mb-2",children:s("topup.customAmount")}),e.jsx("div",{className:"flex gap-3",children:e.jsx("input",{type:"number",value:b?Math.round(b*m):"",onChange:t=>{const a=t.target.value,n=a?Math.max(1,Math.ceil(a/m)):"";V(n),z(null)},onBlur:t=>{const a=t.target.value,n=a?Math.max(1,Math.ceil(a/m)):"";Y(n)},min:Math.round(I*m),placeholder:s("topup.amountPlaceholder",{min:Math.round(I*m)}),className:"input flex-1"})}),de?e.jsx("p",{className:"text-xs text-page-muted mt-2",children:s("topup.calculating")}):B?e.jsxs("p",{className:"text-xs text-page-secondary mt-2",children:[s("topup.payAmountLabel"),": ",e.jsxs("span",{className:"text-page-success font-medium",children:["¥",B]})]}):null]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-page-label mb-3",children:s("topup.paymentMethod")}),e.jsx("div",{className:"flex flex-wrap gap-2",children:ee.map(t=>{const a=y&&E===t.type;return e.jsx("button",{onClick:()=>ve(t.type),disabled:y||!b,className:"px-4 py-2.5 rounded-xl text-sm font-medium glass-sm text-page-label hover:text-page hover:bg-page-surface-hover disabled:opacity-40 disabled:cursor-not-allowed transition-all",children:a?s("topup.processing"):t.name},t.type)})})]}),$&&C.length>0&&e.jsxs("div",{className:"mt-6 pt-6 border-t border-page-divider",children:[e.jsx("label",{className:"block text-sm font-medium text-page-label mb-3",children:s("topup.cryptoPayment")}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsx("div",{className:"flex gap-1.5",children:C.map(t=>e.jsx("button",{onClick:()=>J(t.key),className:`px-3 py-1.5 rounded-lg text-xs font-medium transition-all ${u===t.key?"bg-brand-500 text-white shadow-lg shadow-brand-500/25":"glass-sm text-page-label hover:text-page hover:bg-page-surface-hover"}`,children:t.label},t.key))}),e.jsx("div",{className:"flex gap-1.5",children:["usdt","usdc"].map(t=>e.jsx("button",{onClick:()=>me(t),className:`px-3 py-1.5 rounded-lg text-xs font-medium transition-all ${A===t?"bg-brand-500 text-white shadow-lg shadow-brand-500/25":"glass-sm text-page-label hover:text-page hover:bg-page-surface-hover"}`,children:t.toUpperCase()},t))}),e.jsx("button",{onClick:Ce,disabled:y||!b,className:"px-4 py-2.5 rounded-xl text-sm font-medium glass-sm text-page-label hover:text-page hover:bg-page-surface-hover disabled:opacity-40 disabled:cursor-not-allowed transition-all",children:y&&E==="crypto"?s("topup.processing"):`${A.toUpperCase()} (${((te=C.find(t=>t.key===u))==null?void 0:te.label)||u})`})]})]})]}),(o==null?void 0:o.enable_topup)&&qe&&Z.length>0&&e.jsxs("div",{className:"glass rounded-2xl p-6 mb-6",children:[e.jsx("h2",{className:"text-lg font-semibold text-page mb-4",children:s("topup.creemPayment")||"Creem Payment"}),e.jsx("div",{className:"grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4",children:Z.map(t=>e.jsxs("div",{className:`glass-sm rounded-xl p-4 cursor-pointer transition-all ${(v==null?void 0:v.productId)===t.productId?"ring-2 ring-brand-500 bg-brand-500/10":"hover:bg-page-surface"}`,onClick:()=>pe(t),children:[e.jsx("h3",{className:"text-sm font-semibold text-page mb-1",children:t.name}),e.jsxs("p",{className:"text-xl font-bold text-page-success mb-1",children:["$",t.price," ",e.jsx("span",{className:"text-xs text-page-muted font-normal",children:t.currency||"USD"})]}),t.quota&&e.jsx("p",{className:"text-xs text-page-secondary",children:s("topup.quotaIncluded")||`${t.quota} quota`})]},t.productId))}),v&&e.jsx("div",{className:"mt-4 flex justify-end",children:e.jsx("button",{onClick:()=>we(v),disabled:y,className:"btn-primary px-6",children:y&&E==="creem"?s("topup.processing"):`${s("topup.payNow")||"Pay"} $${v.price}`})})]}),_&&e.jsx("div",{className:"modal-overlay fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm",onClick:()=>P(null),children:e.jsxs("div",{className:"glass rounded-2xl p-6 max-w-md w-full mx-4",onClick:t=>t.stopPropagation(),children:[e.jsx("h3",{className:"text-lg font-semibold text-page mb-4",children:s("topup.cryptoPayment")}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"glass-sm rounded-xl p-4",children:[e.jsx("p",{className:"text-xs text-page-secondary mb-1",children:s("topup.walletAddress")}),e.jsx("p",{className:"text-sm text-page font-mono break-all",children:_.wallet})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"glass-sm rounded-xl p-3",children:[e.jsx("p",{className:"text-xs text-page-secondary mb-1",children:s("topup.amount")}),e.jsxs("p",{className:"text-sm text-page font-medium",children:[_.amount," ",_.token]})]}),e.jsxs("div",{className:"glass-sm rounded-xl p-3",children:[e.jsx("p",{className:"text-xs text-page-secondary mb-1",children:s("topup.chain")}),e.jsx("p",{className:"text-sm text-page font-medium",children:_.chain})]})]}),ie&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-page-secondary",children:[e.jsx("div",{className:"w-4 h-4 border-2 border-brand-500/30 border-t-brand-500 rounded-full animate-spin"}),s("topup.waitingPayment")]}),e.jsx("button",{onClick:()=>{P(null),N(!1)},className:"w-full py-2 rounded-xl text-sm glass-sm text-page-secondary hover:text-page transition-colors",children:s("topup.close")})]})]})}),M&&e.jsxs("div",{className:"glass rounded-2xl p-6 mb-6",children:[e.jsx("h2",{className:"text-lg font-semibold text-page mb-4",children:s("topup.history")}),ge?e.jsx("div",{className:"flex justify-center py-8",children:e.jsx("div",{className:"w-6 h-6 border-2 border-brand-500/30 border-t-brand-500 rounded-full animate-spin"})}):G.length===0?e.jsx("p",{className:"text-sm text-page-muted text-center py-8",children:s("topup.noHistory")}):e.jsx("div",{className:"space-y-2",children:G.map((t,a)=>e.jsxs("div",{className:"flex items-center justify-between glass-sm rounded-xl px-4 py-3",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-sm text-page",children:[k,(Number(t.amount)*m).toFixed(2)]}),e.jsxs("p",{className:"text-xs text-page-muted",children:[new Date(t.create_time*1e3).toLocaleString()," · ",t.payment_method||s("topup.redeemCode")]})]}),e.jsx("span",{className:`text-xs px-2 py-1 rounded-full ${t.status==="success"?"bg-green-500/10 text-page-success":t.status==="pending"?"bg-yellow-500/10 text-page-warning":"bg-red-500/10 text-page-danger"}`,children:t.status==="success"?s("topup.statusSuccess"):t.status==="pending"?s("topup.statusPending"):s("topup.statusFailed")})]},a))})]}),e.jsxs("div",{className:"glass rounded-2xl p-6",children:[e.jsx("h2",{className:"text-lg font-semibold text-page mb-4",children:s("topup.redeemTitle")}),e.jsxs("form",{onSubmit:fe,className:"flex gap-3",children:[e.jsx("input",{type:"text",value:O,onChange:t=>R(t.target.value),className:"input flex-1",placeholder:s("topup.enterRedeemCode")}),e.jsx("button",{type:"submit",disabled:D,className:"btn-primary whitespace-nowrap",children:s(D?"topup.redeeming":"topup.redeem")})]})]})]})}export{Xe as default};
|