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,9 +1,9 @@
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';
/// 资产页面
/// 资产页面 - 使用 shadcn_ui 现代化设计
class AssetPage extends StatefulWidget {
const AssetPage({super.key});
@@ -17,6 +17,10 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
int _activeTab = 0; // 0=资金账户, 1=交易账户
// 颜色常量
static const upColor = Color(0xFF00C853);
static const downColor = Color(0xFFFF5252);
@override
void initState() {
super.initState();
@@ -32,13 +36,15 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
@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),
@@ -61,13 +67,21 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
}
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(24),
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [AppColors.primary, AppColors.primaryDark],
colors: gradientColors,
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
@@ -75,15 +89,14 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
),
child: Column(
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: 36,
style: theme.textTheme.h1.copyWith(
fontWeight: FontWeight.bold,
color: Colors.white,
),
@@ -92,11 +105,15 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.trending_up, color: Colors.white70, size: 16),
Icon(
LucideIcons.trendingUp,
color: Colors.white70,
size: 16,
),
const SizedBox(width: 4),
Text(
'总盈亏: ${overview?.totalProfit ?? '0.00'} USDT',
style: const TextStyle(fontSize: 14, color: Colors.white70),
style: theme.textTheme.small.copyWith(color: Colors.white70),
),
],
),
@@ -106,10 +123,12 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
}
Widget _buildAccountTabs() {
final theme = ShadTheme.of(context);
return Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: AppColors.cardBackground,
color: theme.colorScheme.card,
borderRadius: BorderRadius.circular(12),
),
child: Row(
@@ -120,15 +139,21 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
child: Container(
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: _activeTab == 0 ? AppColors.primary : Colors.transparent,
color: _activeTab == 0
? theme.colorScheme.primary
: Colors.transparent,
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
'资金账户',
style: TextStyle(
color: _activeTab == 0 ? Colors.white : AppColors.textSecondary,
fontWeight: _activeTab == 0 ? FontWeight.w600 : FontWeight.normal,
color: _activeTab == 0
? Colors.white
: theme.colorScheme.mutedForeground,
fontWeight: _activeTab == 0
? FontWeight.w600
: FontWeight.normal,
),
),
),
@@ -141,15 +166,21 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
child: Container(
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: _activeTab == 1 ? AppColors.primary : Colors.transparent,
color: _activeTab == 1
? theme.colorScheme.primary
: Colors.transparent,
borderRadius: BorderRadius.circular(8),
),
child: Center(
child: Text(
'交易账户',
style: TextStyle(
color: _activeTab == 1 ? Colors.white : AppColors.textSecondary,
fontWeight: _activeTab == 1 ? FontWeight.w600 : FontWeight.normal,
color: _activeTab == 1
? Colors.white
: theme.colorScheme.mutedForeground,
fontWeight: _activeTab == 1
? FontWeight.w600
: FontWeight.normal,
),
),
),
@@ -162,99 +193,111 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
}
Widget _buildFundAccount(AssetProvider provider) {
final theme = ShadTheme.of(context);
final fund = provider.fundAccount;
return Column(
children: [
Container(
padding: const EdgeInsets.all(20),
decoration: BoxDecoration(
color: AppColors.cardBackground,
borderRadius: BorderRadius.circular(16),
return ShadCard(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'USDT余额',
style: theme.textTheme.muted,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
const SizedBox(height: 8),
Text(
fund?.balance ?? '0.00',
style: theme.textTheme.h2.copyWith(
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 24),
Row(
children: [
const Text(
'USDT余额',
style: TextStyle(fontSize: 14, color: AppColors.textSecondary),
),
const SizedBox(height: 8),
Text(
fund?.balance ?? '0.00',
style: const TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: AppColors.textPrimary,
Expanded(
child: ShadButton(
backgroundColor: const Color(0xFF00C853),
onPressed: () => _showDepositDialog(provider),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(LucideIcons.plus, size: 18, color: Colors.white),
const SizedBox(width: 4),
const Text('充值'),
],
),
),
),
const SizedBox(height: 24),
Row(
children: [
Expanded(
child: ElevatedButton.icon(
onPressed: () => _showDepositDialog(provider),
icon: const Icon(Icons.add, size: 18),
label: const Text('充值'),
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.success,
),
),
const SizedBox(width: 12),
Expanded(
child: ShadButton(
backgroundColor: const Color(0xFFFF9800),
onPressed: () => _showWithdrawDialog(provider),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(LucideIcons.minus, size: 18, color: Colors.white),
const SizedBox(width: 4),
const Text('提现'),
],
),
const SizedBox(width: 12),
Expanded(
child: ElevatedButton.icon(
onPressed: () => _showWithdrawDialog(provider),
icon: const Icon(Icons.remove, size: 18),
label: const Text('提现'),
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.warning,
),
),
),
),
const SizedBox(width: 12),
Expanded(
child: ShadButton.outline(
onPressed: () => _showTransferDialog(provider),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(LucideIcons.arrowRightLeft, size: 18),
const SizedBox(width: 4),
const Text('划转'),
],
),
const SizedBox(width: 12),
Expanded(
child: ElevatedButton.icon(
onPressed: () => _showTransferDialog(provider),
icon: const Icon(Icons.swap_horiz, size: 18),
label: const Text('划转'),
),
),
],
),
),
],
),
),
],
],
),
);
}
Widget _buildTradeAccount(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 Text(
Text(
'持仓列表',
style: TextStyle(
fontSize: 16,
style: theme.textTheme.large.copyWith(
fontWeight: FontWeight.bold,
color: AppColors.textPrimary,
),
),
const SizedBox(height: 16),
if (holdings.isEmpty)
const Center(
Center(
child: Padding(
padding: EdgeInsets.all(32),
child: Text(
'暂无持仓',
style: TextStyle(color: AppColors.textSecondary),
padding: const EdgeInsets.all(32),
child: Column(
children: [
Icon(
LucideIcons.wallet,
size: 48,
color: theme.colorScheme.mutedForeground,
),
const SizedBox(height: 12),
Text(
'暂无持仓',
style: theme.textTheme.muted,
),
],
),
),
)
@@ -263,47 +306,13 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: holdings.length,
separatorBuilder: (_, __) => const Divider(color: AppColors.border),
separatorBuilder: (_, __) => Divider(
color: theme.colorScheme.border,
height: 1,
),
itemBuilder: (context, index) {
final holding = holdings[index];
return ListTile(
contentPadding: EdgeInsets.zero,
leading: CircleAvatar(
backgroundColor: AppColors.primary.withOpacity(0.1),
child: Text(
holding.coinCode.substring(0, 1),
style: const TextStyle(color: AppColors.primary),
),
),
title: Text(
holding.coinCode,
style: const TextStyle(
color: AppColors.textPrimary,
fontWeight: FontWeight.w600,
),
),
subtitle: Text(
'数量: ${holding.quantity}',
style: const TextStyle(color: AppColors.textSecondary, fontSize: 12),
),
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'${holding.currentValue} USDT',
style: const TextStyle(color: AppColors.textPrimary),
),
Text(
holding.formattedProfitRate,
style: TextStyle(
color: holding.isProfit ? AppColors.up : AppColors.down,
fontSize: 12,
),
),
],
),
);
return _buildHoldingItem(holding);
},
),
],
@@ -311,145 +320,253 @@ class _AssetPageState extends State<AssetPage> with AutomaticKeepAliveClientMixi
);
}
Widget _buildHoldingItem(holding) {
final theme = ShadTheme.of(context);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
CircleAvatar(
radius: 20,
backgroundColor: theme.colorScheme.primary.withValues(alpha: 0.1),
child: Text(
holding.coinCode.substring(0, 1),
style: TextStyle(
color: theme.colorScheme.primary,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
holding.coinCode,
style: theme.textTheme.large.copyWith(
fontWeight: FontWeight.w600,
),
),
Text(
'数量: ${holding.quantity}',
style: theme.textTheme.muted.copyWith(fontSize: 12),
),
],
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'${holding.currentValue} USDT',
style: theme.textTheme.small,
),
Text(
holding.formattedProfitRate,
style: TextStyle(
color: holding.isProfit ? upColor : downColor,
fontSize: 12,
),
),
],
),
],
),
);
}
void _showDepositDialog(AssetProvider provider) {
final controller = TextEditingController();
showDialog(
context: context,
builder: (context) => AlertDialog(
backgroundColor: AppColors.cardBackground,
title: const Text('充值', style: TextStyle(color: AppColors.textPrimary)),
content: TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
style: const TextStyle(color: AppColors.textPrimary),
decoration: const InputDecoration(
hintText: '请输入充值金额(USDT)',
hintStyle: TextStyle(color: AppColors.textHint),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
ElevatedButton(
onPressed: () async {
Navigator.pop(context);
final response = await provider.deposit(amount: controller.text);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(response.message ?? (response.success ? '申请成功' : '申请失败'))),
);
}
},
child: const Text('确认'),
),
],
),
_showActionDialog(
title: '充值',
hint: '请输入充值金额(USDT)',
onSubmit: (amount) async {
final response = await provider.deposit(amount: amount);
if (mounted) {
_showResult(response.success ? '申请成功' : '申请失败', response.message);
}
},
);
}
void _showWithdrawDialog(AssetProvider provider) {
final controller = TextEditingController();
showDialog(
context: context,
builder: (context) => AlertDialog(
backgroundColor: AppColors.cardBackground,
title: const Text('提现', style: TextStyle(color: AppColors.textPrimary)),
content: TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
style: const TextStyle(color: AppColors.textPrimary),
decoration: const InputDecoration(
hintText: '请输入提现金额(USDT)',
hintStyle: TextStyle(color: AppColors.textHint),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('取消'),
),
ElevatedButton(
onPressed: () async {
Navigator.pop(context);
final response = await provider.withdraw(amount: controller.text);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(response.message ?? (response.success ? '申请成功' : '申请失败'))),
);
}
},
child: const Text('确认'),
),
],
),
_showActionDialog(
title: '提现',
hint: '请输入提现金额(USDT)',
onSubmit: (amount) async {
final response = await provider.withdraw(amount: amount);
if (mounted) {
_showResult(response.success ? '申请成功' : '申请失败', response.message);
}
},
);
}
void _showTransferDialog(AssetProvider provider) {
final controller = TextEditingController();
int direction = 1; // 1=资金转交易, 2=交易转资金
showDialog(
final formKey = GlobalKey<ShadFormState>();
int direction = 1;
showShadDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setState) => AlertDialog(
backgroundColor: AppColors.cardBackground,
title: const Text('划转', style: TextStyle(color: AppColors.textPrimary)),
content: Column(
builder: (context, setState) => ShadDialog(
title: const Text('划转'),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ChoiceChip(
label: const Text('资金→交易'),
selected: direction == 1,
onSelected: (v) => setState(() => direction = 1),
ShadButton.outline(
size: ShadButtonSize.sm,
onPressed: () => setState(() => direction = 1),
child: Row(
children: [
if (direction == 1)
Icon(LucideIcons.check, size: 14)
else
const SizedBox(width: 14),
const SizedBox(width: 4),
const Text('资金→交易'),
],
),
),
const SizedBox(width: 8),
ChoiceChip(
label: const Text('交易→资金'),
selected: direction == 2,
onSelected: (v) => setState(() => direction = 2),
ShadButton.outline(
size: ShadButtonSize.sm,
onPressed: () => setState(() => direction = 2),
child: Row(
children: [
if (direction == 2)
Icon(LucideIcons.check, size: 14)
else
const SizedBox(width: 14),
const SizedBox(width: 4),
const Text('交易→资金'),
],
),
),
],
),
const SizedBox(height: 16),
TextField(
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
style: const TextStyle(color: AppColors.textPrimary),
decoration: const InputDecoration(
hintText: '请输入划转金额(USDT)',
hintStyle: TextStyle(color: AppColors.textHint),
ShadForm(
key: formKey,
child: ShadInputFormField(
id: 'amount',
controller: controller,
placeholder: const Text('请输入划转金额(USDT)'),
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(),
),
ElevatedButton(
ShadButton(
child: const Text('确认'),
onPressed: () async {
Navigator.pop(context);
final response = await provider.transfer(
direction: direction,
amount: controller.text,
);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(response.message ?? (response.success ? '划转成功' : '划转失败'))),
if (formKey.currentState!.saveAndValidate()) {
Navigator.of(context).pop();
final response = await provider.transfer(
direction: direction,
amount: controller.text,
);
if (mounted) {
_showResult(
response.success ? '划转成功' : '划转失败',
response.message,
);
}
}
},
child: const Text('确认'),
),
],
),
),
);
}
void _showActionDialog({
required String title,
required String hint,
required Function(String) onSubmit,
}) {
final controller = TextEditingController();
final formKey = GlobalKey<ShadFormState>();
showShadDialog(
context: context,
builder: (context) => ShadDialog(
title: Text(title),
child: ShadForm(
key: formKey,
child: ShadInputFormField(
id: 'amount',
controller: controller,
placeholder: Text(hint),
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: [
ShadButton.outline(
child: const Text('取消'),
onPressed: () => Navigator.of(context).pop(),
),
ShadButton(
child: const Text('确认'),
onPressed: () async {
if (formKey.currentState!.saveAndValidate()) {
Navigator.of(context).pop();
onSubmit(controller.text);
}
},
),
],
),
);
}
void _showResult(String title, String? message) {
final theme = ShadTheme.of(context);
showShadDialog(
context: context,
builder: (context) => ShadDialog.alert(
title: Text(title),
description: message != null ? Text(message) : null,
actions: [
ShadButton(
child: const Text('确定'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
}
}