48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { onBeforeUnmount, ref, watch } from 'vue'
|
|
import { getConsultationTranscripts, type ConsultationCallRecord } from '@/api/tcm'
|
|
import { normalizeConsultationRecords } from '@/utils/consultation-transcripts'
|
|
|
|
export function useConsultationTranscripts(diagnosisId: () => number) {
|
|
const records = ref<ConsultationCallRecord[]>([])
|
|
const loading = ref(false)
|
|
const error = ref('')
|
|
let sequence = 0
|
|
let controller: AbortController | undefined
|
|
|
|
const refresh = async () => {
|
|
const id = Number(diagnosisId())
|
|
const request = ++sequence
|
|
controller?.abort()
|
|
error.value = ''
|
|
if (!Number.isSafeInteger(id) || id <= 0) {
|
|
records.value = []
|
|
loading.value = false
|
|
return
|
|
}
|
|
controller = new AbortController()
|
|
loading.value = true
|
|
try {
|
|
const result = await getConsultationTranscripts({ diagnosis_id: id }, controller.signal)
|
|
if (request !== sequence || Number(diagnosisId()) !== id) return
|
|
records.value = normalizeConsultationRecords(result, id)
|
|
} catch (reason: any) {
|
|
if (request !== sequence || Number(diagnosisId()) !== id) return
|
|
error.value = String(reason?.msg || reason?.message || '面诊文字对话加载失败,请重试')
|
|
} finally {
|
|
if (request === sequence) loading.value = false
|
|
}
|
|
}
|
|
|
|
watch(diagnosisId, () => {
|
|
records.value = []
|
|
void refresh()
|
|
}, { immediate: true, flush: 'sync' })
|
|
|
|
onBeforeUnmount(() => {
|
|
sequence++
|
|
controller?.abort()
|
|
records.value = []
|
|
})
|
|
return { records, loading, error, refresh }
|
|
}
|