This commit is contained in:
Your Name
2026-11-17 14:05:05 +08:00
parent 08892f4270
commit 64df667585
8 changed files with 2021 additions and 551 deletions
@@ -0,0 +1,335 @@
<template>
<section class="staff-chat-panel" aria-label="站内沟通会话">
<div class="chat-toolbar">
<span class="freshness">近 {{ days }} 天患者沟通会话(站内聊天 / 通话 / 服务记录)</span>
<div class="days-switch">
<button
v-for="d in ([7, 30, 90] as const)"
:key="d"
type="button"
:class="{ active: days === d }"
@click="changeDays(d)"
>
{{ d }} 天
</button>
</div>
</div>
<el-alert v-if="error" :title="error" type="error" :closable="false" show-icon class="mb-3" />
<el-skeleton v-if="loading && !cases.length" :rows="5" animated />
<el-empty
v-else-if="!cases.length"
:image-size="72"
:description="`近 ${days} 天暂无站内聊天或沟通记录`"
/>
<div v-else class="split">
<ul class="conversations">
<li v-for="item in cases" :key="item.diagnosis_id">
<button
type="button"
:class="{ active: selectedDiagnosisId === item.diagnosis_id }"
@click="openCase(item)"
>
<div class="conv-head">
<strong>{{ item.patient_name || '未命名患者' }}</strong>
<small>#{{ item.diagnosis_id }}</small>
</div>
<small>
聊天 {{ item.im_messages ?? 0 }} 条 · 记录 {{ item.manual_records }} 条
</small>
</button>
</li>
</ul>
<div class="messages-wrap">
<el-skeleton v-if="contextLoading" :rows="4" animated />
<ol v-else-if="sources.length" class="messages">
<li
v-for="src in sources"
:key="src.id"
:class="src.speaker === 'patient' ? 'inbound' : 'outbound'"
>
<span class="msg-meta">
{{ speakerLabel(src.speaker) }} · {{ channelLabel(src.channel) }} ·
{{ shanghaiTime(src.occurred_at) }}
</span>
<p>{{ src.text }}</p>
</li>
</ol>
<el-empty
v-else-if="selectedDiagnosisId"
:image-size="64"
description="该患者近阶段暂无文字或转写明细"
/>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
import {
assistantContext,
assistantStaffCases,
type Days,
type Source,
type StaffCase
} from '@/api/assistantService'
import { errorText, isCancelled, shanghaiTime } from '../shared'
const props = defineProps<{
assistantId: number
refreshKey: number
}>()
const days = ref<Days>(30)
const cases = ref<StaffCase[]>([])
const sources = ref<Source[]>([])
const selectedDiagnosisId = ref(0)
const loading = ref(false)
const contextLoading = ref(false)
const error = ref('')
let listRevision = 0
let ctxRevision = 0
function speakerLabel(speaker: Source['speaker']) {
if (speaker === 'patient') return '患者'
if (speaker === 'service') return '员工'
return '系统'
}
function channelLabel(channel: string) {
if (channel === 'im') return '站内聊天'
if (channel === 'call') return '通话转写'
if (channel === 'wecom') return '企微'
if (channel === 'manual') return '服务记录'
return channel || '记录'
}
async function loadCases() {
if (props.assistantId <= 0) {
cases.value = []
sources.value = []
selectedDiagnosisId.value = 0
return
}
const token = ++listRevision
loading.value = true
error.value = ''
try {
const res = await assistantStaffCases({
assistant_id: props.assistantId,
days: days.value,
page_no: 1,
page_size: 30
})
if (token !== listRevision) return
const sorted = [...(res.lists || [])].sort(
(a, b) =>
(b.im_messages ?? 0) + b.manual_records - ((a.im_messages ?? 0) + a.manual_records)
)
cases.value = sorted
if (sorted.length) {
const keep = sorted.find((c) => c.diagnosis_id === selectedDiagnosisId.value)
await openCase(keep || sorted[0])
} else {
selectedDiagnosisId.value = 0
sources.value = []
}
} catch (cause) {
if (token !== listRevision || isCancelled(cause)) return
cases.value = []
sources.value = []
error.value = errorText(cause)
} finally {
if (token === listRevision) loading.value = false
}
}
async function openCase(item: StaffCase) {
selectedDiagnosisId.value = item.diagnosis_id
const token = ++ctxRevision
contextLoading.value = true
try {
const ctx = await assistantContext({
diagnosis_id: item.diagnosis_id,
days: days.value,
assistant_id: props.assistantId
})
if (token !== ctxRevision) return
sources.value = ctx.sources || []
} catch (cause) {
if (token !== ctxRevision || isCancelled(cause)) return
sources.value = []
} finally {
if (token === ctxRevision) contextLoading.value = false
}
}
function changeDays(d: Days) {
if (days.value === d) return
days.value = d
void loadCases()
}
watch(
() => [props.assistantId, props.refreshKey],
() => {
selectedDiagnosisId.value = 0
void loadCases()
},
{ immediate: true }
)
</script>
<style scoped>
.chat-toolbar {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 12px;
}
.freshness {
font-size: 12px;
color: var(--el-text-color-secondary);
}
.days-switch {
display: flex;
gap: 6px;
}
.days-switch button {
border: 1px solid var(--el-border-color-light);
border-radius: 4px;
padding: 4px 10px;
background: var(--el-bg-color);
color: var(--el-text-color-regular);
font-size: 12px;
cursor: pointer;
}
.days-switch button.active {
background: var(--el-color-primary-light-9);
border-color: var(--el-color-primary);
color: var(--el-color-primary);
font-weight: 500;
}
.split {
display: grid;
grid-template-columns: 240px minmax(0, 1fr);
gap: 12px;
min-height: 360px;
}
.conversations {
list-style: none;
margin: 0;
padding: 0;
border: 1px solid var(--el-border-color-lighter);
border-radius: 6px;
max-height: 480px;
overflow-y: auto;
}
.conversations li + li {
border-top: 1px solid var(--el-border-color-lighter);
}
.conversations button {
width: 100%;
text-align: left;
border: 0;
background: transparent;
padding: 10px 12px;
cursor: pointer;
}
.conversations button:hover {
background: var(--el-fill-color-light);
}
.conversations button.active {
background: var(--el-color-primary-light-9);
}
.conv-head {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 6px;
}
.conversations strong {
font-size: 13px;
font-weight: 600;
color: var(--el-text-color-primary);
}
.conversations small {
display: block;
margin-top: 3px;
font-size: 11px;
color: var(--el-text-color-secondary);
}
.messages-wrap {
border: 1px solid var(--el-border-color-lighter);
border-radius: 6px;
padding: 12px;
max-height: 480px;
overflow-y: auto;
background: var(--el-fill-color-blank);
}
.messages {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
gap: 10px;
}
.messages li {
max-width: 82%;
padding: 8px 12px;
border-radius: 6px;
font-size: 13px;
line-height: 1.5;
}
.messages li.inbound {
align-self: flex-start;
background: var(--el-fill-color-light);
}
.messages li.outbound {
align-self: flex-end;
background: var(--el-color-primary-light-9);
}
.msg-meta {
display: block;
font-size: 11px;
color: var(--el-text-color-secondary);
margin-bottom: 4px;
}
.messages p {
margin: 0;
white-space: pre-wrap;
word-break: break-word;
}
@media (max-width: 768px) {
.split {
grid-template-columns: 1fr;
}
}
</style>