Files
wxapp_escort/pages/ai/aichat.wxml
T
lik 7d793bfd3e feat(ai): 语音输入实时流式回显,图片输入直接上屏
- 录音按 1KB 分片(约 170ms)实时上传,ASR 中间结果逐字回填输入框;结束时定稿并直接发送
- 分片缺失 / 供应商失败 / 超时统一走整段转写兜底,状态条提示「结束后整段转写」
- 补齐全链路日志:语音流开始、首帧头字节、分片计数、每条中间结果、定稿汇总,
  便于区分客户端没发分片、服务端解码失败与供应商失败
- 新增 scope.record 权限说明,录音仅用于本次转写
2026-10-31 18:50:27 +08:00

214 lines
8.9 KiB
Plaintext

<view class="chat-container">
<!-- 未登录提示条:提前说明「能不能聊」,把登录入口放在这里,
用户不必等打完字被拦下来才知道要先登录 -->
<view class="login-notice" wx:if="{{authChecked && !isLoggedIn}}">
<text class="login-notice-text">尚未登录,暂时无法与 AI 客服对话</text>
<text class="login-notice-link" bindtap="goLogin" aria-role="link">去登录</text>
</view>
<!-- 消息列表区域 -->
<scroll-view
class="message-list"
scroll-y
scroll-into-view="{{scrollToMessage}}"
enhanced
show-scrollbar
>
<!-- 欢迎区:空会话时的落点 -->
<view class="welcome-area" wx:if="{{messages.length === 0}}">
<view class="welcome-icon">
<image class="welcome-icon-img" src="/images/ai-ink.png" mode="aspectFit" />
</view>
<text class="welcome-title">陪诊助手</text>
<text class="welcome-desc">我是您的智能陪诊助手,可以为您解答陪诊相关问题</text>
<view class="question-list">
<view
class="quick-item"
wx:for="{{quickQuestions}}"
wx:key="id"
bindtap="onQuickQuestionTap"
data-question="{{item.text}}"
>
<text>{{item.text}}</text>
<t-icon name="chevron-right" size="26rpx" color="#9B4508" />
</view>
</view>
</view>
<!-- 消息:头像在上、气泡在下,时间紧跟头像右侧;用户消息整条靠右 -->
<view
class="message-item {{item.type}}"
wx:for="{{messages}}"
wx:key="id"
id="msg-{{item.id}}"
>
<!-- 连接或服务错误是系统状态,不使用用户/AI 头像,也不带 AI 内容声明。
未登录提示额外带一个登录入口,避免只给一句「请先登录」的死胡同 -->
<view wx:if="{{item.type === 'system'}}" class="system-message">
<text>{{item.content}}</text>
<text
wx:if="{{item.contentType === 'login'}}"
class="system-login-link"
bindtap="goLogin"
aria-role="link"
>去登录</text>
</view>
<block wx:else>
<view class="message-head">
<!-- AI:品牌标记(暖墨线稿);底色与用户侧同构,身份只由图形区分 -->
<view wx:if="{{item.type === 'ai'}}" class="avatar">
<image class="avatar-img" src="/images/home-ink.png" mode="aspectFit" />
</view>
<!-- 用户:人像图标(暖陶土) -->
<view wx:else class="avatar">
<t-icon name="user-1" size="36rpx" color="#9B4508" />
</view>
<text class="message-time">{{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">{{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>
<!-- 常见问题:由「常见问题」按钮触发,把问题清单直接铺进会话流里。
复用欢迎区的白卡 + chevron 语言,点一下即发送该问题 -->
<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}}"
>
<text>{{question.text}}</text>
<t-icon name="chevron-right" size="26rpx" color="#9B4508" />
</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}}">
<view class="message-head">
<view class="avatar">
<image class="avatar-img" src="/images/home-ink.png" mode="aspectFit" />
</view>
</view>
<view class="message-content">
<view class="message-bubble typing-bubble">
<view class="typing-indicator">
<view class="dot"></view>
<view class="dot"></view>
<view class="dot"></view>
</view>
</view>
</view>
</view>
<!-- 底部留白,确保最新消息可见 -->
<view style="height: 40rpx;"></view>
</scroll-view>
<!-- 语音状态:录音 / 识别状态紧贴输入区上方一行。
只占一行内联提示,不做浮层、不替换输入区,输入框全程可见可读 -->
<view class="voice-status" wx:if="{{recording || isRecognizing}}">
<view class="voice-dot {{recording ? 'recording' : 'recognizing'}}"></view>
<text class="voice-status-text" wx:if="{{recording}}">正在录音 {{recordTime}},再次点击麦克风结束并发送</text>
<text class="voice-status-text" wx:else>正在识别并发送…</text>
<text class="voice-status-sub" wx:if="{{recording && voiceRealtimeOff}}">结束后整段转写</text>
</view>
<!-- 输入区:整行让给文字输入 + 右侧发送。
语音识别结果实时回填到这里,结束时直接发送;手动输入仍走右侧发送键 -->
<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}}"
bindinput="onInputChange"
bindconfirm="sendMessage"
focus="{{inputFocus}}"
/>
<view
class="action-btn send-btn {{inputValue && !recording && !isRecognizing ? 'active' : ''}}"
bindtap="sendMessage"
aria-role="button"
aria-label="发送消息"
aria-disabled="{{!inputValue || recording || isRecognizing}}"
>
<t-icon name="send" size="40rpx" color="{{inputValue && !recording && !isRecognizing ? '#FFFFFF' : '#6F655C'}}" />
</view>
</view>
<!-- 辅助入口:紧贴输入区下方——拇指最容易够到的位置,操作方便。
左端是会话级动作(常见问题 / 清空聊天),只在有消息时出现,做成只描边不填充的
暖橙小按钮(圆角与输入框同为 20rpx),与输入区右侧浅橙底的发送键区分开;
靠左与上方输入框、消息气泡同一条左边线。
右端两个只留图标的入口是语音 / 图片:空会话也要能点,故常驻,不做成描边按钮。
麦克风在录音中转为实心暖橙底的白图标,本身就是「再点一次结束」的入口。
清空聊天是破坏性动作,放在左组最右端(不顶到整行最右)、右侧留给输入入口 -->
<view class="tool-bar">
<view class="tool-group" wx:if="{{messages.length > 0}}">
<view class="tool-btn" bindtap="showQuickQuestions">
<t-icon name="help-circle" size="26rpx" color="#9B4508" />
<text class="tool-btn-text">常见问题</text>
</view>
<view class="tool-btn" bindtap="clearChat">
<t-icon name="delete" size="26rpx" color="#9B4508" />
<text class="tool-btn-text">清空聊天</text>
</view>
</view>
<view class="media-group">
<view
class="media-btn {{recording ? 'active' : ''}}"
bindtap="onMicTap"
aria-role="button"
aria-label="{{recording ? '结束语音输入' : '语音输入'}}"
>
<t-icon name="microphone" size="32rpx" color="{{recording ? '#FFFFFF' : '#9B4508'}}" />
</view>
<view
class="media-btn"
bindtap="onImageTap"
aria-role="button"
aria-label="拍照或选择图片"
>
<t-icon name="image" size="32rpx" color="#9B4508" />
</view>
</view>
</view>
</view>