Files
monkey-ai-web/src/components/analytics/UserSurveyStats.jsx
T
qiubaishen 6160a23117 sync: 同步总仓库 luoguang/monkey-ai weiyuan@bfdfb339 的 web/ 改动
同步基准:总仓库 a2ca13cb(= 本仓库初始导入 d8f3aee2)
范围:a2ca13cb..bfdfb339 中触及 web/ 的提交(不含 web/.claude/settings.local.json)

- 8490ac02 feat(aliyun): integrate Wan 3 and Qwen Image models
- 2aff08ec perf(analytics): 运营看板概览改读汇总表,首屏从 19s 降到 0.4s
- dc69335e feat(experience): 体验中心图像/视频创作升级(能力 schema、估价、新创作台)
- b059c657 fix(org): 下线 /org/create 自助加入企业页与 POST /api/org/join
- 4fcf47e6 fix(i18n): 修复中/英/韩/繁体多语言错乱,补齐日/越/泰译文
- e4c301d5 fix: merge enterprise task record date filters
- c9369b95 feat: expose enterprise image and video task records
- a410fee5 feat(claude): 无断点请求自动注入 cache_control 断点(方案 2)
- 521b3376 feat(claude): 缓存命中率优化——上游粘性路由 + 缓存时长强制改写
- 73b615dd feat(pricing): 企业成员不论登录身份均按所属企业等级计价与扣费
- f73f55c3 feat(client): 新增 WeyAi 版本说明管理与公开读取接口
- 8cdf1994 fix(topup): reset amount when switching from PayPal
- 93a4ac34 feat(baidu): configure Seedance liveness header by channel
- cb1818c5 fix(weyai): 按评审修正内容清理竞态、默认适用端与中文退回(M21)
- 04214d83 feat(weyai): WeyAi 内容管理与客户端内容接口(M21 第二部分)
- e3c4a57c feat(weyai): 设置页新增 WeyAi 页签与内容专用 COS 桶(M21 第一部分)
- b5ee8e70 fix(auth): 会话角色/状态以 DB 为准,修复提升管理员后管理接口报权限不足
- e4618fc2 fix: 修复企业看板日志聚合死循环导致 CPU 打满,并降低看板查询开销
- 584aad52 style(web): 个人中心侧边栏按 prettier 换行
- b8d87064 fix: 小程序教程隐藏充值后保留原步骤编号,企业充值判断直接用 isMiniProgramWebView
- eee7f5b2 fix: 小程序里企业首页不落到充值页,隐藏钱包入口与教程充值步骤
- 8b254d49 fix: 小程序里隐藏企业充值入口与页面
- a67661b4 fix(web): 小程序里个人中心默认页跳过充值和发票,避免来回跳转
- cff6c543 feat(web): WeyAi 小程序里打开个人中心时不出现充值和开票
- c6c99eb6 feat(billing): Claude 全部缓存写入统一按 1h 倍率计费
- 0699d1bf fix(web): 小程序 web-view 里注册页同样只显示站内方式
- 04853331 feat(web): WeyAi 小程序 web-view 里登录页只显示站内登录方式
2026-11-10 11:05:34 +08:00

586 lines
22 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, { useMemo, useState, useEffect, useCallback } from 'react';
import { Card, Spin, Tag, Progress, Empty } from '@douyinfe/semi-ui';
import { VChart } from '@visactor/react-vchart';
import { API, showError } from '../../helpers';
import {
UserCheck,
Briefcase,
Cpu,
Users,
TrendingUp,
BarChart3,
PieChart,
ChevronDown,
ChevronUp,
} from 'lucide-react';
// 职业类型标签配置
const JOB_TYPE_CONFIG = {
1: { label: '技术岗位', color: 'blue', icon: '💻', desc: '开发/测试/运维等' },
2: {
label: '技术周边',
color: 'green',
icon: '🎨',
desc: '产品/设计/运营等',
},
3: { label: '其他行业', color: 'orange', icon: '🏢', desc: '非IT行业' },
};
// AI工具使用标签配置
const AI_TOOL_CONFIG = {
1: { label: '正在使用', color: 'green', icon: '🚀', desc: '活跃用户' },
2: { label: '了解/尝试过', color: 'blue', icon: '🔍', desc: '潜在用户' },
3: { label: '有兴趣', color: 'orange', icon: '✨', desc: '待转化' },
4: { label: '暂不需要', color: 'grey', icon: '💤', desc: '观望用户' },
};
const UserSurveyStats = ({ title, icon, t }) => {
const [loading, setLoading] = useState(false);
const [surveyStats, setSurveyStats] = useState(null);
const [surveyTrend, setSurveyTrend] = useState([]);
const [collapsed, setCollapsed] = useState(true); // 默认折叠
// 获取统计数据
const fetchSurveyStats = useCallback(async () => {
try {
const res = await API.get('/api/analytics/survey/stats');
if (res.data.success) {
setSurveyStats(res.data.data);
} else {
showError(res.data.message);
}
} catch (error) {
console.error('获取问卷统计失败:', error);
}
}, []);
// 获取趋势数据
const fetchSurveyTrend = useCallback(async () => {
try {
const res = await API.get('/api/analytics/survey/trend');
if (res.data.success) {
setSurveyTrend(res.data.data || []);
}
} catch (error) {
console.error('获取问卷趋势失败:', error);
}
}, []);
// 初始加载
useEffect(() => {
const loadData = async () => {
setLoading(true);
await Promise.all([fetchSurveyStats(), fetchSurveyTrend()]);
setLoading(false);
};
loadData();
}, [fetchSurveyStats, fetchSurveyTrend]);
// 职业分布饼图配置
const jobTypePieSpec = useMemo(() => {
if (!surveyStats?.job_type_stats || surveyStats.job_type_stats.length === 0)
return null;
const chartData = surveyStats.job_type_stats.map((item) => ({
name: t(JOB_TYPE_CONFIG[item.job_type]?.label || '未知'),
value: item.count,
percentage: item.percentage,
}));
return {
type: 'pie',
data: [{ id: 'jobType', values: chartData }],
valueField: 'value',
categoryField: 'name',
outerRadius: 0.8,
innerRadius: 0.5,
padAngle: 0.02,
pie: {
style: {
cornerRadius: 4,
},
},
tooltip: {
mark: {
content: [
{
key: (datum) => datum?.name,
value: (datum) => t('{{n}} 人', { n: datum?.value || 0, count: datum?.value || 0 }),
},
{
key: () => t('占比'),
value: (datum) => `${(datum?.percentage || 0).toFixed(1)}%`,
},
],
},
},
legends: {
visible: true,
position: 'bottom',
orient: 'horizontal',
},
label: {
visible: true,
position: 'outside',
content: '{name}',
},
color: ['#3b82f6', '#10b981', '#f59e0b'],
};
}, [surveyStats, t]);
// AI工具使用饼图配置
const aiToolPieSpec = useMemo(() => {
if (!surveyStats?.ai_tool_stats || surveyStats.ai_tool_stats.length === 0)
return null;
const chartData = surveyStats.ai_tool_stats.map((item) => ({
name: t(AI_TOOL_CONFIG[item.ai_tool_usage]?.label || '未知'),
value: item.count,
percentage: item.percentage,
}));
return {
type: 'pie',
data: [{ id: 'aiTool', values: chartData }],
valueField: 'value',
categoryField: 'name',
outerRadius: 0.8,
innerRadius: 0.5,
padAngle: 0.02,
pie: {
style: {
cornerRadius: 4,
},
},
tooltip: {
mark: {
content: [
{
key: (datum) => datum?.name,
value: (datum) => t('{{n}} 人', { n: datum?.value || 0, count: datum?.value || 0 }),
},
{
key: () => t('占比'),
value: (datum) => `${(datum?.percentage || 0).toFixed(1)}%`,
},
],
},
},
legends: {
visible: true,
position: 'bottom',
orient: 'horizontal',
},
label: {
visible: true,
position: 'outside',
content: '{name}',
},
color: ['#10b981', '#3b82f6', '#f59e0b', '#9ca3af'],
};
}, [surveyStats, t]);
// 趋势图配置
const trendSpec = useMemo(() => {
if (!surveyTrend || surveyTrend.length === 0) return null;
const chartData = [...surveyTrend].reverse().map((item) => ({
date: item.date,
count: item.count,
}));
return {
type: 'bar',
data: [{ id: 'trend', values: chartData }],
xField: 'date',
yField: 'count',
bar: {
style: {
cornerRadius: [4, 4, 0, 0],
fill: {
gradient: 'linear',
x0: 0,
y0: 0,
x1: 0,
y1: 1,
stops: [
{ offset: 0, color: '#8b5cf6' },
{ offset: 1, color: '#6366f1' },
],
},
},
},
tooltip: {
mark: {
content: [
{
key: () => t('日期'),
value: (datum) => datum?.date,
},
{
key: () => t('新增'),
value: (datum) => t('{{n}} 人', { n: datum?.count || 0, count: datum?.count || 0 }),
},
],
},
},
axes: [
{
orient: 'bottom',
label: {
autoRotate: true,
autoRotateAngle: [0, 45],
},
},
{
orient: 'left',
label: {
formatMethod: (v) => Math.round(v),
},
},
],
};
}, [surveyTrend, t]);
// 计算统计数据
const totalUsers = surveyStats?.total || 0;
const techUsers =
surveyStats?.job_type_stats?.find((s) => s.job_type === 1)?.count || 0;
const activeAIUsers =
surveyStats?.ai_tool_stats?.find((s) => s.ai_tool_usage === 1)?.count || 0;
const interestedUsers =
surveyStats?.ai_tool_stats?.find((s) => s.ai_tool_usage === 3)?.count || 0;
return (
<Card
className='shadow-sm hover:shadow-md transition-shadow'
title={
<div className='flex items-center justify-between w-full'>
<div className='flex items-center gap-2'>
<span className='text-brand'>{icon}</span>
<span className='font-semibold'>{title}</span>
{totalUsers > 0 && (
<Tag color='purple' size='small'>
{t('{{n}} 人已填写', { n: totalUsers, count: totalUsers })}
</Tag>
)}
</div>
<button
onClick={() => setCollapsed(!collapsed)}
className='flex items-center gap-1 px-2 py-1 text-sm text-gray-500 hover:text-brand-deep hover:bg-brand/5 dark:hover:bg-brand-deep/20 rounded transition-colors'
>
<span className='text-xs'>{collapsed ? t('展开') : t('收起')}</span>
{collapsed ? <ChevronDown size={16} /> : <ChevronUp size={16} />}
</button>
</div>
}
bodyStyle={{ padding: collapsed ? '0' : '16px' }}
>
{!collapsed && (
<Spin spinning={loading}>
{totalUsers === 0 ? (
<Empty
image={<UserCheck className='w-16 h-16 text-gray-300' />}
description={t('暂无问卷数据')}
style={{ padding: '40px 0' }}
/>
) : (
<div className='space-y-6'>
{/* 概览统计卡片 */}
<div className='grid grid-cols-2 sm:grid-cols-4 gap-4'>
<div className='p-4 bg-gradient-to-br from-brand-from/5 to-brand-to/5 dark:from-brand-from/20 dark:to-brand-to/20 rounded-xl'>
<div className='flex items-center gap-2 mb-2'>
<Users className='w-4 h-4 text-brand' />
<span className='text-xs text-gray-500 dark:text-gray-400'>
{t('总填写')}
</span>
</div>
<div className='text-2xl font-bold text-brand-deep dark:text-brand-from'>
{totalUsers}
</div>
</div>
<div className='p-4 bg-gradient-to-br from-blue-50 to-cyan-50 dark:from-blue-900/20 dark:to-cyan-900/20 rounded-xl'>
<div className='flex items-center gap-2 mb-2'>
<Briefcase className='w-4 h-4 text-blue-500' />
<span className='text-xs text-gray-500 dark:text-gray-400'>
{t('技术岗位')}
</span>
</div>
<div className='text-2xl font-bold text-blue-600 dark:text-blue-400'>
{techUsers}
</div>
<div className='text-xs text-gray-400 mt-1'>
{totalUsers > 0
? ((techUsers / totalUsers) * 100).toFixed(1)
: 0}
%
</div>
</div>
<div className='p-4 bg-gradient-to-br from-green-50 to-emerald-50 dark:from-green-900/20 dark:to-emerald-900/20 rounded-xl'>
<div className='flex items-center gap-2 mb-2'>
<Cpu className='w-4 h-4 text-green-500' />
<span className='text-xs text-gray-500 dark:text-gray-400'>
{t('正在使用AI')}
</span>
</div>
<div className='text-2xl font-bold text-green-600 dark:text-green-400'>
{activeAIUsers}
</div>
<div className='text-xs text-gray-400 mt-1'>
{totalUsers > 0
? ((activeAIUsers / totalUsers) * 100).toFixed(1)
: 0}
%
</div>
</div>
<div className='p-4 bg-gradient-to-br from-orange-50 to-amber-50 dark:from-orange-900/20 dark:to-amber-900/20 rounded-xl'>
<div className='flex items-center gap-2 mb-2'>
<TrendingUp className='w-4 h-4 text-orange-500' />
<span className='text-xs text-gray-500 dark:text-gray-400'>
{t('有兴趣尝试')}
</span>
</div>
<div className='text-2xl font-bold text-orange-600 dark:text-orange-400'>
{interestedUsers}
</div>
<div className='text-xs text-gray-400 mt-1'>
{totalUsers > 0
? ((interestedUsers / totalUsers) * 100).toFixed(1)
: 0}
%
</div>
</div>
</div>
{/* 图表区域 */}
<div className='grid grid-cols-1 lg:grid-cols-2 gap-6'>
{/* 职业分布 */}
<div className='bg-gray-50 dark:bg-gray-800/50 rounded-xl p-4'>
<div className='flex items-center gap-2 mb-4'>
<PieChart className='w-4 h-4 text-blue-500' />
<span className='font-medium text-gray-700 dark:text-gray-300'>
{t('职业分布')}
</span>
</div>
{jobTypePieSpec ? (
<>
<VChart
spec={jobTypePieSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 180 }}
/>
<div className='mt-3 space-y-2'>
{surveyStats?.job_type_stats?.map((item) => {
const config = JOB_TYPE_CONFIG[item.job_type];
return (
<div
key={item.job_type}
className='flex items-center justify-between'
>
<div className='flex items-center gap-2'>
<span className='text-lg'>{config?.icon}</span>
<span className='text-sm text-gray-600 dark:text-gray-400'>
{t(config?.label || '')}
</span>
</div>
<div className='flex items-center gap-2'>
<Progress
percent={item.percentage}
size='small'
style={{ width: 60 }}
showInfo={false}
stroke={
config?.color === 'blue'
? '#3b82f6'
: config?.color === 'green'
? '#10b981'
: '#f59e0b'
}
/>
<span className='text-sm font-medium w-12 text-right'>
{item.count}
</span>
</div>
</div>
);
})}
</div>
</>
) : (
<div className='h-[200px] flex items-center justify-center text-gray-400'>
{t('暂无数据')}
</div>
)}
</div>
{/* AI工具使用情况 */}
<div className='bg-gray-50 dark:bg-gray-800/50 rounded-xl p-4'>
<div className='flex items-center gap-2 mb-4'>
<PieChart className='w-4 h-4 text-green-500' />
<span className='font-medium text-gray-700 dark:text-gray-300'>
{t('AI工具使用情况')}
</span>
</div>
{aiToolPieSpec ? (
<>
<VChart
spec={aiToolPieSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 180 }}
/>
<div className='mt-3 space-y-2'>
{surveyStats?.ai_tool_stats?.map((item) => {
const config = AI_TOOL_CONFIG[item.ai_tool_usage];
const colors = {
green: '#10b981',
blue: '#3b82f6',
orange: '#f59e0b',
grey: '#9ca3af',
};
return (
<div
key={item.ai_tool_usage}
className='flex items-center justify-between'
>
<div className='flex items-center gap-2'>
<span className='text-lg'>{config?.icon}</span>
<span className='text-sm text-gray-600 dark:text-gray-400'>
{t(config?.label || '')}
</span>
</div>
<div className='flex items-center gap-2'>
<Progress
percent={item.percentage}
size='small'
style={{ width: 60 }}
showInfo={false}
stroke={colors[config?.color] || '#9ca3af'}
/>
<span className='text-sm font-medium w-12 text-right'>
{item.count}
</span>
</div>
</div>
);
})}
</div>
</>
) : (
<div className='h-[200px] flex items-center justify-center text-gray-400'>
{t('暂无数据')}
</div>
)}
</div>
</div>
{/* 填写趋势 */}
<div className='bg-gray-50 dark:bg-gray-800/50 rounded-xl p-4'>
<div className='flex items-center gap-2 mb-4'>
<BarChart3 className='w-4 h-4 text-brand' />
<span className='font-medium text-gray-700 dark:text-gray-300'>
{t('近30天填写趋势')}
</span>
</div>
{trendSpec ? (
<VChart
spec={trendSpec}
option={{ mode: 'desktop-browser' }}
style={{ height: 200 }}
/>
) : (
<div className='h-[200px] flex items-center justify-center text-gray-400'>
{t('暂无数据')}
</div>
)}
</div>
{/* 交叉分析表格 */}
{surveyStats?.cross_analysis &&
surveyStats.cross_analysis.length > 0 && (
<div className='bg-gray-50 dark:bg-gray-800/50 rounded-xl p-4'>
<div className='flex items-center gap-2 mb-4'>
<BarChart3 className='w-4 h-4 text-brand' />
<span className='font-medium text-gray-700 dark:text-gray-300'>
{t('交叉分析')}
</span>
</div>
<div className='overflow-x-auto'>
<table className='w-full text-sm'>
<thead>
<tr className='border-b border-gray-200 dark:border-gray-700'>
<th className='text-left py-2 px-3 font-medium text-gray-600 dark:text-gray-400'>
{t('职业类型')}
</th>
<th className='text-left py-2 px-3 font-medium text-gray-600 dark:text-gray-400'>
{t('AI工具使用')}
</th>
<th className='text-right py-2 px-3 font-medium text-gray-600 dark:text-gray-400'>
{t('人数')}
</th>
</tr>
</thead>
<tbody>
{surveyStats.cross_analysis.map((item, index) => (
<tr
key={index}
className='border-b border-gray-100 dark:border-gray-700/50 last:border-0'
>
<td className='py-2 px-3'>
<div className='flex items-center gap-2'>
<span>
{JOB_TYPE_CONFIG[item.job_type]?.icon}
</span>
<span className='text-gray-700 dark:text-gray-300'>
{t(JOB_TYPE_CONFIG[item.job_type]?.label || '')}
</span>
</div>
</td>
<td className='py-2 px-3'>
<Tag
color={
AI_TOOL_CONFIG[item.ai_tool_usage]?.color
}
size='small'
>
{t(AI_TOOL_CONFIG[item.ai_tool_usage]?.label || '')}
</Tag>
</td>
<td className='py-2 px-3 text-right font-medium'>
{item.count}
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
</div>
)}
</Spin>
)}
</Card>
);
};
export default UserSurveyStats;