/g,">"),n=e.indexOf("```")>-1,i=t.replace(/(```)/g,((e,n,i)=>(t.slice(0,i+e.length).split(/```/g).length-1)%2==1?"
":"
"));return n?i:formatText(i)}function scrollToBottom(){const e=$(".msg");e.scrollTop(e[0].scrollHeight)}function noExistContainerRender(){$(".container").show().html('非内测用户请勿登录!
')}function offlineContainerRender(){$(".container").show().html('\n 网页版已停止服务,如果您需要进一步的帮助,请联系客服。
\n ')}let useInfo,page=1,totalNum=0,totalPages=0,recordIsCompleted=!1,pageSize=10,isLoading=!1;function renderChatList(e,t){e.forEach((e=>{let t;const n=formatCode(e.content);"user"===e.role&&(t=$('
'+n+" ")),"assistant"===e.role&&(t=$('
'+n+" ")),$(".msg").prepend(t)})),hljs.highlightAll(),t&&t()}function getChatRecord(e){return $.ajax({url:`/api/chats?current=${e}&pageSize=${pageSize}`,type:"GET"})}function initChatRecord(){getChatRecord(page).then((e=>{const{list:t,total:n}=e.data;totalNum=n,totalPages=Math.ceil(totalNum/pageSize),t.length>0?renderChatList(t,(function(){scrollToBottom()})):($(".examples").show(),$(".btn-record").hide())}))}function renderPricingContent(e){const{byPeriod:t,byCount:n}=e,i=$(".modal-pricing__content.byPeriod"),a=$(".modal-pricing__content.byCount");Object.keys(t).map((function(e){i.append(`\n \n
${t[e]?.name}
\n
\n
\n ¥${t[e]?.price}\n
\n
\n 原价¥${t[e]?.costPrice}\n
\n
\n
每日${t[e]?.dayAmount}次问答机会
\n
\n
\n `)})),a.append(`\n \n
1份次数包(含100次对话)
\n
\n ¥${n.default?.price}\n 原价¥19.9\n
\n
购买份数:
\n \n
\n - 每日赠送额度用完才会消耗次数包
\n - 永久有效期
\n
\n
\n
\n `),$("#byCount").addClass("active"),$("#byCount .btn").removeClass("btn-reverse").addClass("btn-primary"),$("#month").addClass("active"),$("#month .btn").removeClass("btn-reverse").addClass("btn-primary"),$(".modal-pricing__tab.byPeriod").addClass("active"),$(".modal-pricing__item").on("click",(function(){$(this).siblings().removeClass("active"),$(this).siblings().find(".btn").removeClass("btn-primary").addClass("btn-reverse"),$(this).addClass("active"),$(this).find(".btn").removeClass("btn-reverse").addClass("btn-primary")})),$(".modal-pricing__tab").on("click",(function(){const e=$(this).index();$(this).siblings().removeClass("active"),$(this).addClass("active"),$(".modal-pricing__content").hide(),$(".modal-pricing__content").eq(e).show()}))}function getChatsRemais(){$.get("/api/chats/remains").done((function(e){$(".container").show();const{data:t}=e,{isPaidUser:n,isExpired:i,chatRemains:a}=t;n&&(!i||a>0)?["chatRemains","dayAmount","dayRemains","serviceExpiredAt"].forEach((function(e){"serviceExpiredAt"!==e||t?.serviceExpiredAt?$("#"+e).text("serviceExpiredAt"===e&&t[e]?t[e].substring(0,10):t[e]):$("#serviceExpiredAtWrap").hide()})):noExistContainerRender()})).fail((e=>{}))}function checkPayStatus(e){t=setInterval((()=>{$.get("/api/orders/"+e).done((function(e){const{tradeState:n}=e.data;"SUCCESS"===n&&(clearInterval(t),window.location.reload())}))}),2e3)}$("ul.msg").scroll((function(){if(!$(".msg").find("li").length)return;const e=this;if(recordIsCompleted)return;if(isLoading)return;if(!(page已经到顶啦~
'),void(recordIsCompleted=!0);const t=$(this).find("li").eq(0);t.scrollTop();0===$(this).scrollTop()&&page>=1&&($(".msg").prepend('加载中...
'),page++,getChatRecord(page).then((n=>{const{list:i}=n.data;$(".msg").find("li.loading").remove(),i.length>0&&renderChatList(i,(function(){$(e).scrollTop(t.position().top)}))})).catch((()=>{$(".msg").find("li.loading").remove()})))}));const getOrdersPackages=()=>{$.get("/api/constant").done((function(e){pricingData=e.data.PACKAGES,renderPricingContent(pricingData)}))};$((function(){if(window.location.origin.indexOf("chat2ai.cn")>-1)return offlineContainerRender(),void $("#beian").show();if($(document).ajaxError((function(e,t,n,i){Raven.captureException(i)})),$.get("/api/constant").done((function(e){pricingData=e.data.PACKAGES,renderPricingContent(pricingData)})),isMobile){"1"===getParameterByName("buy")&&setTimeout((()=>{$(document).find("#pay-byPeriod").trigger("click")}),0),$(document).find("#slidebar").removeClass("active")}else $(document).find("#slidebar").addClass("active");$("textarea").on("input",(function(){this.style.height="50px",this.style.height=this.scrollHeight+2+"px"})),$.get("/api/user/profile").done((function(e){const{botName:t,name:n,avatar:i,userType:a,id:o}=e.data||{};ocSubscribe=e.data.ocSubscribe,"snapshot"!==a&&(i&&(userAvatar=i),$("#userId").text(o),$("#name").text(n),$("#bot-name").text(t||"ThinkBot"),$("#avatar").html('
'),getChatsRemais(),initChatRecord())})).fail((e=>{isMobile?window.location.href="/api/wechat/login":window.location.replace("/login")})),$("#pay-byPeriod").on("click",(function(){$(".qrcode-pay").hide(),$("#packages").show(),$(".modal-pricing").show().attr("style","display:flex")})),$(document).on("click","#qrcode-back",(function(){t&&clearInterval(t),$(".qrcode-pay").hide(),$("#packages").show()})),$("#pricing-close").on("click",(function(){t&&clearInterval(t),$(".modal-pricing").hide()})),$(document).on("click",".pay",(function(){const e={payFrom:"h5"},t=$(this).attr("data-packageType");if(e.packageType=t,"byCount"===t){if($("#input-byCount").val()>999)return void alert("你购买的份数太多啦");e.packageCount=$("#input-byCount").val()}"byPeriod"===t&&(e.packageName=$(this).attr("data-packageName")),isMobile?$.post("/api/orders",e).done((function(e){const{outTradeNo:t}=e.data;$((async function(){const{data:e}=await $.get("/api/wechat/js_signature");wx.config({...e,success:function(e){alert(JSON.stringify(e))},fail:function(e){alert(JSON.stringify(e))}}),$.get("/api/wxpay/params",{outTradeNo:t},(function({data:e}){WeixinJSBridge.invoke("getBrandWCPayRequest",e,(function(e){"get_brand_wcpay_request:ok"==e.err_msg&&window.location.reload()}))}))}))})):$.post("/api/orders",e).done((function(e){const{outTradeNo:t,id:n}=e.data;$(".qrcode-pay").show(),$("#packages").hide(),$("#qrcode-pay").html("").qrcode({render:"canvas",width:200,height:200,text:location.origin+`/pay?outTradeNo=${t}`}),checkPayStatus(n)}))}));const e=2e4;let n=!1,i="";$(".btn-response").hide();let a="";const o=$(".msg"),s='
',c=new TextDecoder("utf-8"),r=$(".input textarea"),l=$(".input .send");let d=null;$(".send").removeClass("active");let p=null;const m=()=>{$(".btn-record").hide(),r.attr("disabled",!0),l.attr("disabled",!0)},g=(e,t)=>{t.trim()||(d.abort(),d=null,e.find("span").html('发生意外错误,请重试!')),n=!0,r.attr("disabled",!1).focus(),l.attr("disabled",!1),$(".btn-response").text("重新生成"),$(".btn-record").show(),getChatsRemais()};function u(t,a=!1){let r;$(".btn-response").show().text("停止生成"),m(),a?($(".msg-detail:last").html(s),r=$("li.completion:last")):($('
'+t+" ").appendTo(o),r=$('
'+s+" ").appendTo(o)),o.scrollTop(o[0].scrollHeight);let l=setTimeout((()=>g(r,"")),e);d||(d=new AbortController),fetch("/api/completion",{method:"POST",signal:d.signal,headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:t,autoContext:n,inputType:"text"})}).then((async t=>{p=t.body.getReader();let n="",a="",s=0;for(;;){clearTimeout(l),l=setTimeout((()=>g(r,n)),e);let{value:t,done:d}=await p.read();if(d){g(r,n);break}const m=c.decode(t).split(/\n(?=data:)/).filter((e=>""!==e.trim()));for(let e of m){if(e=e.replace(/^data: /,"").trim(),"[DONE]"===e){g(r,n),chatCount++,3===chatCount&&isMobile&&!ocSubscribe&&$(".modal-mpQrcode").show();break}try{e=JSON.parse(i+e),i=""}catch(t){i=e;continue}if("ID"===e.dataType)continue;const t=e.error||e.errorMessage;e?.errorCode;if(t)n+=''+(e.error?.message??e.errorMessage)+"";else{const t=e.choices[0].delta?.content||"";n+=s<=1?t.replace(/^[\n\r]+/g,""):t,a+=t}if(t)r.find("span").html(n.trim());else{const e=formatCode(n);r.find("span").html(e),hljs.highlightAll(),o.scrollTop(o[0].scrollHeight)}if(t){g(r,n);break}}s++}})).catch((e=>{console.log("error",e)}))}$(".send").on("click",(function(){h()})),$(".btn-response").on("click",(function(){const e=$(".btn-response").text();"停止生成"===e&&(p?.cancel&&p.cancel(),$(this).text("重新生成")),"重新生成"===e&&($(this).hide(),u(a,!0))})),$(document).on("keypress",(function(e){13==e.which&&h()}));const h=()=>{$(".examples").hide();const e=r.val();return e&&(u(e),a=e,r.val("").blur()),null};$(".examples__content li").on("click",(function(){n=!1;const e=$(this).text().split(" ")[0];r.val(e)})),$(".examples__more").on("click",(function(){$(".modal-group").show()})),$(".overlay").on("click",(function(){t&&clearInterval(t),$(".modal").hide()})),$(".float-qrcode-close").on("click",(function(){$(".float-qrcode").hide()})),$(document).on("click","#menu",(function(){$(document).find("#slidebar").addClass("active"),$(document).find(".slidebar .overlay").show()})),$(document).on("click",".slidebar .overlay",(function(){$(this).hide(),$(document).find("#slidebar").removeClass("active")})),$(document).on("click","#slidebar-trigger.close",(function(){$(this).addClass("open").removeClass("close"),$(document).find("#slidebar").removeClass("active"),$(".content").attr("style","padding-left:0")})),$(document).on("click","#slidebar-trigger.open",(function(){$(this).addClass("close").removeClass("open"),$(document).find("#slidebar").addClass("active"),$(".content").attr("style","padding-left:260px")})),$(".modal-close").on("click",(function(){$(this).parents(".modal").hide()})),$("#btn-record-clear").on("click",(function(){$(".msg").empty(),$(".modal-clear").hide()})),$("#btn-record-delete").on("click",(function(){$.ajax({url:"/api/chats",type:"DELETE",data:{clear:!0},success:function(e){$(".msg").html(""),$(".modal-clear").hide()}})})),$(".btn-record").click((function(){$(".modal-clear").show()}))}))