mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-30 05:13:59 +08:00
updata:移动端添加语言切换功能
This commit is contained in:
@@ -15,6 +15,7 @@ import { computed, ref } from 'vue'
|
||||
import { getAudioId, getChatHistory } from '@/api/chat-history/chat-history'
|
||||
import { getEnvBaseUrl } from '@/utils'
|
||||
import { toast } from '@/utils/toast'
|
||||
import { t } from '@/i18n'
|
||||
|
||||
defineOptions({
|
||||
name: 'ChatDetail',
|
||||
@@ -49,7 +50,7 @@ const agentId = ref('')
|
||||
const currentAgent = computed(() => {
|
||||
return {
|
||||
id: agentId.value,
|
||||
agentName: '智能助手',
|
||||
agentName: t('chatHistory.assistantName'),
|
||||
}
|
||||
})
|
||||
|
||||
@@ -80,7 +81,7 @@ async function loadChatHistory() {
|
||||
}
|
||||
catch (error) {
|
||||
console.error('获取聊天记录失败:', error)
|
||||
toast.error('获取聊天记录失败')
|
||||
toast.error(t('chatHistory.loadFailed'))
|
||||
}
|
||||
finally {
|
||||
loading.value = false
|
||||
@@ -114,10 +115,10 @@ function getMessageContent(message: ChatMessage): string {
|
||||
function getSpeakerName(message: ChatMessage): string {
|
||||
if (message.chatType === 1) {
|
||||
const parsed = parseUserMessage(message.content)
|
||||
return parsed ? parsed.speaker : '用户'
|
||||
return parsed ? parsed.speaker : t('chatHistory.userName')
|
||||
}
|
||||
else {
|
||||
return currentAgent.value?.agentName || 'AI助手'
|
||||
return currentAgent.value?.agentName || t('chatHistory.aiAssistantName')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +131,7 @@ function formatTime(timeStr: string) {
|
||||
// 播放音频
|
||||
async function playAudio(audioId: string) {
|
||||
if (!audioId) {
|
||||
toast.error('音频ID无效')
|
||||
toast.error(t('chatHistory.invalidAudioId'))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -168,7 +169,7 @@ async function playAudio(audioId: string) {
|
||||
// 监听播放错误
|
||||
audioContext.value.onError((error) => {
|
||||
console.error('音频播放失败:', error)
|
||||
toast.error('音频播放失败')
|
||||
toast.error(t('chatHistory.audioPlayFailed'))
|
||||
playingAudioId.value = null
|
||||
if (audioContext.value) {
|
||||
audioContext.value.destroy()
|
||||
@@ -181,7 +182,7 @@ async function playAudio(audioId: string) {
|
||||
}
|
||||
catch (error) {
|
||||
console.error('播放音频失败:', error)
|
||||
toast.error('播放音频失败')
|
||||
toast.error(t('chatHistory.playAudioFailed'))
|
||||
playingAudioId.value = null
|
||||
}
|
||||
}
|
||||
@@ -194,7 +195,7 @@ onLoad((options) => {
|
||||
}
|
||||
else {
|
||||
console.error('缺少必要参数')
|
||||
toast.error('页面参数错误')
|
||||
toast.error(t('chatHistory.parameterError'))
|
||||
}
|
||||
})
|
||||
|
||||
@@ -214,7 +215,7 @@ onUnload(() => {
|
||||
<view class="w-full bg-white" :style="{ height: `${safeAreaInsets?.top}px` }" />
|
||||
|
||||
<!-- 导航栏 -->
|
||||
<wd-navbar title="聊天详情">
|
||||
<wd-navbar :title="t('chatHistory.pageTitle')">
|
||||
<template #left>
|
||||
<wd-icon name="arrow-left" size="18" @click="goBack" />
|
||||
</template>
|
||||
@@ -230,7 +231,7 @@ onUnload(() => {
|
||||
<view v-if="loading" class="flex flex-col items-center justify-center gap-[20rpx] p-[100rpx_0]">
|
||||
<wd-loading />
|
||||
<text class="text-[28rpx] text-[#65686f]">
|
||||
加载中...
|
||||
{{ t('chatHistory.loading') }}
|
||||
</text>
|
||||
</view>
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import type { ChatSession } from '@/api/chat-history/types'
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { getChatSessions } from '@/api/chat-history/chat-history'
|
||||
import { t } from '@/i18n'
|
||||
|
||||
defineOptions({
|
||||
name: 'ChatHistory',
|
||||
@@ -53,11 +54,11 @@ const currentAgentId = computed(() => {
|
||||
// 加载聊天会话列表
|
||||
async function loadChatSessions(page = 1, isRefresh = false) {
|
||||
try {
|
||||
console.log('获取聊天会话列表', { page, isRefresh })
|
||||
console.log(t('chatHistory.getChatSessions'), { page, isRefresh })
|
||||
|
||||
// 检查是否有当前选中的智能体
|
||||
if (!currentAgentId.value) {
|
||||
console.warn('没有选中的智能体')
|
||||
console.warn(t('chatHistory.noSelectedAgent'))
|
||||
sessionList.value = []
|
||||
return
|
||||
}
|
||||
@@ -86,7 +87,7 @@ async function loadChatSessions(page = 1, isRefresh = false) {
|
||||
currentPage.value = page
|
||||
}
|
||||
catch (error) {
|
||||
console.error('获取聊天会话列表失败:', error)
|
||||
console.error(t('chatHistory.getChatSessionsFailed'), error)
|
||||
if (page === 1) {
|
||||
sessionList.value = []
|
||||
}
|
||||
@@ -115,48 +116,48 @@ async function loadMore() {
|
||||
// 格式化时间
|
||||
function formatTime(timeStr: string) {
|
||||
if (!timeStr)
|
||||
return '未知时间'
|
||||
|
||||
return t('chatHistory.unknownTime')
|
||||
|
||||
// 处理时间字符串,确保格式正确
|
||||
const date = new Date(timeStr.replace(' ', 'T')) // 转换为ISO格式
|
||||
const now = new Date()
|
||||
|
||||
|
||||
// 检查日期是否有效
|
||||
if (Number.isNaN(date.getTime())) {
|
||||
return timeStr // 如果解析失败,直接返回原字符串
|
||||
}
|
||||
|
||||
|
||||
const diff = now.getTime() - date.getTime()
|
||||
|
||||
|
||||
// 小于1分钟
|
||||
if (diff < 60000)
|
||||
return '刚刚'
|
||||
|
||||
return t('chatHistory.justNow')
|
||||
|
||||
// 小于1小时
|
||||
if (diff < 3600000)
|
||||
return `${Math.floor(diff / 60000)}分钟前`
|
||||
|
||||
return t('chatHistory.minutesAgo', { minutes: Math.floor(diff / 60000) })
|
||||
|
||||
// 小于1天(24小时)
|
||||
if (diff < 86400000)
|
||||
return `${Math.floor(diff / 3600000)}小时前`
|
||||
|
||||
return t('chatHistory.hoursAgo', { hours: Math.floor(diff / 3600000) })
|
||||
|
||||
// 小于7天
|
||||
if (diff < 604800000) {
|
||||
const days = Math.floor(diff / 86400000)
|
||||
return `${days}天前`
|
||||
return t('chatHistory.daysAgo', { days })
|
||||
}
|
||||
|
||||
|
||||
// 超过7天,显示具体日期
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
const currentYear = now.getFullYear()
|
||||
|
||||
|
||||
// 如果是当前年份,不显示年份
|
||||
if (year === currentYear) {
|
||||
return `${month}-${day}`
|
||||
}
|
||||
|
||||
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
@@ -186,8 +187,8 @@ defineExpose({
|
||||
<view v-if="loading && sessionList.length === 0" class="loading-container">
|
||||
<wd-loading color="#336cff" />
|
||||
<text class="loading-text">
|
||||
加载中...
|
||||
</text>
|
||||
{{ t('chatHistory.loading') }}
|
||||
</text>
|
||||
</view>
|
||||
|
||||
<!-- 会话列表 -->
|
||||
@@ -204,7 +205,7 @@ defineExpose({
|
||||
<view class="session-info">
|
||||
<view class="session-header">
|
||||
<text class="session-title">
|
||||
对话记录 {{ session.sessionId.substring(0, 8) }}...
|
||||
{{ t('chatHistory.conversationRecord') }} {{ session.sessionId.substring(0, 8) }}...
|
||||
</text>
|
||||
<text class="session-time">
|
||||
{{ formatTime(session.createdAt) }}
|
||||
@@ -212,7 +213,7 @@ defineExpose({
|
||||
</view>
|
||||
<view class="session-meta">
|
||||
<text class="chat-count">
|
||||
共 {{ session.chatCount }} 条对话
|
||||
{{ t('chatHistory.totalChats', { count: session.chatCount }) }}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
@@ -225,14 +226,14 @@ defineExpose({
|
||||
<view v-if="loadingMore" class="loading-more">
|
||||
<wd-loading color="#336cff" size="24" />
|
||||
<text class="loading-more-text">
|
||||
加载中...
|
||||
{{ t('chatHistory.loading') }}
|
||||
</text>
|
||||
</view>
|
||||
|
||||
<!-- 没有更多数据 -->
|
||||
<view v-else-if="!hasMore && sessionList.length > 0" class="no-more">
|
||||
<text class="no-more-text">
|
||||
没有更多数据了
|
||||
{{ t('chatHistory.noMoreData') }}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
@@ -241,11 +242,11 @@ defineExpose({
|
||||
<view v-else-if="!loading" class="empty-state">
|
||||
<wd-icon name="chat" custom-class="empty-icon" />
|
||||
<text class="empty-text">
|
||||
暂无聊天记录
|
||||
</text>
|
||||
<text class="empty-desc">
|
||||
与智能体的对话记录会显示在这里
|
||||
</text>
|
||||
{{ t('chatHistory.noChatRecords') }}
|
||||
</text>
|
||||
<text class="empty-desc">
|
||||
{{ t('chatHistory.chatRecordsDescription') }}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user