Dart packages
- 网络请求使用dio.dart
- 数据解析使用json_serializable和json_annotation
- 创建.g.dart使用build_runner 使用方法
- 监听事件使用 event_bus.dart
- Dart packages地址
文件相关
- PXAdress.dart 请求地址相关
- PXRequest.dart 具体请求类
- PXResultModel 请求数据对象
- YYRequestManager 请求管理类
- YYResultCode 状态码
- YYResultErrorEvent错误时间类
- YYResultModel基础数据对象
- YYConfig配置文件
配合界面使用
// 界面使用
void _requestData() async {
UserInfo userInfo = await PXRequest.px_getuser();
setState(() {
_userInfo = userInfo;
});
}
dependencies:
flutter:
sdk: flutter
flutter_spinkit: "^2.1.0" # load more loading
dio: 1.0.6 #无网络请求
json_annotation: ^1.2.0 #json_serializable助手
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.0.0 #创建.g.dart
json_serializable: ^1.5.1 #序列化json
import 'package:dio/dio.dart';
import 'package:connectivity/connectivity.dart';
import 'dart:collection';
import 'package:ly_app/net/YYResultModel.dart';
import 'package:ly_app/net/YYResultCode.dart';
import 'package:ly_app/config/YYConfig.dart';
class YYRequestManager {
static String baseUrl = "";
static Map baseHeaders = {
"packageName":"com.puxin.financePlanner",
"appName":"",
"version":"1.8.7.3",
"os":"ios",
"channel":"appStore",
"platform":"11.1999998092651",
"model":"",
"factory":"apple",
"screenSize":"(0.0, 0.0, 375.0, 667.0)",
"clientId":"15444",
"token":"7fc30ec2206ec3135ca9d33d11406b36b048e4950836a678c4642e492",
"sign":"",
"pid":"pid",
"registrationId":"pid",
};
static const CONTENT_TYPE_JSON = "application/json";
static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
static Map optionParams = {
"timeoutMs": 15000,
"token": null,
"authorizationCode": null,
};
static requestPost(url, params, {noTip = false}) async {
Options option = new Options(method: "post");
return await requestBase(url, params, baseHeaders, option, noTip: noTip);
}
static requestBase(url, params, Mapheader, Options option, {noTip = false}) async {
// 判断网络
var connectivityResult = await (new Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
} else if (connectivityResult == ConnectivityResult.wifi) {
} else if (connectivityResult == ConnectivityResult.none){
return YYResultModel(YYResultErrorEvent(YYResultCode.NETWORK_ERROR, "请检查网络"), false, YYResultCode.NETWORK_ERROR);
}
//处理请求头
Map headers = new HashMap();
if (header!=null) {
headers.addAll(header);
}
//options处理
if (option != null) {
option.headers = headers;
} else{
option = new Options(method: "get");
option.headers = headers;
}
option.baseUrl = baseUrl;
option.connectTimeout = 15000;
var dio = new Dio();
Response response;
try {
response = await dio.request(url, data: params, options: option);
} on DioError catch (error) {
// 请求错误处理
Response errorResponse;
if (error.response != null) {
errorResponse = error.response;
} else {
errorResponse = new Response(statusCode: 666);
}
// 超时
if (error.type == DioErrorType.CONNECT_TIMEOUT) {
errorResponse.statusCode = YYResultCode.NETWORK_TIMEOUT;
}
// debug模式才打印
if (YYConfig.DEBUG) {
print('请求异常: ' + error.toString());
print('请求异常url: ' + url);
print('请求头: ' + option.headers.toString());
print('method: ' + option.method);
}
// 返回错误信息
return new YYResultModel(YYResultCode.errorHandleFunction(errorResponse.statusCode, error.message, noTip), false, errorResponse.statusCode);
};
// debug模式打印相关数据
if (YYConfig.DEBUG) {
print('请求url: ' + url);
print('请求头: ' + option.headers.toString());
if (params != null) {
print('请求参数: ' + params.toString());
}
if (response != null) {
print('返回参数: ' + response.toString());
}
}
try {
if (response.statusCode == 200 || response.statusCode == 201) {
return new YYResultModel(response.data, true, YYResultCode.SUCCESS, headers: response.headers);
}
} catch (error) {
print(error.toString() + url);
return new YYResultModel(response.data, false, response.statusCode, headers: response.headers);
}
return new YYResultModel(YYResultCode.errorHandleFunction(response.statusCode, "", noTip), false, response.statusCode);
}
}
import 'YYRequestManager.dart';
import 'YYResultModel.dart';
import 'package:ly_app/Model/UserInfo.dart';
import 'PXResultModel.dart';
class PXRequest {
static px_getuser() async {
YYResultModel model = await YYRequestManager.requestPost("/employee/getuser", null);
PXResultModel px_model = PXResultModel.init(model.data);
UserInfo userInfo = UserInfo.fromJson(px_model.result);
return userInfo;
}
}
class YYResultModel {
var data;
bool success;
int code;
var headers;
YYResultModel(this.data, this.success, this.code, {this.headers});
}
class PXResultModel {
String message;
var result;
int returnCode;
String token;
PXResultModel(this.message, this.result, this.returnCode, this.token);
static PXResultModel init(Map json) {
return PXResultModel(json["message"], json["result"], json["returnCode"], json["token"]);
}
}
import 'package:event_bus/event_bus.dart';
class YYResultCode {
///网络错误
static const NETWORK_ERROR = -1;
///网络超时
static const NETWORK_TIMEOUT = -2;
///网络返回数据格式化一次
static const NETWORK_JSON_EXCEPTION = -3;
static const SUCCESS = 200;
static final EventBus eventBus = new EventBus();
static errorHandleFunction(code, message, noTip) {
if(noTip) {
return message;
}
eventBus.fire(new YYResultErrorEvent(code, message));
return message;
}
}
class YYResultErrorEvent {
final int code;
final String message;
YYResultErrorEvent(this.code, this.message);
}
class PXAdress {
static getuser() {
return "";
}
}
class YYConfig {
static bool DEBUG = true;
}