/* 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 . For commercial licensing, please contact support@quantumnous.com */ import React, { useState, useEffect, useCallback } from 'react'; import { Card, Table, Button, Modal, Form, Select, Tag, Avatar, Popconfirm, Typography, Space, Banner, Spin, Upload, } from '@/components/ui'; import { Plus, Pencil, Trash2, Power, PowerOff, RefreshCw, Users, Upload as UploadIcon, } from 'lucide-react'; import { API, showError, showSuccess } from '../../helpers'; import { useTranslation } from 'react-i18next'; const { Text } = Typography; const CustomerServiceSetting = () => { const { t } = useTranslation(); const [agents, setAgents] = useState([]); const [loading, setLoading] = useState(false); const [modalVisible, setModalVisible] = useState(false); const [editingAgent, setEditingAgent] = useState(null); const [formApi, setFormApi] = useState(null); const [uploadingQrcode, setUploadingQrcode] = useState(false); const [uploadingAvatar, setUploadingAvatar] = useState(false); // 上传图片 const handleUpload = async (fileInfo, field) => { const setUploading = field === 'qrcode_url' ? setUploadingQrcode : setUploadingAvatar; setUploading(true); try { const formData = new FormData(); // Semi UI 的 customRequest 回调中,fileInfo.fileInstance 是原始 File 对象 const file = fileInfo.fileInstance || fileInfo.file; formData.append('file', file); const res = await API.post('/api/customer-service/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }); if (res.data.success && res.data.data?.url) { formApi?.setValue(field, res.data.data.url); showSuccess(t('上传成功')); fileInfo.onSuccess?.(); } else { showError(res.data.message || t('上传失败')); fileInfo.onError?.(); } } catch (err) { showError(t('上传失败')); fileInfo.onError?.(); } finally { setUploading(false); } }; // 工作日选项 const workDayOptions = [ { label: t('周一'), value: '1' }, { label: t('周二'), value: '2' }, { label: t('周三'), value: '3' }, { label: t('周四'), value: '4' }, { label: t('周五'), value: '5' }, { label: t('周六'), value: '6' }, { label: t('周日'), value: '7' }, ]; // 获取客服列表 const fetchAgents = useCallback(async () => { setLoading(true); try { const res = await API.get('/api/customer-service/'); if (res.data.success) { setAgents(res.data.data || []); } else { showError(res.data.message); } } catch (err) { showError(t('获取客服列表失败')); } finally { setLoading(false); } }, [t]); useEffect(() => { fetchAgents(); }, [fetchAgents]); // 打开新建/编辑弹窗 const openModal = (agent = null) => { setEditingAgent(agent); setModalVisible(true); }; // 关闭弹窗 const closeModal = () => { setModalVisible(false); setEditingAgent(null); if (formApi) { formApi.reset(); } }; // 提交表单 const handleSubmit = async () => { if (!formApi) return; try { await formApi.validate(); const values = formApi.getValues(); // 处理工作日 const workDays = Array.isArray(values.work_days) ? values.work_days.join(',') : values.work_days || '1,2,3,4,5'; // 处理工作时间(Form.Input 返回字符串,直接使用) const workStartTime = values.work_start_time || ''; const workEndTime = values.work_end_time || ''; const payload = { ...values, work_days: workDays, work_start_time: workStartTime, work_end_time: workEndTime, weight: values.weight || 100, max_sessions: values.max_sessions || 50, priority: values.priority || 0, status: values.status ?? 1, }; if (editingAgent) { payload.id = editingAgent.id; const res = await API.put('/api/customer-service/', payload); if (res.data.success) { showSuccess(t('更新成功')); closeModal(); fetchAgents(); } else { showError(res.data.message); } } else { const res = await API.post('/api/customer-service/', payload); if (res.data.success) { showSuccess(t('创建成功')); closeModal(); fetchAgents(); } else { showError(res.data.message); } } } catch (err) { // 表单验证失败或其他错误 if (err && err.message) { showError(err.message); } } }; // 删除客服 const handleDelete = async (id) => { try { const res = await API.delete(`/api/customer-service/${id}`); if (res.data.success) { showSuccess(t('删除成功')); fetchAgents(); } else { showError(res.data.message); } } catch (err) { showError(t('删除失败')); } }; // 切换状态 const handleToggleStatus = async (agent) => { const newStatus = agent.status === 1 ? 0 : 1; try { const res = await API.put(`/api/customer-service/${agent.id}/status`, { status: newStatus, }); if (res.data.success) { showSuccess(newStatus === 1 ? t('已上线') : t('已下线')); fetchAgents(); } else { showError(res.data.message); } } catch (err) { showError(t('操作失败')); } }; // 状态标签 const getStatusTag = (status) => { const statusMap = { 1: { text: t('在线'), color: 'green' }, 2: { text: t('忙碌'), color: 'orange' }, 0: { text: t('离线'), color: 'grey' }, }; const s = statusMap[status] || statusMap[0]; return {s.text}; }; // 表格列定义 const columns = [ { title: t('客服'), dataIndex: 'name', render: (text, record) => ( {text?.[0]?.toUpperCase()} {text} ), }, { title: t('状态'), dataIndex: 'status', render: (status) => getStatusTag(status), }, { title: t('接待数'), dataIndex: 'current_sessions', render: (current, record) => ( {current} / {record.max_sessions} ), }, { title: t('权重'), dataIndex: 'weight', }, { title: t('优先级'), dataIndex: 'priority', }, { title: t('工作时间'), dataIndex: 'work_start_time', render: (start, record) => { if (start && record.work_end_time) { return `${start} - ${record.work_end_time}`; } return t('全天'); }, }, { title: t('工作日'), dataIndex: 'work_days', render: (days) => { if (!days) return t('工作日'); const dayNames = { 1: t('一'), 2: t('二'), 3: t('三'), 4: t('四'), 5: t('五'), 6: t('六'), 7: t('日'), }; return days .split(',') .map((d) => dayNames[d] || d) .join('、'); }, }, { title: t('操作'), dataIndex: 'action', render: (_, record) => ( handleDelete(record.id)} > ), }, ]; // 初始表单值 const getInitialValues = () => { if (editingAgent) { return { ...editingAgent, work_days: editingAgent.work_days ? editingAgent.work_days.split(',') : ['1', '2', '3', '4', '5'], }; } return { status: 1, weight: 100, max_sessions: 50, priority: 0, work_days: ['1', '2', '3', '4', '5'], }; }; return (
{t('客服列表')} {agents.length} {t('个客服')}
{/* 新建/编辑弹窗 */}
setFormApi(api)} initValues={getInitialValues()} labelPosition='left' labelWidth={100} >
handleUpload(fileInfo, 'avatar')} >
handleUpload(fileInfo, 'qrcode_url') } >
{formApi?.getValue('qrcode_url') && ( {t('二维码预览')} )}
{t('在线')} {t('忙碌')} {t('离线')}
); }; export default CustomerServiceSetting;