-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParamData.dart
39 lines (36 loc) · 1 KB
/
ParamData.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import './isEmpty.dart';
/// 序列化字符串,参照以前JS写的ParamData
String paramData (Map data) {
String res = '';
void addOne (String key, dynamic item) {
if(item is Function) {
item = item();
} else if(isEmpty(item)) {
item = '';
} else {
item = item.toString();
}
res = res + (isEmpty(res) ? '' : '&') + Uri.encodeComponent(key) + '=' + Uri.encodeComponent(item);
}
void buildParam (String prefix, dynamic item) {
if(prefix != '') {
if(item is List) {
for(var index = 0; index < item.length; index++) {
buildParam('$prefix[${item[index] is Map && item[index] ? index : ''}]', item[index]);
}
} else if(item is Map) {
item.forEach((key, val){
buildParam(prefix + '[' + key + ']', item[key]);
});
} else {
addOne(prefix, item);
}
} else if(item is Map) {
item.forEach((key, val){
buildParam(key, val);
});
}
}
buildParam('', data);
return res;
}