WXL (wul)
19 小时以前 f2bd87968bf34e0a952870701593df39d6ff2075
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",
        wsUrl: "wss://192.168.10.124:7443",
        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;
        // 正确设置 sipUri
        this.sipConfig.sipUri = `${this.randomNum}@192.168.10.124`;
        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>