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

232
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,213 +1,188 @@
<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')"> --> <!-- #ifdef MP-WEIXIN -->
<button
<!-- AI智能助手 --> class="btn-item"
<view class="btn-item" v-if="fixBtnShow && enableAIChat" @click="openAIChat" :style="{backgroundImage:'url('+(aiAgentimg?aiAgentimg:'')+')',backgroundSize:'100% 100%'}"> v-if="fixBtnShow"
<text class="ai-icon" v-if="!aiAgentimg">🤖</text> hoverClass="none"
<!-- 未读消息小红点 --> openType="contact"
<view v-if="unreadCount > 0" class="unread-badge"> sessionFrom="weapp"
<text class="badge-text">{{ unreadCount > 99 ? '99+' : unreadCount }}</text> showMessageCard="true"
</view> :style="{backgroundImage:'url('+(kefuimg?kefuimg:'')+')',backgroundSize:'100% 100%'}"
</view> >
<text class="icox icox-kefu" v-if="!kefuimg"></text>
<!-- #ifdef MP-WEIXIN --> </button>
<button class="btn-item" v-if="fixBtnShow" hoverClass="none" openType="contact" sessionFrom="weapp" showMessageCard="true" :style="{backgroundImage:'url('+(kefuimg?kefuimg:'')+')',backgroundSize:'100% 100%'}"> <!-- #endif -->
<text class="icox icox-kefu" v-if="!kefuimg"></text>
<!-- <view>首页</view> --> <!-- 其他按钮AI 电话已隐藏 -->
</button> </view>
<!-- #endif --> </template>
<!-- 电话 --> <script>
<view class="btn-item" v-if="fixBtnShow" @click="call()" :style="{backgroundImage:'url('+(phoneimg?phoneimg:'')+')',backgroundSize:'100% 100%'}"> import { mapGetters, mapMutations } from 'vuex'
<text class="iconfont icon-dianhua" v-if="!phoneimg"></text>
<!-- <view>我的</view> --> export default {
</view> name: 'hover-nav',
props: {
<!-- <view class="btn-item icon-xiala" v-if="fixBtnShow" @click="fixBtnShow ? (fixBtnShow = false) : (fixBtnShow = true)"> need: {
<text class="iconfont icon-unfold"></text> type: Boolean,
</view> default: false
<view class="btn-item switch" v-else :class="{ show: fixBtnShow }" },
@click="fixBtnShow ? (fixBtnShow = false) : (fixBtnShow = true)"> },
<view class="">快捷</view> data() {
<view>导航</view> return {
</view> --> pageCount: 0,
</view> fixBtnShow: true,
</template> tel: '',
kefuimg: '',
<script> phoneimg: ''
import { mapGetters, mapMutations } from 'vuex' };
},
export default { created() {
name: 'hover-nav', this.kefuimg = this.$util.getDefaultImage().kefu
props: { this.phoneimg = this.$util.getDefaultImage().phone
need: { this.pageCount = getCurrentPages().length;
type: Boolean,
default: false var that = this
}, uni.getStorage({
}, key: 'shopInfo',
data() { success(e) {
return { that.tel = e.data.mobile
pageCount: 0, }
fixBtnShow: true, })
tel:'', },
kefuimg:'', computed: {
phoneimg:'' ...mapGetters([
}; 'globalAIAgentConfig',
}, 'aiUnreadCount'
created() { ]),
this.kefuimg = this.$util.getDefaultImage().kefu aiAgentimg() {
this.phoneimg = this.$util.getDefaultImage().phone return this.globalAIAgentConfig?.icon || this.$util.getDefaultImage().aiAgent || ''
this.pageCount = getCurrentPages().length; },
unreadCount() {
return this.aiUnreadCount
var that = this },
uni.getStorage({ enableAIChat() {
key:'shopInfo', return this.globalAIAgentConfig?.enable || true
success(e){ },
that.tel = e.data.mobile },
} methods: {
}) ...mapMutations([
'setAiUnreadCount'
}, ]),
computed: {
...mapGetters([ // 拨打电话(已无调用,可保留或删除)
'globalAIAgentConfig', call() {
'aiUnreadCount' uni.makePhoneCall({
]), phoneNumber: this.tel + ''
aiAgentimg() { })
return this.globalAIAgentConfig?.icon || this.$util.getDefaultImage().aiAgent || '' // AI智能助手的头像 },
},
unreadCount() { // 打开AI聊天已无调用可保留或删除
return this.aiUnreadCount openAIChat() {
}, if (this.enableAIChat) {
enableAIChat() { this.setAiUnreadCount(0);
return this.globalAIAgentConfig?.enable || true // 是否开启AI智能助手 }
}, this.$util.redirectTo('/pages_tool/ai-chat/index')
}, }
methods: { }
...mapMutations([ };
'setAiUnreadCount' </script>
]),
<style lang="scss">
//拨打电话 .container-box {
call(){ width: 100%;
uni.makePhoneCall({
phoneNumber:this.tel+'' .item-wrap {
}) border-radius: 10rpx;
},
.image-box {
// 打开AI聊天弹窗 border-radius: 10rpx;
openAIChat() { }
if (this.enableAIChat) {
this.setAiUnreadCount(0); image {
} width: 100%;
height: auto;
this.$util.redirectTo('/pages_tool/ai-chat/index') border-radius: 10rpx;
} will-change: transform;
}
} }
}; }
</script>
//悬浮按钮
<style lang="scss"> .fixed-box {
.container-box { position: fixed;
width: 100%; right: 0rpx;
bottom: 200rpx;
.item-wrap { z-index: 10;
border-radius: 10rpx; border-radius: 120rpx;
padding: 20rpx 0;
.image-box { display: flex;
border-radius: 10rpx; justify-content: center;
} flex-direction: column;
width: 100rpx;
image { box-sizing: border-box;
width: 100%; transition: 0.3s;
height: auto; overflow: hidden;
border-radius: 10rpx;
will-change: transform; .btn-item {
} display: flex;
} justify-content: center;
} text-align: center;
flex-direction: column;
//悬浮按钮 line-height: 1;
.fixed-box { margin: 14rpx 0;
position: fixed; transition: 0.1s;
right: 0rpx; background: #fff;
bottom: 200rpx; border-radius: 50rpx;
z-index: 10; width: 80rpx;
// background: #fff; height: 80rpx;
// box-shadow: 2rpx 2rpx 22rpx rgba(0, 0, 0, 0.3); padding: 0;
border-radius: 120rpx; position: relative;
padding: 20rpx 0; text {
display: flex; font-size: 36rpx;
justify-content: center; font-weight: bold;
flex-direction: column; }
width: 100rpx;
box-sizing: border-box; view {
transition: 0.3s; font-size: 26rpx;
overflow: hidden; font-weight: bold;
}
.btn-item {
display: flex; &.show {
justify-content: center; transform: rotate(180deg);
text-align: center; }
flex-direction: column;
line-height: 1; &.icon-xiala {
margin: 14rpx 0; margin: 0;
transition: 0.1s; margin-top: 0.1rpx;
background: #fff; }
border-radius: 50rpx;
width: 80rpx; // 未读消息小红点(已无使用,可保留样式)
height: 80rpx; .unread-badge {
padding: 0; position: absolute;
position: relative; top: -5rpx;
text { right: -5rpx;
font-size: 36rpx; background-color: #ff4544;
font-weight: bold; color: white;
} border-radius: 20rpx;
min-width: 30rpx;
view { height: 30rpx;
font-size: 26rpx; font-size: 10rpx;
font-weight: bold; line-height: 30rpx;
} text-align: center;
padding: 0 8rpx;
&.show { z-index: 1;
transform: rotate(180deg); box-shadow: 0 2rpx 10rpx rgba(255, 69, 68, 0.3);
}
.badge-text {
&.switch {} font-size: 8rpx;
// #ifdef MP-WEIXIN
&.icon-xiala { font-size: 20rpx;
margin: 0; // #endif
margin-top: 0.1rpx; }
} }
}
// 未读消息小红点 }
.unread-badge {
position: absolute;
top: -5rpx;
right: -5rpx;
background-color: #ff4544;
color: white;
border-radius: 20rpx;
min-width: 30rpx;
height: 30rpx;
font-size: 10rpx;
line-height: 30rpx;
text-align: center;
padding: 0 8rpx;
z-index: 1;
box-shadow: 0 2rpx 10rpx rgba(255, 69, 68, 0.3);
.badge-text {
font-size: 8rpx;
// #ifdef MP-WEIXIN
font-size: 20rpx;
// #endif
}
}
}
}
</style> </style>

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>