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;
@@ -9,17 +25,40 @@
    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,
@@ -77,7 +116,9 @@
      const remaining = minDelay - timeSinceRegistration;
      return {
        canCall: false,
        reason: `注册成功,请等待 ${Math.ceil(remaining / 1000)} 秒后再呼叫`,
        reason: `注册成功,资源加载中请等待 ${Math.ceil(
          remaining / 1000
        )} 秒后再呼叫`,
      };
    }
@@ -86,6 +127,7 @@
  makeCall(targetNumber) {
    const { canCall, reason } = this.canMakeCall();
    if (!canCall) {
      Message.error(reason);
      return Promise.reject(new Error(reason));
    }
    return new Promise((resolve, reject) => {
@@ -97,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,
@@ -120,10 +163,7 @@
          },
        };
        this.currentSession = this.ua.call(
          `sip:${targetNumber}@1192.170.66.107`,
          options
        );
        this.currentSession = this.ua.call(targetUri, options);
        this.setupPeerConnection(this.currentSession);
        this.setupAudio(this.currentSession);