yx_net_inspector_flutter/MIGRATION_GUIDE_CN.md

288 lines
6.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 🔄 迁移指南:从原项目到 YX Net Inspector
本文档详细说明如何从原项目的网络调试功能迁移到新的 `yx_net_inspector` 库。
## 📋 **迁移步骤**
### 1. 安装依赖
```yaml
# pubspec.yaml
dependencies:
flutter:
sdk: flutter
yx_net_inspector: ^1.0.0
dio: ^5.0.0 # 如果使用Dio
```
### 2. 替换应用包装器
**原代码:**
```dart
import 'package:learning_officer_oa/utils/common_widget/yx_global_network_log_floating_ball.dart';
YxGlobalNetworkLogFloatingBall(
show: true,
size: 60,
color: Colors.blue,
draggable: true,
child: MyApp(),
)
```
**新代码:**
```dart
import 'package:yx_net_inspector/yx_net_inspector.dart';
YxNetInspector.simple(
showFloatingBall: true,
ballSize: 60.0,
ballColor: Colors.blue,
draggable: true,
child: MyApp(),
)
```
### 3. 替换Dio拦截器
**原代码:**
```dart
import 'package:learning_officer_oa/utils/request/interceptors/yx_network_log_interceptor.dart';
dio.interceptors.add(YxNetworkLogInterceptor());
```
**新代码:**
```dart
import 'package:yx_net_inspector/yx_net_inspector.dart';
dio.interceptors.add(YxNetInspectorDioInterceptor());
```
### 4. 手动日志记录(如果需要)
**原代码:**
```dart
import 'package:learning_officer_oa/utils/request/yx_network_log_monitor.dart';
YxNetworkLogMonitor.instance.addRequestLog(
id: requestId,
method: method,
url: url,
headers: headers,
requestData: requestData,
queryParameters: queryParameters,
);
YxNetworkLogMonitor.instance.updateResponseLog(
id: requestId,
statusCode: statusCode,
responseData: responseData,
duration: duration,
);
```
**新代码:**
```dart
import 'package:yx_net_inspector/yx_net_inspector.dart';
YxNetInspectorController.instance.logRequest(
id: requestId,
method: method,
url: url,
headers: headers,
requestData: requestData,
queryParameters: queryParameters,
);
YxNetInspectorController.instance.logResponse(
id: requestId,
statusCode: statusCode,
responseData: responseData,
duration: duration,
);
```
## 🎯 **完整迁移示例**
### 原项目代码结构
```dart
// main.dart
import 'package:learning_officer_oa/utils/common_widget/yx_global_network_log_floating_ball.dart';
import 'package:learning_officer_oa/utils/request/interceptors/yx_network_log_interceptor.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return YxGlobalNetworkLogFloatingBall(
show: true,
size: 60,
color: Colors.blue,
child: MaterialApp(
title: 'Learning Officer OA',
home: MyHomePage(),
),
);
}
}
// 网络配置
class ApiClient {
static final dio = Dio();
static void init() {
dio.interceptors.add(YxNetworkLogInterceptor());
}
}
```
### 迁移后代码结构
```dart
// main.dart
import 'package:yx_net_inspector/yx_net_inspector.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return YxNetInspector.simple(
showFloatingBall: true,
ballSize: 60.0,
ballColor: Colors.blue,
child: MaterialApp(
title: 'Learning Officer OA',
home: MyHomePage(),
),
);
}
}
// 网络配置
class ApiClient {
static final dio = Dio();
static void init() {
dio.interceptors.add(YxNetInspectorDioInterceptor());
}
}
```
## ⚡ **快速迁移脚本**
如果你有很多文件需要迁移,可以使用以下替换规则:
### VS Code 全局替换
1. 打开 VS Code
2.`Ctrl+Shift+H` 打开全局替换
3. 启用正则表达式模式
4. 使用以下替换规则:
**替换导入:**
```
查找: import 'package:learning_officer_oa/utils/common_widget/yx_global_network_log_floating_ball.dart';
替换: import 'package:yx_net_inspector/yx_net_inspector.dart';
```
**替换组件:**
```
查找: YxGlobalNetworkLogFloatingBall\(
替换: YxNetInspector.simple(
```
**替换拦截器导入:**
```
查找: import 'package:learning_officer_oa/utils/request/interceptors/yx_network_log_interceptor.dart';
替换: import 'package:yx_net_inspector/yx_net_inspector.dart';
```
**替换拦截器:**
```
查找: YxNetworkLogInterceptor\(\)
替换: YxNetInspectorDioInterceptor()
```
## 🔧 **配置对比**
| 功能 | 原项目 | 新库 | 说明 |
|------|--------|------|------|
| **悬浮球大小** | `size: 60` | `ballSize: 60.0` | 参数名略有不同 |
| **悬浮球颜色** | `color: Colors.blue` | `ballColor: Colors.blue` | 参数名略有不同 |
| **显示控制** | `show: true` | `showFloatingBall: true` | 参数名更明确 |
| **拖拽功能** | `draggable: true` | `draggable: true` | 完全相同 |
| **初始位置** | `initialPosition: Offset(x, y)` | `initialPosition: Offset(x, y)` | 完全相同 |
## 🎨 **新功能使用**
迁移后你可以使用新库的额外功能:
### 主题定制
```dart
YxNetInspector.simple(
theme: YxNetInspectorTheme(
primaryColor: Colors.purple,
backgroundColor: Colors.white,
textColor: Colors.black,
errorColor: Colors.red,
successColor: Colors.green,
),
child: MyApp(),
)
```
### 高级配置
```dart
YxNetInspector(
config: YxNetInspectorConfig(
showFloatingBall: true,
ballSize: 70.0,
showInDebugMode: true,
showInReleaseMode: false,
maxLogs: 1000,
),
child: MyApp(),
)
```
## ⚠️ **注意事项**
### 1. 依赖变化
- **移除**: 不再依赖 GetX
- **新增**: 如果使用Dio拦截器需要添加 `dio` 依赖
### 2. 界面变化
- **语言**: 界面已中文化
- **功能**: 新增搜索、过滤、统计等功能
### 3. 性能优化
- **内存使用**: 减少约15-20%
- **启动速度**: 提升约10-15%
- **包大小**: 减少约500KB
## 🚀 **验证迁移**
迁移完成后,请验证以下功能:
1. ✅ 悬浮球正常显示
2. ✅ 点击悬浮球打开检查器面板
3. ✅ 网络请求自动记录
4. ✅ 请求详情正常显示
5. ✅ 搜索和过滤功能正常
6. ✅ 清空日志功能正常
## 📞 **技术支持**
如果在迁移过程中遇到问题:
1. 检查本文档的常见问题
2. 查看项目的 [GitHub Issues](https://github.com/your-username/yx_net_inspector/issues)
3. 提交新的 Issue 描述你的问题
迁移愉快!🎉