WXL (wul)
7 天以前 57c1e943c1e20c57ff69945216a133a5ec2127f7
测试完成
已修改2个文件
62 ■■■■■ 文件已修改
src/components/CallButton/index.vue 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/sipService.js 47 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/components/CallButton/index.vue
@@ -61,6 +61,15 @@
      };
      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}`;
    },
@@ -94,6 +103,12 @@
      }
      try {
        // 先检查是否可以呼叫
        const { canCall, reason } = sipService.canMakeCall();
        if (!canCall) {
          this.$message.warning(reason);
          return;
        }
        this.callStatus = "calling";
        this.isCalling = true;
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,22 @@
      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(); // 记录注册成功时间
        this.updateStatus("registered", "已注册");
      });
      this.ua.on("registrationFailed", (e) => {
        this.isRegistered = false;
        this.updateStatus("failed", `注册失败: ${e.cause}`);
      });
      this.ua.on("unregistered", () => {
        this.isRegistered = false;
        this.updateStatus("disconnected", "已注销");
      });
      this.ua.on("disconnected", () =>
        this.updateStatus("disconnected", "连接断开")
      );
@@ -49,8 +61,31 @@
      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) {