WXL (wul)
16 小时以前 1aaa2392eea4694076c6a329a5b88436ceb50194
src/components/CallButton/index.vue
@@ -28,6 +28,7 @@
<script>
import sipService from "@/utils/sipService";
import { CallsetState, CallgetList } from "@/api/AiCentre/index";
export default {
  props: {
@@ -37,15 +38,17 @@
    },
  },
  data() {
    const randomNum = Math.floor(Math.random() * 20) + 1000; // 内部定义
    const randomNum = Math.floor(Math.random() * 20) + 1000; // 定义随机分机号
    return {
      isCalling: false,
      randomNum: randomNum,
      randomID: null,
      callStatus: "idle", // idle, calling, connected, ended
      sipStatus: "未连接",
      sipStatusClass: "status-disconnected",
      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",
@@ -78,13 +81,22 @@
      return this.isCalling ? "通话中..." : "一键呼叫";
    },
  },
  mounted() {
    console.log('当前分机号',this.sipConfig);
  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(); // 释放分机号
      }
    };
    // 监听通话状态变化
@@ -115,7 +127,7 @@
        this.isCalling = true;
        console.log("开始呼叫:", sipService);
        await sipService.makeCall("0"+this.phoneNumber);
        await sipService.makeCall("0" + this.phoneNumber);
      } catch (error) {
        let registrationTime = Date.now(); // 记录注销成功时间
        console.log(registrationTime, "呼叫失败时间");
@@ -123,7 +135,7 @@
        // this.callStatus = "ended";
        // this.isCalling = false;
        //this.$message.error(`呼叫失败: ${error.message}`);
          try {
        try {
          // 先检查是否可以呼叫
          const { canCall, reason } = sipService.canMakeCall();
          if (!canCall) {
@@ -133,19 +145,69 @@
          this.isCalling = true;
          console.log("开始呼叫:", sipService);
          await sipService.makeCall("0"+this.phoneNumber);
          await sipService.makeCall("0" + this.phoneNumber);
        } catch (error) {
           this.callStatus = "ended";
           this.isCalling = false;
          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;
        // 正确设置 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.isCalling = false;
    },
    cleanupResources() {
      // 结束通话
      if (this.isCalling) {
        sipService.endCall();
      }
      // 释放分机号
      this.overCallsetState();
      // 断开 SIP 连接
      if (sipService.ua) {
        sipService.ua.stop();
      }
    },
  },
  beforeUnmount() {
    // 组件销毁时确保释放资源
    this.cleanupResources();
  },
};
</script>