WXL
3 天以前 9a5dd255e3e4530e3330fa2fc8f1de3befa426ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<template>
  <div class="call-container">
    <!-- 号码输入 -->
    <input
      v-model="phoneNumber"
      type="text"
      placeholder="输入电话号码"
      @keyup.enter="startCall"
    >
 
    <!-- 呼叫按钮 -->
    <button
      :class="['call-btn', { 'calling': isCalling }]"
      @click="startCall"
    >
      {{ isCalling ? '通话中...' : '一键呼叫' }}
    </button>
 
    <!-- 挂断按钮 -->
    <button
      v-if="isCalling"
      class="end-call-btn"
      @click="endCall"
    >
      挂断
    </button>
 
    <!-- 音频元素(隐藏) -->
    <audio id="remoteAudio" autoplay></audio>
 
    <!-- 状态显示 -->
    <div class="call-status">
      {{ callStatus }}
    </div>
  </div>
</template>
 
<script>
import sipService from '@/utils/sipService'
 
export default {
  data() {
    return {
      phoneNumber: '',
      isCalling: false,
      callStatus: '准备就绪',
      sipConfig: {
        wsUrl: 'wss://192.168.100.6:7443',
        sipUri: '1000@192.168.100.6',
        password: 'Smartor@2023',
        displayName: 'Web 小龙',
        realm: '192.168.100.6:8090'
      }
    }
  },
  mounted() {
    // 初始化SIP连接
    sipService.init(this.sipConfig)
  },
  beforeDestroy() {
    // 组件销毁时结束通话
    this.endCall()
  },
  methods: {
    // 开始呼叫
    async startCall() {
      if (!this.phoneNumber) {
        this.callStatus = '请输入电话号码'
        return
      }
      try {
        this.isCalling = true
        this.callStatus = '呼叫中...'
        // 调用SIP服务
        sipService.makeCall(this.phoneNumber)
 
        this.callStatus = '通话已建立'
      } catch (error) {
        console.error('呼叫失败:', error)
        this.callStatus = `呼叫失败: ${error.message}`
        this.isCalling = false
      }
    },
 
    // 结束通话
    endCall() {
      sipService.endCall()
      this.isCalling = false
      this.callStatus = '通话已结束'
    }
  }
}
</script>
 
<style scoped>
.call-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 300px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
}
 
input {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
 
.call-btn {
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
 
.call-btn:hover {
  background-color: #45a049;
}
 
.call-btn.calling {
  background-color: #2196F3;
}
 
.end-call-btn {
  padding: 10px;
  background-color: #f44336;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
 
.end-call-btn:hover {
  background-color: #d32f2f;
}
 
.call-status {
  margin-top: 10px;
  font-size: 14px;
  color: #666;
}
</style>