| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- import 'dart:convert';
- import 'package:archive/archive.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:dio/dio.dart' as dio;
- import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';
- import '../../app/constants/keys.dart';
- import '../crypto.dart';
- import '../log/logger.dart';
- import 'simple_log_card.dart';
- import 'simple_api_card.dart';
- /// 简化版开发者工具 - 只包含控制台日志和API监控
- class IXDeveloperTools {
- static final IXDeveloperTools _instance = IXDeveloperTools._internal();
- factory IXDeveloperTools() => _instance;
- IXDeveloperTools._internal();
- final List<ConsoleLogEntry> _logs = [];
- final List<ApiRequestInfo> _apiRequests = [];
- static const int maxLogs = 1000;
- static const int maxApiRequests = 500;
- /// 添加控制台日志
- static void addLog(String message, String tag) {
- final entry = ConsoleLogEntry(
- timestamp: DateTime.now(),
- message: message,
- tag: tag,
- );
- _instance._logs.insert(0, entry);
- if (_instance._logs.length > maxLogs) {
- _instance._logs.removeRange(maxLogs, _instance._logs.length);
- }
- }
- /// 添加API请求信息
- static void addApiRequest(ApiRequestInfo request) {
- _instance._apiRequests.insert(0, request);
- if (_instance._apiRequests.length > maxApiRequests) {
- _instance._apiRequests.removeRange(
- maxApiRequests,
- _instance._apiRequests.length,
- );
- }
- }
- /// 获取所有日志
- static List<ConsoleLogEntry> get logs => List.unmodifiable(_instance._logs);
- /// 获取所有API请求
- static List<ApiRequestInfo> get apiRequests =>
- List.unmodifiable(_instance._apiRequests);
- /// 清除所有日志
- static void clearLogs() {
- _instance._logs.clear();
- }
- /// 清除所有API请求
- static void clearApiRequests() {
- _instance._apiRequests.clear();
- }
- /// 显示开发者工具
- static void show() {
- try {
- Get.to(
- () => const SimpleDeveloperToolsScreen(),
- transition: Transition.cupertino,
- );
- } catch (e) {
- log('IXDeveloperTools', 'Unable to open Developer Tools: $e');
- }
- }
- }
- /// 控制台日志条目
- class ConsoleLogEntry {
- final DateTime timestamp;
- final String message;
- final String tag;
- ConsoleLogEntry({
- required this.timestamp,
- required this.message,
- required this.tag,
- });
- String get formattedTime {
- return '${timestamp.hour.toString().padLeft(2, '0')}:'
- '${timestamp.minute.toString().padLeft(2, '0')}:'
- '${timestamp.second.toString().padLeft(2, '0')}.'
- '${timestamp.millisecond.toString().padLeft(3, '0')}';
- }
- }
- /// API请求信息
- class ApiRequestInfo {
- final String id;
- final DateTime timestamp;
- final String method;
- final String url;
- final Map<String, dynamic>? headers;
- final dynamic requestData;
- final int? statusCode;
- final dynamic responseData;
- final String? error;
- final Duration? duration;
- ApiRequestInfo({
- required this.id,
- required this.timestamp,
- required this.method,
- required this.url,
- this.headers,
- this.requestData,
- this.statusCode,
- this.responseData,
- this.error,
- this.duration,
- });
- String get statusText {
- if (error != null) return 'ERROR';
- if (statusCode == null) return 'PENDING';
- if (statusCode! >= 200 && statusCode! < 300) return 'SUCCESS';
- if (statusCode! >= 400) return 'ERROR';
- return 'UNKNOWN';
- }
- Color get statusColor {
- switch (statusText) {
- case 'SUCCESS':
- return Colors.green;
- case 'ERROR':
- return Colors.red;
- case 'PENDING':
- return Colors.orange;
- default:
- return Colors.grey;
- }
- }
- }
- /// 简化版API监控拦截器
- class SimpleApiMonitorInterceptor extends dio.Interceptor {
- final Map<String, DateTime> _requestStartTimes = {};
- @override
- void onRequest(
- dio.RequestOptions options,
- dio.RequestInterceptorHandler handler,
- ) {
- final requestId = _generateRequestId();
- _requestStartTimes[requestId] = DateTime.now();
- final request = ApiRequestInfo(
- id: requestId,
- timestamp: DateTime.now(),
- method: options.method,
- url: options.uri.toString(),
- headers: options.headers,
- requestData: options.data is FormData
- ? options.data.fields
- : _decryptData(options.data),
- );
- IXDeveloperTools.addApiRequest(request);
- handler.next(options);
- }
- @override
- void onResponse(
- dio.Response response,
- dio.ResponseInterceptorHandler handler,
- ) {
- final requestId = _findRequestId(response.requestOptions.uri.toString());
- if (requestId != null) {
- final startTime = _requestStartTimes.remove(requestId);
- final duration = startTime != null
- ? DateTime.now().difference(startTime)
- : null;
- final updatedRequest = ApiRequestInfo(
- id: requestId,
- timestamp: DateTime.now(),
- method: response.requestOptions.method,
- url: response.requestOptions.uri.toString(),
- headers: response.requestOptions.headers,
- requestData: response.requestOptions.data is FormData
- ? response.requestOptions.data.fields
- : _decryptData(response.requestOptions.data),
- statusCode: response.statusCode,
- responseData: _decryptData(response.data),
- duration: duration,
- );
- _updateApiRequest(updatedRequest);
- }
- handler.next(response);
- }
- @override
- void onError(dio.DioException err, dio.ErrorInterceptorHandler handler) {
- final requestId = _findRequestId(err.requestOptions.uri.toString());
- if (requestId != null) {
- final startTime = _requestStartTimes.remove(requestId);
- final duration = startTime != null
- ? DateTime.now().difference(startTime)
- : null;
- final updatedRequest = ApiRequestInfo(
- id: requestId,
- timestamp: DateTime.now(),
- method: err.requestOptions.method,
- url: err.requestOptions.uri.toString(),
- headers: err.requestOptions.headers,
- requestData: err.requestOptions.data is FormData
- ? err.requestOptions.data.fields
- : _decryptData(err.requestOptions.data),
- statusCode: err.response?.statusCode,
- error: err.message,
- duration: duration,
- );
- _updateApiRequest(updatedRequest);
- }
- handler.next(err);
- }
- dynamic _decryptData(dynamic encryptedData) {
- try {
- // 1. Base64 编码
- final base64Data = base64Encode(encryptedData);
- // 2. AES 解密
- final decryptedBytes = Crypto.decryptBytes(
- base64Data,
- Keys.aesKey,
- Keys.aesIv,
- );
- // 3. GZip 解压
- final gzipDecoder = GZipDecoder();
- final decompressedData = gzipDecoder.decodeBytes(decryptedBytes);
- // 4. UTF-8 解码
- final data = utf8.decode(decompressedData);
- // string to json
- return jsonDecode(data);
- } catch (e) {
- return encryptedData;
- }
- }
- String _generateRequestId() {
- return '${DateTime.now().millisecondsSinceEpoch}_${IXDeveloperTools.apiRequests.length}';
- }
- String? _findRequestId(String url) {
- try {
- final request = IXDeveloperTools.apiRequests.firstWhere(
- (r) => r.url == url && r.statusCode == null,
- );
- return request.id;
- } catch (e) {
- return null;
- }
- }
- void _updateApiRequest(ApiRequestInfo updatedRequest) {
- final requests = IXDeveloperTools._instance._apiRequests;
- final index = requests.indexWhere((r) => r.id == updatedRequest.id);
- if (index != -1) {
- requests[index] = updatedRequest;
- }
- }
- }
- class SimpleNoSignApiMonitorInterceptor extends dio.Interceptor {
- final Map<String, DateTime> _requestStartTimes = {};
- @override
- void onRequest(
- dio.RequestOptions options,
- dio.RequestInterceptorHandler handler,
- ) {
- final requestId = _generateRequestId();
- _requestStartTimes[requestId] = DateTime.now();
- final request = ApiRequestInfo(
- id: requestId,
- timestamp: DateTime.now(),
- method: options.method,
- url: options.uri.toString(),
- headers: options.headers,
- requestData: options.data,
- );
- IXDeveloperTools.addApiRequest(request);
- handler.next(options);
- }
- @override
- void onResponse(
- dio.Response response,
- dio.ResponseInterceptorHandler handler,
- ) {
- final requestId = _findRequestId(response.requestOptions.uri.toString());
- if (requestId != null) {
- final startTime = _requestStartTimes.remove(requestId);
- final duration = startTime != null
- ? DateTime.now().difference(startTime)
- : null;
- final updatedRequest = ApiRequestInfo(
- id: requestId,
- timestamp: DateTime.now(),
- method: response.requestOptions.method,
- url: response.requestOptions.uri.toString(),
- headers: response.requestOptions.headers,
- requestData: response.requestOptions.data,
- statusCode: response.statusCode,
- responseData: response.data,
- duration: duration,
- );
- _updateApiRequest(updatedRequest);
- }
- handler.next(response);
- }
- @override
- void onError(dio.DioException err, dio.ErrorInterceptorHandler handler) {
- final requestId = _findRequestId(err.requestOptions.uri.toString());
- if (requestId != null) {
- final startTime = _requestStartTimes.remove(requestId);
- final duration = startTime != null
- ? DateTime.now().difference(startTime)
- : null;
- final updatedRequest = ApiRequestInfo(
- id: requestId,
- timestamp: DateTime.now(),
- method: err.requestOptions.method,
- url: err.requestOptions.uri.toString(),
- headers: err.requestOptions.headers,
- requestData: err.requestOptions.data,
- statusCode: err.response?.statusCode,
- error: err.message,
- duration: duration,
- );
- _updateApiRequest(updatedRequest);
- }
- handler.next(err);
- }
- String _generateRequestId() {
- return '${DateTime.now().millisecondsSinceEpoch}_${IXDeveloperTools.apiRequests.length}';
- }
- String? _findRequestId(String url) {
- try {
- final request = IXDeveloperTools.apiRequests.firstWhere(
- (r) => r.url == url && r.statusCode == null,
- );
- return request.id;
- } catch (e) {
- return null;
- }
- }
- void _updateApiRequest(ApiRequestInfo updatedRequest) {
- final requests = IXDeveloperTools._instance._apiRequests;
- final index = requests.indexWhere((r) => r.id == updatedRequest.id);
- if (index != -1) {
- requests[index] = updatedRequest;
- }
- }
- }
- /// 简化版开发者工具主界面
- class SimpleDeveloperToolsScreen extends StatefulWidget {
- const SimpleDeveloperToolsScreen({super.key});
- @override
- State<SimpleDeveloperToolsScreen> createState() =>
- _SimpleDeveloperToolsScreenState();
- }
- class _SimpleDeveloperToolsScreenState extends State<SimpleDeveloperToolsScreen>
- with SingleTickerProviderStateMixin {
- late TabController _tabController;
- @override
- void initState() {
- super.initState();
- _tabController = TabController(length: 2, vsync: this);
- _tabController.index = 1;
- }
- @override
- void dispose() {
- _tabController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
- appBar: AppBar(
- elevation: 0,
- iconTheme: IconThemeData(
- color: Get.reactiveTheme.textTheme.bodyLarge!.color,
- ),
- title: Text(
- '开发者工具',
- style: TextStyle(
- fontWeight: FontWeight.w600,
- fontSize: 17,
- color: Get.reactiveTheme.textTheme.bodyLarge!.color,
- ),
- ),
- bottom: PreferredSize(
- preferredSize: const Size.fromHeight(44),
- child: Container(
- margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
- decoration: BoxDecoration(
- color: Colors.grey[100],
- borderRadius: BorderRadius.circular(8),
- ),
- child: TabBar(
- controller: _tabController,
- indicator: BoxDecoration(
- color: Colors.black87,
- borderRadius: BorderRadius.circular(6),
- ),
- indicatorSize: TabBarIndicatorSize.tab,
- dividerColor: Colors.transparent,
- labelColor: Colors.white,
- unselectedLabelColor: Colors.grey[600],
- labelStyle: const TextStyle(
- fontWeight: FontWeight.w500,
- fontSize: 13,
- ),
- unselectedLabelStyle: const TextStyle(
- fontWeight: FontWeight.w400,
- fontSize: 13,
- ),
- indicatorPadding: const EdgeInsets.all(3),
- tabs: const [
- Tab(text: '日志'),
- Tab(text: 'API'),
- ],
- ),
- ),
- ),
- ),
- body: TabBarView(
- controller: _tabController,
- children: const [SimpleConsoleLogTab(), SimpleApiMonitorTab()],
- ),
- );
- }
- }
- /// 简化版开发者工具对话框
- class SimpleDeveloperToolsDialog extends StatelessWidget {
- const SimpleDeveloperToolsDialog({super.key});
- @override
- Widget build(BuildContext context) {
- return Dialog(
- backgroundColor: Colors.transparent,
- elevation: 0,
- child: Container(
- width: Get.width * 0.95,
- height: Get.height * 0.9,
- decoration: BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.circular(12),
- ),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(12),
- child: const SimpleDeveloperToolsScreen(),
- ),
- ),
- );
- }
- }
- /// 简化版控制台日志标签页
- class SimpleConsoleLogTab extends StatefulWidget {
- const SimpleConsoleLogTab({super.key});
- @override
- State<SimpleConsoleLogTab> createState() => _SimpleConsoleLogTabState();
- }
- class _SimpleConsoleLogTabState extends State<SimpleConsoleLogTab> {
- final TextEditingController _searchController = TextEditingController();
- List<ConsoleLogEntry> _filteredLogs = [];
- @override
- void initState() {
- super.initState();
- _updateLogs();
- }
- void _updateLogs() {
- setState(() {
- final allLogs = IXDeveloperTools.logs;
- if (_searchController.text.isEmpty) {
- _filteredLogs = allLogs;
- } else {
- _filteredLogs = allLogs
- .where(
- (log) =>
- log.message.toLowerCase().contains(
- _searchController.text.toLowerCase(),
- ) ||
- log.tag.toLowerCase().contains(
- _searchController.text.toLowerCase(),
- ),
- )
- .toList();
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return Column(
- children: [
- // 搜索和操作栏
- Container(
- padding: const EdgeInsets.all(12),
- child: Row(
- children: [
- // 搜索框
- Expanded(
- child: Container(
- height: 36,
- decoration: BoxDecoration(
- color: Colors.grey[100],
- borderRadius: BorderRadius.circular(8),
- ),
- child: TextField(
- controller: _searchController,
- style: const TextStyle(fontSize: 13),
- decoration: InputDecoration(
- hintText: '搜索...',
- hintStyle: TextStyle(
- color: Colors.grey[400],
- fontSize: 13,
- ),
- prefixIcon: Icon(
- Icons.search,
- color: Colors.grey[400],
- size: 18,
- ),
- border: InputBorder.none,
- isDense: true,
- contentPadding: const EdgeInsets.symmetric(
- vertical: 8,
- horizontal: 0,
- ),
- ),
- onChanged: (_) => _updateLogs(),
- ),
- ),
- ),
- const SizedBox(width: 8),
- // 刷新按钮
- _buildIconButton(Icons.refresh, _updateLogs),
- const SizedBox(width: 6),
- // 清除按钮
- _buildIconButton(Icons.delete_outline, () {
- IXDeveloperTools.clearLogs();
- _updateLogs();
- }),
- ],
- ),
- ),
- // 日志列表
- Expanded(
- child: _filteredLogs.isEmpty
- ? Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(
- Icons.inbox_outlined,
- size: 48,
- color: Colors.grey[300],
- ),
- const SizedBox(height: 12),
- Text(
- '暂无日志',
- style: TextStyle(fontSize: 14, color: Colors.grey[400]),
- ),
- ],
- ),
- )
- : ListView.separated(
- padding: const EdgeInsets.symmetric(horizontal: 12),
- itemCount: _filteredLogs.length,
- separatorBuilder: (_, __) => const SizedBox(height: 1),
- itemBuilder: (context, index) {
- final log = _filteredLogs[index];
- return SimpleLogCard(log: log);
- },
- ),
- ),
- ],
- );
- }
- Widget _buildIconButton(IconData icon, VoidCallback onTap) {
- return InkWell(
- onTap: onTap,
- borderRadius: BorderRadius.circular(8),
- child: Container(
- width: 36,
- height: 36,
- decoration: BoxDecoration(
- color: Colors.grey[100],
- borderRadius: BorderRadius.circular(8),
- ),
- child: Icon(icon, size: 18, color: Colors.grey[600]),
- ),
- );
- }
- }
- /// 简化版API监控标签页
- class SimpleApiMonitorTab extends StatefulWidget {
- const SimpleApiMonitorTab({super.key});
- @override
- State<SimpleApiMonitorTab> createState() => _SimpleApiMonitorTabState();
- }
- class _SimpleApiMonitorTabState extends State<SimpleApiMonitorTab> {
- @override
- Widget build(BuildContext context) {
- final requests = IXDeveloperTools.apiRequests;
- return Column(
- children: [
- // 操作栏
- Container(
- padding: const EdgeInsets.all(12),
- child: Row(
- children: [
- // 统计信息
- Text(
- '${requests.length} 个请求',
- style: TextStyle(fontSize: 13, color: Colors.grey[600]),
- ),
- const Spacer(),
- // 刷新按钮
- _buildIconButton(Icons.refresh, () => setState(() {})),
- const SizedBox(width: 6),
- // 清除按钮
- _buildIconButton(Icons.delete_outline, () {
- IXDeveloperTools.clearApiRequests();
- setState(() {});
- }),
- ],
- ),
- ),
- // API请求列表
- Expanded(
- child: requests.isEmpty
- ? Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Icon(
- Icons.cloud_off_outlined,
- size: 48,
- color: Colors.grey[300],
- ),
- const SizedBox(height: 12),
- Text(
- '暂无请求',
- style: TextStyle(fontSize: 14, color: Colors.grey[400]),
- ),
- ],
- ),
- )
- : ListView.separated(
- padding: const EdgeInsets.symmetric(horizontal: 12),
- itemCount: requests.length,
- separatorBuilder: (_, __) => const SizedBox(height: 1),
- itemBuilder: (context, index) {
- final request = requests[index];
- return SimpleApiCard(request: request);
- },
- ),
- ),
- ],
- );
- }
- Widget _buildIconButton(IconData icon, VoidCallback onTap) {
- return InkWell(
- onTap: onTap,
- borderRadius: BorderRadius.circular(8),
- child: Container(
- width: 36,
- height: 36,
- decoration: BoxDecoration(
- color: Colors.grey[100],
- borderRadius: BorderRadius.circular(8),
- ),
- child: Icon(icon, size: 18, color: Colors.grey[600]),
- ),
- );
- }
- }
|