| | |
| | | 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; |
| | |
| | | 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, |
| | |
| | | this.ua.start(); |
| | | |
| | | // 事件监听 |
| | | // 事件监听 |
| | | this.ua.on("registered", () => { |
| | | this.isRegistered = true; |
| | | this.registrationTime = Date.now(); // 记录注册成功时间 |
| | | console.log(this.registrationTime, "注册时间"); |
| | | |
| | | this.updateStatus("registered", "已注册"); |
| | | }); |
| | | |
| | |
| | | |
| | | this.ua.on("unregistered", () => { |
| | | this.isRegistered = false; |
| | | let registrationTime = Date.now(); // 记录注销成功时间 |
| | | console.log(registrationTime, "注销时间"); |
| | | this.updateStatus("disconnected", "已注销"); |
| | | }); |
| | | this.ua.on("disconnected", () => |
| | |
| | | throw error; |
| | | } |
| | | } |
| | | // 新增方法:检查是否可以呼叫 |
| | | // 新增方法:检查是否可以呼叫 |
| | | canMakeCall(minDelay = 2000) { |
| | | if (!this.isRegistered) { |
| | | return { canCall: false, reason: "SIP未注册,无法呼叫" }; |
| | |
| | | const remaining = minDelay - timeSinceRegistration; |
| | | return { |
| | | canCall: false, |
| | | reason: `注册成功,请等待 ${Math.ceil(remaining / 1000)} 秒后再呼叫` |
| | | reason: `注册成功,资源加载中请等待 ${Math.ceil( |
| | | remaining / 1000 |
| | | )} 秒后再呼叫`, |
| | | }; |
| | | } |
| | | |
| | |
| | | makeCall(targetNumber) { |
| | | const { canCall, reason } = this.canMakeCall(); |
| | | if (!canCall) { |
| | | Message.error(reason); |
| | | return Promise.reject(new Error(reason)); |
| | | } |
| | | return new Promise((resolve, reject) => { |
| | |
| | | if (!this.ua.isRegistered()) { |
| | | throw new Error("SIP未注册,无法呼叫"); |
| | | } |
| | | |
| | | const targetUri = `sip:${targetNumber}@${this.currentConfig.domain}`; |
| | | console.log(`呼叫目标: ${targetUri}`); |
| | | const options = { |
| | | sessionTimers: true, // 启用会话计时器 |
| | | sessionTimersExpires: 150, |
| | |
| | | }, |
| | | }; |
| | | |
| | | 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); |
| | |
| | | errorMessage = "会话参数不满足服务器要求"; |
| | | break; |
| | | default: |
| | | errorMessage = `呼叫失败: ${e.cause || e.message}`; |
| | | errorMessage = `呼叫失败3: ${e.cause || e.message}`; |
| | | } |
| | | |
| | | this.updateCallStatus("failed55", errorMessage); |