feat: 添加业务分析后端接口

新增 AnalysisController 提供 6 个分析接口:
- /admin/analysis/profit - 盈利分析(交易手续费/充提手续费/资金利差)
- /admin/analysis/cash-flow - 资金流动趋势(按月统计充值/提现/净流入)
- /admin/analysis/trade - 交易分析(买入/卖出统计+趋势)
- /admin/analysis/coin-distribution - 币种交易分布
- /admin/analysis/user-growth - 用户增长分析(新增/活跃用户)
- /admin/analysis/risk - 风险指标(大额交易/异常提现/KYC/冻结账户)
- /admin/analysis/health - 综合健康度评分

更新 Mapper 添加分析查询方法:
- OrderFundMapper: 手续费统计、时间范围查询、大额交易、异常提现
- OrderTradeMapper: 交易金额统计、活跃用户、币种分布

前端 API 对接:
- 新增 6 个分析相关 Query hooks
- 更新 analytics.vue 使用真实数据
- 动态决策建议基于实际数据
This commit is contained in:
2026-03-22 04:50:19 +08:00
parent 0e95890d68
commit c3f196ded4
23 changed files with 3520 additions and 1055 deletions

View File

@@ -1,12 +1,10 @@
import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:provider/provider.dart';
import '../../../core/constants/app_colors.dart';
import '../../../providers/asset_provider.dart';
import '../../../providers/auth_provider.dart';
import '../asset/asset_page.dart';
import '../trade/trade_page.dart';
/// 首页
/// 首页 - 使用 shadcn_ui 现代化设计
class HomePage extends StatefulWidget {
const HomePage({super.key});
@@ -35,13 +33,15 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
@override
Widget build(BuildContext context) {
super.build(context);
final theme = ShadTheme.of(context);
return Scaffold(
backgroundColor: AppColors.background,
backgroundColor: theme.colorScheme.background,
body: Consumer<AssetProvider>(
builder: (context, provider, _) {
return RefreshIndicator(
onRefresh: () => provider.refreshAll(),
color: AppColors.primary,
color: theme.colorScheme.primary,
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16),
@@ -66,6 +66,8 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
}
Widget _buildHeader() {
final theme = ShadTheme.of(context);
return Consumer<AuthProvider>(
builder: (context, auth, _) {
final user = auth.user;
@@ -73,11 +75,11 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
children: [
CircleAvatar(
radius: 20,
backgroundColor: AppColors.primary.withOpacity(0.2),
backgroundColor: theme.colorScheme.primary.withValues(alpha: 0.2),
child: Text(
user?.avatarText ?? 'U',
style: const TextStyle(
color: AppColors.primary,
style: TextStyle(
color: theme.colorScheme.primary,
fontWeight: FontWeight.bold,
),
),
@@ -89,37 +91,40 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
children: [
Text(
'你好,${user?.username ?? '用户'}',
style: const TextStyle(
fontSize: 16,
style: theme.textTheme.large.copyWith(
fontWeight: FontWeight.bold,
color: AppColors.textPrimary,
),
),
const SizedBox(height: 2),
const Text(
Text(
'欢迎来到模拟所',
style: TextStyle(
fontSize: 12,
color: AppColors.textSecondary,
),
style: theme.textTheme.muted,
),
],
),
),
],
);
).animate().fadeIn(duration: 300.ms).slideX(begin: -0.1, end: 0);
},
);
}
Widget _buildAssetCard(AssetProvider provider) {
final theme = ShadTheme.of(context);
final overview = provider.overview;
// 自定义渐变色
const gradientColors = [
Color(0xFF00D4AA),
Color(0xFF00B894),
];
return Container(
width: double.infinity,
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [AppColors.primary, AppColors.primaryDark],
colors: gradientColors,
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
@@ -128,20 +133,16 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
Text(
'总资产(USDT)',
style: TextStyle(
fontSize: 14,
color: Colors.white70,
),
style: theme.textTheme.small.copyWith(color: Colors.white70),
),
const SizedBox(height: 8),
Text(
overview?.totalAsset ?? '0.00',
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
style: theme.textTheme.h2.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
@@ -154,7 +155,7 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
),
],
),
);
).animate().fadeIn(duration: 400.ms).slideY(begin: 0.1, end: 0);
}
Widget _buildAssetItem(String label, String value) {
@@ -179,25 +180,48 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
}
Widget _buildQuickActions() {
return Container(
final theme = ShadTheme.of(context);
return ShadCard(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AppColors.cardBackground,
borderRadius: BorderRadius.circular(16),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildActionItem('', '充值', AppColors.success, () => _showDeposit()),
_buildActionItem('', '提现', AppColors.warning, () => _showWithdraw()),
_buildActionItem('', '划转', AppColors.primary, () => _showTransfer()),
_buildActionItem('', '交易', AppColors.info, () => _navigateToTrade()),
_buildActionItem(
icon: LucideIcons.arrowDownToLine,
text: '充值',
color: const Color(0xFF00C853),
onTap: () => _showDeposit(),
),
_buildActionItem(
icon: LucideIcons.arrowUpFromLine,
text: '提现',
color: const Color(0xFFFF9800),
onTap: () => _showWithdraw(),
),
_buildActionItem(
icon: LucideIcons.arrowRightLeft,
text: '划转',
color: theme.colorScheme.primary,
onTap: () => _showTransfer(),
),
_buildActionItem(
icon: LucideIcons.trendingUp,
text: '交易',
color: const Color(0xFF2196F3),
onTap: () => _navigateToTrade(),
),
],
),
);
).animate().fadeIn(duration: 500.ms, delay: 100.ms);
}
Widget _buildActionItem(String icon, String text, Color color, VoidCallback onTap) {
Widget _buildActionItem({
required IconData icon,
required String text,
required Color color,
required VoidCallback onTap,
}) {
return GestureDetector(
onTap: onTap,
child: Column(
@@ -209,23 +233,14 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
color: color.withOpacity(0.15),
shape: BoxShape.circle,
),
child: Center(
child: Text(
icon,
style: TextStyle(
fontSize: 18,
color: color,
fontWeight: FontWeight.bold,
),
),
),
child: Icon(icon, color: color, size: 22),
),
const SizedBox(height: 8),
Text(
text,
style: const TextStyle(
style: TextStyle(
fontSize: 12,
color: AppColors.textPrimary,
color: ShadTheme.of(context).colorScheme.foreground,
),
),
],
@@ -234,61 +249,51 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
}
Widget _buildHoldings(AssetProvider provider) {
final theme = ShadTheme.of(context);
final holdings = provider.holdings;
return Container(
return ShadCard(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: AppColors.cardBackground,
borderRadius: BorderRadius.circular(16),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Row(
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'我的持仓',
style: TextStyle(
fontSize: 16,
style: theme.textTheme.large.copyWith(
fontWeight: FontWeight.bold,
color: AppColors.textPrimary,
),
),
Icon(
Icons.chevron_right,
color: AppColors.textSecondary,
LucideIcons.chevronRight,
color: theme.colorScheme.mutedForeground,
size: 20,
),
],
),
const SizedBox(height: 16),
if (holdings.isEmpty)
const Center(
Center(
child: Padding(
padding: EdgeInsets.all(32),
padding: const EdgeInsets.all(32),
child: Column(
children: [
Icon(
Icons.account_balance_wallet_outlined,
LucideIcons.wallet,
size: 48,
color: AppColors.textHint,
color: theme.colorScheme.mutedForeground,
),
SizedBox(height: 12),
const SizedBox(height: 12),
Text(
'暂无持仓',
style: TextStyle(
color: AppColors.textSecondary,
fontSize: 14,
),
style: theme.textTheme.muted,
),
SizedBox(height: 4),
const SizedBox(height: 4),
Text(
'快去交易吧~',
style: TextStyle(
color: AppColors.textHint,
fontSize: 12,
),
style: theme.textTheme.muted.copyWith(fontSize: 12),
),
],
),
@@ -299,18 +304,27 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: holdings.length > 5 ? 5 : holdings.length,
separatorBuilder: (_, __) => const Divider(color: AppColors.border),
separatorBuilder: (_, __) => Divider(
color: theme.colorScheme.border,
height: 1,
),
itemBuilder: (context, index) {
final holding = holdings[index];
return _buildHoldingItem(holding);
return _buildHoldingItem(holding)
.animate()
.fadeIn(delay: Duration(milliseconds: 50 * index));
},
),
],
),
);
).animate().fadeIn(duration: 500.ms, delay: 200.ms);
}
Widget _buildHoldingItem(holding) {
final theme = ShadTheme.of(context);
final upColor = const Color(0xFF00C853);
final downColor = const Color(0xFFFF5252);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
@@ -318,20 +332,14 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
children: [
Row(
children: [
Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: AppColors.primary.withOpacity(0.1),
borderRadius: BorderRadius.circular(18),
),
child: Center(
child: Text(
holding.coinCode.substring(0, 1),
style: const TextStyle(
color: AppColors.primary,
fontWeight: FontWeight.bold,
),
CircleAvatar(
radius: 18,
backgroundColor: theme.colorScheme.primary.withValues(alpha: 0.1),
child: Text(
holding.coinCode.substring(0, 1),
style: TextStyle(
color: theme.colorScheme.primary,
fontWeight: FontWeight.bold,
),
),
),
@@ -341,18 +349,13 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
children: [
Text(
holding.coinCode,
style: const TextStyle(
fontSize: 16,
style: theme.textTheme.large.copyWith(
fontWeight: FontWeight.bold,
color: AppColors.textPrimary,
),
),
Text(
holding.quantity,
style: const TextStyle(
fontSize: 12,
color: AppColors.textSecondary,
),
style: theme.textTheme.muted,
),
],
),
@@ -363,15 +366,14 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
children: [
Text(
'${holding.currentValue} USDT',
style: const TextStyle(
color: AppColors.textPrimary,
style: theme.textTheme.small.copyWith(
fontWeight: FontWeight.w500,
),
),
Text(
holding.formattedProfitRate,
style: TextStyle(
color: holding.isProfit ? AppColors.up : AppColors.down,
color: holding.isProfit ? upColor : downColor,
fontSize: 12,
),
),
@@ -383,21 +385,18 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
}
void _showDeposit() {
// 显示充值弹窗
_showActionDialog('充值', '请输入充值金额(USDT)', (amount) {
context.read<AssetProvider>().deposit(amount: amount);
});
}
void _showWithdraw() {
// 显示提现弹窗
_showActionDialog('提现', '请输入提现金额(USDT)', (amount) {
context.read<AssetProvider>().withdraw(amount: amount);
});
}
void _showTransfer() {
// 显示划转弹窗
_showActionDialog('划转', '请输入划转金额(USDT)', (amount) {
context.read<AssetProvider>().transfer(direction: 1, amount: amount);
});
@@ -405,31 +404,44 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
void _showActionDialog(String title, String hint, Function(String) onSubmit) {
final controller = TextEditingController();
showDialog(
final formKey = GlobalKey<ShadFormState>();
showShadDialog(
context: context,
builder: (context) => AlertDialog(
backgroundColor: AppColors.cardBackground,
title: Text(title, style: const TextStyle(color: AppColors.textPrimary)),
content: TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
style: const TextStyle(color: AppColors.textPrimary),
decoration: InputDecoration(
hintText: hint,
hintStyle: const TextStyle(color: AppColors.textHint),
builder: (context) => ShadDialog(
title: Text(title),
child: ShadForm(
key: formKey,
child: ShadInputFormField(
id: 'amount',
placeholder: Text(hint),
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
validator: (value) {
if (value == null || value.isEmpty) {
return '请输入金额';
}
final amount = double.tryParse(value);
if (amount == null || amount <= 0) {
return '请输入有效金额';
}
return null;
},
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
ShadButton.outline(
child: const Text('取消'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
onPressed: () {
onSubmit(controller.text);
Navigator.pop(context);
},
ShadButton(
child: const Text('确认'),
onPressed: () {
if (formKey.currentState!.saveAndValidate()) {
onSubmit(controller.text);
Navigator.of(context).pop();
}
},
),
],
),
@@ -437,6 +449,6 @@ class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin
}
void _navigateToTrade() {
// 切换到交易页
// 切换到交易页 - 通过 MainController
}
}