pages-chat-speechtotext.cb86d45b.js 3.4 KB

1
  1. import{s as e,N as a,a0 as t,a2 as s,f as l,g as o,w as n,m as u,k as c,B as r,l as i,r as d,F as p,u as v,v as f,i as h,j as m,J as y,n as _,I as g,y as w}from"./index-ba48620e.js";import{_ as b}from"./s-layout.04b95866.js";import{_ as j}from"./_plugin-vue_export-helper.1b428a4d.js";import"./phone-international-input.dd3ca29e.js";import"./useGoods.edb8879f.js";const x=""+new URL("audioPaly-372169db.png",import.meta.url).href,M=j({__name:"speechtotext",setup(j){const{safeAreaInsets:M,safeArea:D}=e.$platform.device,I=e.$platform.navbar,k=D.height-I-20-60,T=a([]),U=a(!1);let E=a(null);const R=a("");let A=null;const B=a(0);let L=null,P=[],z=a(!1);t((()=>{navigator.mediaDevices.getUserMedia({audio:!0}).then((e=>{L=new MediaRecorder(e),L.ondataavailable=e=>{P.push(e.data)},L.onstop=()=>{z.value||C(),P=[]}})).catch((e=>{console.error("Error accessing media devices.",e)}))}));const C=()=>{const e=new Blob(P,{type:"audio/mpeg"}),a=URL.createObjectURL(e),t=Math.max(Math.floor((new Date-E.value)/1e3),1);!async function(e,a,t,s){const l=new FormData;l.append("audio_file",e);try{const e=await fetch("https://zxgz.newfeifan.cn/voice2text/",{method:"POST",body:l}),a=await e.json();a.success&&(T.value[s].transcription=a.transcription),console.log("Server response:",a)}catch(o){console.error("Error sending audio file:",o)}}(e,0,0,T.value.push({duration:t,audioUrl:a,transcription:""})-1),s((()=>{let e=document.getElementById("messages");e.scrollTop=e.scrollHeight}))},F=()=>{const e=Math.floor((new Date-E.value)/1e3);R.value=e+"s",e>=60&&S(new Event("mouseup"))},O=e=>{U.value||"mousedown"!==e.type&&"touchstart"!==e.type||(E.value=new Date,U.value=!0,A=setInterval(F,1e3),R.value="1s",e.preventDefault(),B.value=e.touches?e.touches[0].clientY:0,P=[],z.value=!1,L.start())},S=e=>{!U.value||"mouseup"!==e.type&&"touchend"!==e.type&&"mouseleave"!==e.type&&"touchcancel"!==e.type||(clearInterval(A),U.value=!1,R.value="",new Date-E.value>=500?L.stop():console.log("录音时间太短,不保存"))},Y=()=>{U.value&&(clearInterval(A),console.log("录音取消"),z.value=!0,L.stop(),$())},$=()=>{U.value=!1,R.value=0,E.value=null,P=[]},G=e=>{const a=e.touches[0].clientY;B.value-a>280&&Y()};return(e,a)=>{const t=g,s=w,j=m,M=y,D=u(_("s-layout"),b);return l(),o(D,{class:"chat-wrap",title:"语音转文字"},{default:n((()=>[c(j,{class:"chat-container"},{default:n((()=>[c(j,{class:"messages",style:r({height:k+"px"}),id:"messages"},{default:n((()=>[(l(!0),i(p,null,d(T.value,((e,a)=>(l(),o(j,{key:a,class:"message",onClick:e=>(e=>{const a=T.value[e];new Audio(a.audioUrl).play()})(a)},{default:n((()=>[c(j,{class:"bubble"},{default:n((()=>[c(t,{class:"duration"},{default:n((()=>[v(f(e.duration)+'" ',1)])),_:2},1024),c(t,{class:"ss-m-l-10"},{default:n((()=>[c(s,{src:x,class:"audioPaly"})])),_:1})])),_:2},1024),null!=e.transcription&&""!=e.transcription?(l(),o(j,{key:0,class:"text"},{default:n((()=>[v(f(e.transcription),1)])),_:2},1024)):h("",!0)])),_:2},1032,["onClick"])))),128))])),_:1},8,["style"]),c(j,{class:"input-area"},{default:n((()=>[c(M,{onMousedown:O,onMouseup:S,onMouseleave:Y,onTouchstart:O,onTouchend:S,onTouchcancel:Y,onTouchmove:G},{default:n((()=>[v(" 按住 说话 ")])),_:1}),U.value?(l(),o(j,{key:0,class:"recording-overlay"},{default:n((()=>[c(j,null,{default:n((()=>[v(f(R.value),1)])),_:1}),c(j,null,{default:n((()=>[v("上滑至此取消")])),_:1})])),_:1})):h("",!0)])),_:1})])),_:1})])),_:1})}}},[["__scopeId","data-v-d67bddcf"]]);export{M as default};