From 56c5f1f9c7f9ec8d229faf8139093698be65ec87 Mon Sep 17 00:00:00 2001 From: WXL (wul) <wl_5969728@163.com> Date: 星期四, 18 九月 2025 09:42:42 +0800 Subject: [PATCH] 测试完成 --- src/components/CallButton/index.vue | 144 ++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 121 insertions(+), 23 deletions(-) diff --git a/src/components/CallButton/index.vue b/src/components/CallButton/index.vue index ca7639a..d3cb005 100644 --- a/src/components/CallButton/index.vue +++ b/src/components/CallButton/index.vue @@ -28,60 +28,84 @@ <script> import sipService from "@/utils/sipService"; +import { CallsetState, CallgetList } from "@/api/AiCentre/index"; export default { props: { phoneNumber: { type: String, - default: '' - } + default: "", + }, }, data() { + const randomNum = Math.floor(Math.random() * 20) + 1000; // 瀹氫箟闅忔満鍒嗘満鍙� return { isCalling: false, - callStatus: 'idle', // idle, calling, connected, ended + randomNum: randomNum, + randomID: null, + callStatus: "idle", // idle, calling, connected, ended sipStatus: "鏈繛鎺�", sipStatusClass: "status-disconnected", - randomNum = Math.floor(Math.random() * 21) + 1000, // 鐢熸垚 1000-1020 鐨勯殢鏈烘暣鏁� sipConfig: { wsUrl: "wss://192.168.100.6:7443", - sipUri: `${randomNum}`+"@192.168.100.6", + sipUri: "", password: "Smartor@2023", displayName: "Web 灏忛緳", + // realm: "9.208.5.18:8090", }, }; }, computed: { callStatusText() { const statusMap = { - idle: '鍑嗗灏辩华', - calling: '鍛煎彨涓�...', - connected: '閫氳瘽涓�', - ended: '閫氳瘽缁撴潫' + idle: "鍑嗗灏辩华", + calling: "鍛煎彨涓�...", + connected: "閫氳瘽涓�", + ended: "閫氳瘽缁撴潫", }; return statusMap[this.callStatus]; + }, + countdownText() { + if (this.sipStatus !== "宸叉敞鍐�") return ""; + + const { canCall, reason } = sipService.canMakeCall(); + if (!canCall && reason.includes("绛夊緟")) { + return reason; + } + return ""; }, callStatusClass() { return `status-${this.callStatus}`; }, callButtonText() { return this.isCalling ? "閫氳瘽涓�..." : "涓�閿懠鍙�"; - } + }, }, - mounted() { + created() { + // CallgetList(); + }, + + async mounted() { + await this.CallgetList(); sipService.init(this.sipConfig); + // 璁剧疆鐘舵�佸洖璋� sipService.onStatusChange = (status) => { this.sipStatus = status.text; this.sipStatusClass = `status-${status.type}`; + + // 澶勭悊娉ㄥ唽澶辫触鍜屾柇寮�杩炴帴鎯呭喌 + if (status.type === "failed" || status.type === "disconnected") { + this.overCallsetState(); // 閲婃斁鍒嗘満鍙� + } }; // 鐩戝惉閫氳瘽鐘舵�佸彉鍖� sipService.onCallStatusChange = (status) => { this.callStatus = status.type; - this.isCalling = status.type === 'calling' || status.type === 'connected'; + this.isCalling = status.type === "calling" || status.type === "connected"; // 閫氱煡鐖剁粍浠堕�氳瘽鐘舵�佸彉鍖� - this.$emit('call-status-change', status); + this.$emit("call-status-change", status); }; }, methods: { @@ -92,25 +116,99 @@ } try { - this.callStatus = 'calling'; + // 鍏堟鏌ユ槸鍚﹀彲浠ュ懠鍙� + const { canCall, reason } = sipService.canMakeCall(); + if (!canCall) { + const { canCall, reason } = sipService.canMakeCall(); + //this.$message.warning(reason); + //return; + } + this.callStatus = "calling"; this.isCalling = true; + console.log("寮�濮嬪懠鍙細", sipService); - await sipService.makeCall(this.phoneNumber); - + await sipService.makeCall("0" + this.phoneNumber); } catch (error) { - console.error("鍛煎彨澶辫触:", error); - this.callStatus = 'ended'; - this.isCalling = false; - this.$message.error(`鍛煎彨澶辫触: ${error.message}`); + let registrationTime = Date.now(); // 璁板綍娉ㄩ攢鎴愬姛鏃堕棿 + console.log(registrationTime, "鍛煎彨澶辫触鏃堕棿"); + console.error("鍛煎彨澶辫触1:", error); + // this.callStatus = "ended"; + // this.isCalling = false; + //this.$message.error(`鍛煎彨澶辫触: ${error.message}`); + try { + // 鍏堟鏌ユ槸鍚﹀彲浠ュ懠鍙� + const { canCall, reason } = sipService.canMakeCall(); + if (!canCall) { + const { canCall, reason } = sipService.canMakeCall(); + } + this.callStatus = "calling"; + this.isCalling = true; + console.log("寮�濮嬪懠鍙細", sipService); + + await sipService.makeCall("0" + this.phoneNumber); + } catch (error) { + this.callStatus = "ended"; + this.isCalling = false; + } + } + }, + // 鏌ヨ鍙敤鍒嗘満鍙� + async CallgetList() { + try { + const res = await CallgetList(); + this.randomNum = res.data[0].tel; + this.randomID = res.data[0].id; + // 姝g‘璁剧疆 sipUri + this.sipConfig.sipUri = `${this.randomNum}@192.168.100.6`; + this.startCallsetState(); + } catch (error) { + console.error("鑾峰彇鍒嗘満鍙峰け璐�:", error); + this.updateStatus("failed", "鑾峰彇鍒嗘満鍙峰け璐�"); + } + }, + async startCallsetState() { + try { + await CallsetState({ id: this.randomID, state: 1 }); + console.log("鍒嗘満鍙风姸鎬佹洿鏂颁负浣跨敤涓�"); + } catch (error) { + console.error("鏇存柊鍒嗘満鍙风姸鎬佸け璐�:", error); } }, + async overCallsetState() { + try { + if (this.randomID) { + await CallsetState({ id: this.randomID, state: 0 }); + console.log("鍒嗘満鍙风姸鎬佹洿鏂颁负鍙敤"); + } + } catch (error) { + console.error("閲婃斁鍒嗘満鍙峰け璐�:", error); + } + }, endCall() { sipService.endCall(); - this.callStatus = 'ended'; + this.callStatus = "ended"; this.isCalling = false; - } - } + }, + cleanupResources() { + // 缁撴潫閫氳瘽 + if (this.isCalling) { + sipService.endCall(); + } + + // 閲婃斁鍒嗘満鍙� + this.overCallsetState(); + + // 鏂紑 SIP 杩炴帴 + if (sipService.ua) { + sipService.ua.stop(); + } + }, + }, + beforeUnmount() { + // 缁勪欢閿�姣佹椂纭繚閲婃斁璧勬簮 + this.cleanupResources(); + }, }; </script> -- Gitblit v1.9.3