2 Commits

Author SHA1 Message Date
2416eab34f chore:加了dify 2025-12-05 17:12:14 +08:00
c4f2cea1a9 chore:加了智能体 2025-12-05 16:47:53 +08:00
4 changed files with 370 additions and 283 deletions

216
App.vue
View File

@@ -31,7 +31,6 @@
if (uni.getSystemInfoSync().platform == 'ios') { if (uni.getSystemInfoSync().platform == 'ios') {
uni.setStorageSync('initUrl', location.href); uni.setStorageSync('initUrl', location.href);
} }
// DOM加载完成后创建纯前端聊天窗口无任何Dify残留
this.$nextTick(() => { this.$nextTick(() => {
this.createIndependentChatbot(); this.createIndependentChatbot();
}); });
@@ -80,11 +79,9 @@
} }
// #ifdef H5 // #ifdef H5
// 自动授权登录(未登录时)
if (!uni.getStorageSync('memberInfo')) { if (!uni.getStorageSync('memberInfo')) {
this.getAuthInfo(); this.getAuthInfo();
} }
// 已登录时同步会员信息
if (this.$store.state.token) { if (this.$store.state.token) {
this.$api.sendRequest({ this.$api.sendRequest({
url: '/api/member/info', url: '/api/member/info',
@@ -103,7 +100,6 @@
}, },
onShow: function(options) { onShow: function(options) {
// #ifdef MP // #ifdef MP
// 修复语法错误l0 → 10
this.getAuthInfo(); this.getAuthInfo();
if (this.$store.state.token) { if (this.$store.state.token) {
this.$api.sendRequest({ this.$api.sendRequest({
@@ -123,101 +119,179 @@
}, },
onHide: function() {}, onHide: function() {},
methods: { methods: {
/** // 安全的 HTML 转义(防止 XSS
* 纯前端聊天机器人无Dify依赖 htmlEncode(str) {
*/ if (typeof str !== 'string') return '';
return str
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
},
createIndependentChatbot() { createIndependentChatbot() {
console.log('创建纯前端聊天窗口...'); console.log('创建响应式智能客服窗口...');
// 1. 移除所有旧的Dify相关元素彻底清理残留 // 清除已存在的元素
document.querySelectorAll('[id^="dify-"]').forEach(el => el.remove()); document.querySelectorAll('[id^="independent-chat-"]').forEach(el => el.remove());
// 2. 创建聊天按钮 // ========== 按钮 ==========
const chatBtn = document.createElement('div'); const chatBtn = document.createElement('div');
chatBtn.id = 'independent-chat-btn'; chatBtn.id = 'independent-chat-btn';
chatBtn.style = ` chatBtn.innerHTML = '💬';
width: 56px; height: 56px; border-radius: 50%; background: #1C64F2; chatBtn.style.cssText = `
position: fixed; bottom: 30px; right: 30px; z-index: 999999; width: 56px;
cursor: pointer; display: flex; align-items: center; justify-content: center; height: 56px;
color: white; font-size: 24px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); border-radius: 50%;
position: fixed;
bottom: calc(180px + env(safe-area-inset-bottom, 0px));
right: 0px;
z-index: 999999;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 24px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
background: linear-gradient(135deg, #ff9a9e, #fad0c4, #a1c4fd, #c2e9fb, #d4fc79, #96e6a1);
background-size: 300% 300%;
animation: rainbowPulse 4s ease infinite;
user-select: none;
-webkit-user-drag: none;
`; `;
chatBtn.innerText = '💬';
document.body.appendChild(chatBtn); document.body.appendChild(chatBtn);
// 3. 创建聊天窗口 // ========== 聊天窗口 ==========
const chatWindow = document.createElement('div'); const chatWindow = document.createElement('div');
chatWindow.id = 'independent-chat-window'; chatWindow.id = 'independent-chat-window';
chatWindow.style = ` chatWindow.style.cssText = `
width: 360px; height: 520px; border-radius: 12px; background: white; position: fixed;
position: fixed; bottom: 100px; right: 30px; z-index: 999998; z-index: 999998;
box-shadow: 0 4px 20px rgba(0,0,0,0.15); display: none; border-radius: 12px;
flex-direction: column; overflow: hidden; background: white;
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
display: none;
flex-direction: column;
overflow: hidden;
`; `;
document.body.appendChild(chatWindow); document.body.appendChild(chatWindow);
// 3.1 标题栏 // 头部
const chatHeader = document.createElement('div'); const chatHeader = document.createElement('div');
chatHeader.style = ` chatHeader.style.cssText = `
height: 50px; background: #1C64F2; color: white; height: 50px;
display: flex; align-items: center; justify-content: space-between; background: #1C64F2;
padding: 0 16px; font-size: 18px; font-weight: 600; color: white;
`; display: flex;
chatHeader.innerHTML = ` align-items: center;
<span>智能客服</span> justify-content: space-between;
<span id="close-chat-window" style="cursor: pointer; font-size: 20px;">×</span> padding: 0 16px;
font-size: 18px;
font-weight: 600;
`; `;
chatHeader.innerHTML = `<span>智能客服</span><span id="close-chat-window" style="cursor:pointer;font-size:24px;">×</span>`;
chatWindow.appendChild(chatHeader); chatWindow.appendChild(chatHeader);
// 3.2 聊天内容区 // 内容区
const chatContent = document.createElement('div'); const chatContent = document.createElement('div');
chatContent.style = ` chatContent.style.cssText = `
flex: 1; padding: 16px; overflow-y: auto; flex: 1;
padding: 16px;
overflow-y: auto;
background: #f9fafb; background: #f9fafb;
`; `;
chatContent.innerHTML = ` chatContent.innerHTML = `
<div style="display:flex;margin-bottom:16px;"> <div style="display:flex;margin-bottom:16px;">
<div style="width:36px;height:36px;border-radius:50%;background:#1C64F2;color:white;display:flex;align-items:center;justify-content:center;margin-right:8px;">🤖</div> <div style="width:36px;height:36px;border-radius:50%;background:#1C64F2;color:white;display:flex;align-items:center;justify-content:center;margin-right:8px;">🤖</div>
<div style="background: white; padding: 8px 12px; border-radius: 8px; max-width: 70%;"> <div style="background:white;padding:8px 12px;border-radius:8px;max-width:70%;">您好!有什么可以帮到您的吗?</div>
您好!有什么可以帮到您的吗?
</div>
</div> </div>
`; `;
chatWindow.appendChild(chatContent); chatWindow.appendChild(chatContent);
// 3.3 输入区 // 输入区
const chatInputArea = document.createElement('div'); const chatInputArea = document.createElement('div');
chatInputArea.style = ` chatInputArea.style.cssText = `
height: 60px; display: flex; align-items: center; height: 60px;
padding: 0 16px; border-top: 1px solid #eee; display: flex;
align-items: center;
padding: 0 16px;
border-top: 1px solid #eee;
`; `;
const chatInput = document.createElement('input'); const chatInput = document.createElement('input');
chatInput.type = 'text'; chatInput.type = 'text';
chatInput.placeholder = '请输入您的问题...'; chatInput.placeholder = '请输入您的问题...';
chatInput.style = ` chatInput.style.cssText = `
flex: 1; height: 36px; padding: 0 12px; border: 1px solid #ddd; flex: 1;
border-radius: 18px; outline: none; font-size: 14px; height: 36px;
padding: 0 12px;
border: 1px solid #ddd;
border-radius: 18px;
outline: none;
font-size: 14px;
`; `;
const sendBtn = document.createElement('button'); const sendBtn = document.createElement('button');
sendBtn.innerText = '发送'; sendBtn.innerText = '发送'; // 横排文字
sendBtn.style = ` sendBtn.style.cssText = `
margin-left: 12px; padding: 6px 16px; background: #1C64F2; margin-left: 12px;
color: white; border: none; border-radius: 18px; cursor: pointer; padding: 6px 16px;
background: #1C64F2;
color: white;
border: none;
border-radius: 18px;
cursor: pointer;
font-size: 14px; font-size: 14px;
white-space: nowrap; /* 防止换行 */
text-align: center; /* 居中 */
line-height: 1.5; /* 调整高度 */
`; `;
chatInputArea.appendChild(chatInput); chatInputArea.appendChild(chatInput);
chatInputArea.appendChild(sendBtn); chatInputArea.appendChild(sendBtn);
chatWindow.appendChild(chatInputArea); chatWindow.appendChild(chatInputArea);
// 4. 绑定事件 // ========== 响应式尺寸控制 ==========
const updateWindowSize = () => {
const isMobile = window.innerWidth <= 768;
if (isMobile) {
chatWindow.style.width = 'calc(90vw - 40px)';
chatWindow.style.maxWidth = '400px';
chatWindow.style.height = '70vh';
chatWindow.style.maxHeight = '600px';
chatWindow.style.right = '5vw';
chatWindow.style.bottom = 'calc(86px + env(safe-area-inset-bottom, 0px))';
} else {
chatWindow.style.width = '360px';
chatWindow.style.height = '520px';
chatWindow.style.maxWidth = 'none';
chatWindow.style.maxHeight = 'none';
chatWindow.style.right = '20px';
chatWindow.style.bottom = '86px';
}
chatContent.style.maxHeight = 'calc(100% - 110px)';
};
// 初始化并监听窗口变化
updateWindowSize();
window.addEventListener('resize', updateWindowSize);
// ========== 交互逻辑 ==========
let isShow = false; let isShow = false;
chatBtn.onclick = () => { const toggleChat = () => {
isShow = !isShow; isShow = !isShow;
chatWindow.style.display = isShow ? 'flex' : 'none'; chatWindow.style.display = isShow ? 'flex' : 'none';
if (isShow) {
updateWindowSize(); // 确保旋转/切换后尺寸正确
chatInput.focus();
}
}; };
chatBtn.onclick = toggleChat;
document.getElementById('close-chat-window').onclick = () => { document.getElementById('close-chat-window').onclick = () => {
isShow = false; isShow = false;
chatWindow.style.display = 'none'; chatWindow.style.display = 'none';
}; };
document.addEventListener('click', (e) => { document.addEventListener('click', (e) => {
if (!chatBtn.contains(e.target) && !chatWindow.contains(e.target)) { if (!chatBtn.contains(e.target) && !chatWindow.contains(e.target)) {
isShow = false; isShow = false;
@@ -225,43 +299,45 @@
} }
}); });
// 5. 发送消息逻辑 // 发送消息
const sendMessage = () => { const sendMessage = () => {
const msg = chatInput.value.trim(); const msg = chatInput.value.trim();
if (!msg) return; if (!msg) return;
// 显示用户消息 const safeMsg = this.htmlEncode(msg);
chatContent.insertAdjacentHTML('beforeend', ` chatContent.insertAdjacentHTML('beforeend', `
<div style="display:flex;margin-bottom:16px;justify-content:flex-end;"> <div style="display:flex;margin-bottom:16px;justify-content:flex-end;">
<div style="background:#1C64F2;color:white;padding:8px 12px;border-radius:8px;max-width:70%;"> <div style="background:#1C64F2;color:white;padding:8px 12px;border-radius:8px;max-width:70%;">
${msg} ${safeMsg}
</div> </div>
<div style="width:36px;height:36px;border-radius:50%;background:#eee;display:flex;align-items:center;justify-content:center;margin-left:8px;">👤</div> <div style="width:36px;height:36px;border-radius:50%;background:#eee;display:flex;align-items:center;justify-content:center;margin-left:8px;">👤</div>
</div> </div>
`); `);
chatInput.value = ''; chatInput.value = '';
chatContent.scrollTop = chatContent.scrollHeight; chatContent.scrollTop = chatContent.scrollHeight;
// 模拟回复
setTimeout(() => { setTimeout(() => {
chatContent.insertAdjacentHTML('beforeend', ` chatContent.insertAdjacentHTML('beforeend', `
<div style="display:flex;margin-bottom:16px;"> <div style="display:flex;margin-bottom:16px;">
<div style="width:36px;height:36px;border-radius:50%;background:#1C64F2;color:white;display:flex;align-items:center;justify-content:center;margin-right:8px;">🤖</div> <div style="width:36px;height:36px;border-radius:50%;background:#1C64F2;color:white;display:flex;align-items:center;justify-content:center;margin-right:8px;">🤖</div>
<div style="background:white;padding:8px 12px;border-radius:8px;max-width:70%;"> <div style="background:white;padding:8px 12px;border-radius:8px;max-width:70%;">
已收到您的问题:"${msg}",我们会尽快回复! 已收到您的问题:"${safeMsg}",我们会尽快回复!
</div> </div>
</div> </div>
`); `);
chatContent.scrollTop = chatContent.scrollHeight; chatContent.scrollTop = chatContent.scrollHeight;
}, 800); }, 800);
}; };
sendBtn.onclick = sendMessage;
chatInput.onkeydown = (e) => e.key === 'Enter' && sendMessage();
console.log('纯前端聊天窗口创建完成'); sendBtn.onclick = sendMessage;
chatInput.onkeydown = (e) => {
if (e.key === 'Enter') {
e.preventDefault();
sendMessage();
}
};
console.log('响应式智能客服窗口创建完成');
}, },
/**
* 原逻辑(授权/登录/分享等)
*/
getAuthInfo() { getAuthInfo() {
// #ifdef H5 // #ifdef H5
if (this.$util.isWeiXin()) { if (this.$util.isWeiXin()) {
@@ -355,12 +431,15 @@
@import './common/css/iconfont.css'; @import './common/css/iconfont.css';
@import './common/css/icondiy.css'; @import './common/css/icondiy.css';
@import './common/css/icon/extend.css'; @import './common/css/icon/extend.css';
page { page {
background: #f4f6fa; background: #f4f6fa;
} }
body { body {
padding-bottom: 80px !important; padding-bottom: 80px !important;
} }
/* 聊天窗口滚动条美化 */
#independent-chat-window div::-webkit-scrollbar { #independent-chat-window div::-webkit-scrollbar {
width: 4px; width: 4px;
} }
@@ -368,4 +447,17 @@
background: #ddd; background: #ddd;
border-radius: 2px; border-radius: 2px;
} }
/* 彩虹动画 */
@keyframes rainbowPulse {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* 强制固定定位,防止被其他样式干扰 */
#independent-chat-btn,
#independent-chat-window {
position: fixed !important;
}
</style> </style>

View File

@@ -1,38 +1,21 @@
<template> <template>
<!-- 悬浮按钮 --> <!-- 悬浮按钮 -->
<view v-if="pageCount == 1 || need" class="fixed-box" :style="{ height: fixBtnShow ? '330rpx' : '120rpx' }"> <view v-if="pageCount == 1 || need" class="fixed-box" :style="{ height: fixBtnShow ? '120rpx' : '120rpx' }">
<!-- <view class="btn-item" v-if="fixBtnShow" @click="$util.redirectTo('/pages/index/index')"> -->
<!-- AI智能助手 -->
<view class="btn-item" v-if="fixBtnShow && enableAIChat" @click="openAIChat" :style="{backgroundImage:'url('+(aiAgentimg?aiAgentimg:'')+')',backgroundSize:'100% 100%'}">
<text class="ai-icon" v-if="!aiAgentimg">🤖</text>
<!-- 未读消息小红点 -->
<view v-if="unreadCount > 0" class="unread-badge">
<text class="badge-text">{{ unreadCount > 99 ? '99+' : unreadCount }}</text>
</view>
</view>
<!-- #ifdef MP-WEIXIN --> <!-- #ifdef MP-WEIXIN -->
<button class="btn-item" v-if="fixBtnShow" hoverClass="none" openType="contact" sessionFrom="weapp" showMessageCard="true" :style="{backgroundImage:'url('+(kefuimg?kefuimg:'')+')',backgroundSize:'100% 100%'}"> <button
class="btn-item"
v-if="fixBtnShow"
hoverClass="none"
openType="contact"
sessionFrom="weapp"
showMessageCard="true"
:style="{backgroundImage:'url('+(kefuimg?kefuimg:'')+')',backgroundSize:'100% 100%'}"
>
<text class="icox icox-kefu" v-if="!kefuimg"></text> <text class="icox icox-kefu" v-if="!kefuimg"></text>
<!-- <view>首页</view> -->
</button> </button>
<!-- #endif --> <!-- #endif -->
<!-- 电话 --> <!-- 其他按钮AI 电话已隐藏 -->
<view class="btn-item" v-if="fixBtnShow" @click="call()" :style="{backgroundImage:'url('+(phoneimg?phoneimg:'')+')',backgroundSize:'100% 100%'}">
<text class="iconfont icon-dianhua" v-if="!phoneimg"></text>
<!-- <view>我的</view> -->
</view>
<!-- <view class="btn-item icon-xiala" v-if="fixBtnShow" @click="fixBtnShow ? (fixBtnShow = false) : (fixBtnShow = true)">
<text class="iconfont icon-unfold"></text>
</view>
<view class="btn-item switch" v-else :class="{ show: fixBtnShow }"
@click="fixBtnShow ? (fixBtnShow = false) : (fixBtnShow = true)">
<view class="">快捷</view>
<view>导航</view>
</view> -->
</view> </view>
</template> </template>
@@ -61,7 +44,6 @@
this.phoneimg = this.$util.getDefaultImage().phone this.phoneimg = this.$util.getDefaultImage().phone
this.pageCount = getCurrentPages().length; this.pageCount = getCurrentPages().length;
var that = this var that = this
uni.getStorage({ uni.getStorage({
key: 'shopInfo', key: 'shopInfo',
@@ -69,7 +51,6 @@
that.tel = e.data.mobile that.tel = e.data.mobile
} }
}) })
}, },
computed: { computed: {
...mapGetters([ ...mapGetters([
@@ -77,13 +58,13 @@
'aiUnreadCount' 'aiUnreadCount'
]), ]),
aiAgentimg() { aiAgentimg() {
return this.globalAIAgentConfig?.icon || this.$util.getDefaultImage().aiAgent || '' // AI智能助手的头像 return this.globalAIAgentConfig?.icon || this.$util.getDefaultImage().aiAgent || ''
}, },
unreadCount() { unreadCount() {
return this.aiUnreadCount return this.aiUnreadCount
}, },
enableAIChat() { enableAIChat() {
return this.globalAIAgentConfig?.enable || true // 是否开启AI智能助手 return this.globalAIAgentConfig?.enable || true
}, },
}, },
methods: { methods: {
@@ -91,22 +72,20 @@
'setAiUnreadCount' 'setAiUnreadCount'
]), ]),
//拨打电话 // 拨打电话(已无调用,可保留或删除)
call() { call() {
uni.makePhoneCall({ uni.makePhoneCall({
phoneNumber: this.tel + '' phoneNumber: this.tel + ''
}) })
}, },
// 打开AI聊天弹窗 // 打开AI聊天(已无调用,可保留或删除)
openAIChat() { openAIChat() {
if (this.enableAIChat) { if (this.enableAIChat) {
this.setAiUnreadCount(0); this.setAiUnreadCount(0);
} }
this.$util.redirectTo('/pages_tool/ai-chat/index') this.$util.redirectTo('/pages_tool/ai-chat/index')
} }
} }
}; };
</script> </script>
@@ -137,8 +116,6 @@
right: 0rpx; right: 0rpx;
bottom: 200rpx; bottom: 200rpx;
z-index: 10; z-index: 10;
// background: #fff;
// box-shadow: 2rpx 2rpx 22rpx rgba(0, 0, 0, 0.3);
border-radius: 120rpx; border-radius: 120rpx;
padding: 20rpx 0; padding: 20rpx 0;
display: flex; display: flex;
@@ -177,14 +154,12 @@
transform: rotate(180deg); transform: rotate(180deg);
} }
&.switch {}
&.icon-xiala { &.icon-xiala {
margin: 0; margin: 0;
margin-top: 0.1rpx; margin-top: 0.1rpx;
} }
// 未读消息小红点 // 未读消息小红点(已无使用,可保留样式)
.unread-badge { .unread-badge {
position: absolute; position: absolute;
top: -5rpx; top: -5rpx;

View File

@@ -0,0 +1,20 @@
<template>
<view class="container">
<web-view src="https://dify.aigc-quickapp.com/chatbot/ILbr8HHEgpBW2ggp"></web-view>
</view>
</template>
<script>
export default {
onLoad() {
console.log('Dify 聊天页已加载')
}
}
</script>
<style>
.container {
width: 100%;
height: 100vh;
}
</style>

View File

@@ -122,7 +122,7 @@
</view> </view>
</uni-popup> </uni-popup>
</view> </view>
<hover-nav></hover-nav>
<!-- #ifdef MP-WEIXIN --> <!-- #ifdef MP-WEIXIN -->
<!-- 小程序隐私协议 --> <!-- 小程序隐私协议 -->
<privacy-popup ref="privacyPopup"></privacy-popup> <privacy-popup ref="privacyPopup"></privacy-popup>