feat(ai): 新增 AI 客服聊天功能 + 消息页重构为小华助手
- 新增 POST /ai/chat 端点,由 LLM(Ollama qwen3)担任 24h 健康客服"小华" - 新增 ai.chat.send 权限,绑定管理员/患者/医生/护士/健康管理师角色 - 消息页从咨询列表重构为单窗口 AI 对话(欢迎态 + 聊天态 + 快捷问诊) - 通知功能迁移到"我的"页面菜单项(带未读角标),独立通知列表页 - 修复气泡文字截断:改用百分比 max-width + block Text + pre-wrap 换行 - 修复权限绑定:迁移 SQL 角色名从英文改为中文(admin→管理员,patient→患者)
This commit is contained in:
42
apps/miniprogram/src/services/ai-chat.ts
Normal file
42
apps/miniprogram/src/services/ai-chat.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { api } from './request';
|
||||
|
||||
export interface AiChatMessage {
|
||||
id: string;
|
||||
role: 'user' | 'assistant';
|
||||
content: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface AiChatResponse {
|
||||
reply: string;
|
||||
message_id: string;
|
||||
}
|
||||
|
||||
/** 发送消息给 AI 客服 */
|
||||
export async function sendAiMessage(
|
||||
message: string,
|
||||
history?: AiChatMessage[],
|
||||
): Promise<AiChatResponse> {
|
||||
const resp = await api.post<AiChatResponse>('/ai/chat', {
|
||||
message,
|
||||
history: history?.slice(-10),
|
||||
});
|
||||
return resp;
|
||||
}
|
||||
|
||||
/** 获取聊天历史(本地缓存) */
|
||||
export function getLocalHistory(): AiChatMessage[] {
|
||||
try {
|
||||
const raw = wx.getStorageSync('ai_chat_history');
|
||||
return raw ? JSON.parse(raw) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** 保存聊天历史到本地 */
|
||||
export function saveLocalHistory(messages: AiChatMessage[]): void {
|
||||
try {
|
||||
wx.setStorageSync('ai_chat_history', JSON.stringify(messages.slice(-100)));
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
Reference in New Issue
Block a user