2025-07-02 16:54:52 +08:00
|
|
|
|
import axios from "axios"
|
|
|
|
|
|
import { message } from "ant-design-vue"
|
|
|
|
|
|
|
|
|
|
|
|
// 创建 Axios 实例
|
|
|
|
|
|
const service = axios.create({
|
2025-07-07 09:53:02 +08:00
|
|
|
|
baseURL: "/api/",
|
|
|
|
|
|
timeout: 15000,
|
2025-07-02 16:54:52 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
service.interceptors.request.use(
|
2025-07-03 15:36:34 +08:00
|
|
|
|
(config) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
const token = localStorage.getItem("token")
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
config.headers["Authorization"] = `Bearer ${token}`
|
|
|
|
|
|
}
|
|
|
|
|
|
return config
|
2025-07-03 15:36:34 +08:00
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
console.error("Request error:", error)
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
2025-07-02 16:54:52 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
service.interceptors.response.use(
|
2025-07-03 15:36:34 +08:00
|
|
|
|
(response) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
const { code, result, message: msg } = response.data
|
|
|
|
|
|
if (code === 0) {
|
|
|
|
|
|
return result
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (code === 401) {
|
|
|
|
|
|
message.error(msg)
|
|
|
|
|
|
localStorage.removeItem("token")
|
|
|
|
|
|
localStorage.removeItem("chatUser")
|
|
|
|
|
|
window.location.href = "/login"
|
2025-07-07 16:51:55 +08:00
|
|
|
|
return Promise.reject(new Error(msg))
|
2025-07-07 09:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
message.error(msg)
|
|
|
|
|
|
return Promise.reject(new Error(msg))
|
2025-07-03 15:36:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
(error) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
console.error("Response error:", error)
|
|
|
|
|
|
|
|
|
|
|
|
// 详细的错误处理
|
|
|
|
|
|
if (error.code === "ECONNABORTED") {
|
|
|
|
|
|
console.log("请求超时,请检查网络连接")
|
|
|
|
|
|
} else if (error.response?.status === 0 || error.message.includes("CORS")) {
|
|
|
|
|
|
console.log("CORS错误检测到,尝试备用方案...")
|
|
|
|
|
|
handleCORSError(error.config)
|
|
|
|
|
|
} else if (error.response?.status >= 400 && error.response?.status < 500) {
|
|
|
|
|
|
console.log("客户端错误:", error.response.status, error.response.data)
|
|
|
|
|
|
} else if (error.response?.status >= 500) {
|
|
|
|
|
|
console.log("服务器错误:", error.response.status)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
console.log("网络错误:", error.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
message.error("网络请求失败")
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
2025-07-02 16:54:52 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-07-03 15:36:34 +08:00
|
|
|
|
// CORS错误的备用处理方案
|
|
|
|
|
|
const handleCORSError = (config) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
console.log("执行CORS错误备用方案...")
|
|
|
|
|
|
const apiUrl = config.url.replace("/api", "")
|
2025-07-03 15:36:34 +08:00
|
|
|
|
|
2025-07-07 09:53:02 +08:00
|
|
|
|
return fetch(apiUrl, {
|
|
|
|
|
|
method: config.method.toUpperCase(),
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
...config.headers,
|
|
|
|
|
|
},
|
|
|
|
|
|
mode: "cors",
|
|
|
|
|
|
body: config.data ? JSON.stringify(config.data) : undefined,
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((response) => {
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
|
|
|
|
}
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
|
console.error("备用方案也失败:", error)
|
|
|
|
|
|
throw error
|
|
|
|
|
|
})
|
2025-07-03 15:36:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-02 16:54:52 +08:00
|
|
|
|
// 封装请求方法
|
|
|
|
|
|
export const get = (url, params = {}) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
return service.get(url, { params })
|
2025-07-02 16:54:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const post = (url, data = {}) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
return service.post(url, data)
|
2025-07-02 16:54:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const upload = (url, file) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
const formData = new FormData()
|
|
|
|
|
|
formData.append("file", file)
|
|
|
|
|
|
return service.post(url, formData, {
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
2025-07-02 16:54:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 09:53:02 +08:00
|
|
|
|
// 发送消息API(支持普通消息和通话信令)
|
2025-07-02 16:54:52 +08:00
|
|
|
|
export const sendMessage = (data) => {
|
2025-07-07 09:53:02 +08:00
|
|
|
|
const apiUrl = "/api/send-to-user"
|
|
|
|
|
|
|
|
|
|
|
|
// 消息类型映射 - 根据用户提供的完整映射更新
|
|
|
|
|
|
const messageTypeMap = {
|
|
|
|
|
|
text: 0,
|
|
|
|
|
|
image: 1,
|
|
|
|
|
|
audio: 2,
|
|
|
|
|
|
video: 3,
|
|
|
|
|
|
prescription: 4,
|
|
|
|
|
|
"medical-record": 5,
|
|
|
|
|
|
"video-call": 6,
|
|
|
|
|
|
"audio-call": 7,
|
|
|
|
|
|
file: 8
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const requestData = {
|
|
|
|
|
|
sender_user_id: data.senderId,
|
|
|
|
|
|
receiver_user_id: data.receiverId,
|
|
|
|
|
|
message_type: messageTypeMap[data.type] || 0,
|
|
|
|
|
|
message_content: data.content || "",
|
2025-07-07 16:51:55 +08:00
|
|
|
|
// 添加来源标记
|
|
|
|
|
|
isFromHttp: true
|
2025-07-07 09:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果包含通话信令,添加call_id和call_status
|
|
|
|
|
|
if (data.callId) {
|
|
|
|
|
|
requestData.call_id = data.callId
|
|
|
|
|
|
}
|
|
|
|
|
|
if (data.callStatus) {
|
|
|
|
|
|
requestData.call_status = data.callStatus
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
console.log("发送消息到API:", apiUrl, requestData)
|
|
|
|
|
|
|
|
|
|
|
|
return axios
|
|
|
|
|
|
.post(apiUrl, requestData, {
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
},
|
|
|
|
|
|
timeout: 15000,
|
|
|
|
|
|
withCredentials: false,
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((response) => {
|
|
|
|
|
|
console.log("消息发送成功:", response.data)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
|
console.error("消息发送失败:", error)
|
|
|
|
|
|
throw error
|
|
|
|
|
|
})
|
2025-07-02 16:54:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 09:53:02 +08:00
|
|
|
|
export default service
|