fix:客服上传图片及接受问题修复
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { getStoredToken } from '@/utils/auth-token'
|
||||
import { resolveCsAppToken } from '@/api/request'
|
||||
import type { CsChatWsPayload, CsMessageVo, CsSessionVo } from './support.types'
|
||||
|
||||
const RECONNECT_DELAY_MS = 2000
|
||||
/** 断线后首次重连等待(非轮询;连接成功后会保持长连接) */
|
||||
const RECONNECT_BASE_MS = 10000
|
||||
const RECONNECT_MAX_MS = 30000
|
||||
|
||||
let socketTask: UniApp.SocketTask | null = null
|
||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
@@ -11,6 +13,7 @@ let activeSessionId = ''
|
||||
let socketOpened = false
|
||||
/** 每次 connect / disconnect 递增,忽略已废弃连接的 onClose */
|
||||
let connectionSeq = 0
|
||||
let reconnectAttempt = 0
|
||||
|
||||
const listeners = new Set<(payload: CsChatWsPayload) => void>()
|
||||
|
||||
@@ -25,13 +28,21 @@ function resolveCsChatWsBase(): string {
|
||||
|
||||
if (isH5Dev() && !forceDirect) {
|
||||
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
return `${proto}//${window.location.host}/app/cs/chat`
|
||||
const useCsHost = !!import.meta.env.VITE_CS_API_BASE_URL?.trim()
|
||||
const path = useCsHost ? '/cs-app/cs/chat' : '/app/cs/chat'
|
||||
return `${proto}//${window.location.host}${path}`
|
||||
}
|
||||
|
||||
if (configured) {
|
||||
return configured.replace(/[?#].*$/, '').replace(/\/$/, '')
|
||||
}
|
||||
|
||||
const csBase = import.meta.env.VITE_CS_API_BASE_URL?.trim()
|
||||
if (csBase) {
|
||||
const wsBase = csBase.replace(/^https/i, 'wss').replace(/^http/i, 'ws').replace(/\/$/, '')
|
||||
return `${wsBase}/app/cs/chat`
|
||||
}
|
||||
|
||||
const envBase = import.meta.env.VITE_API_BASE_URL?.trim() || ''
|
||||
const fallback =
|
||||
import.meta.env.VITE_API_PROXY_TARGET?.trim() || 'http://192.168.0.222:8082'
|
||||
@@ -68,11 +79,16 @@ function clearReconnectTimer() {
|
||||
}
|
||||
|
||||
function scheduleReconnect() {
|
||||
if (manualClosed || !getStoredToken() || !activeSessionId) return
|
||||
if (manualClosed || !resolveCsAppToken() || !activeSessionId) return
|
||||
clearReconnectTimer()
|
||||
const delay = Math.min(
|
||||
RECONNECT_BASE_MS * Math.pow(1.5, reconnectAttempt),
|
||||
RECONNECT_MAX_MS,
|
||||
)
|
||||
reconnectAttempt += 1
|
||||
reconnectTimer = setTimeout(() => {
|
||||
connectCsChatSocket(activeSessionId)
|
||||
}, RECONNECT_DELAY_MS)
|
||||
}, delay)
|
||||
}
|
||||
|
||||
function resetSocketState() {
|
||||
@@ -81,7 +97,7 @@ function resetSocketState() {
|
||||
connecting = false
|
||||
}
|
||||
|
||||
export function parseCsChatPayload(raw: string): CsChatWsPayload | null {
|
||||
function parseCsChatPayload(raw: string): CsChatWsPayload | null {
|
||||
if (!raw || raw === 'ping' || raw === 'pong') return null
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>
|
||||
@@ -163,6 +179,7 @@ function bindSocketTask(task: UniApp.SocketTask, seq: number) {
|
||||
if (seq !== connectionSeq) return
|
||||
socketOpened = true
|
||||
connecting = false
|
||||
reconnectAttempt = 0
|
||||
if (import.meta.env.DEV) console.log('[CS Chat WS] connected')
|
||||
})
|
||||
|
||||
@@ -192,7 +209,7 @@ function bindSocketTask(task: UniApp.SocketTask, seq: number) {
|
||||
}
|
||||
|
||||
export function connectCsChatSocket(sessionId: string) {
|
||||
const token = getStoredToken()
|
||||
const token = resolveCsAppToken()
|
||||
if (!token || !sessionId) return
|
||||
if (connecting) return
|
||||
if (socketOpened && socketTask && activeSessionId === sessionId) return
|
||||
@@ -244,6 +261,7 @@ export function disconnectCsChatSocket() {
|
||||
manualClosed = true
|
||||
connectionSeq += 1
|
||||
activeSessionId = ''
|
||||
reconnectAttempt = 0
|
||||
clearReconnectTimer()
|
||||
const task = socketTask
|
||||
socketTask = null
|
||||
|
||||
Reference in New Issue
Block a user