ai-clone/frontend-ai/unpackage/dist/build/mp-weixin/pages/phone-call/phone-call.js
2026-03-06 18:05:51 +08:00

2 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";const e=require("../../common/vendor.js"),t=require("../../config/api.js"),o=require("../../common/assets.js"),i={data:()=>({API_BASE:t.API_BASE,lastUserId:null,loading:!1,videos:[],selectedVideoId:"",selectedVideoName:"",selectedVideoUrl:"",selectedVoiceId:"",selectedDialect:"",selectedLanguageHint:"",selectedLanguageHintLabel:"",languageHintOptions:["中文(zh)","英文(en)","法语(fr)","德语(de)","日语(ja)","韩语(ko)","俄语(ru)"],dialectOptions:["广东话","东北话","甘肃话","贵州话","河南话","湖北话","江西话","闽南话","宁夏话","山西话","陕西话","山东话","上海话","四川话","天津话","云南话"],callStarted:!1,callStatus:"视频通话中",callDuration:"00:00",callStartTime:null,durationTimer:null,messages:[],scrollTop:0,isRecording:!1,isProcessing:!1,isSpeaking:!1,processingText:"处理中...",recorderManager:null,audioFilePath:"",audioContext:null,showSettingsDialog:!1,dialogMemoryIdentity:"",dialogMemoryInfo:"",dialogMemoryCatchphrase:""}),onLoad(){console.log("[PhoneCall] 页面加载");const t=e.index.getStorageSync("userId");this.lastUserId=t,this.loadVideos(),this.initRecorder(),this.initAudioContext()},onShow(){console.log("[PhoneCall] 页面显示");const t=e.index.getStorageSync("userId");this.lastUserId=t,this.callStarted||this.$nextTick((()=>{setTimeout((()=>{this.loadVideos()}),100)}))},onUnload(){this.cleanup()},methods:{async loadVideos(){this.loading=!0;const t=e.index.getStorageSync("userId")||"",o=e.index.getStorageSync("token")||"";e.index.request({url:`${this.API_BASE}/api/photo-revival/videos`,method:"GET",header:{"X-User-Id":t,Authorization:o?`Bearer ${o}`:""},success:e=>{if(console.log("[PhoneCall] 视频列表响应状态码:",e.statusCode),console.log("[PhoneCall] 视频列表响应数据:",e.data),console.log("[PhoneCall] 数据类型:",typeof e.data),200===e.statusCode){let t=[];Array.isArray(e.data)?t=e.data:e.data&&Array.isArray(e.data.data)?t=e.data.data:e.data&&e.data.videos&&Array.isArray(e.data.videos)&&(t=e.data.videos),t=t.filter((e=>{const t=e.name&&(e.name.startsWith("生成失败")||e.name.startsWith("生成失败:")),o=e.edited_video_url||e.video_url||e.local_video_path||e.videoUrl||e.localVideoPath,i=o&&""!==o.trim();return!t&&i})),this.videos=t,console.log("[PhoneCall] 加载视频列表成功:",this.videos.length),console.log("[PhoneCall] 视频列表:",this.videos)}else console.error("[PhoneCall] 响应状态码异常:",e.statusCode)},fail:t=>{console.error("[PhoneCall] 加载视频列表失败:",t),e.index.showToast({title:"加载失败",icon:"none"})},complete:()=>{this.loading=!1}})},selectVideo(e){console.log("[PhoneCall] 选择视频:",e),this.selectedVideoId=e.id,this.selectedVideoName=e.name||"复活视频",this.selectedVideoUrl=e.edited_video_url||e.local_video_path||e.video_url,this.selectedVoiceId=e.voice_id,this.selectedDialect="",this.selectedLanguageHint="",this.selectedLanguageHintLabel="",console.log("[PhoneCall] 选择视频:",this.selectedVideoId,"名称:",this.selectedVideoName),console.log("[PhoneCall] 视频URL:",this.selectedVideoUrl)},onDialectChange(e){this.selectedDialect=this.dialectOptions[e.detail.value]||""},onLanguageHintChange(e){const t=this.languageHintOptions[e.detail.value]||"";this.selectedLanguageHintLabel=t;const o=t.match(/\(([^)]+)\)/);this.selectedLanguageHint=o&&o[1]?o[1]:""},startCall(){if(!this.selectedVideoId)return void e.index.showToast({title:"请先选择视频",icon:"none"});console.log("[PhoneCall] 准备视频通话视频ID:",this.selectedVideoId);const t=this.videos.find((e=>e.id===this.selectedVideoId));if(!t)return void e.index.showToast({title:"视频信息丢失",icon:"none"});const o=t.edited_video_url||t.videoUrl||t.local_video_path||t.video_url||t.localVideoPath||"",i=t.name||"复活视频",s=this.selectedVoiceId||"";console.log("[PhoneCall] 跳转参数:",{videoId:this.selectedVideoId,videoName:i,videoUrl:o,voiceId:s}),e.index.navigateTo({url:`/pages/video-call-new/video-call-new?videoId=${this.selectedVideoId}&videoName=${encodeURIComponent(i)}&videoUrl=${encodeURIComponent(o)}&voiceId=${s}`,success:()=>{console.log("[PhoneCall] 跳转到视频通话页面成功")},fail:t=>{console.error("[PhoneCall] 跳转失败:",t),e.index.showToast({title:"跳转失败",icon:"none"})}})},closeSettingsDialog(){this.showSettingsDialog=!1},confirmSettings(){this.showSettingsDialog=!1},startCallTimer(){this.callStartTime=Date.now(),this.durationTimer=setInterval((()=>{const e=Math.floor((Date.now()-this.callStartTime)/1e3),t=Math.floor(e/60).toString().padStart(2,"0"),o=(e%60).toString().padStart(2,"0");this.callDuration=`${t}:${o}`}),1e3)},initRecorder(){this.recorderManager=e.index.getRecorderManager(),this.recorderManager.onStart((()=>{console.log("[PhoneCall] 开始录音")})),this.recorderManager.onStop((e=>{console.log("[PhoneCall] 录音完成:",e.tempFilePath),this.audioFilePath=e.tempFilePath,this.processConversation()})),this.recorderManager.onError((t=>{console.error("[PhoneCall] 录音失败:",t),this.isRecording=!1,e.index.showToast({title:"录音失败",icon:"none"})}))},initAudioContext(){this.audioContext=e.index.createInnerAudioContext(),this.audioContext.onPlay((()=>{console.log("[PhoneCall] 开始播放AI回复"),this.isSpeaking=!0,this.callStatus="对方正在说话...",e.index.vibrateShort({type:"light"})})),this.audioContext.onEnded((()=>{console.log("[PhoneCall] AI回复播放完成"),this.isSpeaking=!1,this.callStatus="通话中"})),this.audioContext.onError((e=>{console.error("[PhoneCall] 音频播放失败:",e),this.isSpeaking=!1,this.callStatus="通话中"}))},startRecording(){this.isProcessing||(this.isRecording=!0,this.recorderManager.start({format:"mp3",sampleRate:16e3}),this.callStatus="正在录音...",e.index.vibrateShort({type:"medium"}))},stopRecording(){this.isRecording&&(this.isRecording=!1,this.recorderManager.stop(),this.isProcessing=!0,this.processingText="识别中...",this.callStatus="处理中...")},async processConversation(){try{this.processingText="正在识别...";const t=e.index.getStorageSync("userId")||"",o=e.index.getStorageSync("token")||"";e.index.uploadFile({url:`${this.API_BASE}/api/conversation/talk`,filePath:this.audioFilePath,name:"audio",header:{"X-User-Id":t,Authorization:o?`Bearer ${o}`:""},formData:(()=>{const e=this.selectedVoiceId,t=(()=>{const t=(e||"").trim();if(!t)return"CLONE";return["Cherry","Kai","Mochi","Bunny","Jada","Dylan","Li","Marcus","Roy","Peter","Sunny","Eric","Rocky","Kiki"].includes(t)||t.startsWith("BV")||t.endsWith("_streaming")||t.endsWith("_offline")||t.endsWith("_bigtts")?"OFFICIAL":"CLONE"})(),o={voiceId:e,voiceType:t};return this.selectedDialect&&(o.dialect=this.selectedDialect),this.selectedLanguageHint&&(o.languageHints=this.selectedLanguageHint),o})(),success:e=>{if(console.log("[PhoneCall] 对话响应:",e),200!==e.statusCode)throw new Error("对话请求失败");{const t=JSON.parse(e.data);if(!t.success)throw new Error(t.message||"对话失败");this.addMessage("user",t.recognizedText),this.addMessage("ai",t.aiResponse),this.playAIResponse(t.audioFile)}},fail:t=>{console.error("[PhoneCall] 对话失败:",t),e.index.showToast({title:"对话失败",icon:"none"}),this.isProcessing=!1,this.callStatus="通话中"}})}catch(t){console.error("[PhoneCall] 对话失败:",t),e.index.showToast({title:"对话失败: "+t.message,icon:"none"}),this.isProcessing=!1,this.callStatus="通话中"}},playAIResponse(e){this.processingText="正在回复...",this.audioContext.src=`${this.API_BASE}/api/conversation/audio/${e}`,this.audioContext.play(),this.isProcessing=!1},addMessage(e,t){const o=new Date,i=`${o.getHours()}:${o.getMinutes().toString().padStart(2,"0")}`;this.messages.push({role:e,content:t,time:i}),this.$nextTick((()=>{this.scrollTop=999999}))},handleClearHistory(){e.index.showModal({title:"确认清空",content:"确定要清空对话历史吗?",success:t=>{if(t.confirm){const t=e.index.getStorageSync("userId")||"",o=e.index.getStorageSync("token")||"";e.index.request({url:`${this.API_BASE}/api/conversation/clear-history`,method:"POST",header:{"X-User-Id":t,Authorization:o?`Bearer ${o}`:""},success:t=>{t.data.success&&(this.messages=[],e.index.showToast({title:"已清空",icon:"success"}))}})}}})},endCall(){e.index.showModal({title:"结束通话",content:`通话时长:${this.callDuration}\n确定要结束通话吗?`,confirmColor:"#eb3349",success:t=>{t.confirm&&(e.index.vibrateShort({type:"heavy"}),e.index.showLoading({title:"正在挂断...",mask:!0}),setTimeout((()=>{e.index.hideLoading(),this.cleanup(),e.index.showToast({title:"通话已结束",icon:"success",duration:1500,success:()=>{setTimeout((()=>{e.index.navigateBack()}),1500)}})}),800))}})},goToRevival(){console.log("[PhoneCall] 跳转到复活照片页面"),e.index.navigateTo({url:"/pages/revival/revival-original",success:()=>{console.log("[PhoneCall] 跳转成功")},fail:e=>{console.error("[PhoneCall] 跳转失败:",e)}})},cleanup(){this.durationTimer&&clearInterval(this.durationTimer),this.audioContext&&this.audioContext.destroy(),this.recorderManager&&this.recorderManager.stop()},formatTime(e){if(!e)return"";let t=e;String(e).length<=10&&(t=1e3*e);const o=new Date(t);if(isNaN(o.getTime()))return"时间格式错误";return`${o.getFullYear()}${String(o.getMonth()+1).padStart(2,"0")}${String(o.getDate()).padStart(2,"0")}${String(o.getHours()).padStart(2,"0")}:${String(o.getMinutes()).padStart(2,"0")}`}}};const s=e._export_sfc(i,[["render",function(t,i,s,a,n,l){return e.e({a:e.o(((...e)=>l.goToRevival&&l.goToRevival(...e))),b:!n.callStarted},n.callStarted?e.e({k:n.selectedVideoUrl},n.selectedVideoUrl?{l:n.selectedVideoUrl}:{},{m:!n.selectedVideoUrl},(n.selectedVideoUrl,{}),{n:e.t(n.selectedVideoName),o:e.t(n.callStatus),p:n.isSpeaking||n.isRecording?1:"",q:e.t(n.callDuration),r:e.f(n.messages,((t,o,i)=>({a:e.t("user"===t.role?"👤":"🤖"),b:e.t(t.content),c:e.t(t.time),d:o,e:e.n(t.role)}))),s:0===n.messages.length},(n.messages.length,{}),{t:n.scrollTop,v:n.selectedVoiceId&&n.selectedVoiceId.startsWith("cosyvoice-v3-plus-")},n.selectedVoiceId&&n.selectedVoiceId.startsWith("cosyvoice-v3-plus-")?{w:e.t(n.selectedDialect||"请选择方言(可选)"),x:n.dialectOptions,y:e.o(((...e)=>l.onDialectChange&&l.onDialectChange(...e)))}:{},{z:n.selectedVoiceId&&n.selectedVoiceId.startsWith("cosyvoice-v3-plus-")},n.selectedVoiceId&&n.selectedVoiceId.startsWith("cosyvoice-v3-plus-")?{A:e.t(n.selectedLanguageHintLabel||"请选择语言(可选)"),B:n.languageHintOptions,C:e.o(((...e)=>l.onLanguageHintChange&&l.onLanguageHintChange(...e)))}:{},{D:!n.isRecording&&!n.isProcessing},n.isRecording||n.isProcessing?{}:{E:e.o(((...e)=>l.startRecording&&l.startRecording(...e))),F:e.o(((...e)=>l.stopRecording&&l.stopRecording(...e)))},{G:n.isRecording},n.isRecording?{H:e.o(((...e)=>l.stopRecording&&l.stopRecording(...e)))}:{},{I:n.isProcessing},n.isProcessing?{J:e.t(n.processingText)}:{},{K:o._imports_0,L:e.o(((...e)=>l.handleClearHistory&&l.handleClearHistory(...e))),M:e.o(((...e)=>l.endCall&&l.endCall(...e)))}):e.e({c:n.loading},n.loading?{}:n.videos.length>0?{e:e.f(n.videos,((t,o,i)=>e.e({a:e.t(t.name||"复活视频"),b:e.t(t.text||"暂无描述"),c:e.t(l.formatTime(t.create_time)),d:n.selectedVideoId===t.id},(n.selectedVideoId,t.id,{}),{e:t.id,f:e.n(n.selectedVideoId===t.id?"selected":""),g:e.o((e=>l.selectVideo(t)),t.id)})))}:{},{d:n.videos.length>0,f:n.videos.length>0},n.videos.length>0?e.e({g:n.selectedVideoId},(n.selectedVideoId,{}),{h:!n.selectedVideoId,i:e.o(((...e)=>l.startCall&&l.startCall(...e)))}):{},{j:n.videos.length>0},(n.videos.length,{})),{N:n.showSettingsDialog},n.showSettingsDialog?{O:e.o(((...e)=>l.closeSettingsDialog&&l.closeSettingsDialog(...e))),P:n.dialogMemoryIdentity,Q:e.o((e=>n.dialogMemoryIdentity=e.detail.value)),R:n.dialogMemoryInfo,S:e.o((e=>n.dialogMemoryInfo=e.detail.value)),T:n.dialogMemoryCatchphrase,U:e.o((e=>n.dialogMemoryCatchphrase=e.detail.value)),V:e.o(((...e)=>l.closeSettingsDialog&&l.closeSettingsDialog(...e))),W:e.o(((...e)=>l.confirmSettings&&l.confirmSettings(...e))),X:e.o((()=>{})),Y:e.o(((...e)=>l.closeSettingsDialog&&l.closeSettingsDialog(...e)))}:{})}],["__scopeId","data-v-a48cd222"]]);wx.createPage(s);