jQuery(document).ready(function($) { const chatContainer = $('#wangyi-ai-chat-container'); const chatMessages = $('#wangyi-ai-chat-messages'); const chatInput = $('#wangyi-ai-chat-input-field'); const chatSend = $('#wangyi-ai-chat-send'); const publishPostBtn = $('#wangyi-ai-publish-post'); let lastAiResponse = ''; // 存储最后一条AI回复 // 获取格式化时间 function getFormattedTime() { return new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }); } // 添加消息到对话框 function addMessage(message, isUser = true) { // 存储最后一条AI回复 if (!isUser) { lastAiResponse = message; } const messageContainer = $('
').addClass('message-container').css({ 'display': 'flex', 'flex-direction': 'column', 'margin-bottom': '10px' }); // 添加时间戳 const timestamp = $('
').css({ 'font-size': '0.8em', 'color': '#666', 'align-self': isUser ? 'flex-end' : 'flex-start', 'margin-bottom': '2px' }).text('[' + getFormattedTime() + ']'); messageContainer.append(timestamp); const messageElement = $('
').css({ 'max-width': '70%', 'padding': '12px 16px', 'border-radius': isUser ? '18px 18px 4px 18px' : '18px 18px 18px 4px', 'background': isUser ? '#0073aa' : '#f1f1f1', 'color': isUser ? 'white' : '#333', 'align-self': isUser ? 'flex-end' : 'flex-start', 'margin-bottom': '10px', 'box-shadow': '0 2px 4px rgba(0,0,0,0.1)', 'position': 'relative', 'opacity': '0', 'transform': isUser ? 'translateX(20px)' : 'translateX(-20px)', 'transition': 'all 0.3s ease' }).text(message); // 添加小三角指示 const arrow = $('
').css({ 'position': 'absolute', 'width': '0', 'height': '0', 'border': '6px solid transparent', 'right': isUser ? '-12px' : 'auto', 'left': isUser ? 'auto' : '-12px', 'top': '12px', 'border-left-color': isUser ? '#0073aa' : 'transparent', 'border-right-color': isUser ? 'transparent' : '#f1f1f1' }); messageElement.append(arrow); // 添加动画 setTimeout(() => { messageElement.css({ 'opacity': '1', 'transform': 'translateX(0)' }); }, 10); messageContainer.append(messageElement); chatMessages.append(messageContainer); chatMessages.scrollTop(chatMessages[0].scrollHeight); } // 处理发送消息 chatSend.on('click', function() { const message = chatInput.val().trim(); if (message) { addMessage(message, true); chatInput.val(''); // 发送AJAX请求 $.ajax({ url: wangyi_ai_params.ajax_url, type: 'POST', data: { action: 'wangyi_ai_chat', message: message, _ajax_nonce: wangyi_ai_params.nonce }, beforeSend: function() { chatSend.prop('disabled', true); addMessage('AI正在思考中...', false); // 加载提示 }, success: function(response) { // 移除加载提示 $('.message-container').last().remove(); if (response.success) { addMessage(response.data.message, false); } else { addMessage('抱歉:' + response.data.message, false); } }, error: function() { // 移除加载提示 $('.message-container').last().remove(); addMessage('网络连接错误,请检查Ollama是否正常运行', false); }, complete: function() { chatSend.prop('disabled', false); } }); } }); // 发布文章按钮点击事件 publishPostBtn.on('click', function() { if (!wangyi_ai_params.is_logged_in) { alert(wangyi_ai_params.no_permission); return; } if (!lastAiResponse) { alert('暂无AI回复内容可发布'); return; } // 确认发布 if (!confirm('确定要将最后一条AI回复发布为文章吗?')) { return; } $(this).prop('disabled', true).text('发布中...'); // 发送发布请求 $.ajax({ url: wangyi_ai_params.ajax_url, type: 'POST', data: { action: 'wangyi_ai_publish_post', ai_content: lastAiResponse, _ajax_nonce: wangyi_ai_params.publish_nonce }, success: function(response) { if (response.success) { alert(response.data.message + (response.data.post_url ? '\n文章地址:' + response.data.post_url : '')); } else { alert(response.data.message); } }, error: function() { alert(wangyi_ai_params.publish_failed); }, complete: function() { publishPostBtn.prop('disabled', false).text('发布文章'); } }); }); // 支持回车键发送 chatInput.on('keypress', function(e) { if (e.which === 13 && !e.shiftKey) { // 排除Shift+Enter换行 e.preventDefault(); chatSend.trigger('click'); } }); }); 未找到页面_乐帮资源库
未找到页面_乐帮资源库