Files
monkey-ai-web/src/components/customer-service/CustomerServiceQRCard.jsx
T

252 lines
11 KiB
React

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useState, useEffect } from 'react';
import { Avatar, Button } from '@/components/ui';
import {
MessageCircle,
Sparkles,
ScanLine,
Zap,
ArrowRight,
} from 'lucide-react';
import { API } from '../../helpers';
import { useTranslation } from 'react-i18next';
/**
* 自定义 Hook:获取客服展示数据
* 供父组件使用,避免多个 CustomerServiceQRCard 实例重复请求
*/
export const useCustomerServiceDisplay = () => {
const [loading, setLoading] = useState(true);
const [agent, setAgent] = useState(null);
const [hasConfig, setHasConfig] = useState(false);
useEffect(() => {
const fetchCustomerService = async () => {
try {
// 先检查是否有启用的客服配置
const configRes = await API.get('/api/customer-service/config');
if (configRes.data.success && configRes.data.data?.enabled) {
setHasConfig(true);
// 使用专门的展示接口获取客服信息(只读,不增加会话计数)
const agentRes = await API.get('/api/customer-service/display');
if (agentRes.data.success && agentRes.data.data) {
setAgent(agentRes.data.data);
}
}
} catch (error) {
// 静默处理错误,不显示给用户
console.error('Failed to fetch customer service:', error);
} finally {
setLoading(false);
}
};
fetchCustomerService();
}, []);
// 检查客服是否至少有一种联系方式
const hasContactMethod = agent && (agent.qrcode_url || agent.wecom_url);
// 是否应该显示客服卡片
const showCard = !loading && hasConfig && !!agent && !!hasContactMethod;
return { loading, agent, hasConfig, showCard };
};
/**
* 客服二维码卡片组件(紧凑版)
* 用于在密钥管理页面侧边栏展示客服二维码,引导用户添加客服咨询
*
* @param {Object} props
* @param {Object} props.agent - 客服信息(由父组件通过 useCustomerServiceDisplay 获取后传入)
*/
const CustomerServiceQRCard = ({ agent }) => {
const { t } = useTranslation();
const [qrcodeError, setQrcodeError] = useState(false);
// 如果没有客服数据,不显示组件
if (!agent) {
return null;
}
// 处理二维码加载失败
const handleQrcodeError = () => {
console.warn('QR code image failed to load:', agent.qrcode_url);
setQrcodeError(true);
};
return (
<div className='relative group/card'>
{/* 卡片外发光 */}
<div className='absolute -inset-0.5 bg-gradient-to-br from-brand-from/20 via-brand-via/20 to-pink-500/20 rounded-[18px] blur-sm opacity-0 group-hover/card:opacity-100 transition-opacity duration-500' />
<div className='relative bg-white/80 dark:bg-neutral-900/80 backdrop-blur-xl rounded-2xl border border-gray-200/60 dark:border-neutral-800/60 overflow-hidden shadow-sm hover:shadow-lg transition-all duration-300'>
{/* 顶部渐变装饰条 */}
<div className='h-1 bg-gradient-to-r from-brand-from via-brand-via to-pink-500' />
<div className='p-4'>
{/* 标题 + 客服信息 合并为一行 */}
<div className='flex items-center gap-2.5 mb-3'>
{agent.avatar ? (
<div className='relative'>
<div className='absolute -inset-0.5 bg-gradient-to-br from-brand-from to-brand-to rounded-full opacity-60 blur-[2px]' />
<Avatar
src={agent.avatar}
size='small'
className='relative !w-9 !h-9 border-2 border-white dark:border-neutral-800 flex-shrink-0'
/>
</div>
) : (
<div className='relative'>
<div className='absolute -inset-0.5 bg-gradient-to-br from-brand-from to-brand-to rounded-full opacity-60 blur-[2px]' />
<div className='relative w-9 h-9 rounded-full bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center text-white font-bold text-xs flex-shrink-0'>
{agent.name ? (
agent.name.charAt(0)
) : (
<MessageCircle size={16} />
)}
</div>
</div>
)}
<div className='flex-1 min-w-0'>
<div className='flex items-center gap-1.5'>
<span className='font-semibold text-sm text-gray-900 dark:text-white truncate'>
{agent.name || t('专属客服')}
</span>
<span className='inline-flex items-center gap-1 px-1.5 py-0.5 bg-emerald-50 dark:bg-emerald-900/20 text-emerald-600 dark:text-emerald-400 rounded-full text-[10px] font-medium flex-shrink-0'>
<span className='w-1 h-1 bg-emerald-500 rounded-full animate-pulse'></span>
{t('在线')}
</span>
</div>
<p className='text-[11px] text-gray-400 dark:text-gray-500 mt-0.5'>
{t('1对1 密钥配置指导')}
</p>
</div>
</div>
{/* 提示文案 - 吸引用户扫码 */}
<div className='relative mb-3 overflow-hidden rounded-xl'>
{/* 动态渐变背景 */}
<div className='absolute inset-0 bg-gradient-to-r from-brand-from via-brand-via to-pink-500 opacity-[0.08] dark:opacity-[0.15]' />
<div className='absolute inset-0 bg-gradient-to-br from-amber-400/10 to-transparent' />
<div className='relative px-3 py-2.5 border border-brand/50 dark:border-brand-deep/30 rounded-xl'>
<div className='flex items-start gap-2'>
<div className='flex-shrink-0 mt-0.5'>
<div className='w-5 h-5 rounded-md bg-gradient-to-br from-amber-400 to-orange-500 flex items-center justify-center shadow-sm shadow-amber-400/30'>
<Zap size={11} className='text-white' />
</div>
</div>
<div className='flex-1 min-w-0'>
<p className='text-xs font-medium text-gray-800 dark:text-gray-200 leading-relaxed'>
{t('创建密钥后不知道怎么用?')}
</p>
<p className='text-[11px] text-brand-deep dark:text-brand-from font-medium mt-0.5 flex items-center gap-1'>
<Sparkles
size={10}
className='text-amber-500 flex-shrink-0'
/>
{t('扫码咨询获取帮助')}
<ArrowRight
size={10}
className='animate-bounce-x flex-shrink-0'
style={{
animation: 'bounce-x 1.5s ease-in-out infinite',
}}
/>
</p>
</div>
</div>
</div>
<style>{`
@keyframes bounce-x {
0%, 100% { transform: translateX(0); }
50% { transform: translateX(3px); }
}
`}</style>
</div>
{/* 二维码或联系方式 */}
{agent.qrcode_url && !qrcodeError ? (
<div className='flex flex-col items-center'>
{/* 二维码 */}
<div className='relative w-full group/qr'>
<div className='absolute -inset-0.5 bg-gradient-to-br from-brand-from/20 via-brand-via/20 to-pink-400/20 rounded-xl blur opacity-0 group-hover/qr:opacity-100 transition-opacity duration-300' />
<div className='relative w-full p-3 bg-white dark:bg-neutral-800/50 rounded-xl border border-gray-100 dark:border-neutral-700/50 shadow-inner'>
<img
src={agent.qrcode_url}
alt={t('客服二维码')}
className='w-full aspect-square object-contain rounded-lg'
onError={handleQrcodeError}
/>
</div>
</div>
<div className='mt-2.5 flex items-center gap-1.5 px-3 py-1.5 bg-gradient-to-r from-brand-from/5 to-brand-to/5 dark:from-brand-from/20 dark:to-brand-to/20 rounded-full'>
<ScanLine
size={12}
className='text-brand dark:text-brand-from'
/>
<span className='text-xs font-medium text-brand-deep dark:text-brand-from'>
{t('微信扫码添加客服')}
</span>
</div>
{/* 备用企业微信按钮 */}
{agent.wecom_url && (
<Button
theme='light'
type='tertiary'
size='small'
icon={<MessageCircle size={12} />}
onClick={() => window.open(agent.wecom_url, '_blank')}
className='w-full !rounded-lg mt-2 !text-xs'
>
{t('或点击联系企微')}
</Button>
)}
</div>
) : agent.wecom_url ? (
/* 降级方案 */
<div className='text-center'>
{qrcodeError && (
<div className='mb-2 p-2 bg-amber-50 dark:bg-amber-900/20 rounded-lg'>
<p className='text-[11px] text-amber-600 dark:text-amber-400'>
{t('二维码加载失败,请点击下方按钮')}
</p>
</div>
)}
<Button
theme='solid'
type='primary'
icon={<MessageCircle size={14} />}
onClick={() => window.open(agent.wecom_url, '_blank')}
className='w-full !bg-gradient-to-r !from-brand-from !to-brand-to hover:!from-brand-from hover:!to-brand-to !rounded-xl'
>
{t('联系客服')}
</Button>
</div>
) : null}
</div>
</div>
</div>
);
};
export default CustomerServiceQRCard;