feat:蓝牙绑定和在线客服
This commit is contained in:
169
src/pages/mine/support/cs-chat-socket.ts
Normal file
169
src/pages/mine/support/cs-chat-socket.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { getStoredToken } from '@/utils/auth-token'
|
||||
import type { CsChatWsPayload, CsMessageVo, CsSessionVo } from './support.types'
|
||||
|
||||
const HEARTBEAT_INTERVAL_MS = 25000
|
||||
const RECONNECT_DELAY_MS = 25000
|
||||
|
||||
let socketTask: UniApp.SocketTask | null = null
|
||||
let heartbeatTimer: ReturnType<typeof setInterval> | null = null
|
||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let connecting = false
|
||||
let manualClosed = false
|
||||
let activeSessionId = ''
|
||||
let socketOpened = false
|
||||
|
||||
const listeners = new Set<(payload: CsChatWsPayload) => void>()
|
||||
|
||||
function resolveCsChatWsBase(): string {
|
||||
const configured = import.meta.env.VITE_CS_CHAT_WS_URL?.trim()
|
||||
if (configured) {
|
||||
return configured.replace(/[?#].*$/, '').replace(/\/$/, '')
|
||||
}
|
||||
|
||||
const envBase = import.meta.env.VITE_API_BASE_URL?.trim() || ''
|
||||
const fallback = import.meta.env.VITE_API_PROXY_TARGET?.trim() || 'http://192.168.0.212:8082'
|
||||
const httpBase = envBase || fallback
|
||||
const wsBase = httpBase.replace(/^https/i, 'wss').replace(/^http/i, 'ws')
|
||||
return `${wsBase}/app/cs/chat`
|
||||
}
|
||||
|
||||
function resolveCsChatWsUrl(sessionId: string, token: string): string {
|
||||
const base = resolveCsChatWsBase()
|
||||
return `${base}?token=${encodeURIComponent(token)}&sessionId=${encodeURIComponent(sessionId)}`
|
||||
}
|
||||
|
||||
function stopHeartbeat() {
|
||||
if (heartbeatTimer) clearInterval(heartbeatTimer)
|
||||
heartbeatTimer = null
|
||||
}
|
||||
|
||||
function startHeartbeat() {
|
||||
stopHeartbeat()
|
||||
heartbeatTimer = setInterval(() => {
|
||||
socketTask?.send({ data: 'ping' })
|
||||
}, HEARTBEAT_INTERVAL_MS)
|
||||
}
|
||||
|
||||
function clearReconnectTimer() {
|
||||
if (reconnectTimer) clearTimeout(reconnectTimer)
|
||||
reconnectTimer = null
|
||||
}
|
||||
|
||||
function scheduleReconnect() {
|
||||
if (manualClosed || !getStoredToken() || !activeSessionId) return
|
||||
clearReconnectTimer()
|
||||
reconnectTimer = setTimeout(() => {
|
||||
connectCsChatSocket(activeSessionId)
|
||||
}, RECONNECT_DELAY_MS)
|
||||
}
|
||||
|
||||
function cleanupSocketTask() {
|
||||
stopHeartbeat()
|
||||
socketTask = null
|
||||
socketOpened = false
|
||||
connecting = false
|
||||
}
|
||||
|
||||
export function parseCsChatPayload(raw: string): CsChatWsPayload | null {
|
||||
if (!raw || raw === 'ping' || raw === 'pong') return null
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>
|
||||
const type = String(parsed.type ?? '').toLowerCase()
|
||||
|
||||
if (type === 'message' && parsed.data && typeof parsed.data === 'object') {
|
||||
return { type: 'message', message: parsed.data as CsMessageVo }
|
||||
}
|
||||
if (type === 'session' && parsed.data && typeof parsed.data === 'object') {
|
||||
return { type: 'session', session: parsed.data as CsSessionVo }
|
||||
}
|
||||
if (parsed.message && typeof parsed.message === 'object') {
|
||||
return { type: 'message', message: parsed.message as CsMessageVo }
|
||||
}
|
||||
if (parsed.session && typeof parsed.session === 'object') {
|
||||
return { type: 'session', session: parsed.session as CsSessionVo }
|
||||
}
|
||||
if (parsed.content !== undefined || parsed.seq !== undefined || parsed.messageId !== undefined) {
|
||||
return { type: 'message', message: parsed as CsMessageVo }
|
||||
}
|
||||
if (parsed.status !== undefined || parsed.sessionStatus !== undefined) {
|
||||
return { type: 'session', session: parsed as CsSessionVo }
|
||||
}
|
||||
return { type: type || 'unknown', raw: parsed }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function handleSocketMessage(raw: string) {
|
||||
const payload = parseCsChatPayload(raw)
|
||||
if (!payload) return
|
||||
listeners.forEach((listener) => listener(payload))
|
||||
}
|
||||
|
||||
export function onCsChatPush(listener: (payload: CsChatWsPayload) => void) {
|
||||
listeners.add(listener)
|
||||
return () => listeners.delete(listener)
|
||||
}
|
||||
|
||||
export function isCsChatSocketConnected(): boolean {
|
||||
return socketOpened
|
||||
}
|
||||
|
||||
export function connectCsChatSocket(sessionId: string) {
|
||||
const token = getStoredToken()
|
||||
if (!token || !sessionId) return
|
||||
if (connecting) return
|
||||
if (socketOpened && socketTask && activeSessionId === sessionId) return
|
||||
|
||||
if (socketTask) {
|
||||
manualClosed = true
|
||||
socketTask.close({})
|
||||
cleanupSocketTask()
|
||||
}
|
||||
|
||||
activeSessionId = sessionId
|
||||
manualClosed = false
|
||||
connecting = true
|
||||
clearReconnectTimer()
|
||||
|
||||
const task = uni.connectSocket({
|
||||
url: resolveCsChatWsUrl(sessionId, token),
|
||||
complete: () => {
|
||||
connecting = false
|
||||
},
|
||||
})
|
||||
|
||||
socketTask = task
|
||||
|
||||
task.onOpen(() => {
|
||||
socketOpened = true
|
||||
startHeartbeat()
|
||||
})
|
||||
|
||||
task.onMessage((res) => {
|
||||
const raw = typeof res.data === 'string' ? res.data : ''
|
||||
handleSocketMessage(raw)
|
||||
})
|
||||
|
||||
task.onClose(() => {
|
||||
cleanupSocketTask()
|
||||
scheduleReconnect()
|
||||
})
|
||||
|
||||
task.onError(() => {
|
||||
cleanupSocketTask()
|
||||
scheduleReconnect()
|
||||
})
|
||||
}
|
||||
|
||||
export function disconnectCsChatSocket() {
|
||||
manualClosed = true
|
||||
activeSessionId = ''
|
||||
clearReconnectTimer()
|
||||
stopHeartbeat()
|
||||
if (socketTask) {
|
||||
socketTask.close({})
|
||||
}
|
||||
socketTask = null
|
||||
connecting = false
|
||||
}
|
||||
319
src/pages/mine/support/index.vue
Normal file
319
src/pages/mine/support/index.vue
Normal file
@@ -0,0 +1,319 @@
|
||||
<template>
|
||||
<view class="support-page">
|
||||
<app-navbar title="在线客服">
|
||||
<template #right>
|
||||
<view v-if="showTransfer" class="support-page__nav-action" @tap="onTransferHuman">
|
||||
<text class="support-page__nav-action-text">转人工</text>
|
||||
</view>
|
||||
<view v-else-if="showEndSession" class="support-page__nav-action" @tap="onEndSession">
|
||||
<text class="support-page__nav-action-text support-page__nav-action-text--end">结束</text>
|
||||
</view>
|
||||
</template>
|
||||
</app-navbar>
|
||||
|
||||
<view class="support-page__header">
|
||||
<view
|
||||
class="support-page__header-avatar"
|
||||
:class="`support-page__header-avatar--${headerInfo.avatarShape}`"
|
||||
>
|
||||
{{ headerInfo.avatarText }}
|
||||
</view>
|
||||
<view class="support-page__header-main">
|
||||
<view class="support-page__header-row">
|
||||
<text class="support-page__header-name">{{ headerInfo.name }}</text>
|
||||
<view
|
||||
class="support-page__header-badge"
|
||||
:class="`support-page__header-badge--${headerInfo.badgeType}`"
|
||||
>
|
||||
<view
|
||||
v-if="headerInfo.badgeType === 'online'"
|
||||
class="support-page__header-badge-dot"
|
||||
/>
|
||||
<text>{{ headerInfo.badgeText }}</text>
|
||||
</view>
|
||||
</view>
|
||||
<text v-if="headerInfo.subtitle" class="support-page__header-subtitle">
|
||||
<text class="support-page__header-subtitle-dot">●</text>{{ headerInfo.subtitle }}
|
||||
</text>
|
||||
</view>
|
||||
<text v-if="headerInfo.serviceHours" class="support-page__header-hours">
|
||||
{{ headerInfo.serviceHours }}
|
||||
</text>
|
||||
</view>
|
||||
|
||||
<scroll-view
|
||||
class="support-page__chat"
|
||||
scroll-y
|
||||
:scroll-into-view="scrollIntoView"
|
||||
scroll-with-animation
|
||||
>
|
||||
<view class="support-page__chat-inner">
|
||||
<template v-for="item in messages" :key="item.id">
|
||||
<view v-if="item.type === 'time'" class="support-page__time">
|
||||
<text class="support-page__time-pill">🕐 {{ item.text }}</text>
|
||||
</view>
|
||||
|
||||
<view
|
||||
v-else-if="item.type === 'text' && item.role === 'system'"
|
||||
class="support-page__msg support-page__msg--system"
|
||||
>
|
||||
<view class="support-page__bubble support-page__bubble--system">
|
||||
<text>{{ item.content }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view
|
||||
v-else-if="item.type === 'text'"
|
||||
class="support-page__msg"
|
||||
:class="`support-page__msg--${item.role}`"
|
||||
>
|
||||
<view
|
||||
v-if="item.role === 'bot' || item.role === 'agent'"
|
||||
class="support-page__msg-avatar"
|
||||
:class="`support-page__msg-avatar--${item.role}`"
|
||||
>
|
||||
{{ item.role === 'bot' ? '智' : humanAvatarText }}
|
||||
</view>
|
||||
|
||||
<view
|
||||
class="support-page__msg-body"
|
||||
:class="{ 'support-page__msg-body--user': item.role === 'user' }"
|
||||
>
|
||||
<view
|
||||
class="support-page__bubble"
|
||||
:class="`support-page__bubble--${item.role}`"
|
||||
>
|
||||
<text>{{ item.content }}</text>
|
||||
</view>
|
||||
<text v-if="item.time" class="support-page__bubble-time">
|
||||
{{ item.time }}
|
||||
</text>
|
||||
</view>
|
||||
|
||||
<view v-if="item.role === 'user'" class="support-page__msg-avatar support-page__msg-avatar--user">
|
||||
我
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-else-if="item.type === 'quick-actions'" class="support-page__quick">
|
||||
<view
|
||||
v-for="action in item.actions"
|
||||
:key="action.id"
|
||||
class="support-page__quick-item"
|
||||
@tap="onQuickAction(action)"
|
||||
>
|
||||
<text class="support-page__quick-dot">●</text>
|
||||
<text>{{ action.label }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-else-if="item.type === 'wifi-help'" class="support-page__card">
|
||||
<text class="support-page__card-title">{{ item.title }}</text>
|
||||
<view
|
||||
v-for="(step, index) in item.steps"
|
||||
:key="`${item.id}-${index}`"
|
||||
class="support-page__card-step"
|
||||
>
|
||||
<text class="support-page__card-step-index">{{ index + 1 }}</text>
|
||||
<text class="support-page__card-step-text">{{ step }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-else-if="item.type === 'queue'" class="support-page__card">
|
||||
<view class="support-page__queue-head">
|
||||
<view class="support-page__queue-title-wrap">
|
||||
<text class="support-page__queue-icon">🎧</text>
|
||||
<text class="support-page__queue-title">转换人工客服</text>
|
||||
</view>
|
||||
<text class="support-page__queue-tag">分配中</text>
|
||||
</view>
|
||||
<view class="support-page__queue-steps">
|
||||
<view class="support-page__queue-step">
|
||||
<view class="support-page__queue-step-dot support-page__queue-step-dot--done">✓</view>
|
||||
<text class="support-page__queue-step-label">已提交</text>
|
||||
</view>
|
||||
<view class="support-page__queue-step">
|
||||
<view class="support-page__queue-step-dot support-page__queue-step-dot--active">Ⅱ</view>
|
||||
<text class="support-page__queue-step-label">分配中</text>
|
||||
</view>
|
||||
<view class="support-page__queue-step">
|
||||
<view class="support-page__queue-step-dot">○</view>
|
||||
<text class="support-page__queue-step-label">接入</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="support-page__queue-info">
|
||||
<text class="support-page__queue-info-text">
|
||||
排队第 {{ item.position }} 位 · 已等待 {{ formattedQueueWait }}
|
||||
</text>
|
||||
<text class="support-page__queue-estimate">{{ item.estimateText }}</text>
|
||||
</view>
|
||||
<view class="support-page__queue-actions">
|
||||
<text class="support-page__queue-link" @tap="onResolveWithoutHuman">
|
||||
已解决,无需人工
|
||||
</text>
|
||||
<text class="support-page__queue-cancel" @tap="onCancelQueue">取消排队</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-else-if="item.type === 'diagnosis'" class="support-page__card">
|
||||
<view class="support-page__diagnosis-head">
|
||||
<view class="support-page__diagnosis-title-wrap">
|
||||
<view class="support-page__diagnosis-bar" />
|
||||
<text class="support-page__card-title">{{ item.title }}</text>
|
||||
</view>
|
||||
<text class="support-page__diagnosis-estimate">{{ item.estimateText }}</text>
|
||||
</view>
|
||||
<view
|
||||
v-for="diag in item.items"
|
||||
:key="diag.id"
|
||||
class="support-page__diagnosis-item"
|
||||
>
|
||||
<text class="support-page__diagnosis-icon">{{ diag.icon }}</text>
|
||||
<text class="support-page__diagnosis-label">{{ diag.label }}</text>
|
||||
<text
|
||||
class="support-page__diagnosis-status"
|
||||
:class="`support-page__diagnosis-status--${diag.status}`"
|
||||
>
|
||||
{{ diagnosisStatusText(diag.status) }}
|
||||
</text>
|
||||
</view>
|
||||
<view class="support-page__progress">
|
||||
<view
|
||||
class="support-page__progress-bar"
|
||||
:style="{ width: `${item.progress}%` }"
|
||||
/>
|
||||
</view>
|
||||
<view class="support-page__diagnosis-suggest">
|
||||
<text
|
||||
class="support-page__diagnosis-suggest-item"
|
||||
@tap="onDiagnosisSuggestion('补充设备信息')"
|
||||
>
|
||||
补充设备信息
|
||||
</text>
|
||||
<text
|
||||
class="support-page__diagnosis-suggest-item"
|
||||
@tap="onDiagnosisSuggestion('大概还要多久?')"
|
||||
>
|
||||
大概还要多久?
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view
|
||||
v-else-if="item.type === 'image'"
|
||||
class="support-page__msg support-page__msg--user"
|
||||
>
|
||||
<view class="support-page__image-wrap">
|
||||
<image class="support-page__image" :src="item.src" mode="aspectFill" />
|
||||
<text v-if="item.time" class="support-page__bubble-time">{{ item.time }}</text>
|
||||
</view>
|
||||
<view class="support-page__msg-avatar support-page__msg-avatar--user">我</view>
|
||||
</view>
|
||||
</template>
|
||||
<view id="chat-bottom" />
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<view class="support-page__footer">
|
||||
<view class="support-page__footer-shortcuts">
|
||||
<text
|
||||
v-for="shortcut in footerShortcuts"
|
||||
:key="shortcut"
|
||||
class="support-page__footer-shortcut"
|
||||
@tap="onFooterShortcut(shortcut)"
|
||||
>
|
||||
{{ shortcut }}
|
||||
</text>
|
||||
</view>
|
||||
<view class="support-page__input-row">
|
||||
<view class="support-page__mic">
|
||||
<image
|
||||
class="support-page__mic-icon"
|
||||
:src="staticAssets.iconSupportMic"
|
||||
mode="aspectFit"
|
||||
/>
|
||||
</view>
|
||||
<view class="support-page__input-wrap">
|
||||
<input
|
||||
v-model="inputValue"
|
||||
class="support-page__input"
|
||||
type="text"
|
||||
placeholder="输入你的问题..."
|
||||
placeholder-class="support-page__input-placeholder"
|
||||
confirm-type="send"
|
||||
@confirm="onSend"
|
||||
/>
|
||||
</view>
|
||||
<view class="support-page__tag">
|
||||
<image
|
||||
class="support-page__tag-icon"
|
||||
:src="staticAssets.iconSupportTag"
|
||||
mode="aspectFit"
|
||||
/>
|
||||
</view>
|
||||
<view class="support-page__send" @tap="onSend">
|
||||
<image
|
||||
class="support-page__send-icon"
|
||||
:src="staticAssets.iconSupportSend"
|
||||
mode="aspectFit"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
<text class="support-page__secure">
|
||||
本次会话已加密,客服不会向您索要密码或验证码
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, ref, watch } from 'vue'
|
||||
import { staticAssets } from '@/utils/static-assets'
|
||||
import { useSupport } from './use-support'
|
||||
import type { DiagnosisItem } from './support.types'
|
||||
|
||||
const {
|
||||
messages,
|
||||
inputValue,
|
||||
headerInfo,
|
||||
showTransfer,
|
||||
showEndSession,
|
||||
formattedQueueWait,
|
||||
footerShortcuts,
|
||||
onQuickAction,
|
||||
onTransferHuman,
|
||||
onEndSession,
|
||||
onCancelQueue,
|
||||
onResolveWithoutHuman,
|
||||
onFooterShortcut,
|
||||
onDiagnosisSuggestion,
|
||||
onSend,
|
||||
} = useSupport()
|
||||
|
||||
const humanAvatarText = computed(() => headerInfo.value.avatarText || '服')
|
||||
|
||||
const scrollIntoView = ref('chat-bottom')
|
||||
|
||||
watch(
|
||||
() => messages.value.length,
|
||||
async () => {
|
||||
await nextTick()
|
||||
scrollIntoView.value = 'chat-bottom'
|
||||
},
|
||||
)
|
||||
|
||||
function diagnosisStatusText(status: DiagnosisItem['status']): string {
|
||||
if (status === 'done') return '已完成'
|
||||
if (status === 'running') return '进行中'
|
||||
return '等待中'
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
@import './support.scss';
|
||||
|
||||
.support-page__input-placeholder {
|
||||
color: #c0c4cc;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
</style>
|
||||
46
src/pages/mine/support/support-runtime.ts
Normal file
46
src/pages/mine/support/support-runtime.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/** 客服页运行时状态(模块级,确保离开页面后定时器可被彻底清理) */
|
||||
let pageActive = false
|
||||
let pollTimer: ReturnType<typeof setInterval> | null = null
|
||||
let queueTimer: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
const QUEUE_POLL_INTERVAL_MS = 5000
|
||||
|
||||
export function isSupportPageActive(): boolean {
|
||||
return pageActive
|
||||
}
|
||||
|
||||
export function markSupportPageActive(active: boolean) {
|
||||
pageActive = active
|
||||
}
|
||||
|
||||
export function stopSupportPollTimer() {
|
||||
if (pollTimer) clearInterval(pollTimer)
|
||||
pollTimer = null
|
||||
}
|
||||
|
||||
export function stopSupportQueueTimer() {
|
||||
if (queueTimer) clearInterval(queueTimer)
|
||||
queueTimer = null
|
||||
}
|
||||
|
||||
export function teardownSupportRuntime() {
|
||||
markSupportPageActive(false)
|
||||
stopSupportPollTimer()
|
||||
stopSupportQueueTimer()
|
||||
}
|
||||
|
||||
export function startSupportQueuePolling(onTick: () => void) {
|
||||
stopSupportPollTimer()
|
||||
pollTimer = setInterval(() => {
|
||||
if (!pageActive) return
|
||||
onTick()
|
||||
}, QUEUE_POLL_INTERVAL_MS)
|
||||
}
|
||||
|
||||
export function startSupportQueueWaitTimer(onTick: () => void) {
|
||||
stopSupportQueueTimer()
|
||||
queueTimer = setInterval(() => {
|
||||
if (!pageActive) return
|
||||
onTick()
|
||||
}, 1000)
|
||||
}
|
||||
136
src/pages/mine/support/support.api.ts
Normal file
136
src/pages/mine/support/support.api.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { request } from '@/api/request'
|
||||
import { ApiError } from '@/utils/api-error'
|
||||
import {
|
||||
isSessionEnded,
|
||||
normalizeSessionId,
|
||||
normalizeSessionResponse,
|
||||
unwrapCsActionResponse,
|
||||
} from './support.utils'
|
||||
import type {
|
||||
CsAppActionVo,
|
||||
CsClickButtonBody,
|
||||
CsCreateSessionBody,
|
||||
CsMessageVo,
|
||||
CsResolvedSession,
|
||||
CsSendMessageBody,
|
||||
CsSessionListQuery,
|
||||
CsSessionVo,
|
||||
CsTableData,
|
||||
} from './support.types'
|
||||
|
||||
function buildSessionPath(sessionId: string, suffix = '') {
|
||||
return `/app/cs/session/${encodeURIComponent(sessionId)}${suffix}`
|
||||
}
|
||||
|
||||
function unwrapRows<T>(data: T[] | CsTableData<T> | undefined): T[] {
|
||||
if (!data) return []
|
||||
if (Array.isArray(data)) return data
|
||||
return data.rows ?? []
|
||||
}
|
||||
|
||||
/** POST /app/cs/session — 创建客服会话;已有未结束会话时返回原会话 */
|
||||
export function createCsSession(data: CsCreateSessionBody = {}) {
|
||||
return request<CsAppActionVo | CsSessionVo | string | null>({
|
||||
url: '/app/cs/session',
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
async function fetchActiveCsSession(): Promise<CsSessionVo | null> {
|
||||
const list = await fetchCsSessionList({ pageNum: 1, pageSize: 20 })
|
||||
const active = list.find((item) => !isSessionEnded(item)) ?? list[0]
|
||||
if (!active || !normalizeSessionId(active)) return null
|
||||
return normalizeSessionResponse(active)
|
||||
}
|
||||
|
||||
/** 创建或获取当前客服会话(兼容 action 包装、id 字段、需二次查询列表) */
|
||||
export async function resolveCsSession(): Promise<CsResolvedSession> {
|
||||
let resolved = unwrapCsActionResponse(await createCsSession())
|
||||
let session = resolved.session
|
||||
let messages = resolved.messages
|
||||
let sessionId = normalizeSessionId(session)
|
||||
|
||||
if (!sessionId) {
|
||||
const active = await fetchActiveCsSession()
|
||||
if (active) {
|
||||
session = active
|
||||
sessionId = normalizeSessionId(active)
|
||||
messages = undefined
|
||||
}
|
||||
}
|
||||
|
||||
if (!sessionId) {
|
||||
throw new ApiError('未获取客服会话,请确认已登录后重试')
|
||||
}
|
||||
|
||||
if (session.status === undefined && session.sessionStatus === undefined) {
|
||||
try {
|
||||
session = normalizeSessionResponse(await fetchCsSessionDetail(sessionId))
|
||||
} catch {
|
||||
session = { ...session, sessionId }
|
||||
}
|
||||
}
|
||||
|
||||
return { session, messages }
|
||||
}
|
||||
|
||||
/** GET /app/cs/session/list — 查询自己的会话 */
|
||||
export function fetchCsSessionList(query: CsSessionListQuery = {}) {
|
||||
return request<CsSessionVo[] | CsTableData<CsSessionVo>>({
|
||||
url: '/app/cs/session/list',
|
||||
method: 'GET',
|
||||
data: query,
|
||||
}).then(unwrapRows)
|
||||
}
|
||||
|
||||
/** GET /app/cs/session/{sessionId} — 查询会话详情 */
|
||||
export function fetchCsSessionDetail(sessionId: string) {
|
||||
return request<CsSessionVo>({
|
||||
url: buildSessionPath(sessionId),
|
||||
method: 'GET',
|
||||
})
|
||||
}
|
||||
|
||||
/** GET /app/cs/session/{sessionId}/messages — 拉取会话消息 */
|
||||
export function fetchCsMessages(sessionId: string, afterSeq = 0) {
|
||||
return request<CsMessageVo[] | CsTableData<CsMessageVo>>({
|
||||
url: buildSessionPath(sessionId, '/messages'),
|
||||
method: 'GET',
|
||||
data: { afterSeq },
|
||||
}).then(unwrapRows)
|
||||
}
|
||||
|
||||
/** POST /app/cs/session/{sessionId}/message — 发送消息 */
|
||||
export function sendCsMessage(sessionId: string, data: CsSendMessageBody) {
|
||||
return request<CsAppActionVo>({
|
||||
url: buildSessionPath(sessionId, '/message'),
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
/** POST /app/cs/session/{sessionId}/transfer — 转人工 */
|
||||
export function transferCsSession(sessionId: string) {
|
||||
return request<void>({
|
||||
url: buildSessionPath(sessionId, '/transfer'),
|
||||
method: 'POST',
|
||||
})
|
||||
}
|
||||
|
||||
/** POST /app/cs/session/{sessionId}/end — 结束会话 */
|
||||
export function endCsSession(sessionId: string) {
|
||||
return request<void>({
|
||||
url: buildSessionPath(sessionId, '/end'),
|
||||
method: 'POST',
|
||||
})
|
||||
}
|
||||
|
||||
/** POST /app/cs/session/{sessionId}/button — 点击机器人按钮 */
|
||||
export function clickCsButton(sessionId: string, data: CsClickButtonBody) {
|
||||
return request<void>({
|
||||
url: buildSessionPath(sessionId, '/button'),
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
695
src/pages/mine/support/support.scss
Normal file
695
src/pages/mine/support/support.scss
Normal file
@@ -0,0 +1,695 @@
|
||||
$support-green: #1ec671;
|
||||
$support-green-dark: #18b863;
|
||||
$support-page-bg: #f7f8fa;
|
||||
$support-card-border: #eef0f3;
|
||||
$support-text: #1a1a1a;
|
||||
$support-text-secondary: #98a2b3;
|
||||
$support-text-muted: #667085;
|
||||
$support-orange: #f5a623;
|
||||
$support-orange-bg: #fff7e6;
|
||||
$support-danger: #f04438;
|
||||
|
||||
.support-page {
|
||||
height: 100vh;
|
||||
background: #fff;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.support-page :deep(.app-navbar) {
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.support-page :deep(.app-navbar__right) {
|
||||
width: auto;
|
||||
min-width: 80rpx;
|
||||
padding-left: 16rpx;
|
||||
}
|
||||
|
||||
.support-page__nav-action {
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.support-page__nav-action-text {
|
||||
font-size: 30rpx;
|
||||
color: $support-green;
|
||||
line-height: 1;
|
||||
font-weight: 400;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.support-page__nav-action-text--end {
|
||||
color: $support-text-muted;
|
||||
}
|
||||
|
||||
.support-page__header {
|
||||
flex-shrink: 0;
|
||||
margin: 0;
|
||||
padding: 16rpx 32rpx 20rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 20rpx;
|
||||
}
|
||||
|
||||
.support-page__header-avatar {
|
||||
width: 80rpx;
|
||||
height: 80rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
background: linear-gradient(145deg, #45d685 0%, #21c25e 100%);
|
||||
color: #fff;
|
||||
font-size: 34rpx;
|
||||
font-weight: 600;
|
||||
line-height: 1;
|
||||
|
||||
&--square {
|
||||
border-radius: 18rpx;
|
||||
}
|
||||
|
||||
&--circle {
|
||||
border-radius: 50%;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__header-main {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.support-page__header-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12rpx;
|
||||
}
|
||||
|
||||
.support-page__header-name {
|
||||
font-size: 30rpx;
|
||||
font-weight: 600;
|
||||
color: $support-text;
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.support-page__header-badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6rpx;
|
||||
padding: 4rpx 12rpx;
|
||||
border-radius: 20rpx;
|
||||
font-size: 20rpx;
|
||||
line-height: 1.2;
|
||||
font-weight: 500;
|
||||
flex-shrink: 0;
|
||||
|
||||
&--online {
|
||||
color: $support-green;
|
||||
background: rgba(30, 198, 113, 0.1);
|
||||
}
|
||||
|
||||
&--serving {
|
||||
color: $support-green;
|
||||
background: rgba(30, 198, 113, 0.1);
|
||||
border: 1rpx solid rgba(30, 198, 113, 0.18);
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__header-badge-dot {
|
||||
width: 10rpx;
|
||||
height: 10rpx;
|
||||
border-radius: 50%;
|
||||
background: $support-green;
|
||||
}
|
||||
|
||||
.support-page__header-subtitle {
|
||||
display: block;
|
||||
margin-top: 8rpx;
|
||||
font-size: 24rpx;
|
||||
color: $support-text-secondary;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.support-page__header-subtitle-dot {
|
||||
color: $support-green;
|
||||
margin-right: 4rpx;
|
||||
}
|
||||
|
||||
.support-page__header-hours {
|
||||
flex-shrink: 0;
|
||||
font-size: 24rpx;
|
||||
color: $support-green;
|
||||
line-height: 1.3;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.support-page__chat {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
padding: 0 24rpx;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.support-page__chat-inner {
|
||||
padding: 8rpx 0 24rpx;
|
||||
}
|
||||
|
||||
.support-page__time {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
margin: 8rpx 0 24rpx;
|
||||
}
|
||||
|
||||
.support-page__time-pill {
|
||||
padding: 8rpx 20rpx;
|
||||
border-radius: 20rpx;
|
||||
background: #eef1f5;
|
||||
font-size: 22rpx;
|
||||
color: $support-text-secondary;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.support-page__msg {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 16rpx;
|
||||
margin-bottom: 28rpx;
|
||||
|
||||
&--user {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
&--system {
|
||||
justify-content: center;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__msg-avatar {
|
||||
width: 72rpx;
|
||||
height: 72rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
font-size: 28rpx;
|
||||
font-weight: 600;
|
||||
line-height: 1;
|
||||
color: #fff;
|
||||
|
||||
&--bot {
|
||||
border-radius: 16rpx;
|
||||
background: linear-gradient(145deg, #45d685 0%, #21c25e 100%);
|
||||
}
|
||||
|
||||
&--agent {
|
||||
border-radius: 50%;
|
||||
background: linear-gradient(145deg, #45d685 0%, #21c25e 100%);
|
||||
}
|
||||
|
||||
&--user {
|
||||
border-radius: 16rpx;
|
||||
background: #d0d5dd;
|
||||
color: #fff;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__msg-body {
|
||||
max-width: calc(100% - 96rpx);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
&--user {
|
||||
align-items: flex-end;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__bubble {
|
||||
max-width: 100%;
|
||||
padding: 20rpx 24rpx;
|
||||
font-size: 28rpx;
|
||||
line-height: 1.55;
|
||||
word-break: break-word;
|
||||
|
||||
&--bot,
|
||||
&--agent {
|
||||
background: #fff;
|
||||
color: $support-text;
|
||||
border: 1rpx solid $support-card-border;
|
||||
border-radius: 4rpx 20rpx 20rpx 20rpx;
|
||||
box-shadow: 0 2rpx 12rpx rgba(16, 24, 40, 0.04);
|
||||
}
|
||||
|
||||
&--user {
|
||||
background: $support-green;
|
||||
color: $support-text;
|
||||
border-radius: 20rpx 4rpx 20rpx 20rpx;
|
||||
}
|
||||
|
||||
&--system {
|
||||
max-width: 92%;
|
||||
padding: 12rpx 20rpx;
|
||||
background: #eef1f5;
|
||||
color: $support-text-muted;
|
||||
font-size: 24rpx;
|
||||
line-height: 1.5;
|
||||
text-align: center;
|
||||
border-radius: 20rpx;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__bubble-time {
|
||||
margin-top: 8rpx;
|
||||
font-size: 22rpx;
|
||||
color: $support-text-secondary;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
.support-page__quick {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 16rpx;
|
||||
margin: 0 0 28rpx 88rpx;
|
||||
}
|
||||
|
||||
.support-page__quick-item {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 8rpx;
|
||||
padding: 14rpx 22rpx;
|
||||
background: #fff;
|
||||
border: 1rpx solid #e4e7ec;
|
||||
border-radius: 32rpx;
|
||||
font-size: 26rpx;
|
||||
color: #3b82f6;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.support-page__quick-dot {
|
||||
color: #3b82f6;
|
||||
font-size: 20rpx;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.support-page__card {
|
||||
margin: 0 0 28rpx 88rpx;
|
||||
background: #fff;
|
||||
border: 1rpx solid $support-card-border;
|
||||
border-radius: 20rpx;
|
||||
padding: 28rpx;
|
||||
box-shadow: 0 2rpx 16rpx rgba(16, 24, 40, 0.04);
|
||||
}
|
||||
|
||||
.support-page__card-title {
|
||||
font-size: 30rpx;
|
||||
font-weight: 600;
|
||||
color: $support-text;
|
||||
line-height: 1.4;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.support-page__card-step {
|
||||
display: flex;
|
||||
gap: 16rpx;
|
||||
margin-bottom: 16rpx;
|
||||
|
||||
&:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__card-step-index {
|
||||
width: 36rpx;
|
||||
height: 36rpx;
|
||||
border-radius: 50%;
|
||||
background: rgba(30, 198, 113, 0.12);
|
||||
color: $support-green;
|
||||
font-size: 22rpx;
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.support-page__card-step-text {
|
||||
flex: 1;
|
||||
font-size: 26rpx;
|
||||
color: $support-text-muted;
|
||||
line-height: 1.55;
|
||||
}
|
||||
|
||||
.support-page__queue-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 28rpx;
|
||||
}
|
||||
|
||||
.support-page__queue-title-wrap {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10rpx;
|
||||
}
|
||||
|
||||
.support-page__queue-icon {
|
||||
font-size: 30rpx;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.support-page__queue-title {
|
||||
font-size: 30rpx;
|
||||
font-weight: 600;
|
||||
color: $support-text;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.support-page__queue-tag {
|
||||
padding: 6rpx 14rpx;
|
||||
border-radius: 8rpx;
|
||||
background: $support-orange-bg;
|
||||
color: $support-orange;
|
||||
font-size: 22rpx;
|
||||
line-height: 1.2;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.support-page__queue-steps {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 28rpx;
|
||||
padding: 0 8rpx;
|
||||
}
|
||||
|
||||
.support-page__queue-step {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 12rpx;
|
||||
position: relative;
|
||||
|
||||
&:not(:last-child)::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 20rpx;
|
||||
left: calc(50% + 24rpx);
|
||||
width: calc(100% - 48rpx);
|
||||
height: 2rpx;
|
||||
background: #e4e7ec;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__queue-step-dot {
|
||||
width: 40rpx;
|
||||
height: 40rpx;
|
||||
border-radius: 50%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 20rpx;
|
||||
line-height: 1;
|
||||
background: #f2f4f7;
|
||||
color: $support-text-secondary;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
|
||||
&--done {
|
||||
background: $support-green;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
&--active {
|
||||
background: $support-orange-bg;
|
||||
color: $support-orange;
|
||||
border: 2rpx solid $support-orange;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__queue-step-label {
|
||||
font-size: 22rpx;
|
||||
color: $support-text-muted;
|
||||
line-height: 1.3;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.support-page__queue-info {
|
||||
padding: 20rpx 0;
|
||||
border-top: 1rpx solid #f0f0f0;
|
||||
border-bottom: 1rpx solid #f0f0f0;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.support-page__queue-info-text {
|
||||
display: block;
|
||||
font-size: 28rpx;
|
||||
font-weight: 600;
|
||||
color: $support-text;
|
||||
line-height: 1.5;
|
||||
margin-bottom: 8rpx;
|
||||
}
|
||||
|
||||
.support-page__queue-estimate {
|
||||
font-size: 24rpx;
|
||||
color: $support-text-secondary;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.support-page__queue-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.support-page__queue-link {
|
||||
font-size: 26rpx;
|
||||
color: $support-text-muted;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.support-page__queue-cancel {
|
||||
font-size: 26rpx;
|
||||
color: $support-danger;
|
||||
line-height: 1.4;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 24rpx;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-title-wrap {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12rpx;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-bar {
|
||||
width: 6rpx;
|
||||
height: 28rpx;
|
||||
border-radius: 3rpx;
|
||||
background: $support-green;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-estimate {
|
||||
padding: 6rpx 14rpx;
|
||||
border-radius: 8rpx;
|
||||
background: #f2f4f7;
|
||||
font-size: 22rpx;
|
||||
color: $support-text-secondary;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12rpx;
|
||||
padding: 14rpx 0;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-icon {
|
||||
width: 36rpx;
|
||||
font-size: 24rpx;
|
||||
color: $support-text-secondary;
|
||||
text-align: center;
|
||||
line-height: 1;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-label {
|
||||
flex: 1;
|
||||
font-size: 26rpx;
|
||||
color: $support-text;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-status {
|
||||
padding: 4rpx 12rpx;
|
||||
border-radius: 8rpx;
|
||||
font-size: 22rpx;
|
||||
line-height: 1.2;
|
||||
flex-shrink: 0;
|
||||
|
||||
&--done {
|
||||
color: $support-green;
|
||||
background: rgba(30, 198, 113, 0.12);
|
||||
}
|
||||
|
||||
&--running {
|
||||
color: $support-orange;
|
||||
background: $support-orange-bg;
|
||||
}
|
||||
|
||||
&--pending {
|
||||
color: $support-text-secondary;
|
||||
background: #f2f4f7;
|
||||
}
|
||||
}
|
||||
|
||||
.support-page__progress {
|
||||
height: 8rpx;
|
||||
background: #eef1f5;
|
||||
border-radius: 4rpx;
|
||||
overflow: hidden;
|
||||
margin: 12rpx 0 20rpx;
|
||||
}
|
||||
|
||||
.support-page__progress-bar {
|
||||
height: 100%;
|
||||
background: linear-gradient(90deg, #45d685 0%, #21c25e 100%);
|
||||
border-radius: 4rpx;
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-suggest {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 16rpx;
|
||||
}
|
||||
|
||||
.support-page__diagnosis-suggest-item {
|
||||
padding: 12rpx 20rpx;
|
||||
border-radius: 28rpx;
|
||||
background: #f5f6f8;
|
||||
font-size: 24rpx;
|
||||
color: $support-text-muted;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.support-page__image-wrap {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
.support-page__image {
|
||||
width: 280rpx;
|
||||
height: 200rpx;
|
||||
border-radius: 16rpx;
|
||||
background: #eef1f5;
|
||||
}
|
||||
|
||||
.support-page__footer {
|
||||
flex-shrink: 0;
|
||||
background: #fff;
|
||||
padding: 12rpx 24rpx calc(12rpx + env(safe-area-inset-bottom));
|
||||
border-top: 1rpx solid #f0f0f0;
|
||||
}
|
||||
|
||||
.support-page__footer-shortcuts {
|
||||
display: flex;
|
||||
gap: 16rpx;
|
||||
margin-bottom: 16rpx;
|
||||
}
|
||||
|
||||
.support-page__footer-shortcut {
|
||||
padding: 10rpx 22rpx;
|
||||
border-radius: 28rpx;
|
||||
background: #f5f6f8;
|
||||
font-size: 24rpx;
|
||||
color: $support-text-muted;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.support-page__input-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16rpx;
|
||||
}
|
||||
|
||||
.support-page__mic {
|
||||
width: 80rpx;
|
||||
height: 80rpx;
|
||||
border-radius: 16rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.support-page__mic-icon {
|
||||
width: 56rpx;
|
||||
height: 56rpx;
|
||||
}
|
||||
|
||||
.support-page__input-wrap {
|
||||
flex: 1;
|
||||
height: 72rpx;
|
||||
padding: 0 28rpx;
|
||||
background: #f5f6f8;
|
||||
border-radius: 36rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.support-page__input {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
height: 72rpx;
|
||||
font-size: 28rpx;
|
||||
color: $support-text;
|
||||
}
|
||||
|
||||
.support-page__tag {
|
||||
width: 56rpx;
|
||||
height: 72rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.support-page__tag-icon {
|
||||
width: 48rpx;
|
||||
height: 48rpx;
|
||||
}
|
||||
|
||||
.support-page__send {
|
||||
width: 72rpx;
|
||||
height: 72rpx;
|
||||
border-radius: 18rpx;
|
||||
background: linear-gradient(135deg, #07b57a 0%, #a4e04b 100%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-shrink: 0;
|
||||
box-shadow: 0 6rpx 16rpx rgba(7, 181, 122, 0.28);
|
||||
}
|
||||
|
||||
.support-page__send-icon {
|
||||
width: 36rpx;
|
||||
height: 36rpx;
|
||||
}
|
||||
|
||||
.support-page__secure {
|
||||
margin-top: 12rpx;
|
||||
text-align: center;
|
||||
font-size: 22rpx;
|
||||
color: $support-text-secondary;
|
||||
line-height: 1.4;
|
||||
}
|
||||
179
src/pages/mine/support/support.types.ts
Normal file
179
src/pages/mine/support/support.types.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
/** 会话状态:0 机器人 1 排队中 2 人工 3 已结束 */
|
||||
export const CS_SESSION_STATUS = {
|
||||
bot: '0',
|
||||
queue: '1',
|
||||
human: '2',
|
||||
closed: '3',
|
||||
} as const
|
||||
|
||||
export type CsSessionStatus = (typeof CS_SESSION_STATUS)[keyof typeof CS_SESSION_STATUS]
|
||||
|
||||
/** POST /app/cs/session、GET /app/cs/session/{sessionId} */
|
||||
export interface CsSessionVo {
|
||||
sessionId?: number | string
|
||||
id?: number | string
|
||||
status?: string | number
|
||||
sessionStatus?: string | number
|
||||
ended?: boolean | string
|
||||
agentId?: number | string
|
||||
agentName?: string
|
||||
agentNo?: string
|
||||
queuePosition?: number
|
||||
queueNo?: number
|
||||
estimateWaitTime?: string
|
||||
createTime?: string
|
||||
updateTime?: string
|
||||
}
|
||||
|
||||
export interface CsSessionListQuery {
|
||||
status?: string
|
||||
agentId?: number | string
|
||||
keyword?: string
|
||||
pageNum?: number
|
||||
pageSize?: number
|
||||
orderByColumn?: string
|
||||
isAsc?: string
|
||||
}
|
||||
|
||||
export interface CsTableData<T> {
|
||||
rows?: T[]
|
||||
total?: number
|
||||
}
|
||||
|
||||
/** GET /app/cs/session/{sessionId}/messages */
|
||||
export interface CsMessageVo {
|
||||
messageId?: number | string
|
||||
seq?: number
|
||||
sessionId?: number | string
|
||||
senderType?: string | number
|
||||
messageType?: string
|
||||
content?: string
|
||||
createTime?: string
|
||||
extra?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/** WebSocket /app/cs/chat 推送 */
|
||||
export interface CsChatWsPayload {
|
||||
type?: string
|
||||
message?: CsMessageVo
|
||||
session?: CsSessionVo
|
||||
raw?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/** POST /app/cs/session 请求体 */
|
||||
export interface CsCreateSessionBody {
|
||||
deviceId?: number | string
|
||||
summary?: string
|
||||
accessType?: 'bot' | 'human'
|
||||
}
|
||||
|
||||
/** POST /app/cs/session 及发消息等操作返回 data */
|
||||
export interface CsAppActionVo {
|
||||
session?: CsSessionVo
|
||||
messages?: CsMessageVo[]
|
||||
robot?: {
|
||||
kbId?: number | string
|
||||
title?: string
|
||||
content?: string
|
||||
matched?: boolean
|
||||
buttons?: Array<{ buttonCode?: string; label?: string }>
|
||||
}
|
||||
}
|
||||
|
||||
export interface CsResolvedSession {
|
||||
session: CsSessionVo
|
||||
messages?: CsMessageVo[]
|
||||
}
|
||||
|
||||
/** POST /app/cs/session/{sessionId}/message */
|
||||
export interface CsSendMessageBody {
|
||||
content: string
|
||||
msgType?: 'text' | 'image' | string
|
||||
clientMsgId: string
|
||||
}
|
||||
|
||||
/** POST /app/cs/session/{sessionId}/button */
|
||||
export type CsButtonCode = 'resolved' | 'transfer_human'
|
||||
|
||||
export interface CsClickButtonBody {
|
||||
buttonCode: CsButtonCode
|
||||
}
|
||||
|
||||
export type SupportPhase = 'bot' | 'queue' | 'human'
|
||||
|
||||
export type ChatRole = 'user' | 'bot' | 'agent' | 'system'
|
||||
|
||||
export interface QuickActionItem {
|
||||
id: string
|
||||
label: string
|
||||
}
|
||||
|
||||
export interface DiagnosisItem {
|
||||
id: string
|
||||
icon: string
|
||||
label: string
|
||||
status: 'done' | 'running' | 'pending'
|
||||
}
|
||||
|
||||
export type SupportMessage =
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'time'
|
||||
text: string
|
||||
}
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'text'
|
||||
role: ChatRole
|
||||
content: string
|
||||
time?: string
|
||||
}
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'quick-actions'
|
||||
actions: QuickActionItem[]
|
||||
}
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'wifi-help'
|
||||
title: string
|
||||
steps: string[]
|
||||
}
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'queue'
|
||||
position: number
|
||||
estimateText: string
|
||||
}
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'diagnosis'
|
||||
title: string
|
||||
estimateText: string
|
||||
items: DiagnosisItem[]
|
||||
progress: number
|
||||
}
|
||||
| {
|
||||
id: string
|
||||
seq?: number
|
||||
type: 'image'
|
||||
role: 'user'
|
||||
src: string
|
||||
time?: string
|
||||
}
|
||||
|
||||
export interface SupportHeaderInfo {
|
||||
name: string
|
||||
subtitle?: string
|
||||
badgeText: string
|
||||
badgeType: 'online' | 'serving'
|
||||
avatarText: string
|
||||
avatarShape: 'square' | 'circle'
|
||||
serviceHours?: string
|
||||
}
|
||||
253
src/pages/mine/support/support.utils.ts
Normal file
253
src/pages/mine/support/support.utils.ts
Normal file
@@ -0,0 +1,253 @@
|
||||
import type {
|
||||
ChatRole,
|
||||
CsAppActionVo,
|
||||
CsMessageVo,
|
||||
CsSessionVo,
|
||||
QuickActionItem,
|
||||
SupportMessage,
|
||||
SupportPhase,
|
||||
} from './support.types'
|
||||
import { CS_SESSION_STATUS } from './support.types'
|
||||
|
||||
const QUICK_ACTIONS: QuickActionItem[] = [
|
||||
{ id: 'wifi', label: '设备连不上 Wi-Fi' },
|
||||
{ id: 'order', label: '订单怎么修改' },
|
||||
{ id: 'invoice', label: '申请电子发票' },
|
||||
{ id: 'device', label: '设备使用问题' },
|
||||
]
|
||||
|
||||
export function createClientMsgId(): string {
|
||||
return `${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
||||
}
|
||||
|
||||
export function unwrapCsActionResponse(
|
||||
data: CsAppActionVo | CsSessionVo | string | null | undefined,
|
||||
): { session: CsSessionVo; messages?: CsMessageVo[] } {
|
||||
if (!data) return { session: {} }
|
||||
if (typeof data === 'string') {
|
||||
return { session: normalizeSessionResponse(data) }
|
||||
}
|
||||
if (data.session) {
|
||||
return {
|
||||
session: normalizeSessionResponse(data.session),
|
||||
messages: data.messages,
|
||||
}
|
||||
}
|
||||
const record = data as CsSessionVo & { messages?: CsMessageVo[] }
|
||||
const { messages, ...session } = record
|
||||
return {
|
||||
session: normalizeSessionResponse(session),
|
||||
messages,
|
||||
}
|
||||
}
|
||||
|
||||
export function normalizeSessionResponse(
|
||||
data: CsSessionVo | string | null | undefined,
|
||||
): CsSessionVo {
|
||||
if (!data) return {}
|
||||
if (typeof data === 'string') {
|
||||
const value = data.trim()
|
||||
return value ? { sessionId: value } : {}
|
||||
}
|
||||
const sessionId = data.sessionId ?? data.id
|
||||
if (sessionId === undefined || sessionId === null || sessionId === '') {
|
||||
return data
|
||||
}
|
||||
return { ...data, sessionId: String(sessionId) }
|
||||
}
|
||||
|
||||
export function normalizeSessionId(session: CsSessionVo | string | null | undefined): string {
|
||||
const normalized = normalizeSessionResponse(session)
|
||||
const raw = normalized.sessionId
|
||||
if (raw === undefined || raw === null || raw === '') return ''
|
||||
return String(raw)
|
||||
}
|
||||
|
||||
export function getSessionStatus(session: CsSessionVo): string {
|
||||
return String(session.status ?? session.sessionStatus ?? '')
|
||||
}
|
||||
|
||||
export function isSessionEnded(session: CsSessionVo): boolean {
|
||||
if (session.ended === true || session.ended === 'true' || session.ended === '1') return true
|
||||
const status = getSessionStatus(session)
|
||||
return status === CS_SESSION_STATUS.closed || status === 'ended' || status === 'closed'
|
||||
}
|
||||
|
||||
export function resolvePhaseFromSession(session: CsSessionVo): SupportPhase {
|
||||
if (isSessionEnded(session)) return 'bot'
|
||||
const status = getSessionStatus(session)
|
||||
if (status === CS_SESSION_STATUS.queue || status === '1') return 'queue'
|
||||
if (status === CS_SESSION_STATUS.human || status === '2') return 'human'
|
||||
return 'bot'
|
||||
}
|
||||
|
||||
export function getQueuePosition(session: CsSessionVo): number {
|
||||
const value = session.queuePosition ?? session.queueNo
|
||||
if (typeof value === 'number' && value > 0) return value
|
||||
return 1
|
||||
}
|
||||
|
||||
export function getQueueEstimate(session: CsSessionVo): string {
|
||||
return session.estimateWaitTime?.trim() || '正在为您分配人工客服'
|
||||
}
|
||||
|
||||
function formatMessageTime(value?: string): string | undefined {
|
||||
if (!value) return undefined
|
||||
const date = new Date(value)
|
||||
if (Number.isNaN(date.getTime())) return undefined
|
||||
return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
function resolveSenderRole(senderType?: string | number): ChatRole | null {
|
||||
const value = String(senderType ?? '').toLowerCase()
|
||||
if (!value) return 'bot'
|
||||
if (value === '0' || value.includes('user') || value.includes('app')) return 'user'
|
||||
if (value === '1' || value.includes('bot') || value.includes('robot')) return 'bot'
|
||||
if (value === '2' || value.includes('agent') || value.includes('human')) return 'agent'
|
||||
if (value === '3' || value.includes('system')) return 'system'
|
||||
return 'bot'
|
||||
}
|
||||
|
||||
function createMessageId(message: CsMessageVo): string {
|
||||
if (message.messageId !== undefined && message.messageId !== null && message.messageId !== '') {
|
||||
return String(message.messageId)
|
||||
}
|
||||
if (message.seq !== undefined && message.seq !== null) {
|
||||
return `seq-${message.seq}`
|
||||
}
|
||||
return `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
||||
}
|
||||
|
||||
function mapStructuredMessage(message: CsMessageVo, payload: Record<string, unknown>): SupportMessage | null {
|
||||
const type = String(payload.type ?? message.messageType ?? '').toLowerCase()
|
||||
const id = createMessageId(message)
|
||||
const seq = message.seq
|
||||
|
||||
if (type === 'quick-actions' || type === 'quick_actions') {
|
||||
const actions = Array.isArray(payload.actions) ? payload.actions : QUICK_ACTIONS
|
||||
return {
|
||||
id,
|
||||
seq,
|
||||
type: 'quick-actions',
|
||||
actions: actions.map((item, index) => {
|
||||
if (typeof item === 'string') return { id: `action-${index}`, label: item }
|
||||
const record = item as { id?: string; label?: string }
|
||||
return { id: record.id || `action-${index}`, label: record.label || '' }
|
||||
}).filter((item) => item.label),
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'wifi-help' || type === 'wifi_help') {
|
||||
const steps = Array.isArray(payload.steps) ? payload.steps.map(String) : []
|
||||
return {
|
||||
id,
|
||||
seq,
|
||||
type: 'wifi-help',
|
||||
title: String(payload.title ?? 'Wi-Fi 连接失败 · 常见原因'),
|
||||
steps,
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'diagnosis') {
|
||||
const items = Array.isArray(payload.items) ? payload.items : []
|
||||
return {
|
||||
id,
|
||||
seq,
|
||||
type: 'diagnosis',
|
||||
title: String(payload.title ?? '远程诊断中'),
|
||||
estimateText: String(payload.estimateText ?? ''),
|
||||
progress: Number(payload.progress ?? 0),
|
||||
items: items.map((item, index) => {
|
||||
const record = item as {
|
||||
id?: string
|
||||
icon?: string
|
||||
label?: string
|
||||
status?: 'done' | 'running' | 'pending'
|
||||
}
|
||||
return {
|
||||
id: record.id || `diag-${index}`,
|
||||
icon: record.icon || '•',
|
||||
label: record.label || '',
|
||||
status: record.status || 'pending',
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
if (type === 'image') {
|
||||
const src = String(payload.src ?? payload.url ?? '')
|
||||
if (!src) return null
|
||||
return {
|
||||
id,
|
||||
seq,
|
||||
type: 'image',
|
||||
role: 'user',
|
||||
src,
|
||||
time: formatMessageTime(message.createTime),
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function mapCsMessage(message: CsMessageVo): SupportMessage | null {
|
||||
const content = message.content?.trim() ?? ''
|
||||
const messageType = String(message.messageType ?? '').toLowerCase()
|
||||
|
||||
if (messageType === 'image') {
|
||||
if (!content) return null
|
||||
return {
|
||||
id: createMessageId(message),
|
||||
seq: message.seq,
|
||||
type: 'image',
|
||||
role: 'user',
|
||||
src: content,
|
||||
time: formatMessageTime(message.createTime),
|
||||
}
|
||||
}
|
||||
|
||||
if (content.startsWith('{') && content.endsWith('}')) {
|
||||
try {
|
||||
const payload = JSON.parse(content) as Record<string, unknown>
|
||||
const structured = mapStructuredMessage(message, payload)
|
||||
if (structured) return structured
|
||||
} catch {
|
||||
// ignore invalid json content
|
||||
}
|
||||
}
|
||||
|
||||
const role = resolveSenderRole(message.senderType)
|
||||
if (!role || !content) return null
|
||||
|
||||
return {
|
||||
id: createMessageId(message),
|
||||
seq: message.seq,
|
||||
type: 'text',
|
||||
role,
|
||||
content,
|
||||
time: formatMessageTime(message.createTime),
|
||||
}
|
||||
}
|
||||
|
||||
export function mergeSupportMessages(
|
||||
current: SupportMessage[],
|
||||
incoming: SupportMessage[],
|
||||
): SupportMessage[] {
|
||||
if (!incoming.length) return current
|
||||
const map = new Map<string, SupportMessage>()
|
||||
for (const item of current) map.set(item.id, item)
|
||||
for (const item of incoming) map.set(item.id, item)
|
||||
return Array.from(map.values()).sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0))
|
||||
}
|
||||
|
||||
export function getMaxSeq(messages: SupportMessage[]): number {
|
||||
return messages.reduce((max, item) => Math.max(max, item.seq ?? 0), 0)
|
||||
}
|
||||
|
||||
export function buildQuickActionsMessage(): SupportMessage {
|
||||
return {
|
||||
id: 'local-quick-actions',
|
||||
type: 'quick-actions',
|
||||
actions: QUICK_ACTIONS,
|
||||
}
|
||||
}
|
||||
489
src/pages/mine/support/use-support.ts
Normal file
489
src/pages/mine/support/use-support.ts
Normal file
@@ -0,0 +1,489 @@
|
||||
import { onBeforeUnmount } from 'vue'
|
||||
import { onHide, onShow, onUnload } from '@dcloudio/uni-app'
|
||||
import { computed, ref } from 'vue'
|
||||
import { getErrorMessage } from '@/utils/api-error'
|
||||
import { getStoredToken } from '@/utils/auth-token'
|
||||
import {
|
||||
clickCsButton,
|
||||
endCsSession,
|
||||
fetchCsMessages,
|
||||
fetchCsSessionDetail,
|
||||
resolveCsSession,
|
||||
sendCsMessage,
|
||||
transferCsSession,
|
||||
} from './support.api'
|
||||
import {
|
||||
connectCsChatSocket,
|
||||
disconnectCsChatSocket,
|
||||
isCsChatSocketConnected,
|
||||
onCsChatPush,
|
||||
} from './cs-chat-socket'
|
||||
import {
|
||||
isSupportPageActive,
|
||||
markSupportPageActive,
|
||||
startSupportQueuePolling,
|
||||
startSupportQueueWaitTimer,
|
||||
stopSupportPollTimer,
|
||||
stopSupportQueueTimer,
|
||||
} from './support-runtime'
|
||||
import type {
|
||||
CsSessionVo,
|
||||
QuickActionItem,
|
||||
SupportHeaderInfo,
|
||||
SupportMessage,
|
||||
SupportPhase,
|
||||
} from './support.types'
|
||||
import {
|
||||
buildQuickActionsMessage,
|
||||
createClientMsgId,
|
||||
getMaxSeq,
|
||||
getQueueEstimate,
|
||||
getQueuePosition,
|
||||
mapCsMessage,
|
||||
mergeSupportMessages,
|
||||
normalizeSessionId,
|
||||
resolvePhaseFromSession,
|
||||
unwrapCsActionResponse,
|
||||
} from './support.utils'
|
||||
|
||||
const BOT_HEADER: SupportHeaderInfo = {
|
||||
name: '智能客服 · 小智',
|
||||
badgeText: '在线',
|
||||
badgeType: 'online',
|
||||
avatarText: '智',
|
||||
avatarShape: 'square',
|
||||
serviceHours: '7×24 小时',
|
||||
}
|
||||
|
||||
const HUMAN_HEADER: SupportHeaderInfo = {
|
||||
name: '人工客服',
|
||||
subtitle: '在线',
|
||||
badgeText: '服务中',
|
||||
badgeType: 'serving',
|
||||
avatarText: '服',
|
||||
avatarShape: 'square',
|
||||
}
|
||||
|
||||
const FOOTER_SHORTCUTS = ['常见问题', '退改政策']
|
||||
|
||||
const MESSAGE_SYNC_MIN_GAP_MS = 3000
|
||||
|
||||
function formatWait(seconds: number): string {
|
||||
return `${String(Math.floor(seconds / 60)).padStart(2, '0')}:${String(seconds % 60).padStart(2, '0')}`
|
||||
}
|
||||
|
||||
function buildHumanHeader(session: CsSessionVo): SupportHeaderInfo {
|
||||
const name = session.agentName?.trim() || HUMAN_HEADER.name
|
||||
const agentNo = session.agentNo?.trim()
|
||||
return {
|
||||
...HUMAN_HEADER,
|
||||
name,
|
||||
subtitle: agentNo ? `工号 ${agentNo} · 在线` : HUMAN_HEADER.subtitle,
|
||||
avatarText: name.slice(0, 1) || HUMAN_HEADER.avatarText,
|
||||
}
|
||||
}
|
||||
|
||||
export function useSupport() {
|
||||
const phase = ref<SupportPhase>('bot')
|
||||
const apiMessages = ref<SupportMessage[]>([])
|
||||
const inputValue = ref('')
|
||||
const queueWaitSeconds = ref(0)
|
||||
const queuePosition = ref(1)
|
||||
const queueEstimate = ref('正在为您分配人工客服')
|
||||
const sessionId = ref('')
|
||||
const humanHeader = ref<SupportHeaderInfo>(HUMAN_HEADER)
|
||||
const sessionInitializing = ref(false)
|
||||
const sending = ref(false)
|
||||
const lastSeq = ref(0)
|
||||
|
||||
let unsubscribeChatPush: (() => void) | null = null
|
||||
let ensureSessionPromise: Promise<void> | null = null
|
||||
let syncingMessages = false
|
||||
let lastMessageSyncAt = 0
|
||||
|
||||
const headerInfo = computed<SupportHeaderInfo>(() =>
|
||||
phase.value === 'human' ? humanHeader.value : BOT_HEADER,
|
||||
)
|
||||
|
||||
const formattedQueueWait = computed(() => formatWait(queueWaitSeconds.value))
|
||||
|
||||
const showTransfer = computed(() => phase.value === 'bot')
|
||||
const showEndSession = computed(() => phase.value === 'queue' || phase.value === 'human')
|
||||
|
||||
const messages = computed<SupportMessage[]>(() => {
|
||||
const list = [...apiMessages.value]
|
||||
const hasQuickActions = list.some((item) => item.type === 'quick-actions')
|
||||
if (phase.value === 'bot' && !hasQuickActions) {
|
||||
list.push(buildQuickActionsMessage())
|
||||
}
|
||||
if (phase.value === 'queue') {
|
||||
list.push({
|
||||
id: 'queue-card',
|
||||
type: 'queue',
|
||||
position: queuePosition.value,
|
||||
estimateText: queueEstimate.value,
|
||||
})
|
||||
}
|
||||
return list
|
||||
})
|
||||
|
||||
function clearQueueTimer() {
|
||||
stopSupportQueueTimer()
|
||||
queueWaitSeconds.value = 0
|
||||
}
|
||||
|
||||
function startQueueTimer() {
|
||||
clearQueueTimer()
|
||||
startSupportQueueWaitTimer(() => {
|
||||
queueWaitSeconds.value += 1
|
||||
})
|
||||
}
|
||||
|
||||
function updateQueuePolling() {
|
||||
if (!isSupportPageActive()) return
|
||||
stopSupportPollTimer()
|
||||
if (phase.value === 'queue') {
|
||||
startSupportQueuePolling(() => {
|
||||
void refreshSession()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function handleChatPush(payload: {
|
||||
message?: Parameters<typeof mapCsMessage>[0]
|
||||
session?: CsSessionVo
|
||||
}) {
|
||||
if (!isSupportPageActive()) return
|
||||
if (payload.message) {
|
||||
const mapped = mapCsMessage(payload.message)
|
||||
if (mapped) {
|
||||
apiMessages.value = mergeSupportMessages(apiMessages.value, [mapped])
|
||||
lastSeq.value = getMaxSeq(apiMessages.value)
|
||||
}
|
||||
}
|
||||
if (payload.session) {
|
||||
applySessionState(payload.session)
|
||||
}
|
||||
}
|
||||
|
||||
function bindChatSocket(session: string) {
|
||||
unsubscribeChatPush?.()
|
||||
unsubscribeChatPush = onCsChatPush((payload) => {
|
||||
handleChatPush(payload)
|
||||
})
|
||||
connectCsChatSocket(session)
|
||||
}
|
||||
|
||||
function unbindChatSocket() {
|
||||
unsubscribeChatPush?.()
|
||||
unsubscribeChatPush = null
|
||||
disconnectCsChatSocket()
|
||||
}
|
||||
|
||||
function applyActionMessages(action: ReturnType<typeof unwrapCsActionResponse>) {
|
||||
if (!action.messages?.length) return
|
||||
const mapped = action.messages
|
||||
.map(mapCsMessage)
|
||||
.filter((item): item is SupportMessage => !!item)
|
||||
if (!mapped.length) return
|
||||
apiMessages.value = mergeSupportMessages(apiMessages.value, mapped)
|
||||
lastSeq.value = getMaxSeq(apiMessages.value)
|
||||
}
|
||||
|
||||
function applySessionState(session: CsSessionVo) {
|
||||
const nextPhase = resolvePhaseFromSession(session)
|
||||
queuePosition.value = getQueuePosition(session)
|
||||
queueEstimate.value = getQueueEstimate(session)
|
||||
|
||||
if (nextPhase === 'human') {
|
||||
humanHeader.value = buildHumanHeader(session)
|
||||
phase.value = 'human'
|
||||
clearQueueTimer()
|
||||
updateQueuePolling()
|
||||
return
|
||||
}
|
||||
|
||||
if (nextPhase === 'queue') {
|
||||
phase.value = 'queue'
|
||||
if (queueWaitSeconds.value === 0) startQueueTimer()
|
||||
updateQueuePolling()
|
||||
return
|
||||
}
|
||||
|
||||
phase.value = 'bot'
|
||||
humanHeader.value = HUMAN_HEADER
|
||||
clearQueueTimer()
|
||||
updateQueuePolling()
|
||||
}
|
||||
|
||||
async function syncMessages(reset = false) {
|
||||
if (!isSupportPageActive() || !sessionId.value || syncingMessages) return
|
||||
if (isCsChatSocketConnected() && !reset) return
|
||||
|
||||
const now = Date.now()
|
||||
if (!reset && now - lastMessageSyncAt < MESSAGE_SYNC_MIN_GAP_MS) return
|
||||
|
||||
syncingMessages = true
|
||||
try {
|
||||
const afterSeq = reset ? 0 : lastSeq.value
|
||||
const rows = await fetchCsMessages(sessionId.value, afterSeq)
|
||||
const mapped = rows.map(mapCsMessage).filter((item): item is SupportMessage => !!item)
|
||||
if (reset) {
|
||||
apiMessages.value = mapped
|
||||
} else {
|
||||
apiMessages.value = mergeSupportMessages(apiMessages.value, mapped)
|
||||
}
|
||||
lastSeq.value = getMaxSeq(apiMessages.value)
|
||||
lastMessageSyncAt = Date.now()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '消息同步失败'), icon: 'none' })
|
||||
} finally {
|
||||
syncingMessages = false
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshSession() {
|
||||
if (!isSupportPageActive() || !sessionId.value) return
|
||||
try {
|
||||
const session = await fetchCsSessionDetail(sessionId.value)
|
||||
applySessionState(session)
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '会话状态更新失败'), icon: 'none' })
|
||||
}
|
||||
}
|
||||
|
||||
function teardownPage() {
|
||||
markSupportPageActive(false)
|
||||
stopSupportPollTimer()
|
||||
stopSupportQueueTimer()
|
||||
unbindChatSocket()
|
||||
clearQueueTimer()
|
||||
}
|
||||
|
||||
async function ensureSession() {
|
||||
if (ensureSessionPromise) return ensureSessionPromise
|
||||
|
||||
ensureSessionPromise = (async () => {
|
||||
if (!getStoredToken()) {
|
||||
uni.showToast({ title: '请先登录', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
sessionInitializing.value = true
|
||||
try {
|
||||
const resolved = await resolveCsSession()
|
||||
const session = resolved.session
|
||||
const nextSessionId = normalizeSessionId(session)
|
||||
|
||||
const sessionChanged = sessionId.value !== nextSessionId
|
||||
sessionId.value = nextSessionId
|
||||
applySessionState(session)
|
||||
|
||||
if (sessionChanged) {
|
||||
lastSeq.value = 0
|
||||
if (resolved.messages?.length) {
|
||||
applyActionMessages(resolved)
|
||||
} else {
|
||||
await syncMessages(true)
|
||||
}
|
||||
} else if (resolved.messages?.length) {
|
||||
applyActionMessages(resolved)
|
||||
} else {
|
||||
await syncMessages()
|
||||
}
|
||||
|
||||
bindChatSocket(nextSessionId)
|
||||
updateQueuePolling()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '客服会话创建失败'), icon: 'none' })
|
||||
} finally {
|
||||
sessionInitializing.value = false
|
||||
}
|
||||
})()
|
||||
|
||||
try {
|
||||
await ensureSessionPromise
|
||||
} finally {
|
||||
ensureSessionPromise = null
|
||||
}
|
||||
}
|
||||
|
||||
async function sendUserMessage(content: string) {
|
||||
const text = content.trim()
|
||||
if (!text || sending.value) return
|
||||
|
||||
if (!sessionId.value) {
|
||||
await ensureSession()
|
||||
if (!sessionId.value) {
|
||||
uni.showToast({ title: '会话未就绪,请稍后重试', icon: 'none' })
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
sending.value = true
|
||||
try {
|
||||
const action = unwrapCsActionResponse(await sendCsMessage(sessionId.value, {
|
||||
content: text,
|
||||
msgType: 'text',
|
||||
clientMsgId: createClientMsgId(),
|
||||
}))
|
||||
if (action.session) {
|
||||
applySessionState(action.session)
|
||||
}
|
||||
if (action.messages?.length) {
|
||||
applyActionMessages(action)
|
||||
} else if (!isCsChatSocketConnected()) {
|
||||
await syncMessages()
|
||||
}
|
||||
await refreshSession()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '发送失败'), icon: 'none' })
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function onQuickAction(action: QuickActionItem) {
|
||||
if (phase.value !== 'bot') return
|
||||
void sendUserMessage(action.label)
|
||||
}
|
||||
|
||||
async function onTransferHuman() {
|
||||
if (!sessionId.value || phase.value !== 'bot' || sending.value) return
|
||||
|
||||
sending.value = true
|
||||
try {
|
||||
await transferCsSession(sessionId.value)
|
||||
await refreshSession()
|
||||
await syncMessages()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '转人工失败'), icon: 'none' })
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function restartSession() {
|
||||
teardownPage()
|
||||
sessionId.value = ''
|
||||
apiMessages.value = []
|
||||
lastSeq.value = 0
|
||||
phase.value = 'bot'
|
||||
markSupportPageActive(true)
|
||||
await ensureSession()
|
||||
}
|
||||
|
||||
async function onEndSession() {
|
||||
if (!sessionId.value || sending.value) return
|
||||
if (phase.value !== 'queue' && phase.value !== 'human') return
|
||||
|
||||
const confirmed = await new Promise<boolean>((resolve) => {
|
||||
uni.showModal({
|
||||
title: '结束会话',
|
||||
content: '确定结束本次客服会话?',
|
||||
confirmText: '结束',
|
||||
cancelText: '取消',
|
||||
success: (res) => resolve(!!res.confirm),
|
||||
fail: () => resolve(false),
|
||||
})
|
||||
})
|
||||
if (!confirmed) return
|
||||
|
||||
sending.value = true
|
||||
try {
|
||||
await endCsSession(sessionId.value)
|
||||
await restartSession()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '结束会话失败'), icon: 'none' })
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function onCancelQueue() {
|
||||
if (!sessionId.value) return
|
||||
sending.value = true
|
||||
try {
|
||||
await endCsSession(sessionId.value)
|
||||
await restartSession()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '取消排队失败'), icon: 'none' })
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function onResolveWithoutHuman() {
|
||||
if (!sessionId.value) return
|
||||
sending.value = true
|
||||
try {
|
||||
await clickCsButton(sessionId.value, { buttonCode: 'resolved' })
|
||||
clearQueueTimer()
|
||||
await refreshSession()
|
||||
await syncMessages()
|
||||
} catch (err) {
|
||||
uni.showToast({ title: getErrorMessage(err, '操作失败'), icon: 'none' })
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function onFooterShortcut(label: string) {
|
||||
void sendUserMessage(label)
|
||||
}
|
||||
|
||||
function onDiagnosisSuggestion(label: string) {
|
||||
void sendUserMessage(label)
|
||||
}
|
||||
|
||||
function onSend() {
|
||||
const text = inputValue.value.trim()
|
||||
if (!text) return
|
||||
inputValue.value = ''
|
||||
|
||||
if (/人工|转人工/.test(text) && phase.value === 'bot') {
|
||||
void onTransferHuman()
|
||||
return
|
||||
}
|
||||
|
||||
void sendUserMessage(text)
|
||||
}
|
||||
|
||||
function activatePage() {
|
||||
markSupportPageActive(true)
|
||||
if (!sessionId.value) {
|
||||
void ensureSession()
|
||||
return
|
||||
}
|
||||
bindChatSocket(sessionId.value)
|
||||
updateQueuePolling()
|
||||
if (!isCsChatSocketConnected()) {
|
||||
void syncMessages()
|
||||
}
|
||||
}
|
||||
|
||||
onShow(activatePage)
|
||||
onHide(teardownPage)
|
||||
onUnload(teardownPage)
|
||||
onBeforeUnmount(teardownPage)
|
||||
|
||||
return {
|
||||
phase,
|
||||
messages,
|
||||
inputValue,
|
||||
sessionId,
|
||||
headerInfo,
|
||||
showTransfer,
|
||||
showEndSession,
|
||||
formattedQueueWait,
|
||||
footerShortcuts: FOOTER_SHORTCUTS,
|
||||
onQuickAction,
|
||||
onTransferHuman,
|
||||
onEndSession,
|
||||
onCancelQueue,
|
||||
onResolveWithoutHuman,
|
||||
onFooterShortcut,
|
||||
onDiagnosisSuggestion,
|
||||
onSend,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user