Compare commits
2
Commits
b200d0a1ab
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6da802cff9 | ||
|
|
8e508d6934 |
@@ -2,6 +2,8 @@
|
|||||||
import config from './config';
|
import config from './config';
|
||||||
import createBus from './utils/eventBus';
|
import createBus from './utils/eventBus';
|
||||||
const API = require('./utils/api.js');
|
const API = require('./utils/api.js');
|
||||||
|
const request = require('./utils/request.js');
|
||||||
|
const AIChatSocket = require('./utils/chatmsg.js');
|
||||||
const role = require('./utils/role.js');
|
const role = require('./utils/role.js');
|
||||||
|
|
||||||
// ==================== 全局页面守卫 ====================
|
// ==================== 全局页面守卫 ====================
|
||||||
@@ -44,6 +46,9 @@ Page = function (options = {}) {
|
|||||||
|
|
||||||
App({
|
App({
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
|
// 接口层遇到 code 401(token 已失效)时静默重登并自动重试原请求
|
||||||
|
request.setUnauthorizedHandler(() => this.silentSignin());
|
||||||
|
|
||||||
const updateManager = wx.getUpdateManager();
|
const updateManager = wx.getUpdateManager();
|
||||||
|
|
||||||
updateManager.onCheckForUpdate((res) => {
|
updateManager.onCheckForUpdate((res) => {
|
||||||
@@ -73,12 +78,17 @@ App({
|
|||||||
|
|
||||||
/** 执行登录,返回可复用的 Promise(避免页面请求早于登录完成的竞态) */
|
/** 执行登录,返回可复用的 Promise(避免页面请求早于登录完成的竞态) */
|
||||||
signin() {
|
signin() {
|
||||||
|
// 已登录或登录在飞都复用同一个 Promise:每次回前台都重签会轮换 token,
|
||||||
|
// 让在飞请求拿着旧 token 收到 401;token 真失效时由 silentSignin 主动重登
|
||||||
if (this._loginPromise) return this._loginPromise;
|
if (this._loginPromise) return this._loginPromise;
|
||||||
|
|
||||||
|
this._loginPending = true;
|
||||||
|
const finish = () => { this._loginPending = false };
|
||||||
this._loginPromise = new Promise((resolve, reject) => {
|
this._loginPromise = new Promise((resolve, reject) => {
|
||||||
wx.login({
|
wx.login({
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
if (!res.code) {
|
if (!res.code) {
|
||||||
|
finish();
|
||||||
this._loginPromise = null;
|
this._loginPromise = null;
|
||||||
return reject(new Error('wx.login 未返回 code'));
|
return reject(new Error('wx.login 未返回 code'));
|
||||||
}
|
}
|
||||||
@@ -88,21 +98,25 @@ App({
|
|||||||
console.info('[app] 静默登录成功', data.data.user && data.data.user.profile && data.data.user.profile.mobile)
|
console.info('[app] 静默登录成功', data.data.user && data.data.user.profile && data.data.user.profile.mobile)
|
||||||
this.globalData.user = data.data.user;
|
this.globalData.user = data.data.user;
|
||||||
this.eventBus.emit('user-login', data.data.user);
|
this.eventBus.emit('user-login', data.data.user);
|
||||||
|
finish();
|
||||||
resolve(data.data.user);
|
resolve(data.data.user);
|
||||||
} else {
|
} else {
|
||||||
console.warn('[app] 静默登录被拒:', data.msg)
|
console.warn('[app] 静默登录被拒:', data.msg)
|
||||||
|
finish();
|
||||||
this._loginPromise = null;
|
this._loginPromise = null;
|
||||||
reject(new Error(data.msg || '登录失败'));
|
reject(new Error(data.msg || '登录失败'));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.warn('[app] 静默登录请求异常:', err && err.message)
|
console.warn('[app] 静默登录请求异常:', err && err.message)
|
||||||
|
finish();
|
||||||
this._loginPromise = null;
|
this._loginPromise = null;
|
||||||
reject(err);
|
reject(err);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
console.warn('[app] wx.login 失败:', err && err.errMsg)
|
console.warn('[app] wx.login 失败:', err && err.errMsg)
|
||||||
|
finish();
|
||||||
this._loginPromise = null;
|
this._loginPromise = null;
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
@@ -118,6 +132,34 @@ App({
|
|||||||
return this.signin();
|
return this.signin();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/** AI 页发送前的登录等待:返回是否已拿到可用用户 */
|
||||||
|
waitForAuth() {
|
||||||
|
return this.ensureLogin()
|
||||||
|
.then(() => !!this.globalData.user?._id)
|
||||||
|
.catch(() => false);
|
||||||
|
},
|
||||||
|
|
||||||
|
/** 静默重登:服务端已作废本地 token 时重新走一次 wx.login 换新 token */
|
||||||
|
silentSignin() {
|
||||||
|
// 已有登录在飞时先等它落定,避免并发换 token 造成 token 互相覆盖
|
||||||
|
if (this._loginPending && this._loginPromise) {
|
||||||
|
return this._loginPromise
|
||||||
|
.then(() => !!this.globalData.user?.security?.token)
|
||||||
|
.catch(() => false);
|
||||||
|
}
|
||||||
|
this._loginPromise = null;
|
||||||
|
return this.signin()
|
||||||
|
.then(() => !!this.globalData.user?.security?.token)
|
||||||
|
.catch(() => false);
|
||||||
|
},
|
||||||
|
|
||||||
|
/** AI 聊天 WebSocket 懒创建:语音分片与聊天共用同一条连接 */
|
||||||
|
initChatSocket() {
|
||||||
|
if (this.globalData.chatSocket) return this.globalData.chatSocket;
|
||||||
|
this.globalData.chatSocket = new AIChatSocket();
|
||||||
|
return this.globalData.chatSocket;
|
||||||
|
},
|
||||||
|
|
||||||
/** 全局事件总线 */
|
/** 全局事件总线 */
|
||||||
eventBus: createBus(),
|
eventBus: createBus(),
|
||||||
|
|
||||||
@@ -146,5 +188,6 @@ App({
|
|||||||
user: null,
|
user: null,
|
||||||
unreadNum: 0, // 未读消息数量
|
unreadNum: 0, // 未读消息数量
|
||||||
socket: null, // SocketTask 对象
|
socket: null, // SocketTask 对象
|
||||||
|
chatSocket: null, // AI 聊天 / 语音识别 WebSocket
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 3.2 KiB |
+1426
-162
File diff suppressed because it is too large
Load Diff
+4
-2
@@ -1,9 +1,11 @@
|
|||||||
{
|
{
|
||||||
"navigationBarTitleText": "AI助手",
|
"navigationBarTitleText": "AI助手",
|
||||||
"navigationBarBackgroundColor": "#ffffff",
|
"navigationBarBackgroundColor": "#FAF6F1",
|
||||||
"navigationBarTextStyle": "black",
|
"navigationBarTextStyle": "black",
|
||||||
"backgroundColor": "#ffffff",
|
"backgroundColor": "#FAF6F1",
|
||||||
|
"disableScroll": true,
|
||||||
"usingComponents": {
|
"usingComponents": {
|
||||||
|
"t-icon": "tdesign-miniprogram/icon/icon",
|
||||||
"t-chat-markdown": "tdesign-miniprogram/chat-markdown/chat-markdown"
|
"t-chat-markdown": "tdesign-miniprogram/chat-markdown/chat-markdown"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+188
-46
@@ -1,75 +1,217 @@
|
|||||||
<view class="chat-container">
|
<view class="chat-container">
|
||||||
<scroll-view class="message-list" scroll-y scroll-into-view="{{scrollToMessage}}" scroll-with-animation>
|
<!-- 消息列表区域:页面唯一滚动容器 -->
|
||||||
<view class="quick-questions" wx:if="{{messages.length === 0}}">
|
<scroll-view
|
||||||
<view class="welcome-text">您好,我是您的AI助手,有什么可以帮您?</view>
|
class="message-list"
|
||||||
<view class="quick-list">
|
scroll-y
|
||||||
<view class="quick-item" wx:for="{{quickQuestions}}" wx:key="*this" data-question="{{item}}" bindtap="onQuickQuestionTap">
|
scroll-into-view="{{scrollToMessage}}"
|
||||||
{{item}}
|
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>
|
||||||
</view>
|
</view>
|
||||||
|
<text class="welcome-note">AI 生成内容仅供参考,请以系统实际数据为准。</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="message-item {{item.type}}" wx:for="{{messages}}" wx:key="id" id="msg-{{item.id}}">
|
<!-- 消息:头像在上、气泡在下,时间紧跟头像右侧;用户消息整条靠右 -->
|
||||||
<view class="avatar {{item.type}}-avatar">
|
<view
|
||||||
<text class="avatar-text">{{item.type === 'user' ? '我' : 'AI'}}</text>
|
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>
|
</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-content">
|
||||||
<view class="message-bubble {{item.type}}-bubble">
|
<view class="message-bubble" wx:if="{{item.contentType !== 'questions'}}">
|
||||||
<!-- subagent 消息:引用卡片样式,默认折叠 -->
|
|
||||||
<block wx:if="{{item.source === 'subagent'}}">
|
|
||||||
<view class="subagent-header" data-id="{{item.id}}" bindtap="toggleSubagent">
|
|
||||||
<text class="subagent-label">思考中...</text>
|
|
||||||
<text class="subagent-arrow {{expandedIds[item.id] ? 'expanded' : ''}}">▸</text>
|
|
||||||
</view>
|
|
||||||
<view class="subagent-body" wx:if="{{expandedIds[item.id]}}">
|
|
||||||
<t-chat-markdown class="message-markdown" content="{{item.content}}" />
|
|
||||||
</view>
|
|
||||||
</block>
|
|
||||||
<t-chat-markdown
|
<t-chat-markdown
|
||||||
wx:elif="{{item.contentType === 'text' && item.type === 'ai'}}"
|
wx:if="{{item.type === 'ai' && item.contentType === 'text'}}"
|
||||||
class="message-markdown"
|
class="message-markdown"
|
||||||
content="{{item.content}}"
|
content="{{item.content}}"
|
||||||
/>
|
/>
|
||||||
<text class="message-text" wx:elif="{{item.contentType === 'text'}}">{{item.content}}</text>
|
<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>
|
||||||
<text class="message-time" wx:if="{{item.time}}">{{item.time}}</text>
|
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="typing-indicator" wx:if="{{isTyping}}">
|
<!-- 底部留白,确保最新消息可见 -->
|
||||||
<view class="avatar ai-avatar">
|
<view style="height: 40rpx;"></view>
|
||||||
<text class="avatar-text">AI</text>
|
|
||||||
</view>
|
|
||||||
<view class="typing-bubble">
|
|
||||||
<view class="dot"></view>
|
|
||||||
<view class="dot"></view>
|
|
||||||
<view class="dot"></view>
|
|
||||||
</view>
|
|
||||||
</view>
|
|
||||||
</scroll-view>
|
</scroll-view>
|
||||||
|
|
||||||
<view class="chat-footer">
|
<!-- 语音状态:只在录音阶段显示,紧贴输入区上方 -->
|
||||||
<view class="chat-toolbar" wx:if="{{messages.length > 0}}">
|
<view class="voice-status" wx:if="{{recording}}">
|
||||||
<view class="chat-toolbar-action" bindtap="clearChat">
|
<view class="voice-dot recording"></view>
|
||||||
<text>清空记录</text>
|
<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>
|
</view>
|
||||||
|
|
||||||
|
<!-- 输入面板:上方文字,下方输入方式与发送 -->
|
||||||
|
<view class="composer">
|
||||||
<view class="input-area">
|
<view class="input-area">
|
||||||
<input
|
<textarea
|
||||||
class="chat-input"
|
class="message-input {{recording || isRecognizing ? 'is-readonly' : ''}}"
|
||||||
type="text"
|
|
||||||
placeholder="请输入您的问题..."
|
|
||||||
value="{{inputValue}}"
|
value="{{inputValue}}"
|
||||||
bindinput="onInputChange"
|
placeholder="说说您的问题或需要…"
|
||||||
|
placeholder-class="input-placeholder"
|
||||||
|
auto-height
|
||||||
|
maxlength="-1"
|
||||||
|
cursor-spacing="24"
|
||||||
confirm-type="send"
|
confirm-type="send"
|
||||||
bindconfirm="sendMessage"
|
disabled="{{recording || isRecognizing}}"
|
||||||
adjust-position="{{true}}"
|
adjust-position="{{true}}"
|
||||||
cursor-spacing="20"
|
|
||||||
hold-keyboard="{{true}}"
|
hold-keyboard="{{true}}"
|
||||||
|
bindinput="onInputChange"
|
||||||
|
bindconfirm="sendMessage"
|
||||||
|
focus="{{inputFocus}}"
|
||||||
/>
|
/>
|
||||||
<view class="send-btn {{canSend ? 'active' : ''}}" bindtap="sendMessage">
|
</view>
|
||||||
<text class="send-text">发送</text>
|
<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>
|
||||||
</view>
|
</view>
|
||||||
|
|||||||
+125
-375
@@ -1,6 +1,25 @@
|
|||||||
|
/* 与就诊人端 AI 页同源:暖白纸面、清晰左基线、克制的暖橙强调。
|
||||||
|
页面不滚动,唯一滚动容器是消息区;颜色令牌取自本项目 TDesign 主题色阶。 */
|
||||||
page {
|
page {
|
||||||
background-color: #FAF6F1;
|
--brand-strong: #C26A2E;
|
||||||
color: #2A211B;
|
--brand-soft: #FCF3E9;
|
||||||
|
--brand-line: #EFCAA6;
|
||||||
|
--brand-ink: #A8551F;
|
||||||
|
--ink: #2A211B;
|
||||||
|
--ink-2: #4A4038;
|
||||||
|
--muted: #6F655C;
|
||||||
|
--line: #EAE1D6;
|
||||||
|
--paper: #FAF6F1;
|
||||||
|
--white: #FFFFFF;
|
||||||
|
--fs-24: 24rpx;
|
||||||
|
--fs-26: 26rpx;
|
||||||
|
--fs-28: 28rpx;
|
||||||
|
--fs-30: 30rpx;
|
||||||
|
--shadow-card: 0 2rpx 10rpx rgba(63, 43, 25, 0.07);
|
||||||
|
|
||||||
|
background-color: var(--paper);
|
||||||
|
color: var(--ink);
|
||||||
|
font-size: var(--fs-28);
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8,389 +27,120 @@ page {
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
background-color: #FAF6F1;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-header {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: space-between;
|
|
||||||
height: 88rpx;
|
|
||||||
padding: 0 24rpx;
|
|
||||||
background-color: #ffffff;
|
|
||||||
border-bottom: 1rpx solid #EAE1D6;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-header-title {
|
|
||||||
font-size: 30rpx;
|
|
||||||
font-weight: 600;
|
|
||||||
color: #2A211B;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-list {
|
|
||||||
flex: 1;
|
|
||||||
padding: 20rpx;
|
|
||||||
padding-bottom: 200rpx;
|
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
|
background: var(--paper);
|
||||||
}
|
}
|
||||||
|
|
||||||
.quick-questions {
|
.message-list { flex: 1; height: 0; min-height: 0; box-sizing: border-box; }
|
||||||
display: flex;
|
.welcome-area { padding: 32rpx 32rpx 24rpx; }
|
||||||
flex-direction: column;
|
.welcome-title { display: block; color: var(--ink); font-size: 44rpx; font-weight: 600; line-height: 1.45; }
|
||||||
align-items: center;
|
.question-heading { display: flex; justify-content: space-between; align-items: baseline; gap: 12rpx; margin: 32rpx 0 16rpx; color: var(--ink); font-size: var(--fs-28); font-weight: 500; }
|
||||||
padding: 40rpx 20rpx;
|
.question-heading-sub { color: var(--muted); font-size: var(--fs-24); font-weight: 400; }
|
||||||
}
|
.question-list { display: flex; flex-direction: column; gap: 12rpx; width: 100%; box-sizing: border-box; }
|
||||||
|
.quick-item { display: flex; align-items: center; justify-content: space-between; gap: 16rpx; min-height: 96rpx; box-sizing: border-box; padding: 20rpx; border: 2rpx solid var(--line); border-radius: 20rpx; background: var(--white); color: var(--ink-2); font-size: var(--fs-28); line-height: 1.6; }
|
||||||
|
.quick-item > text { flex: 1; min-width: 0; word-break: break-all; }
|
||||||
|
.question-mark { display: flex; align-items: center; justify-content: center; flex-shrink: 0; width: 40rpx; height: 40rpx; border-radius: 12rpx; background: var(--paper); color: var(--brand-ink); font-size: var(--fs-26); }
|
||||||
|
.quick-item.question-hover, .quick-item:active { background: var(--brand-soft); border-color: var(--brand-line); }
|
||||||
|
.welcome-note { display: block; margin-top: 20rpx; color: var(--muted); font-size: var(--fs-24); line-height: 1.7; }
|
||||||
|
|
||||||
.welcome-text {
|
/* 对话正文占据主要宽度,避免头像左右分栏挤压长回答和报告表格 */
|
||||||
font-size: 32rpx;
|
.message-item { display: flex; flex-direction: column; padding: 20rpx 32rpx; }
|
||||||
color: #2A211B;
|
.message-item.user { align-items: flex-end; }
|
||||||
margin-bottom: 40rpx;
|
.message-head { display: flex; align-items: center; gap: 10rpx; margin-bottom: 12rpx; }
|
||||||
text-align: center;
|
.message-item.user .message-head { flex-direction: row-reverse; }
|
||||||
font-weight: 500;
|
.avatar { display: flex; align-items: center; justify-content: center; flex-shrink: 0; width: 44rpx; height: 44rpx; border-radius: 14rpx; background: var(--white); border: 2rpx solid var(--line); }
|
||||||
}
|
.avatar-img { width: 30rpx; height: 30rpx; }
|
||||||
|
.message-item.user .avatar { background: transparent; border-color: transparent; }
|
||||||
.quick-list {
|
.message-author { color: var(--ink-2); font-size: var(--fs-24); font-weight: 500; }
|
||||||
display: flex;
|
.message-time { color: var(--muted); font-size: var(--fs-24); margin: 0 6rpx; }
|
||||||
flex-direction: column;
|
.message-content { display: flex; flex-direction: column; align-items: flex-start; min-width: 0; max-width: 100%; }
|
||||||
width: 100%;
|
.message-item.ai .message-content { width: 100%; }
|
||||||
gap: 20rpx;
|
.message-item.user .message-content { align-items: flex-end; max-width: 90%; }
|
||||||
}
|
.message-bubble { box-sizing: border-box; min-width: 0; max-width: 100%; padding: 24rpx; border-radius: 24rpx; word-wrap: break-word; word-break: break-word; }
|
||||||
|
.message-item.ai .message-bubble { width: 100%; border: 2rpx solid var(--line); border-top-left-radius: 8rpx; background: var(--white); }
|
||||||
.quick-item {
|
.message-item.user .message-bubble { border: 2rpx solid var(--brand-line); border-top-right-radius: 8rpx; background: var(--brand-soft); }
|
||||||
background-color: #ffffff;
|
.message-text { font-size: var(--fs-30); line-height: 1.75; white-space: pre-wrap; color: var(--ink-2); word-break: break-word; }
|
||||||
border-radius: 24rpx;
|
.message-item.user .message-text { color: var(--brand-ink); }
|
||||||
padding: 24rpx 32rpx;
|
.message-image { display: block; width: 400rpx; max-width: 100%; border-radius: 16rpx; }
|
||||||
font-size: 28rpx;
|
.message-image-placeholder { font-size: var(--fs-28); color: var(--muted); line-height: 1.6; }
|
||||||
color: #C26A2E;
|
.message-note { margin-top: 10rpx; color: var(--muted); font-size: var(--fs-24); line-height: 1.6; }
|
||||||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
|
.message-retry { display: flex; align-items: center; min-height: 88rpx; color: var(--brand-ink); font-size: var(--fs-26); }
|
||||||
text-align: center;
|
.message-item.system { align-items: center; }
|
||||||
border: 1rpx solid #EAE1D6;
|
.system-message { max-width: 100%; padding: 8rpx 16rpx; color: var(--muted); font-size: var(--fs-26); line-height: 1.7; text-align: center; }
|
||||||
transition: all 0.2s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.quick-item:active {
|
|
||||||
transform: scale(0.98);
|
|
||||||
background-color: rgba(194, 106, 46, 0.05);
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
margin-bottom: 32rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.ai {
|
|
||||||
align-items: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.user {
|
|
||||||
align-items: flex-end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-time {
|
|
||||||
font-size: 24rpx;
|
|
||||||
color: #A3988B;
|
|
||||||
margin-top: 8rpx;
|
|
||||||
padding: 0 4rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.ai .message-time {
|
|
||||||
text-align: left;
|
|
||||||
align-self: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.user .message-time {
|
|
||||||
text-align: right;
|
|
||||||
align-self: flex-end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.avatar {
|
|
||||||
width: 64rpx;
|
|
||||||
height: 64rpx;
|
|
||||||
border-radius: 50%;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
flex-shrink: 0;
|
|
||||||
margin-bottom: 12rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.user-avatar {
|
|
||||||
background: #C26A2E;
|
|
||||||
box-shadow: 0 4rpx 12rpx rgba(180, 85, 42, 0.25);
|
|
||||||
}
|
|
||||||
|
|
||||||
.ai-avatar {
|
|
||||||
background: #B4552A;
|
|
||||||
box-shadow: 0 4rpx 12rpx rgba(217, 134, 52, 0.25);
|
|
||||||
}
|
|
||||||
|
|
||||||
.avatar-text {
|
|
||||||
color: #fff;
|
|
||||||
font-size: 28rpx;
|
|
||||||
font-weight: 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-content {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
max-width: 95%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.user .message-content {
|
|
||||||
align-items: flex-end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.ai .message-content {
|
|
||||||
align-items: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-bubble {
|
|
||||||
padding: 20rpx 24rpx;
|
|
||||||
border-radius: 16rpx;
|
|
||||||
font-size: 28rpx;
|
|
||||||
line-height: 1.6;
|
|
||||||
word-break: break-word;
|
|
||||||
max-width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.user-bubble {
|
|
||||||
background: #C26A2E;
|
|
||||||
color: #fff;
|
|
||||||
border-bottom-right-radius: 4rpx;
|
|
||||||
box-shadow: 0 4rpx 12rpx rgba(180, 85, 42, 0.2);
|
|
||||||
margin-right: 4rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.ai-bubble {
|
|
||||||
background-color: #ffffff;
|
|
||||||
color: #2A211B;
|
|
||||||
border-bottom-left-radius: 4rpx;
|
|
||||||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
|
|
||||||
border: 1rpx solid #EAE1D6;
|
|
||||||
margin-left: 4rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Markdown 渲染区域 */
|
|
||||||
.message-markdown {
|
.message-markdown {
|
||||||
|
display: block;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
font-size: 28rpx;
|
min-width: 0;
|
||||||
color: #2A211B;
|
font-size: var(--fs-30);
|
||||||
line-height: 1.6;
|
line-height: 1.75;
|
||||||
|
color: var(--ink-2);
|
||||||
|
/* 组件内的色彩通过实际消费的 TDesign 变量传入,不依赖跨组件选择器 */
|
||||||
|
--td-text-color-primary: var(--ink-2);
|
||||||
|
--td-bg-color-component: var(--brand-soft);
|
||||||
|
--td-bg-color-page: var(--paper);
|
||||||
|
--td-bg-color-secondarycontainer: var(--paper);
|
||||||
|
--td-component-border: var(--line);
|
||||||
}
|
}
|
||||||
|
.message-markdown .t-chat-markdown-table { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; border: 2rpx solid var(--line); border-radius: 12rpx; }
|
||||||
/* 表格样式覆盖(防止过宽) */
|
.message-markdown .t-chat-markdown-table__container { display: table; min-width: 100%; border-collapse: collapse; }
|
||||||
.message-markdown .t-chat-markdown-table {
|
.message-markdown .t-chat-markdown-table__container-th,
|
||||||
width: 100%;
|
.message-markdown .t-chat-markdown-table__container-td { padding: 12rpx; font-size: var(--fs-26); line-height: 1.6; word-break: break-word; }
|
||||||
max-width: 100%;
|
.message-markdown .t-chat-markdown-table__container-th { background: var(--brand-soft); color: var(--ink); }
|
||||||
overflow-x: auto;
|
.message-markdown .t-chat-markdown-table__container-tr:nth-child(2n) { background: var(--paper); }
|
||||||
-webkit-overflow-scrolling: touch;
|
/* 兼容表格子组件的另一套类名 */
|
||||||
border: 1rpx solid #EAE1D6;
|
|
||||||
border-radius: 8rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-markdown .t-chat-markdown-table__container {
|
|
||||||
display: table;
|
|
||||||
min-width: 100%;
|
|
||||||
max-width: max-content;
|
|
||||||
border-collapse: collapse;
|
|
||||||
white-space: normal;
|
|
||||||
table-layout: fixed;
|
|
||||||
word-break: break-word;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-markdown .t-chat-markdown-table__th,
|
.message-markdown .t-chat-markdown-table__th,
|
||||||
.message-markdown .t-chat-markdown-table__td {
|
.message-markdown .t-chat-markdown-table__td { padding: 12rpx; font-size: var(--fs-26); line-height: 1.6; white-space: normal; word-break: break-word; }
|
||||||
padding: 8rpx 12rpx;
|
.message-markdown .t-chat-markdown-table__th { background: var(--brand-soft); color: var(--ink); }
|
||||||
font-size: 26rpx;
|
.message-markdown .t-chat-markdown-table__tr:nth-child(2n) { background: var(--paper); }
|
||||||
line-height: 1.4;
|
.message-markdown .t-chat-markdown-code, .message-markdown .t-chat-markdown-codespan { font-size: var(--fs-26); word-break: break-all; }
|
||||||
white-space: normal;
|
.message-item.ai .typing-bubble { display: flex; align-items: center; width: auto; min-height: 88rpx; }
|
||||||
word-break: break-word;
|
.typing-lines { display: flex; flex-direction: column; gap: 2rpx; }
|
||||||
word-wrap: break-word;
|
.typing-text { font-size: var(--fs-26); color: var(--muted); line-height: 1.6; }
|
||||||
overflow-wrap: break-word;
|
.typing-progress { font-size: 22rpx; color: var(--muted); line-height: 1.5; opacity: 0.85; }
|
||||||
max-width: 320rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-markdown .t-chat-markdown-table__th {
|
/* 统一白色输入面板:文字在上,更多与发送在下,所有操作横向展开。
|
||||||
background-color: #FAF6F1;
|
position: relative 供「更多」菜单定位(菜单从按钮上方展开) */
|
||||||
font-weight: 600;
|
.composer { position: relative; flex-shrink: 0; margin: 12rpx 24rpx 8rpx; padding: 20rpx 20rpx 12rpx; border: 2rpx solid var(--line); border-radius: 28rpx; background: var(--white); box-shadow: var(--shadow-card); }
|
||||||
color: #2A211B;
|
.input-area { padding: 0 4rpx; }
|
||||||
}
|
.message-input { display: block; box-sizing: border-box; width: 100%; min-height: 76rpx; max-height: 180rpx; padding: 4rpx 0 12rpx; color: var(--ink); font-size: var(--fs-30); line-height: 1.6; background: transparent; }
|
||||||
|
.message-input.is-readonly { color: var(--muted); }
|
||||||
|
.input-placeholder { color: var(--muted); font-size: var(--fs-28); }
|
||||||
|
.composer-actions { display: flex; align-items: center; justify-content: space-between; gap: 16rpx; }
|
||||||
|
.input-tools { display: flex; align-items: center; gap: 8rpx; }
|
||||||
|
.input-tool { display: flex; align-items: center; justify-content: center; gap: 8rpx; box-sizing: border-box; min-width: 104rpx; min-height: 88rpx; padding: 12rpx; border-radius: 16rpx; color: var(--muted); font-size: var(--fs-24); }
|
||||||
|
.input-tool.is-open { color: var(--brand-ink); background: var(--brand-soft); }
|
||||||
|
.input-tool.is-recording { color: var(--brand-ink); background: var(--brand-soft); }
|
||||||
|
.input-tool:active { background: var(--paper); }
|
||||||
|
/* 三个点用 CSS 画,不依赖图标名;与文字同一条基线 */
|
||||||
|
.more-dots { display: flex; align-items: center; justify-content: center; gap: 5rpx; width: 34rpx; height: 34rpx; }
|
||||||
|
.more-dot { width: 8rpx; height: 8rpx; border-radius: 50%; background: var(--muted); }
|
||||||
|
.input-tool.is-open .more-dot { background: var(--brand-ink); }
|
||||||
|
.send-btn { display: flex; align-items: center; justify-content: center; flex-shrink: 0; gap: 10rpx; box-sizing: border-box; min-width: 136rpx; min-height: 88rpx; padding: 16rpx 20rpx; border-radius: 20rpx; background: var(--paper); color: var(--muted); font-size: var(--fs-28); font-weight: 500; }
|
||||||
|
.send-btn.active { background: var(--brand-strong); color: var(--white); }
|
||||||
|
.send-btn.active:active { background: var(--brand-ink); }
|
||||||
|
|
||||||
.message-markdown .t-chat-markdown-table__tr:nth-child(2n) {
|
/* ===== 「更多」菜单 =====
|
||||||
background-color: #FCFAF7;
|
透明遮罩负责点外部关闭;菜单锚在输入面板上方左侧,向上展开 */
|
||||||
}
|
.more-mask { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 120; background: transparent; }
|
||||||
|
.more-menu { position: absolute; left: 20rpx; bottom: calc(100% + 8rpx); z-index: 121; box-sizing: border-box; min-width: 320rpx; padding: 8rpx 0; border: 2rpx solid var(--line); border-radius: 20rpx; background: var(--white); box-shadow: var(--shadow-card); animation: more-menu-in 0.16s ease-out; }
|
||||||
|
@keyframes more-menu-in { from { opacity: 0; transform: translateY(8rpx); } to { opacity: 1; transform: translateY(0); } }
|
||||||
|
.more-item { display: flex; align-items: center; gap: 14rpx; box-sizing: border-box; min-height: 88rpx; padding: 0 24rpx; color: var(--ink-2); font-size: var(--fs-28); line-height: 1.4; }
|
||||||
|
.more-item:active { background: var(--brand-soft); }
|
||||||
|
.more-item-muted { color: var(--muted); }
|
||||||
|
.more-divider { height: 2rpx; margin: 0 20rpx; background: var(--line); }
|
||||||
|
|
||||||
/* 代码块样式覆盖 */
|
/* 语音状态条:只在录音阶段显示,紧贴输入区上方 */
|
||||||
.message-markdown .t-chat-markdown-codespan,
|
.voice-status { display: flex; align-items: center; flex-shrink: 0; gap: 16rpx; margin: 8rpx 24rpx 0; padding: 8rpx 16rpx; border: 2rpx solid var(--brand-line); border-radius: 20rpx; background: var(--brand-soft); }
|
||||||
.message-markdown .t-chat-markdown-code {
|
.voice-dot { flex-shrink: 0; width: 12rpx; height: 12rpx; border-radius: 50%; background: var(--brand-strong); }
|
||||||
font-size: 26rpx;
|
.voice-dot.recording { animation: record-pulse 1.2s infinite ease-in-out; }
|
||||||
word-break: break-all;
|
.voice-status-main { display: flex; flex: 1; flex-direction: column; gap: 4rpx; min-width: 0; }
|
||||||
}
|
.voice-status-text { font-size: var(--fs-26); font-weight: 500; color: var(--ink-2); line-height: 1.5; }
|
||||||
|
.voice-status-sub { font-size: var(--fs-24); color: var(--muted); line-height: 1.5; }
|
||||||
/* subagent 引用卡片(默认折叠) */
|
.voice-cancel { display: flex; align-items: center; justify-content: center; flex-shrink: 0; gap: 8rpx; min-width: 88rpx; min-height: 88rpx; padding: 0 8rpx; color: var(--brand-ink); font-size: var(--fs-26); }
|
||||||
.subagent-header {
|
@keyframes record-pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 1; } }
|
||||||
display: flex;
|
@media (prefers-reduced-motion: reduce) { .voice-dot.recording, .more-menu { animation: none; transition: none; } }
|
||||||
align-items: center;
|
@media (max-width: 340px) {
|
||||||
justify-content: space-between;
|
.input-tool { min-width: 88rpx; padding: 8rpx; }
|
||||||
padding: 4rpx 0;
|
.composer-actions { gap: 8rpx; }
|
||||||
}
|
|
||||||
|
|
||||||
.subagent-label {
|
|
||||||
font-size: 26rpx;
|
|
||||||
color: #6F655C;
|
|
||||||
}
|
|
||||||
|
|
||||||
.subagent-arrow {
|
|
||||||
font-size: 26rpx;
|
|
||||||
color: #A3988B;
|
|
||||||
transition: transform 0.2s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.subagent-arrow.expanded {
|
|
||||||
transform: rotate(90deg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.subagent-body {
|
|
||||||
margin-top: 12rpx;
|
|
||||||
padding-top: 12rpx;
|
|
||||||
border-top: 1rpx solid #EAE1D6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.typing-indicator {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: flex-start;
|
|
||||||
margin-bottom: 32rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.typing-bubble {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 12rpx;
|
|
||||||
background-color: #ffffff;
|
|
||||||
padding: 24rpx 32rpx;
|
|
||||||
border-radius: 16rpx;
|
|
||||||
border-bottom-left-radius: 4rpx;
|
|
||||||
box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
|
|
||||||
border: 1rpx solid #EAE1D6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.dot {
|
|
||||||
width: 16rpx;
|
|
||||||
height: 16rpx;
|
|
||||||
background-color: #A3988B;
|
|
||||||
border-radius: 50%;
|
|
||||||
animation: bounce 1.4s infinite ease-in-out both;
|
|
||||||
}
|
|
||||||
|
|
||||||
.dot:nth-child(1) {
|
|
||||||
animation-delay: -0.32s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.dot:nth-child(2) {
|
|
||||||
animation-delay: -0.16s;
|
|
||||||
}
|
|
||||||
|
|
||||||
@keyframes bounce {
|
|
||||||
0%, 80%, 100% {
|
|
||||||
transform: scale(0.6);
|
|
||||||
}
|
|
||||||
40% {
|
|
||||||
transform: scale(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.input-area {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
padding: 20rpx 24rpx;
|
|
||||||
background-color: #ffffff;
|
|
||||||
gap: 16rpx;
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-footer {
|
|
||||||
position: fixed;
|
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
bottom: 0;
|
|
||||||
z-index: 10;
|
|
||||||
background-color: #ffffff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-toolbar {
|
|
||||||
display: flex;
|
|
||||||
align-items: right;
|
|
||||||
justify-content: right;
|
|
||||||
padding: 12rpx 24rpx;
|
|
||||||
background-color: #FAF6F1;
|
|
||||||
border-top: 1rpx solid #EAE1D6;
|
|
||||||
border-bottom: 1rpx solid #EAE1D6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-toolbar-action {
|
|
||||||
display: flex;
|
|
||||||
align-items: right;
|
|
||||||
justify-content: right;
|
|
||||||
padding: 6rpx 24rpx;
|
|
||||||
border-radius: 24rpx;
|
|
||||||
background-color: #ffffff;
|
|
||||||
border: 1rpx solid #EAE1D6;
|
|
||||||
transition: all 0.2s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-toolbar-action:active {
|
|
||||||
transform: scale(0.96);
|
|
||||||
background-color: rgba(194, 106, 46, 0.08);
|
|
||||||
border-color: #C26A2E;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-toolbar-action text {
|
|
||||||
font-size: 26rpx;
|
|
||||||
color: #6F655C;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-input {
|
|
||||||
flex: 1;
|
|
||||||
height: 72rpx;
|
|
||||||
background-color: #FAF6F1;
|
|
||||||
border-radius: 36rpx;
|
|
||||||
padding: 0 28rpx;
|
|
||||||
font-size: 28rpx;
|
|
||||||
color: #2A211B;
|
|
||||||
border: 1rpx solid #EAE1D6;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-input::placeholder {
|
|
||||||
color: #A3988B;
|
|
||||||
}
|
|
||||||
|
|
||||||
.send-btn {
|
|
||||||
width: 120rpx;
|
|
||||||
height: 72rpx;
|
|
||||||
background-color: #EAE1D6;
|
|
||||||
border-radius: 36rpx;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
transition: all 0.2s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.send-btn.active {
|
|
||||||
background: #C26A2E;
|
|
||||||
box-shadow: 0 4rpx 12rpx rgba(180, 85, 42, 0.25);
|
|
||||||
}
|
|
||||||
|
|
||||||
.send-text {
|
|
||||||
color: #A3988B;
|
|
||||||
font-size: 28rpx;
|
|
||||||
font-weight: 500;
|
|
||||||
transition: color 0.2s ease;
|
|
||||||
}
|
|
||||||
|
|
||||||
.send-btn.active .send-text {
|
|
||||||
color: #FFFFFF;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-3
@@ -13,6 +13,8 @@ const SEX_OPTIONS = [
|
|||||||
]
|
]
|
||||||
const SEX_LABELS = SEX_OPTIONS.map((item) => item.label)
|
const SEX_LABELS = SEX_OPTIONS.map((item) => item.label)
|
||||||
const MOBILE_REGEX = /^1[3-9]\d{9}$/
|
const MOBILE_REGEX = /^1[3-9]\d{9}$/
|
||||||
|
// 待审核期间的轮询间隔:后台审核通过后本页自动刷新角色并跳转首页
|
||||||
|
const PENDING_POLL_INTERVAL = 10 * 1000
|
||||||
|
|
||||||
Page({
|
Page({
|
||||||
data: {
|
data: {
|
||||||
@@ -41,6 +43,55 @@ Page({
|
|||||||
|
|
||||||
onShow() {
|
onShow() {
|
||||||
this.refresh()
|
this.refresh()
|
||||||
|
this.startPolling()
|
||||||
|
},
|
||||||
|
|
||||||
|
onHide() {
|
||||||
|
this.stopPolling()
|
||||||
|
},
|
||||||
|
|
||||||
|
onUnload() {
|
||||||
|
this.stopPolling()
|
||||||
|
},
|
||||||
|
|
||||||
|
startPolling() {
|
||||||
|
this.stopPolling()
|
||||||
|
this._pollTimer = setInterval(() => this.pollApproval(), PENDING_POLL_INTERVAL)
|
||||||
|
},
|
||||||
|
|
||||||
|
stopPolling() {
|
||||||
|
if (this._pollTimer) {
|
||||||
|
clearInterval(this._pollTimer)
|
||||||
|
this._pollTimer = null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 待审核轮询:后台审核通过后员工档案生效,角色解析为 attendant,
|
||||||
|
* 这里直接跳首页,无需用户手动重进
|
||||||
|
*/
|
||||||
|
pollApproval() {
|
||||||
|
// 只在待审核/已通过待同步时轮询;加载、提交、修改资料期间跳过
|
||||||
|
if (this.data.loading || this.data.submitting || this.data.editing) return
|
||||||
|
if (this.data.view !== 'pending' && this.data.view !== 'approved') return
|
||||||
|
if (this._polling) return
|
||||||
|
this._polling = true
|
||||||
|
|
||||||
|
role
|
||||||
|
.ensureRoleSafe(true)
|
||||||
|
.then((detail) => {
|
||||||
|
if (detail.ok && detail.role !== 'none') {
|
||||||
|
this.stopPolling()
|
||||||
|
wx.switchTab({ url: '/pages/home/index' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 角色未变(或解析失败):静默刷新申请单,驳回等状态变化也能及时反映
|
||||||
|
return this.loadApplication(true)
|
||||||
|
})
|
||||||
|
.catch(() => {})
|
||||||
|
.then(() => {
|
||||||
|
this._polling = false
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,6 +106,7 @@ Page({
|
|||||||
.ensureRoleSafe(true)
|
.ensureRoleSafe(true)
|
||||||
.then((detail) => {
|
.then((detail) => {
|
||||||
if (detail.ok && detail.role !== 'none') {
|
if (detail.ok && detail.role !== 'none') {
|
||||||
|
this.stopPolling()
|
||||||
wx.switchTab({ url: '/pages/home/index' })
|
wx.switchTab({ url: '/pages/home/index' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -63,7 +115,10 @@ Page({
|
|||||||
.catch(() => this.loadApplication())
|
.catch(() => this.loadApplication())
|
||||||
},
|
},
|
||||||
|
|
||||||
loadApplication() {
|
/**
|
||||||
|
* @param {boolean} [silent] 轮询静默刷新:不弹错误提示,不切换 loading
|
||||||
|
*/
|
||||||
|
loadApplication(silent) {
|
||||||
const app = getApp()
|
const app = getApp()
|
||||||
const user = app && app.globalData ? app.globalData.user : null
|
const user = app && app.globalData ? app.globalData.user : null
|
||||||
const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户'
|
const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户'
|
||||||
@@ -74,7 +129,9 @@ Page({
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.code !== 0) {
|
if (res.code !== 0) {
|
||||||
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示登录入口
|
// 未登录(新用户走到这里时还没有账号)不弹错误,直接展示登录入口
|
||||||
if (res.code !== 401) {
|
if (silent) {
|
||||||
|
console.info('[apply] 轮询刷新未拿到申请单:', res.code, res.msg)
|
||||||
|
} else if (res.code !== 401) {
|
||||||
wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
|
wx.showToast({ title: res.msg || '加载失败', icon: 'none' })
|
||||||
} else {
|
} else {
|
||||||
console.info('[apply] 未登录,展示登录入口')
|
console.info('[apply] 未登录,展示登录入口')
|
||||||
@@ -138,7 +195,7 @@ Page({
|
|||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
this.setData({ loading: false, accountName, loggedIn })
|
this.setData({ loading: false, accountName, loggedIn })
|
||||||
wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
if (!silent) wx.showToast({ title: '网络错误,请重试', icon: 'none' })
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
const request = require('./request.js')
|
const request = require('./request.js')
|
||||||
|
|
||||||
|
// 语音 / 图片识别链路最长可到 60s,单独放宽超时,避免网络稍慢就整单失败
|
||||||
|
const AI_MEDIA_TIMEOUT = 90000
|
||||||
|
|
||||||
const API = {
|
const API = {
|
||||||
user: {
|
user: {
|
||||||
wxGetPhoneNumber: (data) => request.post('https://api.huashengtec.com/user/wxgetphonenumber', data),
|
wxGetPhoneNumber: (data) => request.post('https://api.huashengtec.com/user/wxgetphonenumber', data),
|
||||||
@@ -74,6 +77,10 @@ const API = {
|
|||||||
|
|
||||||
ai: {
|
ai: {
|
||||||
chat: (data) => request.post('/ai/chat', data),
|
chat: (data) => request.post('/ai/chat', data),
|
||||||
|
// 语音 / 图片识别:语音优先走 /chat 的实时分片链路,整段接口用于兜底;
|
||||||
|
// 识别链路最长可到 60s,单独放宽超时
|
||||||
|
asr: (data) => request.post('/health/ai/asr', data, { timeout: AI_MEDIA_TIMEOUT }),
|
||||||
|
ocr: (data) => request.post('/health/ai/ocr', data, { timeout: AI_MEDIA_TIMEOUT }),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+75
-93
@@ -1,22 +1,26 @@
|
|||||||
|
// AI 聊天 / 语音识别 WebSocket(与 health 服务同域)
|
||||||
const WS_BASE_URL = 'wss://api.huashengtec.com/health-ws'
|
const WS_BASE_URL = 'wss://api.huashengtec.com/health-ws'
|
||||||
//const WS_BASE_URL = 'ws://127.0.0.1:9005'
|
//const WS_BASE_URL = 'ws://127.0.0.1:9005'
|
||||||
|
|
||||||
|
const WS_HEARTBEAT_INTERVAL = 30000
|
||||||
|
const WS_MAX_RECONNECT_ATTEMPTS = 3
|
||||||
|
const WS_RECONNECT_DELAY = 3000
|
||||||
|
|
||||||
class AIChatSocket {
|
class AIChatSocket {
|
||||||
constructor(url = `${WS_BASE_URL}/chat`, options = {}) {
|
constructor(url = `${WS_BASE_URL}/chat`, options = {}) {
|
||||||
this.url = url
|
this.url = url
|
||||||
this.options = {
|
this.options = {
|
||||||
timeout: 10000,
|
heartbeatInterval: WS_HEARTBEAT_INTERVAL,
|
||||||
heartbeatInterval: 30000,
|
|
||||||
autoReconnect: true,
|
autoReconnect: true,
|
||||||
maxReconnectAttempts: 3,
|
maxReconnectAttempts: WS_MAX_RECONNECT_ATTEMPTS,
|
||||||
reconnectDelay: 3000,
|
reconnectDelay: WS_RECONNECT_DELAY,
|
||||||
...options
|
...options
|
||||||
}
|
}
|
||||||
|
|
||||||
this.socketTask = null
|
this.socketTask = null
|
||||||
this.isConnected = false
|
this.isConnected = false
|
||||||
this.isConnecting = false
|
this.isConnecting = false
|
||||||
this.messageQueue = []
|
this.connectPromise = null
|
||||||
this.reconnectAttempts = 0
|
this.reconnectAttempts = 0
|
||||||
|
|
||||||
this._openCallback = null
|
this._openCallback = null
|
||||||
@@ -26,15 +30,30 @@ class AIChatSocket {
|
|||||||
this._heartbeatTimer = null
|
this._heartbeatTimer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 连接 Promise 单飞:调用方必须 await 成功后再发送,避免断线后的首条消息竞态
|
||||||
connect() {
|
connect() {
|
||||||
if (this.isConnected || this.isConnecting) {
|
if (this.isConnected) return Promise.resolve()
|
||||||
console.log('[AIChatSocket] Already connected or connecting')
|
if (this.connectPromise) return this.connectPromise
|
||||||
return Promise.resolve()
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isConnecting = true
|
this.isConnecting = true
|
||||||
|
this.connectPromise = new Promise((resolve, reject) => {
|
||||||
|
let settled = false
|
||||||
|
const succeed = () => {
|
||||||
|
if (settled) return
|
||||||
|
settled = true
|
||||||
|
this.isConnecting = false
|
||||||
|
this.connectPromise = null
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
const fail = (err) => {
|
||||||
|
if (settled) return
|
||||||
|
settled = true
|
||||||
|
this.isConnecting = false
|
||||||
|
this.connectPromise = null
|
||||||
|
reject(err instanceof Error ? err : new Error('WebSocket 连接失败'))
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
try {
|
||||||
this.socketTask = wx.connectSocket({
|
this.socketTask = wx.connectSocket({
|
||||||
url: this.url,
|
url: this.url,
|
||||||
header: {
|
header: {
|
||||||
@@ -42,120 +61,94 @@ class AIChatSocket {
|
|||||||
...this.options.header
|
...this.options.header
|
||||||
},
|
},
|
||||||
protocols: this.options.protocols || [],
|
protocols: this.options.protocols || [],
|
||||||
success: () => {
|
fail
|
||||||
console.log('[AIChatSocket] WebSocket connecting...')
|
|
||||||
},
|
|
||||||
fail: (err) => {
|
|
||||||
this.isConnecting = false
|
|
||||||
console.error('[AIChatSocket] Connect failed:', err)
|
|
||||||
reject(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
} catch (err) {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.socketTask) {
|
if (!this.socketTask) {
|
||||||
this.isConnecting = false
|
fail(new Error('SocketTask creation failed'))
|
||||||
reject(new Error('SocketTask creation failed'))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.socketTask.onOpen(() => {
|
this.socketTask.onOpen(() => {
|
||||||
console.log('[AIChatSocket] Connection opened')
|
|
||||||
this.isConnected = true
|
this.isConnected = true
|
||||||
this.isConnecting = false
|
|
||||||
this.reconnectAttempts = 0
|
this.reconnectAttempts = 0
|
||||||
this._startHeartbeat()
|
this._startHeartbeat()
|
||||||
|
if (this._openCallback) this._openCallback()
|
||||||
this._flushMessageQueue()
|
succeed()
|
||||||
|
|
||||||
if (this._openCallback) {
|
|
||||||
this._openCallback()
|
|
||||||
}
|
|
||||||
resolve()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.socketTask.onClose((res) => {
|
this.socketTask.onClose((res) => {
|
||||||
console.log('[AIChatSocket] Connection closed:', res)
|
|
||||||
this.isConnected = false
|
this.isConnected = false
|
||||||
this.isConnecting = false
|
|
||||||
this._stopHeartbeat()
|
this._stopHeartbeat()
|
||||||
|
fail(new Error(`WebSocket 已关闭:${res.code || 'unknown'}`))
|
||||||
if (this._closeCallback) {
|
if (this._closeCallback) this._closeCallback(res)
|
||||||
this._closeCallback(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.options.autoReconnect && this.reconnectAttempts < this.options.maxReconnectAttempts) {
|
if (this.options.autoReconnect && this.reconnectAttempts < this.options.maxReconnectAttempts) {
|
||||||
this._reconnect()
|
this._reconnect()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
this.socketTask.onError((err) => {
|
this.socketTask.onError((err) => {
|
||||||
console.error('[AIChatSocket] WebSocket error:', err)
|
if (this._errorCallback) this._errorCallback(err)
|
||||||
this.isConnected = false
|
fail(new Error(err?.errMsg || 'WebSocket 连接错误'))
|
||||||
this.isConnecting = false
|
|
||||||
|
|
||||||
if (this._errorCallback) {
|
|
||||||
this._errorCallback(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.socketTask.onMessage((res) => {
|
this.socketTask.onMessage((res) => {
|
||||||
|
if (!this._messageCallback) return
|
||||||
try {
|
try {
|
||||||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||||
if (this._messageCallback) {
|
|
||||||
this._messageCallback(data)
|
this._messageCallback(data)
|
||||||
}
|
} catch (err) {
|
||||||
} catch (e) {
|
this._messageCallback({
|
||||||
if (this._messageCallback) {
|
source: 'system',
|
||||||
this._messageCallback(res.data)
|
type: 'error',
|
||||||
}
|
content: '收到无法解析的服务消息',
|
||||||
|
id: ''
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return this.connectPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
send(data) {
|
send(data) {
|
||||||
const message = typeof data === 'string' ? data : JSON.stringify(data)
|
const message = typeof data === 'string' ? data : JSON.stringify(data)
|
||||||
|
|
||||||
if (!this.isConnected || !this.socketTask) {
|
if (!this.isConnected || !this.socketTask) {
|
||||||
console.log('[AIChatSocket] Not connected, queueing message')
|
return Promise.reject(new Error('WebSocket 尚未连接'))
|
||||||
this.messageQueue.push(message)
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
this.socketTask.send({
|
this.socketTask.send({
|
||||||
data: message,
|
data: message,
|
||||||
fail: (err) => {
|
success: resolve,
|
||||||
console.error('[AIChatSocket] Send failed:', err)
|
fail: (err) => reject(new Error(err?.errMsg || '消息发送失败'))
|
||||||
this.messageQueue.push(message)
|
})
|
||||||
|
} catch (err) {
|
||||||
|
reject(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return true
|
|
||||||
} catch (err) {
|
|
||||||
console.error('[AIChatSocket] Send error:', err)
|
|
||||||
this.messageQueue.push(message)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
this._stopHeartbeat()
|
this._stopHeartbeat()
|
||||||
this.options.autoReconnect = false
|
this.options.autoReconnect = false
|
||||||
|
const task = this.socketTask
|
||||||
if (this.socketTask) {
|
|
||||||
this.socketTask.close({
|
|
||||||
code: 1000,
|
|
||||||
reason: 'User closed',
|
|
||||||
fail: (err) => {
|
|
||||||
console.error('[AIChatSocket] Close failed:', err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
this.socketTask = null
|
this.socketTask = null
|
||||||
}
|
|
||||||
|
|
||||||
this.isConnected = false
|
this.isConnected = false
|
||||||
this.isConnecting = false
|
this.isConnecting = false
|
||||||
this.messageQueue = []
|
this.connectPromise = null
|
||||||
|
if (task) {
|
||||||
|
task.close({
|
||||||
|
code: 1000,
|
||||||
|
reason: 'User closed',
|
||||||
|
fail: (err) => console.error('[AIChatSocket] Close failed:', err)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onOpen(callback) {
|
onOpen(callback) {
|
||||||
@@ -174,21 +167,10 @@ class AIChatSocket {
|
|||||||
this._messageCallback = callback
|
this._messageCallback = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
_flushMessageQueue() {
|
|
||||||
if (this.messageQueue.length === 0) return
|
|
||||||
|
|
||||||
console.log(`[AIChatSocket] Flushing ${this.messageQueue.length} queued messages`)
|
|
||||||
while (this.messageQueue.length > 0) {
|
|
||||||
const message = this.messageQueue.shift()
|
|
||||||
this.send(message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_reconnect() {
|
_reconnect() {
|
||||||
this.reconnectAttempts++
|
this.reconnectAttempts++
|
||||||
console.log(`[AIChatSocket] Reconnecting... Attempt ${this.reconnectAttempts}/${this.options.maxReconnectAttempts}`)
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
if (!this.options.autoReconnect || this.isConnected) return
|
||||||
this.connect().catch((err) => {
|
this.connect().catch((err) => {
|
||||||
console.error('[AIChatSocket] Reconnect failed:', err)
|
console.error('[AIChatSocket] Reconnect failed:', err)
|
||||||
})
|
})
|
||||||
@@ -197,17 +179,17 @@ class AIChatSocket {
|
|||||||
|
|
||||||
_startHeartbeat() {
|
_startHeartbeat() {
|
||||||
this._stopHeartbeat()
|
this._stopHeartbeat()
|
||||||
|
|
||||||
this._heartbeatTimer = setInterval(() => {
|
this._heartbeatTimer = setInterval(() => {
|
||||||
if (this.isConnected && this.socketTask) {
|
if (!this.isConnected || !this.socketTask) return
|
||||||
this.socketTask.send({
|
this.send({
|
||||||
data: JSON.stringify({ type: 'ping' }),
|
source: 'client',
|
||||||
fail: (err) => {
|
type: 'ping',
|
||||||
|
content: '',
|
||||||
|
id: `heartbeat_${Date.now()}`
|
||||||
|
}).catch((err) => {
|
||||||
console.error('[AIChatSocket] Heartbeat failed:', err)
|
console.error('[AIChatSocket] Heartbeat failed:', err)
|
||||||
this._stopHeartbeat()
|
this._stopHeartbeat()
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}, this.options.heartbeatInterval)
|
}, this.options.heartbeatInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+26
-2
@@ -3,10 +3,34 @@ class Request {
|
|||||||
constructor(baseURL = 'https://api.huashengtec.com') {
|
constructor(baseURL = 'https://api.huashengtec.com') {
|
||||||
//constructor(baseURL = 'http://127.0.0.1:9004') {
|
//constructor(baseURL = 'http://127.0.0.1:9004') {
|
||||||
this.baseURL = baseURL
|
this.baseURL = baseURL
|
||||||
|
// 401 兜底回调由 app.js 注入(静默重登),避免各页面自己处理登录
|
||||||
|
this.onUnauthorized = null
|
||||||
}
|
}
|
||||||
|
|
||||||
async request(options) {
|
setUnauthorizedHandler(handler) {
|
||||||
const { url, method = 'GET', data = {}, header = {}, skipAuthWait = false, ...rest } = options
|
this.onUnauthorized = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
request(options) {
|
||||||
|
return this.send(options).then((body) => this.recoverUnauthorized(body, options))
|
||||||
|
}
|
||||||
|
|
||||||
|
// token 失效时后端返回 HTTP 200 + code 401:静默重登一次再重试原请求。
|
||||||
|
// 登录请求自身(skipAuthWait)不再触发重登,避免递归
|
||||||
|
recoverUnauthorized(body, options) {
|
||||||
|
if (!body || body.code !== 401 || options._retried || options.skipAuthWait || !this.onUnauthorized) {
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
const notLoggedIn = { code: 401, msg: '请先登录', data: null }
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(() => this.onUnauthorized())
|
||||||
|
.then((ok) => (ok ? this.request({ ...options, _retried: true }) : notLoggedIn))
|
||||||
|
.catch(() => notLoggedIn)
|
||||||
|
}
|
||||||
|
|
||||||
|
async send(options) {
|
||||||
|
const { url, method = 'GET', data = {}, header = {}, skipAuthWait = false, _retried, ...rest } = options
|
||||||
|
|
||||||
// 等待登录完成后再携带 token,避免页面请求早于 app.js 登录的竞态
|
// 等待登录完成后再携带 token,避免页面请求早于 app.js 登录的竞态
|
||||||
// skipAuthWait: 登录请求自身使用,否则会等待自己导致死锁
|
// skipAuthWait: 登录请求自身使用,否则会等待自己导致死锁
|
||||||
|
|||||||
Reference in New Issue
Block a user