WXL (wul)
5 天以前 17efc8b0fb7d3fa66eb8e22f32b81e3e14bcd7f6
src/utils/sipService.js
@@ -1,5 +1,21 @@
import JsSIP from "jssip";
import { Notification, MessageBox, Message, Loading } from "element-ui";
// 医院机构与SIP服务器映射配置
const HOSPITAL_CONFIG = {
  丽水市中医院: {
    wsUrl: "wss://192.168.10.124:7443",
    domain: "192.168.10.124",
  },
  龙泉市人民医院: {
    wsUrl: "wss://10.10.0.220:7443",
    domain: "10.10.0.220",
  },
  // 可以继续添加其他医院配置
  default: {
    wsUrl: "wss://192.168.10.124:7443",
    domain: "192.168.10.124",
  },
};
class SipService {
  constructor() {
    this.ua = null;
@@ -7,17 +23,42 @@
    this.onStatusChange = null;
    this.onCallStatusChange = null;
    this.onIncomingCall = null;
    this.isRegistered = false; // 新增注册状态标志
    this.registrationTime = null; // 新增注册成功时间戳
    this.currentConfig = null; // 存储当前配置
  }
  init(config) {
  // 获取医院配置方法
  getHospitalConfig() {
    const orgName=localStorage.getItem("orgname");
    return HOSPITAL_CONFIG[orgName] || HOSPITAL_CONFIG.default;
  }
  init(baseConfig) {
    try {
      this.updateStatus("connecting", "连接中;...");
      // 获取机构名称,如果没有传入则从localStorage读取
      const orgName = baseConfig.orgName || localStorage.getItem("orgname");
      // 根据机构名称获取对应的服务器配置
      const hospitalConfig = this.getHospitalConfig(orgName);
console.log(hospitalConfig,'88');
      // 合并配置
      this.currentConfig = {
        ...baseConfig,
        ...hospitalConfig,
      };
      console.log(
        `当前机构: ${orgName}, 使用服务器: ${this.currentConfig.domain}`
      );
      this.updateStatus("connecting", "连接中...");
console.log(baseConfig.sipUri,'baseConfig.sipUri');
      this.ua = new JsSIP.UA({
        sockets: [new JsSIP.WebSocketInterface(config.wsUrl)],
        uri: config.sipUri,
        password: config.password,
        display_name: config.displayName,
        sockets: [new JsSIP.WebSocketInterface(this.currentConfig.wsUrl)],
        uri: baseConfig.sipUri, // 这里使用基础的sipUri,domain部分会被动态替换
        password: baseConfig.password,
        display_name: baseConfig.displayName,
        iceServers: [],
        register: true,
        sessionExpires: 1800,
@@ -28,12 +69,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 +103,33 @@
      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) {
      Message.error(reason);
      return Promise.reject(new Error(reason));
    }
    return new Promise((resolve, reject) => {
      try {
        if (!this.ua) {
@@ -60,7 +139,8 @@
        if (!this.ua.isRegistered()) {
          throw new Error("SIP未注册,无法呼叫");
        }
        const targetUri = `sip:${targetNumber}@${this.currentConfig.domain}`;
        console.log(`呼叫目标: ${targetUri}`);
        const options = {
          sessionTimers: true, // 启用会话计时器
          sessionTimersExpires: 150,
@@ -83,10 +163,7 @@
          },
        };
        this.currentSession = this.ua.call(
          `sip:${targetNumber}@192.169.129.198`,
          options
        );
        this.currentSession = this.ua.call(targetUri, options);
        this.setupPeerConnection(this.currentSession);
        this.setupAudio(this.currentSession);
@@ -165,7 +242,7 @@
        errorMessage = "会话参数不满足服务器要求";
        break;
      default:
        errorMessage = `呼叫失败: ${e.cause || e.message}`;
        errorMessage = `呼叫失败3: ${e.cause || e.message}`;
    }
    this.updateCallStatus("failed55", errorMessage);