feat:客服

This commit is contained in:
李伊帆
2026-10-16 08:53:45 +08:00
parent e22ebd6bef
commit 03839624ed
42 changed files with 1375 additions and 483 deletions

View File

@@ -85,9 +85,6 @@
>
<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">
@@ -205,7 +202,6 @@
>
<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>
@@ -215,6 +211,9 @@
</scroll-view>
<view class="support-page__footer">
<text class="support-page__secure">
本次会话已加密,客服不会向您索要密码或验证码
</text>
<view class="support-page__footer-shortcuts">
<text
v-for="shortcut in footerShortcuts"
@@ -226,14 +225,17 @@
</text>
</view>
<view class="support-page__input-row">
<view class="support-page__mic">
<view class="support-page__mic" @tap="toggleInputMode">
<image
v-if="inputMode === 'text'"
class="support-page__mic-icon"
:src="staticAssets.iconSupportMic"
mode="aspectFit"
/>
<text v-else class="support-page__keyboard-icon">⌨</text>
</view>
<view class="support-page__input-wrap">
<view v-if="inputMode === 'text'" class="support-page__input-wrap">
<input
v-model="inputValue"
class="support-page__input"
@@ -244,24 +246,39 @@
@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
v-else
class="support-page__voice-btn"
:class="{
'support-page__voice-btn--holding': voiceHolding,
'support-page__voice-btn--recognizing': voiceRecognizing,
}"
@touchstart.prevent="onVoiceTouchStart"
@touchend.prevent="onVoiceTouchEnd"
@touchcancel.prevent="onVoiceTouchCancel"
>
<text class="support-page__voice-btn-text">
{{ voiceButtonText }}
</text>
</view>
<template v-if="inputMode === 'text'">
<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>
</template>
</view>
<text class="support-page__secure">
本次会话已加密,客服不会向您索要密码或验证码
</text>
</view>
</view>
</template>
@@ -270,6 +287,7 @@
import { computed, nextTick, ref, watch } from 'vue'
import { staticAssets } from '@/utils/static-assets'
import { useSupport } from './use-support'
import { useSupportVoice } from './use-support-voice'
import type { DiagnosisItem } from './support.types'
const {
@@ -290,8 +308,29 @@ const {
onSend,
} = useSupport()
const {
inputMode,
voiceHolding,
voiceRecognizing,
toggleInputMode,
onVoiceTouchStart,
onVoiceTouchEnd,
onVoiceTouchCancel,
} = useSupportVoice({
onRecognized: (text) => {
inputValue.value = text
onSend()
},
})
const humanAvatarText = computed(() => headerInfo.value.avatarText || '服')
const voiceButtonText = computed(() => {
if (voiceRecognizing.value && !voiceHolding.value) return '识别中...'
if (voiceHolding.value) return '松手 发送'
return '按住 说话'
})
const scrollIntoView = ref('chat-bottom')
watch(

View File

@@ -209,7 +209,7 @@ $support-danger: #f04438;
}
&--user {
border-radius: 16rpx;
border-radius: 50%;
background: #d0d5dd;
color: #fff;
}
@@ -259,13 +259,6 @@ $support-danger: #f04438;
}
}
.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;
@@ -637,6 +630,34 @@ $support-danger: #f04438;
height: 56rpx;
}
.support-page__keyboard-icon {
font-size: 40rpx;
line-height: 1;
color: $support-text-muted;
}
.support-page__voice-btn {
flex: 1;
height: 72rpx;
border-radius: 36rpx;
background: #f5f6f8;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
&--holding,
&--recognizing {
background: rgba(30, 198, 113, 0.12);
}
}
.support-page__voice-btn-text {
font-size: 28rpx;
color: $support-text;
line-height: 1;
}
.support-page__input-wrap {
flex: 1;
height: 72rpx;
@@ -687,7 +708,9 @@ $support-danger: #f04438;
}
.support-page__secure {
margin-top: 12rpx;
display: block;
margin: 0 0 16rpx;
padding: 0 16rpx;
text-align: center;
font-size: 22rpx;
color: $support-text-secondary;

View File

@@ -128,7 +128,7 @@ export type SupportMessage =
type: 'text'
role: ChatRole
content: string
time?: string
createTime?: string
}
| {
id: string
@@ -165,7 +165,7 @@ export type SupportMessage =
type: 'image'
role: 'user'
src: string
time?: string
createTime?: string
}
export interface SupportHeaderInfo {

View File

@@ -91,11 +91,62 @@ export function getQueueEstimate(session: CsSessionVo): string {
return session.estimateWaitTime?.trim() || '正在为您分配人工客服'
}
function formatMessageTime(value?: string): string | undefined {
if (!value) return undefined
const TIME_DIVIDER_GAP_MS = 5 * 60 * 1000
function parseMessageTimestamp(value?: string): number | null {
if (!value) return null
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')}`
const ts = date.getTime()
return Number.isNaN(ts) ? null : ts
}
function formatTimeDividerText(value: string): string {
const date = new Date(value)
if (Number.isNaN(date.getTime())) return ''
const hm = `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
const now = new Date()
if (date.toDateString() === now.toDateString()) return hm
const yesterday = new Date(now)
yesterday.setDate(now.getDate() - 1)
if (date.toDateString() === yesterday.toDateString()) return `昨天 ${hm}`
return `${date.getMonth() + 1}月${date.getDate()}日 ${hm}`
}
function getMessageCreateTime(item: SupportMessage): string | undefined {
if (item.type === 'text' || item.type === 'image') return item.createTime
return undefined
}
export function injectTimeDividers(messages: SupportMessage[]): SupportMessage[] {
const result: SupportMessage[] = []
let lastTimestamp = 0
for (const item of messages) {
const createTime = getMessageCreateTime(item)
const timestamp = parseMessageTimestamp(createTime)
const shouldInsertDivider = timestamp !== null
&& (lastTimestamp === 0 || timestamp - lastTimestamp >= TIME_DIVIDER_GAP_MS)
if (shouldInsertDivider && createTime) {
const text = formatTimeDividerText(createTime)
if (text) {
result.push({
id: `time-${timestamp}`,
type: 'time',
text,
})
}
lastTimestamp = timestamp
} else if (timestamp !== null) {
lastTimestamp = timestamp
}
result.push(item)
}
return result
}
function resolveSenderRole(senderType?: string | number): ChatRole | null {
@@ -183,7 +234,7 @@ function mapStructuredMessage(message: CsMessageVo, payload: Record<string, unkn
type: 'image',
role: 'user',
src,
time: formatMessageTime(message.createTime),
createTime: message.createTime,
}
}
@@ -202,7 +253,7 @@ export function mapCsMessage(message: CsMessageVo): SupportMessage | null {
type: 'image',
role: 'user',
src: content,
time: formatMessageTime(message.createTime),
createTime: message.createTime,
}
}
@@ -225,7 +276,7 @@ export function mapCsMessage(message: CsMessageVo): SupportMessage | null {
type: 'text',
role,
content,
time: formatMessageTime(message.createTime),
createTime: message.createTime,
}
}

View File

@@ -0,0 +1,336 @@
import { onBeforeUnmount, ref } from 'vue'
type InputMode = 'text' | 'voice'
interface UseSupportVoiceOptions {
onRecognized: (text: string) => void
}
interface SpeechRecognitionInstance {
lang: string
continuous: boolean
interimResults: boolean
start: () => void
stop: () => void
abort: () => void
onresult: ((event: { results: Array<Array<{ transcript?: string }>> }) => void) | null
onerror: ((event: { error?: string }) => void) | null
onend: (() => void) | null
}
const MAX_VOICE_DURATION_MS = 60000
export function useSupportVoice(options: UseSupportVoiceOptions) {
const inputMode = ref<InputMode>('text')
const voiceHolding = ref(false)
const voiceRecognizing = ref(false)
let touchCanceled = false
let recordStopTimer: ReturnType<typeof setTimeout> | null = null
let h5Recognition: SpeechRecognitionInstance | null = null
// #ifdef MP-WEIXIN
let mpRecoManager: {
start: (opts: { duration: number; lang: string }) => void
stop: () => void
onStop?: (res: { result?: string }) => void
onError?: () => void
} | null = null
try {
const plugin = requirePlugin('WechatSI') as {
getRecordRecognitionManager: () => NonNullable<typeof mpRecoManager>
}
mpRecoManager = plugin.getRecordRecognitionManager()
mpRecoManager.onStop = (res) => {
voiceRecognizing.value = false
const text = res.result?.trim()
if (text) {
options.onRecognized(text)
return
}
uni.showToast({ title: '未识别到语音内容', icon: 'none' })
}
mpRecoManager.onError = () => {
voiceRecognizing.value = false
uni.showToast({ title: '语音识别失败', icon: 'none' })
}
} catch {
mpRecoManager = null
}
// #endif
function clearRecordStopTimer() {
if (recordStopTimer) clearTimeout(recordStopTimer)
recordStopTimer = null
}
function resetVoiceState() {
voiceHolding.value = false
voiceRecognizing.value = false
touchCanceled = false
clearRecordStopTimer()
}
function isVoiceSupported(): boolean {
// #ifdef H5
const win = window as Window & {
SpeechRecognition?: new () => SpeechRecognitionInstance
webkitSpeechRecognition?: new () => SpeechRecognitionInstance
}
return !!(win.SpeechRecognition || win.webkitSpeechRecognition)
// #endif
// #ifdef APP-PLUS
return typeof plus !== 'undefined' && !!plus.speech
// #endif
// #ifdef MP-WEIXIN
return !!mpRecoManager
// #endif
return false
}
function ensureRecordPermission(): Promise<boolean> {
return new Promise((resolve) => {
uni.getSetting({
success: (res) => {
if (res.authSetting['scope.record']) {
resolve(true)
return
}
uni.authorize({
scope: 'scope.record',
success: () => resolve(true),
fail: () => {
uni.showModal({
title: '需要麦克风权限',
content: '请在设置中开启麦克风权限后使用语音输入',
confirmText: '去设置',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting({})
}
},
})
resolve(false)
},
})
},
fail: () => resolve(false),
})
})
}
function cleanupH5Recognition() {
if (!h5Recognition) return
try {
h5Recognition.abort()
} catch {
// ignore
}
h5Recognition.onresult = null
h5Recognition.onerror = null
h5Recognition.onend = null
h5Recognition = null
}
async function startH5Voice() {
const win = window as Window & {
SpeechRecognition?: new () => SpeechRecognitionInstance
webkitSpeechRecognition?: new () => SpeechRecognitionInstance
}
const Ctor = win.SpeechRecognition || win.webkitSpeechRecognition
if (!Ctor) {
uni.showToast({ title: '当前浏览器不支持语音输入', icon: 'none' })
resetVoiceState()
return
}
cleanupH5Recognition()
const recognition = new Ctor()
h5Recognition = recognition
recognition.lang = 'zh-CN'
recognition.continuous = false
recognition.interimResults = false
voiceRecognizing.value = true
recognition.onresult = (event) => {
const text = event.results[0]?.[0]?.transcript?.trim()
if (text) options.onRecognized(text)
}
recognition.onerror = (event) => {
if (event.error !== 'aborted') {
uni.showToast({ title: '语音识别失败', icon: 'none' })
}
}
recognition.onend = () => {
voiceRecognizing.value = false
h5Recognition = null
}
try {
recognition.start()
} catch {
voiceRecognizing.value = false
h5Recognition = null
uni.showToast({ title: '无法启动语音识别', icon: 'none' })
}
}
function stopH5Voice() {
if (!h5Recognition) return
try {
h5Recognition.stop()
} catch {
h5Recognition.abort()
}
}
function startAppVoice() {
if (!plus.speech) {
uni.showToast({ title: '当前环境不支持语音输入', icon: 'none' })
return
}
voiceRecognizing.value = true
plus.speech.startRecognize(
{
engine: 'baidu',
lang: 'zh-cn',
punctuation: false,
timeout: 10000,
},
(text) => {
voiceRecognizing.value = false
const result = String(text || '').trim()
if (result) {
options.onRecognized(result)
return
}
uni.showToast({ title: '未识别到语音内容', icon: 'none' })
},
() => {
voiceRecognizing.value = false
uni.showToast({ title: '语音识别失败', icon: 'none' })
},
)
}
async function startMpVoice() {
if (!mpRecoManager) {
uni.showToast({ title: '未配置同声传译插件,暂无法使用语音输入', icon: 'none' })
resetVoiceState()
return
}
const granted = await ensureRecordPermission()
if (!granted) {
resetVoiceState()
return
}
voiceRecognizing.value = true
mpRecoManager.start({ duration: MAX_VOICE_DURATION_MS, lang: 'zh_CN' })
}
function stopMpVoice() {
mpRecoManager?.stop()
}
function cancelVoiceCapture() {
// #ifdef H5
cleanupH5Recognition()
// #endif
// #ifdef APP-PLUS
if (voiceRecognizing.value) {
plus.speech.stopRecognize()
}
// #endif
// #ifdef MP-WEIXIN
mpRecoManager?.stop()
// #endif
voiceRecognizing.value = false
}
function toggleInputMode() {
if (inputMode.value === 'text') {
if (!isVoiceSupported()) {
uni.showToast({ title: '当前环境不支持语音输入', icon: 'none' })
return
}
inputMode.value = 'voice'
return
}
cancelVoiceCapture()
resetVoiceState()
inputMode.value = 'text'
}
async function onVoiceTouchStart() {
if (voiceRecognizing.value) return
touchCanceled = false
voiceHolding.value = true
// #ifdef H5
await startH5Voice()
// #endif
// #ifdef MP-WEIXIN
await startMpVoice()
// #endif
clearRecordStopTimer()
recordStopTimer = setTimeout(() => {
if (voiceHolding.value) {
onVoiceTouchEnd()
}
}, MAX_VOICE_DURATION_MS)
}
function onVoiceTouchEnd() {
if (!voiceHolding.value) return
voiceHolding.value = false
clearRecordStopTimer()
if (touchCanceled) {
cancelVoiceCapture()
return
}
// #ifdef H5
stopH5Voice()
// #endif
// #ifdef APP-PLUS
startAppVoice()
// #endif
// #ifdef MP-WEIXIN
stopMpVoice()
// #endif
}
function onVoiceTouchCancel() {
touchCanceled = true
voiceHolding.value = false
clearRecordStopTimer()
cancelVoiceCapture()
}
onBeforeUnmount(() => {
cancelVoiceCapture()
clearRecordStopTimer()
})
return {
inputMode,
voiceHolding,
voiceRecognizing,
toggleInputMode,
onVoiceTouchStart,
onVoiceTouchEnd,
onVoiceTouchCancel,
}
}

View File

@@ -39,6 +39,7 @@ import {
getMaxSeq,
getQueueEstimate,
getQueuePosition,
injectTimeDividers,
mapCsMessage,
mergeSupportMessages,
normalizeSessionId,
@@ -124,7 +125,7 @@ export function useSupport() {
estimateText: queueEstimate.value,
})
}
return list
return injectTimeDividers(list)
})
function clearQueueTimer() {