Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
gmd_flutter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mobile
gmd_flutter
Commits
b0695e84
Commit
b0695e84
authored
May 16, 2019
by
jinzhu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add files
parent
57cb9373
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
305 additions
and
0 deletions
+305
-0
Api.dart
lib/api/Api.dart
+10
-0
ALNetWork.dart
lib/netWork/ALNetWork.dart
+195
-0
DioUtil.dart
lib/netWork/DioUtil.dart
+100
-0
No files found.
lib/api/Api.dart
0 → 100644
View file @
b0695e84
class
Api
{
static
final
String
HOST
=
"https://doctor.igengmei.com"
;
// http://doctor.ascle-test.env
static
final
String
force_dialog
=
"/api/client/app/force_dialog"
;
static
final
String
home
=
"/api/client/app/home"
;
}
\ No newline at end of file
lib/netWork/ALNetWork.dart
0 → 100644
View file @
b0695e84
import
'package:dio/dio.dart'
;
import
'dart:async'
;
import
'dart:convert'
;
class
NetworkSuccess
{
NetworkSuccess
({
this
.
data
});
final
Map
data
;
Map
get
dataMap
{
if
(
this
.
data
.
runtimeType
==
Map
)
{
return
data
;
}
else
return
null
;
}
}
class
NetworkError
{
NetworkError
({
this
.
error
});
final
Map
error
;
int
get
errorCode
{
if
(
this
.
error
.
runtimeType
==
Map
)
{
return
error
[
'errorCode'
];
}
else
return
null
;
}
}
class
ALURL
{
ALURL
({
this
.
api
})
:
assert
(
api
!=
null
);
final
String
api
;
static
final
baseUrl
=
'https://earth.iyanzhi.com'
;
String
get
originUrl
=>
(
baseUrl
+
api
);
}
class
ALNetworkHeader
{
BaseOptions
get
options
{
BaseOptions
option
=
BaseOptions
();
option
.
headers
=
this
.
header
;
return
option
;
}
Map
<
String
,
dynamic
>
get
header
{
return
{
'Host'
:
'earth.igengmei.com'
,
'Accept'
:
'*/*'
,
'Cookie'
:
'_gm_token=fb20fe1550833249; sessionid=qntnckxv4n4nzrl49jmaesc5ylru92yt; _gtid=ae355f92310911e9905700163e0a7a995288'
,
'User-Agent'
:
'GMAlpha/1.3.0 (iPhone; iOS 12.1.2; Scale/2.00)'
,
'Accept-Language'
:
'en-CN;q=1, zh-Hans-CN;q=0.9'
,
'Accept-Encoding'
:
'gzip, deflate'
,
'Connection'
:
'keep-alive'
};
}
Map
<
String
,
dynamic
>
get
params
{
return
{
'platform'
:
'iPhone'
,
'os_version'
:
'12.1.2'
,
'version'
:
'1.3.0'
,
'model'
:
'iPhone%206s'
,
'release'
:
'0'
,
'idfa'
:
'119A3567-6C81-40EA-A3ED-A63F7DCAD86B'
,
'idfv'
:
'78BE2D94-7252-4C18-A816-2CEE6350B076'
,
'device_id'
:
'119A3567-6C81-40EA-A3ED-A63F7DCAD86B'
,
'channel'
:
'App%20Store'
,
'app_name'
:
'gengmeiios'
,
'current_city_id'
:
'worldwide'
,
'lat'
:
'0'
,
'lng'
:
'0'
,
'is_WiFi'
:
'(null)'
,
'phone_id'
:
'iPhone8'
};
}
}
// 管理任务
class
ALNetworkTask
{
ALNetworkTask
({
this
.
serviceInstance
,
this
.
networkContext
,
this
.
response
});
final
Dio
serviceInstance
;
final
ALNetwork
networkContext
;
Response
response
;
void
cancle
(
String
api
)
{
// CancelToken
}
}
typedef
NetworkSuccessCallback
=
void
Function
(
NetworkSuccess
success
);
typedef
NetWorkErrorCallback
=
void
Function
(
NetworkError
error
);
typedef
ProgressCallback
=
void
Function
(
int
count
,
int
total
);
class
ALNetwork
{
/**
* 任务映射表
* 外部可以通过 ALNetwork.taskMap 获取
*/
static
Map
<
String
,
ALNetworkTask
>
taskMap
=
{};
const
ALNetwork
(
{
this
.
success
,
this
.
error
,
this
.
progress
,
this
.
api
,
this
.
params
,
this
.
formData
})
:
assert
(
api
!=
null
);
final
NetworkSuccessCallback
success
;
final
NetWorkErrorCallback
error
;
final
ProgressCallback
progress
;
final
String
api
;
final
Map
params
;
final
FormData
formData
;
ALURL
get
url
=>
ALURL
(
api:
this
.
api
);
///post
Future
<
void
>
post
()
async
{
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
=
ALNetworkHeader
().
options
;
ALNetworkTask
task
=
ALNetworkTask
(
serviceInstance:
dio
,
response:
response
,
networkContext:
this
);
//handle task
taskMap
[
this
.
api
]
=
task
;
return
task
;
}
void
_handleNetworkService
(
Response
response
)
{
var
data
=
jsonDecode
(
response
.
toString
());
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
));
}
//remove task
taskMap
.
remove
(
this
.
api
);
}
}
\ No newline at end of file
lib/netWork/DioUtil.dart
0 → 100644
View file @
b0695e84
import
'dart:convert'
;
import
'dart:io'
;
import
'package:dio/dio.dart'
;
class
Method
{
static
final
String
get
=
"GET"
;
static
final
String
post
=
"POST"
;
static
final
String
put
=
"PUT"
;
static
final
String
head
=
"HEAD"
;
static
final
String
delete
=
"DELETE"
;
static
final
String
patch
=
"PATCH"
;
}
class
DioUtil
{
static
final
DioUtil
_instance
=
DioUtil
.
_init
();
static
Dio
_dio
;
static
BaseOptions
_options
=
getDefOptions
();
factory
DioUtil
()
{
return
_instance
;
}
DioUtil
.
_init
()
{
_dio
=
new
Dio
();
}
static
BaseOptions
getDefOptions
()
{
BaseOptions
options
=
BaseOptions
();
options
.
connectTimeout
=
10
*
1000
;
options
.
receiveTimeout
=
20
*
1000
;
options
.
contentType
=
ContentType
.
parse
(
'application/x-www-form-urlencoded'
);
Map
<
String
,
dynamic
>
headers
=
Map
<
String
,
dynamic
>();
headers
[
'Accept'
]
=
'application/json'
;
String
platform
;
if
(
Platform
.
isAndroid
)
{
platform
=
"Android"
;
}
else
if
(
Platform
.
isIOS
)
{
platform
=
"IOS"
;
}
headers
[
'OS'
]
=
platform
;
options
.
headers
=
headers
;
return
options
;
}
setOptions
(
BaseOptions
options
)
{
_options
=
options
;
_dio
.
options
=
_options
;
}
Future
<
Map
<
String
,
dynamic
>>
get
(
String
path
,
{
pathParams
,
data
,
Function
errorCallback
})
{
return
request
(
path
,
method:
Method
.
get
,
pathParams:
pathParams
,
data:
data
,
errorCallback:
errorCallback
);
}
Future
<
Map
<
String
,
dynamic
>>
post
(
String
path
,
{
pathParams
,
data
,
Function
errorCallback
})
{
return
request
(
path
,
method:
Method
.
post
,
pathParams:
pathParams
,
data:
data
,
errorCallback:
errorCallback
);
}
Future
<
Map
<
String
,
dynamic
>>
request
(
String
path
,{
String
method
,
Map
pathParams
,
data
,
Function
errorCallback
})
async
{
///restful请求处理
if
(
pathParams
!=
null
)
{
pathParams
.
forEach
((
key
,
value
)
{
if
(
path
.
indexOf
(
key
)
!=
-
1
)
{
path
=
path
.
replaceAll
(
":
$key
"
,
value
.
toString
());
}
});
}
Response
response
=
await
_dio
.
request
(
path
,
data:
data
,
options:
Options
(
method:
method
));
if
(
response
.
statusCode
==
HttpStatus
.
ok
||
response
.
statusCode
==
HttpStatus
.
created
)
{
try
{
if
(
response
.
data
is
Map
)
{
return
response
.
data
;
}
else
{
return
json
.
decode
(
response
.
data
.
toString
());
}
}
catch
(
e
)
{
return
null
;
}
}
else
{
_handleHttpError
(
response
.
statusCode
);
if
(
errorCallback
!=
null
)
{
errorCallback
(
response
.
statusCode
);
}
return
null
;
}
}
///处理Http错误码
void
_handleHttpError
(
int
errorCode
)
{
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment