Commit 81404e31 authored by ouxiang's avatar ouxiang

optimize network

parent 6ab539f6
......@@ -2,8 +2,8 @@ import 'package:dio/dio.dart';
import 'dart:async';
import 'dart:convert';
class NetWorkSuccess {
NetWorkSuccess({this.data});
class NetworkSuccess {
NetworkSuccess({this.data});
final Map data;
Map get dataMap{
if (this.data.runtimeType == Map) {
......@@ -12,8 +12,8 @@ class NetWorkSuccess {
}
}
class NetWorkError {
NetWorkError({this.error});
class NetworkError {
NetworkError({this.error});
final Map error;
int get errorCode{
if (this.error.runtimeType == Map) {
......@@ -23,13 +23,19 @@ class NetWorkError {
}
class ALURL {
ALURL({this.host}) : assert(host != null);
final String host;
ALURL({this.api}) : assert(api != null);
final String api;
static final baseUrl = 'http://earth.igengmei.com';
String get originUrl => (baseUrl + host);
String get originUrl => (baseUrl + api);
}
class ALNetWorkHeader {
class ALNetworkHeader {
BaseOptions get options{
BaseOptions option = BaseOptions();
option.headers =this.header;
return option;
}
Map<String, dynamic> get header{
return {
......@@ -65,73 +71,124 @@ class ALNetWorkHeader {
}
// 管理任务
class ALNetWorkTask {
ALNetWorkTask({this.success, this.error, this.url, this.serviceInstance});
final NetWorkSuccessCallback success;
final ALURL url;
final NetWorkErrorCallback error;
class ALNetworkTask {
ALNetworkTask({this.serviceInstance,this.networkContext, this.response});
final Dio serviceInstance;
final ALNetwork networkContext;
Response response;
void cancle() {
void cancle(String api) {
// CancelToken
}
}
typedef NetWorkSuccessCallback = void Function(NetWorkSuccess success);
typedef NetWorkErrorCallback = void Function(NetWorkError error);
typedef NetworkSuccessCallback = void Function(NetworkSuccess success);
typedef NetWorkErrorCallback = void Function(NetworkError error);
typedef ProgressCallback = void Function(int count, int total);
class ALNetWork {
//static 修饰的变量,为类变量,通过类对象访问
// static final shareInstance = new ALNetWork();
//任务映射表
static Map<String, ALNetWorkTask> taskMap = {};
class ALNetwork {
/**
* 任务映射表
* 外部可以通过 ALNetwork.taskMap 获取
*/
static Map<String, ALNetworkTask> taskMap = {};
const ALNetWork(
const ALNetwork(
{this.success,
this.error,
this.progress,
this.host,
this.params}) : assert(host != null);
this.api,
this.params,
this.formData}) : assert(api != null);
final NetWorkSuccessCallback success;
final NetworkSuccessCallback success;
final NetWorkErrorCallback error;
final ProgressCallback progress;
final String host;
final String api;
final Map params;
ALURL get url => ALURL(host: this.host);
final FormData formData;
ALURL get url => ALURL(api: this.api);
///post
Future<void> post() async{
Response response;
Dio dio = new Dio();
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 5000;
dio.options.headers = {'user-agent': 'dio', 'common-header': 'xx'};
response = await dio.post(this.url.originUrl, data: this.params);
_handleNetWorkService(response);
ALNetworkTask task = _initNetworkEngin();
task.response = await task.serviceInstance.post(this.url.originUrl, data: this.params);
_handleNetworkService(task.response);
}
/**
* FormData formData = new FormData.from({
"name": "simon",
"age": 25,
});
*/
Future<void> postFormData() async{
ALNetworkTask task = _initNetworkEngin();
task.response = await task.serviceInstance.post(this.url.originUrl, data: this.formData);
_handleNetworkService(task.response);
}
///get
Future<void> excuteGet() async{
ALNetworkTask task = _initNetworkEngin();
task.response = await task.serviceInstance.get(this.url.originUrl, queryParameters: ALNetworkHeader().params);
_handleNetworkService(task.response);
}
/**
* FormData formData = new FormData.from({
"name": "wendux",
"age": 25,
"file1": new UploadFileInfo(new File("./upload.txt"), "upload1.txt"),
// upload with bytes (List<int>)
"file2": new UploadFileInfo.fromBytes(
utf8.encode("hello world"), "word.txt"),
// Pass multiple files within an Array
"files": [
new UploadFileInfo(new File("./example/upload.txt"), "upload.txt"),
new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")
]
});
*/
Future<void> upload() async{
ALNetworkTask task = _initNetworkEngin();
task.response = await task.serviceInstance.post(this.url.originUrl, data: this.formData,onSendProgress: this.progress);
_handleNetworkService(task.response);
}
///download
Future<void> download(String filePath) async{
ALNetworkTask task = _initNetworkEngin();
task.response = await task.serviceInstance.download(this.url.originUrl, filePath, onReceiveProgress: this.progress);
_handleNetworkService(task.response);
}
ALNetworkTask _initNetworkEngin(){
Response response;
Dio dio = new Dio();
dio.options.headers = ALNetWorkHeader().header;
response = await dio.get(this.url.originUrl, queryParameters: ALNetWorkHeader().params);
_handleNetWorkService(response);
dio.options = ALNetworkHeader().options;
ALNetworkTask task = ALNetworkTask(
serviceInstance:dio,
response: response,
networkContext: this
);
//handle task
taskMap[this.api] = task;
return task;
}
void _handleNetWorkService(Response response) {
void _handleNetworkService(Response response) {
var data = jsonDecode(response.toString());
if(response.statusCode == 200){
this.success(NetWorkSuccess(data: data));
if(data.runtimeType ==Map && response.statusCode == 200){
if (data['error'] == 0){
this.success(NetworkSuccess(data: data));
}else{
this.error(NetworkError(error: data));
}
}else{
this.error(NetWorkError(error: data));
this.error(NetworkError(error: data));
}
}
void upload(args) {
}
void download(args) {
//remove task
taskMap.remove(this.api);
}
}
\ No newline at end of file
......@@ -27,7 +27,7 @@ class _NetWorkTestPageState extends State<NetWorkTestPage> {
children: <Widget>[
FlatButton(
child: Text('newtWorkTest'),
color: Colors.purpleAccent,
color: Colors.greenAccent,
onPressed: _netWork,
),
Container(
......@@ -47,20 +47,20 @@ class _NetWorkTestPageState extends State<NetWorkTestPage> {
}
void _netWork() {
ALNetWork(
success: (NetWorkSuccess success){
ALNetwork(
success: (NetworkSuccess success){
print(success.data);
setState(() {
_string = success.data.toString();
});
},
error: (NetWorkError error){
error: (NetworkError error){
print(error.error);
setState(() {
_string = error.error.toString();
});
},
host: '/api/v1/index'
api: '/api/v1/index'
)
..excuteGet();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment