diff --git a/mini-program/src/pages/main/ScriptView.vue b/mini-program/src/pages/main/ScriptView.vue index 73c9cd0..4c93da4 100644 --- a/mini-program/src/pages/main/ScriptView.vue +++ b/mini-program/src/pages/main/ScriptView.vue @@ -694,6 +694,18 @@ const loadMessages = async () => { } } +const getCurrentUserId = () => { + try { + const token = uni.getStorageSync('access_token') + if (!token) return '' + const payload = token.split('.')[1] + const decoded = JSON.parse(decodeURIComponent(Array.from(atob(payload), c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''))) + return decoded.sub || '' + } catch (e) { + return '' + } +} + /** * 历史剧本兼容:基于 plotJson 初始化首条 AI 剧本消息 * 触发条件:loadMessages 返回空列表,且 currentResult.plotJson.fullContent 存在 @@ -703,11 +715,13 @@ const initializeFirstAIMessage = async () => { if (!content) return try { + const userId = getCurrentUserId() // 先保存用户心愿消息(theme) const theme = currentResult.value?.theme || currentResult.value?.title || '' if (theme) { await createMessage({ conversationId: conversationId.value, + userId, content: theme, senderType: 'user', contentType: 'chat' @@ -717,6 +731,7 @@ const initializeFirstAIMessage = async () => { // 再保存 AI 剧本消息 await createMessage({ conversationId: conversationId.value, + userId, content, senderType: 'assistant', contentType: 'script', @@ -796,6 +811,7 @@ const sendChat = async () => { const userMessageRes = await createMessage({ conversationId: conversationId.value, + userId: getCurrentUserId(), content, senderType: 'user', contentType: 'chat' @@ -847,6 +863,7 @@ const rewriteMessage = (msg) => { const sendRewrite = async (targetMessageId, content) => { const userMessageRes = await createMessage({ conversationId: conversationId.value, + userId: getCurrentUserId(), content, senderType: 'user', contentType: 'chat'