调整了页面命名和组织
This commit is contained in:
235
pages/ai/aichat.js
Normal file
235
pages/ai/aichat.js
Normal file
@@ -0,0 +1,235 @@
|
||||
const API = require('../../utils/api.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
messages: [],
|
||||
inputValue: '',
|
||||
inputFocus: false,
|
||||
isTyping: false,
|
||||
scrollToMessage: '',
|
||||
quickQuestions: [
|
||||
'怎么加入暖橙团队?',
|
||||
'陪诊服务的流程是什么?',
|
||||
'如何预约陪诊服务?',
|
||||
'陪诊服务收费标准?'
|
||||
]
|
||||
},
|
||||
|
||||
messageIdCounter: 0,
|
||||
|
||||
onLoad() {
|
||||
this.loadChatHistory()
|
||||
this.initSocketListeners()
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
this.removeSocketListeners()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.setData({ inputFocus: true })
|
||||
},
|
||||
|
||||
initSocketListeners() {
|
||||
const app = getApp()
|
||||
const socket = app.globalData.chatSocket
|
||||
if (!socket) return
|
||||
|
||||
this._onSocketMessage = (data) => {
|
||||
if (data && data.type === 'ai') {
|
||||
this.handleAIReply(data)
|
||||
}
|
||||
}
|
||||
|
||||
this._onSocketError = (err) => {
|
||||
console.error('[AIChat] WebSocket error', err)
|
||||
this.handleAIError('网络连接失败,请检查网络设置')
|
||||
}
|
||||
|
||||
socket.onMessage(this._onSocketMessage)
|
||||
socket.onError(this._onSocketError)
|
||||
},
|
||||
|
||||
removeSocketListeners() {
|
||||
const app = getApp()
|
||||
const socket = app.globalData.chatSocket
|
||||
if (!socket) return
|
||||
|
||||
if (this._onSocketMessage) {
|
||||
socket.onMessage(null)
|
||||
}
|
||||
if (this._onSocketError) {
|
||||
socket.onError(null)
|
||||
}
|
||||
},
|
||||
|
||||
handleAIReply(data) {
|
||||
let messages = null
|
||||
if (this.data.messages.length > 0) {
|
||||
let lastMessage = this.data.messages[this.data.messages.length - 1]
|
||||
if (lastMessage.type === 'ai' && lastMessage._serverId === data.id) {
|
||||
lastMessage.content += data.content || ''
|
||||
messages = this.data.messages
|
||||
}
|
||||
}
|
||||
|
||||
if (!messages) {
|
||||
const aiResponse = {
|
||||
id: `msg_${++this.messageIdCounter}`,
|
||||
_serverId: data.id,
|
||||
type: 'ai',
|
||||
contentType: 'text',
|
||||
content: data.content || '',
|
||||
time: this.formatTime(new Date())
|
||||
}
|
||||
|
||||
if (data.sessionId) {
|
||||
wx.setStorageSync('ai_session_id', data.sessionId)
|
||||
}
|
||||
|
||||
messages = [...this.data.messages, aiResponse]
|
||||
}
|
||||
|
||||
this.setData({
|
||||
messages: messages,
|
||||
isTyping: false
|
||||
})
|
||||
this.saveChatHistory(messages)
|
||||
this.scrollToBottom()
|
||||
},
|
||||
|
||||
loadChatHistory() {
|
||||
try {
|
||||
const history = wx.getStorageSync('ai_chat_history')
|
||||
if (history && history.length > 0) {
|
||||
this.setData({ messages: history })
|
||||
this.scrollToBottom()
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('加载聊天记录失败', e)
|
||||
}
|
||||
},
|
||||
|
||||
saveChatHistory(messages) {
|
||||
try {
|
||||
wx.setStorageSync('ai_chat_history', messages)
|
||||
} catch (e) {
|
||||
console.log('保存聊天记录失败', e)
|
||||
}
|
||||
},
|
||||
|
||||
onInputChange(e) {
|
||||
this.setData({ inputValue: e.detail.value })
|
||||
},
|
||||
|
||||
onQuickQuestionTap(e) {
|
||||
const question = e.currentTarget.dataset.question
|
||||
this.setData({ inputValue: question })
|
||||
this.sendMessage()
|
||||
},
|
||||
|
||||
sendMessage() {
|
||||
const content = this.data.inputValue.trim()
|
||||
if (!content) return
|
||||
|
||||
const userMessage = {
|
||||
id: `msg_${++this.messageIdCounter}`,
|
||||
type: 'user',
|
||||
contentType: 'text',
|
||||
content: content,
|
||||
time: this.formatTime(new Date())
|
||||
}
|
||||
|
||||
const messages = [...this.data.messages, userMessage]
|
||||
this.setData({
|
||||
messages: messages,
|
||||
inputValue: '',
|
||||
isTyping: true
|
||||
})
|
||||
|
||||
this.saveChatHistory(messages)
|
||||
this.scrollToBottom()
|
||||
|
||||
this.sendToAI(content)
|
||||
},
|
||||
|
||||
sendToAI(content, type = 'chat') {
|
||||
const app = getApp()
|
||||
const user = app.globalData.user
|
||||
const socket = app.globalData.chatSocket
|
||||
|
||||
if (!socket.isConnected) {
|
||||
socket.connect()
|
||||
}
|
||||
|
||||
if (socket) {
|
||||
socket.send({
|
||||
type: type,
|
||||
content: content,
|
||||
userId: user ? user._id : '',
|
||||
appId: app.globalData.appId
|
||||
})
|
||||
} else {
|
||||
// 处理连接失败的情况
|
||||
this.handleAIError('网络连接失败,请检查网络设置')
|
||||
}
|
||||
},
|
||||
|
||||
handleAIError(errorMessage) {
|
||||
const errorResponse = {
|
||||
id: `msg_${++this.messageIdCounter}`,
|
||||
type: 'ai',
|
||||
contentType: 'text',
|
||||
content: errorMessage,
|
||||
time: this.formatTime(new Date())
|
||||
}
|
||||
|
||||
const messages = [...this.data.messages, errorResponse]
|
||||
this.setData({
|
||||
messages: messages,
|
||||
isTyping: false
|
||||
})
|
||||
this.saveChatHistory(messages)
|
||||
this.scrollToBottom()
|
||||
},
|
||||
|
||||
scrollToBottom() {
|
||||
setTimeout(() => {
|
||||
const lastMessage = this.data.messages[this.data.messages.length - 1]
|
||||
if (lastMessage) {
|
||||
this.setData({
|
||||
scrollToMessage: `msg-${lastMessage.id}`
|
||||
})
|
||||
}
|
||||
}, 100)
|
||||
},
|
||||
|
||||
previewImage(e) {
|
||||
const src = e.currentTarget.dataset.src
|
||||
wx.previewImage({
|
||||
urls: [src],
|
||||
current: src
|
||||
})
|
||||
},
|
||||
|
||||
formatTime(date) {
|
||||
const hours = String(date.getHours()).padStart(2, '0')
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0')
|
||||
return `${hours}:${minutes}`
|
||||
},
|
||||
|
||||
clearChat() {
|
||||
wx.showModal({
|
||||
title: '确认清空',
|
||||
content: '确定要清空所有聊天记录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
this.sendToAI('clear', 'clear')
|
||||
this.setData({ messages: [] })
|
||||
wx.removeStorageSync('ai_chat_history')
|
||||
wx.removeStorageSync('ai_session_id')
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user