Files
lucky_shop/components/hover-nav/hover-nav.vue

304 lines
8.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view v-if="pageCount == 1 || need" class="fixed-box"
:style="[{ height: fixBtnShow ? '400rpx' : '320rpx' }, customContainerStyle]">
<!-- AI 智能助手优先显示 -->
<view
v-if="fixBtnShow && enableAIChat"
class="btn-item"
@click="openAIChat"
:style="{ backgroundImage: aiAgentimg ? `url(${aiAgentimg})` : '', backgroundSize: '100% 100%' }"
>
<text class="ai-icon" v-if="!aiAgentimg">🤖</text>
</view>
<!-- 普通客服仅当未启用 AI 时显示 -->
<template v-else-if="fixBtnShow">
<!-- #ifdef MP-WEIXIN -->
<button
class="btn-item"
hoverClass="none"
openType="contact"
sessionFrom="weapp"
showMessageCard="true"
:style="[{ backgroundImage: kefuimg ? `url(${kefuimg})` : '', backgroundSize: '100% 100%' }, customButtonStyle]"
>
<text class="icox icox-kefu" v-if="!kefuimg"></text>
</button>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<button
class="btn-item"
hoverClass="none"
@click="openKefuSelectPopup"
:style="[{ backgroundImage: kefuimg ? `url(${kefuimg})` : '', backgroundSize: '100% 100%' }, customButtonStyle]"
>
<text class="icox icox-kefu" v-if="!kefuimg"></text>
</button>
<!-- #endif -->
</template>
<!-- 电话按钮始终显示 -->
<view
v-if="fixBtnShow"
class="btn-item"
@click="call()"
:style="[{ backgroundImage: phoneimg ? `url(${phoneimg})` : '', backgroundSize: '100% 100%' }, customButtonStyle]"
>
<text class="iconfont icon-dianhua" v-if="!phoneimg"></text>
</view>
</view>
</template>
<script>
import { createCustomerService } from '@/common/js/customer-service.js';
export default {
name: 'hover-nav',
props: {
need: { type: Boolean, default: false }
},
data() {
return {
pageCount: 0,
fixBtnShow: true,
tel: '',
kefuimg: '',
phoneimg: '',
shopInfo: null,
enableAIChat: false, // 默认关闭,但会在 created 中智能判断
aiAgentimg: '',
currentLangIndex: 0,
langIndexMap: {},
isLanguageSwitchEnabled: false,
kefuList: [
{ id: 'weixin-official', name: '微信官方客服', isOfficial: true, type: 'weapp' },
{ id: 'custom-kefu', name: '自定义在线客服', isOfficial: false, type: 'custom' },
{ id: 'qyweixin-kefu', name: '企业微信客服', isOfficial: false, type: 'qyweixin' }
],
selectedKefu: null,
customerService: null,
buttonConfig: null
};
},
created() {
this.initLanguage();
this.kefuimg = this.$util.getDefaultImage().kefu;
this.phoneimg = this.$util.getDefaultImage().phone;
this.pageCount = getCurrentPages().length;
uni.getStorage({
key: 'shopInfo',
success: (e) => {
console.log('【调试】当前 shopInfo:', e.data);
this.shopInfo = e.data;
this.tel = e.data.mobile || '';
this.isLanguageSwitchEnabled = !!e.data.ischina;
// 🔧 关键修复:如果后台没传 enableAIChat默认开启方便测试
// 正式环境建议由后台控制,此处可改为 false
const defaultEnableAI = true; // ← 改成 false 可关闭默认开启
this.enableAIChat = e.data.hasOwnProperty('enableAIChat')
? !!e.data.enableAIChat
: defaultEnableAI;
this.aiAgentimg = e.data.aiAgentimg || '';
console.log('【调试】AI 客服是否启用:', this.enableAIChat);
},
fail: () => {
console.warn('未获取到 shopInfo使用默认设置');
// 如果完全没有 shopInfo也可以默认开启 AI
this.enableAIChat = true;
}
});
},
computed: {
currentLangDisplayName() {
const lang = this.langIndexMap[this.currentLangIndex];
return lang === 'zh-cn' ? 'EN' : 'CN';
},
customContainerStyle() {
return this.shopInfo?.floatingButton?.container || {};
},
customButtonStyle() {
return this.shopInfo?.floatingButton?.button || {};
},
customTextStyle() {
return this.shopInfo?.floatingButton?.text || {};
},
unreadCount() {
return this.$store.state.aiUnreadCount || 0;
}
},
methods: {
initLanguage() {
this.langList = this.$langConfig.list();
this.langIndexMap = {};
for (let i = 0; i < this.langList.length; i++) {
this.langIndexMap[i] = this.langList[i].value;
}
const savedLang = uni.getStorageSync('lang');
if (savedLang) {
for (let i = 0; i < this.langList.length; i++) {
if (this.langList[i].value === savedLang) {
this.currentLangIndex = i;
break;
}
}
} else {
this.currentLangIndex = 0;
}
},
call() {
if (this.tel) {
uni.makePhoneCall({ phoneNumber: this.tel + '' });
} else {
uni.showToast({ title: '暂无联系电话', icon: 'none' });
}
},
toggleLanguage() {
this.currentLangIndex = this.currentLangIndex === 0 ? 1 : 0;
const targetLang = this.langIndexMap[this.currentLangIndex];
let currentRoute = this.$util.getCurrentRoute().path;
this.$langConfig.change(targetLang, currentRoute);
},
openKefuSelectPopup() {
const kefuNames = this.kefuList.map(item => item.name);
uni.showActionSheet({
itemList: kefuNames,
success: (res) => {
this.selectedKefu = this.kefuList[res.tapIndex];
this.reinitCustomerService(this.selectedKefu);
this.handleSelectedKefu();
}
});
},
reinitCustomerService(kefu) {
this.customerService = createCustomerService(this, kefu);
this.buttonConfig = this.customerService.getButtonConfig();
},
handleSelectedKefu() {
const kefu = this.selectedKefu;
if (!kefu) return;
if (kefu.isOfficial) {
uni.openCustomerServiceConversation({
sessionFrom: 'weapp',
showMessageCard: true
});
} else if (kefu.id === 'custom-kefu') {
this.customerService.handleCustomerClick();
} else if (kefu.id === 'qyweixin-kefu') {
this.customerService.handleQyWeixinKefuClick();
}
},
openAIChat() {
uni.navigateTo({
url: '/pages_tool/ai-chat/index',
success: () => {
console.log('✅ AI 客服跳转成功');
},
fail: (err) => {
console.error('❌ 跳转失败:', err);
uni.showToast({ title: '跳转失败,请重试', icon: 'none' });
}
});
}
}
}
</script>
<style scoped>
.fixed-box {
position: fixed;
right: 0rpx;
bottom: 240rpx;
/* #ifdef H5 */
bottom: 320rpx;
/* #endif */
z-index: 10;
border-radius: 120rpx;
padding: 20rpx 0;
display: flex;
justify-content: center;
flex-direction: column;
width: 100rpx;
box-sizing: border-box;
transition: 0.3s;
overflow: hidden;
}
.btn-item {
display: flex;
justify-content: center;
text-align: center;
flex-direction: column;
line-height: 1;
margin: 14rpx 0;
transition: 0.1s;
background: var(--hover-nav-bg-color);
color: var(--hover-nav-text-color);
border-radius: 40rpx;
width: 80rpx;
height: 80rpx;
padding: 0;
overflow: hidden;
}
.btn-item text {
font-size: 28rpx;
}
.iconfont,
.icox {
font-size: 36rpx;
font-weight: bold;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
margin: 14rpx 0;
background: #fff;
border-radius: 50rpx;
width: 80rpx;
height: 80rpx;
padding: 0;
position: relative;
}
.iconfont text,
.icox text {
font-size: 36rpx;
font-weight: bold;
}
.unread-badge {
position: absolute;
top: -5rpx;
right: -5rpx;
background-color: #ff4544;
color: white;
border-radius: 20rpx;
min-width: 30rpx;
height: 30rpx;
font-size: 20rpx;
line-height: 30rpx;
text-align: center;
padding: 0 8rpx;
box-shadow: 0 2rpx 10rpx rgba(255, 69, 68, 0.3);
}
.ai-icon {
font-size: 40rpx;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
</style>