一些方法
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { defineStore } from "pinia"
|
||||
import { ref, computed } from "vue"
|
||||
import { chatDB } from "@/utils/db"
|
||||
import { useWebRTC } from "@/composables/useWebRTC"
|
||||
import { useUserStore } from "@/stores/user"
|
||||
import { sendMessage } from "@/utils/request.js"
|
||||
|
||||
export const useChatStore = defineStore("chat", () => {
|
||||
const friends = ref([])
|
||||
@@ -9,34 +12,20 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const connectionStatus = ref("disconnected")
|
||||
const socket = ref(null)
|
||||
|
||||
// 通话相关状态
|
||||
const currentCall = ref(null)
|
||||
const incomingCall = ref(null)
|
||||
const callStatus = ref("idle") // idle, calling, ringing, ongoing, ended
|
||||
|
||||
const webrtc = useWebRTC()
|
||||
const userStore = useUserStore()
|
||||
|
||||
// 计算属性
|
||||
const isConnected = computed(() => connectionStatus.value === "connected")
|
||||
|
||||
const connectionStatusText = computed(() => {
|
||||
switch (connectionStatus.value) {
|
||||
case "connected":
|
||||
return "已连接"
|
||||
case "connecting":
|
||||
return "连接中..."
|
||||
case "disconnected":
|
||||
return "已断开"
|
||||
default:
|
||||
return "未知状态"
|
||||
}
|
||||
})
|
||||
|
||||
const connectionStatusIcon = computed(() => {
|
||||
switch (connectionStatus.value) {
|
||||
case "connected":
|
||||
return "wifi"
|
||||
case "connecting":
|
||||
return "loading"
|
||||
case "disconnected":
|
||||
return "disconnect"
|
||||
default:
|
||||
return "question"
|
||||
}
|
||||
})
|
||||
const isInCall = computed(() => callStatus.value !== "idle")
|
||||
const isCalling = computed(() => callStatus.value === "calling")
|
||||
const isRinging = computed(() => callStatus.value === "ringing")
|
||||
const isOngoingCall = computed(() => callStatus.value === "ongoing")
|
||||
|
||||
// 初始化数据库
|
||||
const initDB = async () => {
|
||||
@@ -84,7 +73,7 @@ export const useChatStore = defineStore("chat", () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 设置当前好友 - 新增方法
|
||||
// 设置当前好友
|
||||
const setCurrentFriend = (friend) => {
|
||||
currentFriend.value = friend
|
||||
// 清除未读消息计数
|
||||
@@ -146,6 +135,12 @@ export const useChatStore = defineStore("chat", () => {
|
||||
|
||||
if (receiverId !== currentUserId) return
|
||||
|
||||
// 如果是通话信令,直接处理
|
||||
if (messageData.call_id || messageData.call_status) {
|
||||
handleCallSignal(messageData)
|
||||
return
|
||||
}
|
||||
|
||||
const newMessage = {
|
||||
type: getMessageType(messageData.message_type),
|
||||
content: messageData.content,
|
||||
@@ -188,24 +183,202 @@ export const useChatStore = defineStore("chat", () => {
|
||||
|
||||
// 根据消息类型编号获取类型字符串
|
||||
const getMessageType = (type) => {
|
||||
const types = ["text", "image", "audio", "video", "prescription", "medical-record", "video-call"]
|
||||
const types = ["text", "image", "audio", "video", "prescription", "medical-record", "video-call", "audio-call", "file"]
|
||||
return types[type] || "text"
|
||||
}
|
||||
|
||||
// 处理通话信令
|
||||
const handleCallSignal = (data) => {
|
||||
console.log("处理通话信令:", data)
|
||||
|
||||
// 根据通话状态处理
|
||||
switch (data.call_status) {
|
||||
case "invite":
|
||||
handleIncomingCall(data)
|
||||
break
|
||||
case "accepted":
|
||||
handleCallAccepted(data)
|
||||
break
|
||||
case "rejected":
|
||||
handleCallRejected(data)
|
||||
break
|
||||
case "ended":
|
||||
handleCallEnded(data)
|
||||
break
|
||||
case "candidate":
|
||||
handleIceCandidate(data)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 处理来电
|
||||
const handleIncomingCall = (data) => {
|
||||
incomingCall.value = {
|
||||
callId: data.call_id,
|
||||
callerId: data.sender_user_id,
|
||||
callType: data.message_type === 6 ? "video" : "audio",
|
||||
content: data.content
|
||||
}
|
||||
callStatus.value = "ringing"
|
||||
|
||||
console.log("收到来电:", incomingCall.value)
|
||||
}
|
||||
|
||||
// 处理通话接受
|
||||
const handleCallAccepted = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
callStatus.value = "ongoing"
|
||||
console.log("通话已被接受")
|
||||
}
|
||||
}
|
||||
|
||||
// 处理通话拒绝
|
||||
const handleCallRejected = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
callStatus.value = "ended"
|
||||
currentCall.value = null
|
||||
console.log("通话已被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
// 处理通话结束
|
||||
const handleCallEnded = (data) => {
|
||||
if ((currentCall.value && currentCall.value.callId === data.call_id) ||
|
||||
(incomingCall.value && incomingCall.value.callId === data.call_id)) {
|
||||
callStatus.value = "ended"
|
||||
currentCall.value = null
|
||||
incomingCall.value = null
|
||||
console.log("通话已结束")
|
||||
}
|
||||
}
|
||||
|
||||
// 处理ICE候选
|
||||
const handleIceCandidate = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
console.log("收到ICE候选:", data.content)
|
||||
// 这里可以添加WebRTC处理逻辑
|
||||
}
|
||||
}
|
||||
|
||||
// 发起通话
|
||||
const startCall = (peerId, type) => {
|
||||
if (isInCall.value) return
|
||||
|
||||
const callId = Date.now().toString() + Math.random().toString(36).substr(2, 9)
|
||||
|
||||
currentCall.value = {
|
||||
callId: callId,
|
||||
peerId: peerId,
|
||||
callType: type
|
||||
}
|
||||
|
||||
callStatus.value = "calling"
|
||||
|
||||
// 发送通话邀请
|
||||
sendMessage({
|
||||
sender_user_id: userStore.currentUser?.id,
|
||||
receiver_user_id: peerId,
|
||||
message_type: type === "video" ? 6 : 7, // 6=视频,7=语音
|
||||
message_content: "", // 对于邀请,内容为空
|
||||
call_id: callId,
|
||||
call_status: "invite"
|
||||
})
|
||||
|
||||
console.log("发起通话:", currentCall.value)
|
||||
}
|
||||
|
||||
// 接听来电
|
||||
const acceptCall = () => {
|
||||
if (!incomingCall.value) return
|
||||
|
||||
// 发送接受响应
|
||||
sendMessage({
|
||||
sender_user_id: userStore.currentUser?.id,
|
||||
receiver_user_id: incomingCall.value.callerId,
|
||||
message_type: incomingCall.value.callType === "video" ? 6 : 7,
|
||||
message_content: "", // 对于接受,内容为空
|
||||
call_id: incomingCall.value.callId,
|
||||
call_status: "accepted"
|
||||
})
|
||||
|
||||
currentCall.value = {
|
||||
callId: incomingCall.value.callId,
|
||||
peerId: incomingCall.value.callerId,
|
||||
callType: incomingCall.value.callType
|
||||
}
|
||||
|
||||
callStatus.value = "ongoing"
|
||||
incomingCall.value = null
|
||||
console.log("接听通话")
|
||||
}
|
||||
|
||||
// 拒绝来电
|
||||
const rejectCall = () => {
|
||||
if (!incomingCall.value) return
|
||||
|
||||
// 发送拒绝响应
|
||||
sendMessage({
|
||||
sender_user_id: userStore.currentUser?.id,
|
||||
receiver_user_id: incomingCall.value.callerId,
|
||||
message_type: incomingCall.value.callType === "video" ? 6 : 7,
|
||||
message_content: "", // 对于拒绝,内容为空
|
||||
call_id: incomingCall.value.callId,
|
||||
call_status: "rejected"
|
||||
})
|
||||
|
||||
callStatus.value = "idle"
|
||||
incomingCall.value = null
|
||||
console.log("拒绝通话")
|
||||
}
|
||||
|
||||
// 结束通话
|
||||
const endCall = () => {
|
||||
const callId = currentCall.value?.callId || incomingCall.value?.callId
|
||||
const peerId = currentCall.value?.peerId || incomingCall.value?.callerId
|
||||
const callType = currentCall.value?.callType || incomingCall.value?.callType
|
||||
|
||||
if (!callId || !peerId) return
|
||||
|
||||
// 发送结束通知
|
||||
sendMessage({
|
||||
sender_user_id: userStore.currentUser?.id,
|
||||
receiver_user_id: peerId,
|
||||
message_type: callType === "video" ? 6 : 7,
|
||||
message_content: "",
|
||||
call_id: callId,
|
||||
call_status: "ended"
|
||||
})
|
||||
|
||||
callStatus.value = "idle"
|
||||
currentCall.value = null
|
||||
incomingCall.value = null
|
||||
console.log("结束通话")
|
||||
}
|
||||
|
||||
return {
|
||||
friends,
|
||||
currentFriend,
|
||||
messages,
|
||||
connectionStatus,
|
||||
socket,
|
||||
currentCall,
|
||||
incomingCall,
|
||||
callStatus,
|
||||
isConnected,
|
||||
connectionStatusText,
|
||||
connectionStatusIcon,
|
||||
isInCall,
|
||||
isCalling,
|
||||
isRinging,
|
||||
isOngoingCall,
|
||||
initDB,
|
||||
loadFriends,
|
||||
setCurrentFriend,
|
||||
switchFriend,
|
||||
addMessage,
|
||||
handleIncomingMessage,
|
||||
handleCallSignal,
|
||||
startCall,
|
||||
acceptCall,
|
||||
rejectCall,
|
||||
endCall
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user