From f2bd87968bf34e0a952870701593df39d6ff2075 Mon Sep 17 00:00:00 2001
From: WXL (wul) <wl_5969728@163.com>
Date: 星期三, 17 九月 2025 15:19:21 +0800
Subject: [PATCH] 分机号动态使用

---
 src/utils/sipService.js |   53 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/src/utils/sipService.js b/src/utils/sipService.js
index 8589481..a8ce592 100644
--- a/src/utils/sipService.js
+++ b/src/utils/sipService.js
@@ -7,6 +7,8 @@
     this.onStatusChange = null;
     this.onCallStatusChange = null;
     this.onIncomingCall = null;
+    this.isRegistered = false; // 鏂板娉ㄥ唽鐘舵�佹爣蹇�
+    this.registrationTime = null; // 鏂板娉ㄥ唽鎴愬姛鏃堕棿鎴�
   }
 
   init(config) {
@@ -28,12 +30,25 @@
       this.ua.start();
 
       // 浜嬩欢鐩戝惉
-      this.ua.on("registered", () =>
-        this.updateStatus("registered", "宸叉敞鍐�56")
-      );
-      this.ua.on("registrationFailed", (e) =>
-        this.updateStatus("failed", `娉ㄥ唽澶辫触11: ${e.cause}`)
-      );
+      this.ua.on("registered", () => {
+        this.isRegistered = true;
+        this.registrationTime = Date.now(); // 璁板綍娉ㄥ唽鎴愬姛鏃堕棿
+        console.log(this.registrationTime, "娉ㄥ唽鏃堕棿");
+
+        this.updateStatus("registered", "宸叉敞鍐�");
+      });
+
+      this.ua.on("registrationFailed", (e) => {
+        this.isRegistered = false;
+        this.updateStatus("failed", `娉ㄥ唽澶辫触: ${e.cause}`);
+      });
+
+      this.ua.on("unregistered", () => {
+        this.isRegistered = false;
+        let registrationTime = Date.now(); // 璁板綍娉ㄩ攢鎴愬姛鏃堕棿
+        console.log(registrationTime, "娉ㄩ攢鏃堕棿");
+        this.updateStatus("disconnected", "宸叉敞閿�");
+      });
       this.ua.on("disconnected", () =>
         this.updateStatus("disconnected", "杩炴帴鏂紑")
       );
@@ -49,8 +64,30 @@
       throw error;
     }
   }
+  // 鏂板鏂规硶锛氭鏌ユ槸鍚﹀彲浠ュ懠鍙�
+  canMakeCall(minDelay = 2000) {
+    if (!this.isRegistered) {
+      return { canCall: false, reason: "SIP鏈敞鍐岋紝鏃犳硶鍛煎彨" };
+    }
 
+    const now = Date.now();
+    const timeSinceRegistration = now - this.registrationTime;
+
+    if (timeSinceRegistration < minDelay) {
+      const remaining = minDelay - timeSinceRegistration;
+      return {
+        canCall: false,
+        reason: `娉ㄥ唽鎴愬姛锛岃绛夊緟 ${Math.ceil(remaining / 1000)} 绉掑悗鍐嶅懠鍙玚,
+      };
+    }
+
+    return { canCall: true, reason: "" };
+  }
   makeCall(targetNumber) {
+    const { canCall, reason } = this.canMakeCall();
+    if (!canCall) {
+      return Promise.reject(new Error(reason));
+    }
     return new Promise((resolve, reject) => {
       try {
         if (!this.ua) {
@@ -84,7 +121,7 @@
         };
 
         this.currentSession = this.ua.call(
-          `sip:${targetNumber}@1192.170.66.107`,
+          `sip:${targetNumber}@192.168.10.124`,
           options
         );
 
@@ -165,7 +202,7 @@
         errorMessage = "浼氳瘽鍙傛暟涓嶆弧瓒虫湇鍔″櫒瑕佹眰";
         break;
       default:
-        errorMessage = `鍛煎彨澶辫触: ${e.cause || e.message}`;
+        errorMessage = `鍛煎彨澶辫触3: ${e.cause || e.message}`;
     }
 
     this.updateCallStatus("failed55", errorMessage);

--
Gitblit v1.9.3