后面在请求加密中要用到,特此记录一下
1.工具类
/// 根据request参数的key进行排序,并生成一个新的map返回
static Map<String, String> keySort(Map<String, String> oldParamsMap) {
Map<String, String> newParamsMap = Map();
List<String> oldKeys = oldParamsMap.keys.toList();
if (oldKeys.isEmpty) return newParamsMap;
oldKeys.sort((a, b) {
List<int> al = a.codeUnits;
List<int> bl = b.codeUnits;
for (int i = 0; i < al.length; i++) {
if (bl.length <= i) return 1;
if (al[i] > bl[i]) {
return 1;
} else if (al[i] < bl[i]) return -1;
}
return 0;
});
print(oldKeys);
for (int i = 0; i < oldKeys.length; i++) {
newParamsMap[oldKeys[i]] = oldParamsMap[oldKeys[i]];
}
return newParamsMap;
}
2.测试
void _paramsSort(){
Map<String,String> params = Map();
params["pageId"] = "1";
params["pageSize"] = "10";
params["sort"] = "1";
params["goodsId"] = "053453";
params["ablout"] = "测试";
params["zoom"] = "145";
params["boot"] = "spring";
Map<String,String> newParams = Utils.keySort(params);
print("排序后的map:${json.encode(newParams)}");
}