Files
wxapp_companion/pages/ai/index.wxml
T
lik 6da802cff9 feat(ai): 对话页接入实时语音、图片识别与失败重试
- 聊天 WebSocket 提到 app 级,语音分片与对话共用连接,补齐心跳、重连与连接单飞
- 语音输入走 asr_start/asr_chunk/asr_stop,中间结果实时回填,链路异常退回整段转写
- 图片选完先上屏再 OCR,识别原文以 format 消息并入对话
- 失败状态挂在原提问上可点击重试,401 静默重登后原样重发
- 消息区改为暖白纸面与头像+时间布局,常见问题与清空收进更多菜单
2026-11-18 22:34:17 +08:00

219 lines
9.1 KiB
Plaintext

<view class="chat-container">
<!-- 消息列表区域:页面唯一滚动容器 -->
<scroll-view
class="message-list"
scroll-y
scroll-into-view="{{scrollToMessage}}"
enhanced
show-scrollbar="{{false}}"
scroll-with-animation
>
<!-- 欢迎区:空会话时的落点 -->
<view class="welcome-area" wx:if="{{messages.length === 0}}">
<text class="welcome-title">陪诊助手</text>
<view class="question-heading"><text>您可以这样问</text><text class="question-heading-sub">点选即发送</text></view>
<view class="question-list">
<view
class="quick-item"
wx:for="{{quickQuestions}}"
wx:key="id"
bindtap="onQuickQuestionTap"
data-question="{{item.text}}"
hover-class="question-hover"
aria-role="button"
aria-label="{{item.text}}"
>
<view class="question-mark" aria-hidden="true">?</view>
<text>{{item.text}}</text>
<t-icon name="chevron-right" size="26rpx" color="#A8551F" />
</view>
</view>
<text class="welcome-note">AI 生成内容仅供参考,请以系统实际数据为准。</text>
</view>
<!-- 消息:头像在上、气泡在下,时间紧跟头像右侧;用户消息整条靠右 -->
<view
class="message-item {{item.type}}"
wx:for="{{messages}}"
wx:key="id"
id="msg-{{item.id}}"
>
<!-- 连接或服务错误是系统状态,不使用头像 -->
<view wx:if="{{item.type === 'system'}}" class="system-message">
<text>{{item.content}}</text>
</view>
<block wx:else>
<view class="message-head">
<!-- AI:品牌线稿标记;用户:人像图标,身份只由图形区分 -->
<view wx:if="{{item.type === 'ai'}}" class="avatar">
<image class="avatar-img" src="/images/ai-ink.png" mode="aspectFit" />
</view>
<view wx:else class="avatar">
<t-icon name="user" size="30rpx" color="#6F655C" />
</view>
<text class="message-author">{{item.type === 'ai' ? 'AI助手' : '我'}}</text>
<text class="message-time" wx:if="{{item.showTime}}">{{item.time}}</text>
</view>
<view class="message-content">
<view class="message-bubble" wx:if="{{item.contentType !== 'questions'}}">
<t-chat-markdown
wx:if="{{item.type === 'ai' && item.contentType === 'text'}}"
class="message-markdown"
content="{{item.content}}"
/>
<text wx:elif="{{item.contentType === 'text'}}" class="message-text" user-select>{{item.content}}</text>
<image
wx:if="{{item.contentType === 'image' && item.content}}"
class="message-image"
src="{{item.content}}"
mode="widthFix"
bindtap="previewImage"
data-src="{{item.content}}"
/>
<!-- 历史里的图片不存临时路径(重启即失效),退化成一行占位 -->
<text wx:elif="{{item.contentType === 'image'}}" class="message-image-placeholder">[图片]</text>
</view>
<!-- 常见问题:把问题清单直接铺进会话流里,点一下即发送 -->
<view wx:else class="question-list">
<view
class="quick-item"
wx:for="{{item.content}}"
wx:for-item="question"
wx:key="id"
bindtap="onQuickQuestionTap"
data-question="{{question.text}}"
hover-class="question-hover"
aria-role="button"
aria-label="{{question.text}}"
>
<text>{{question.text}}</text>
<t-icon name="chevron-right" size="26rpx" color="#A8551F" />
</view>
</view>
<!-- AI 声明:本地问题清单和系统错误均不展示 -->
<text
wx:if="{{item.type === 'ai' && item.contentType !== 'questions'}}"
class="message-note"
>AI 生成,仅供参考</text>
<text
wx:if="{{item.type === 'user' && item.status === 'failed'}}"
class="message-retry"
bindtap="onRetryMessage"
data-id="{{item.id}}"
>发送失败,点击重试</text>
</view>
</block>
</view>
<!-- AI 正在输入:图片/语音识别期间也用它作为等待态 -->
<view class="message-item ai" wx:if="{{isTyping}}" id="msg-typing">
<view class="message-head">
<view class="avatar">
<image class="avatar-img" src="/images/ai-ink.png" mode="aspectFit" />
</view>
<text class="message-author">AI助手</text>
</view>
<view class="message-content">
<view class="message-bubble typing-bubble">
<view class="typing-lines">
<text class="typing-text">{{isRecognizing ? '正在识别,请稍候' : '正在处理...' + processingSeconds + '秒'}}</text>
<!-- 后台进度:工具/子代理有动作时这行会变化,避免只剩一个计时器 -->
<text
class="typing-progress"
wx:if="{{!isRecognizing && progressLabel}}"
>{{progressLabel}}{{progressStep ? ' · 已完成 ' + progressStep + ' 步' : ''}}</text>
</view>
</view>
</view>
</view>
<!-- 底部留白,确保最新消息可见 -->
<view style="height: 40rpx;"></view>
</scroll-view>
<!-- 语音状态:只在录音阶段显示,紧贴输入区上方 -->
<view class="voice-status" wx:if="{{recording}}">
<view class="voice-dot recording"></view>
<view class="voice-status-main">
<text class="voice-status-text">正在录音 {{recordTime}}</text>
<text class="voice-status-sub">{{voiceRealtimeOff ? '结束后整段转写' : '点麦克风或发送键发出'}}</text>
</view>
<view
class="voice-cancel"
bindtap="onCancelVoiceTap"
aria-role="button"
aria-label="取消语音输入"
>
<t-icon name="close" size="24rpx" color="#A8551F" />
<text>取消</text>
</view>
</view>
<!-- 输入面板:上方文字,下方输入方式与发送 -->
<view class="composer">
<view class="input-area">
<textarea
class="message-input {{recording || isRecognizing ? 'is-readonly' : ''}}"
value="{{inputValue}}"
placeholder="说说您的问题或需要…"
placeholder-class="input-placeholder"
auto-height
maxlength="-1"
cursor-spacing="24"
confirm-type="send"
disabled="{{recording || isRecognizing}}"
adjust-position="{{true}}"
hold-keyboard="{{true}}"
bindinput="onInputChange"
bindconfirm="sendMessage"
focus="{{inputFocus}}"
/>
</view>
<view class="composer-actions">
<view class="input-tools">
<view class="input-tool {{recording ? 'is-recording' : ''}}" bindtap="onMicTap" aria-role="button" aria-label="{{recording ? '结束并发送语音' : '语音输入'}}">
<t-icon name="microphone" size="34rpx" color="{{recording ? '#A8551F' : '#6F655C'}}" />
<text>{{recording ? '结束' : '语音'}}</text>
</view>
<view class="input-tool" bindtap="onImageTap" aria-role="button" aria-label="拍照或选择图片">
<t-icon name="image" size="34rpx" color="#6F655C" />
<text>图片</text>
</view>
<view class="input-tool {{showMoreMenu ? 'is-open' : ''}}" bindtap="toggleMoreMenu" aria-role="button" aria-label="更多功能" aria-expanded="{{showMoreMenu}}">
<view class="more-dots" aria-hidden="true">
<view class="more-dot"></view>
<view class="more-dot"></view>
<view class="more-dot"></view>
</view>
<text>更多</text>
</view>
</view>
<view
class="send-btn {{inputValue && !isRecognizing ? 'active' : ''}}"
bindtap="sendMessage"
aria-role="button"
aria-label="发送消息"
aria-disabled="{{!inputValue || isRecognizing}}"
>
<text>发送</text>
<t-icon name="chevron-right" size="30rpx" color="{{inputValue && !isRecognizing ? '#FFFFFF' : '#6F655C'}}" />
</view>
</view>
<!-- 更多菜单:向上展开,点遮罩关闭;清空仍走二次确认 -->
<view class="more-mask" wx:if="{{showMoreMenu}}" bindtap="onCloseMoreMenu" aria-hidden="true"></view>
<view class="more-menu" wx:if="{{showMoreMenu}}" aria-role="menu">
<view class="more-item" bindtap="onMoreQuickQuestions" aria-role="menuitem">
<t-icon name="help-circle" size="30rpx" color="#6F655C" />
<text class="more-item-text">常见问题</text>
</view>
<view class="more-divider" wx:if="{{messages.length}}"></view>
<view class="more-item more-item-muted" wx:if="{{messages.length}}" bindtap="onMoreClearChat" aria-role="menuitem">
<t-icon name="delete" size="30rpx" color="#6F655C" />
<text class="more-item-text">清空聊天记录</text>
</view>
</view>
</view>
</view>