|
@@ -92,29 +92,51 @@ class NetUtil { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Map<String, dynamic> headParam; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 同步请求 |
|
|
/// 同步请求 |
|
|
static Future<dynamic> post(String path, |
|
|
static Future<dynamic> post(String path, |
|
|
{Map<String, dynamic> params, Map<String, dynamic> queryParameters, NetMethod method = NetMethod.POST, bool cache = false, bool showToast = true}) async { |
|
|
{Map<String, dynamic> params, Map<String, dynamic> queryParameters, NetMethod method = NetMethod.POST, bool cache = false, bool showToast = true}) async { |
|
|
|
|
|
|
|
|
var paramsData = { |
|
|
|
|
|
'postData': params ?? {}, |
|
|
|
|
|
'queryParameters': queryParameters ?? {} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; |
|
|
|
|
|
|
|
|
// 根据请求参数,获取缓存的Key |
|
|
// 根据请求参数,获取缓存的Key |
|
|
String cacheKey = getRequestParamsCachedKey(paramsData, path); |
|
|
String cacheKey = getRequestParamsCachedKey(paramsData, path); |
|
|
// 参数签名 TODO 加密? |
|
|
// 参数签名 TODO 加密? |
|
|
// post请求的参数 |
|
|
// post请求的参数 |
|
|
Map<String, dynamic> bodyParams = params; |
|
|
Map<String, dynamic> bodyParams = params; |
|
|
|
|
|
|
|
|
|
|
|
///只拿一次头部数据 |
|
|
|
|
|
if (NetUtil.headParam == null) { |
|
|
|
|
|
NetUtil.headParam = await _getMustHeadParams(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// token 读取SP缓存中的用户token |
|
|
|
|
|
String token = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); |
|
|
|
|
|
|
|
|
|
|
|
if (EmptyUtil.isEmpty(token) && !bool.fromEnvironment("dart.vm.product")) { |
|
|
|
|
|
try { |
|
|
|
|
|
Map setting = await NativeUtil.getSetting(); |
|
|
|
|
|
token = setting['token']; |
|
|
|
|
|
} catch (e, s) { |
|
|
|
|
|
print(s); |
|
|
|
|
|
print(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (!EmptyUtil.isEmpty(token)) { |
|
|
|
|
|
// params['token'] = token; |
|
|
|
|
|
NetUtil.headParam['Authorization'] = 'Bearer ' + token; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (NetUtil.headParam['token'] == null) {} |
|
|
// 请求头参数 |
|
|
// 请求头参数 |
|
|
Map<String, dynamic> headParam = await _getMustHeadParams(); |
|
|
|
|
|
|
|
|
Map<String, dynamic> headParam = NetUtil.headParam; |
|
|
|
|
|
|
|
|
Response response; |
|
|
Response response; |
|
|
try { |
|
|
try { |
|
|
Dio dio = await NetUtil |
|
|
|
|
|
.getInstance() |
|
|
|
|
|
.dio; |
|
|
|
|
|
|
|
|
Dio dio = await NetUtil.getInstance().dio; |
|
|
response = await dio.request( |
|
|
response = await dio.request( |
|
|
path, |
|
|
path, |
|
|
data: !EmptyUtil.isEmpty(bodyParams) ? bodyParams : null, |
|
|
data: !EmptyUtil.isEmpty(bodyParams) ? bodyParams : null, |
|
@@ -171,8 +193,8 @@ class NetUtil { |
|
|
if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE]?.toString() == '401000') { |
|
|
if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE]?.toString() == '401000') { |
|
|
try { |
|
|
try { |
|
|
Future.delayed(Duration(seconds: 0)).then((onValue) async { |
|
|
Future.delayed(Duration(seconds: 0)).then((onValue) async { |
|
|
String token= await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); |
|
|
|
|
|
if(token!=null&&token.length>2){ |
|
|
|
|
|
|
|
|
String token = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); |
|
|
|
|
|
if (token != null && token.length > 2) { |
|
|
BuildContext context = navigatorKey.currentState.overlay.context; |
|
|
BuildContext context = navigatorKey.currentState.overlay.context; |
|
|
Provider.of<UserInfoNotifier>(context, listen: false).unLogin(); |
|
|
Provider.of<UserInfoNotifier>(context, listen: false).unLogin(); |
|
|
} |
|
|
} |
|
@@ -213,27 +235,26 @@ class NetUtil { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 异步请求 |
|
|
/// 异步请求 |
|
|
static void request(String path, {NetMethod method = NetMethod.GET, Map<String, dynamic> params, Map<String, |
|
|
|
|
|
dynamic> queryParameters, OnSuccess onSuccess, OnError onError, OnCache onCache, bool showToast = true}) async { |
|
|
|
|
|
var paramsData = { |
|
|
|
|
|
'postData': params ?? {}, |
|
|
|
|
|
'queryParameters': queryParameters ?? {} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
static void request(String path, |
|
|
|
|
|
{NetMethod method = NetMethod.GET, |
|
|
|
|
|
Map<String, dynamic> params, |
|
|
|
|
|
Map<String, dynamic> queryParameters, |
|
|
|
|
|
OnSuccess onSuccess, |
|
|
|
|
|
OnError onError, |
|
|
|
|
|
OnCache onCache, |
|
|
|
|
|
bool showToast = true}) async { |
|
|
|
|
|
var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; |
|
|
|
|
|
|
|
|
// 根据请求参数,获取缓存的Key |
|
|
// 根据请求参数,获取缓存的Key |
|
|
String cacheKey = getRequestParamsCachedKey(paramsData, path); |
|
|
String cacheKey = getRequestParamsCachedKey(paramsData, path); |
|
|
print("缓存Key"+cacheKey); |
|
|
|
|
|
|
|
|
print("缓存Key" + cacheKey); |
|
|
// // 读取缓存回调 |
|
|
// // 读取缓存回调 |
|
|
if (onCache != null) { |
|
|
if (onCache != null) { |
|
|
await _onCallBackCacheData(onCache, cacheKey); |
|
|
await _onCallBackCacheData(onCache, cacheKey); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
Map result = await NetUtil.post(path, method: method, |
|
|
|
|
|
params: params, |
|
|
|
|
|
queryParameters: queryParameters, |
|
|
|
|
|
showToast: showToast, |
|
|
|
|
|
cache: onCache != null); |
|
|
|
|
|
|
|
|
Map result = await NetUtil.post(path, method: method, params: params, queryParameters: queryParameters, showToast: showToast, cache: onCache != null); |
|
|
// TODO 解密? |
|
|
// TODO 解密? |
|
|
if (isSuccess(result)) { |
|
|
if (isSuccess(result)) { |
|
|
if (onSuccess != null) { |
|
|
if (onSuccess != null) { |
|
@@ -243,7 +264,11 @@ class NetUtil { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (onError != null) { |
|
|
if (onError != null) { |
|
|
onError(!EmptyUtil.isEmpty(result) ? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]) ? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG] : '未知错误' : '未知错误'); |
|
|
|
|
|
|
|
|
onError(!EmptyUtil.isEmpty(result) |
|
|
|
|
|
? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]) |
|
|
|
|
|
? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG] |
|
|
|
|
|
: '未知错误' |
|
|
|
|
|
: '未知错误'); |
|
|
} |
|
|
} |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
Logger.error('error: ' + e.toString()); |
|
|
Logger.error('error: ' + e.toString()); |
|
@@ -348,8 +373,7 @@ class NetUtil { |
|
|
params["os_version"] = iosInfo?.systemVersion?.toString(); |
|
|
params["os_version"] = iosInfo?.systemVersion?.toString(); |
|
|
// 设备型号 如:iPhone 11pro |
|
|
// 设备型号 如:iPhone 11pro |
|
|
|
|
|
|
|
|
params['device_model'] = |
|
|
|
|
|
EncodeUtil.encodeBase64(iosInfo?.name?.toString() ?? ''); |
|
|
|
|
|
|
|
|
params['device_model'] = EncodeUtil.encodeBase64(iosInfo?.name?.toString() ?? ''); |
|
|
// 设备ID |
|
|
// 设备ID |
|
|
params['device_id'] = iosInfo?.identifierForVendor?.toString(); |
|
|
params['device_id'] = iosInfo?.identifierForVendor?.toString(); |
|
|
|
|
|
|
|
@@ -455,11 +479,8 @@ class NetUtil { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// 根据请求参数,获取缓存的数据 |
|
|
/// 根据请求参数,获取缓存的数据 |
|
|
static Future<dynamic> getRequestCachedData(String url, {Map<String, dynamic> params,Map<String, dynamic> queryParameters}) async { |
|
|
|
|
|
var paramsData = { |
|
|
|
|
|
'postData': params ?? {}, |
|
|
|
|
|
'queryParameters': queryParameters ?? {} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
static Future<dynamic> getRequestCachedData(String url, {Map<String, dynamic> params, Map<String, dynamic> queryParameters}) async { |
|
|
|
|
|
var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; |
|
|
Map<String, dynamic> cacheMap = await SharedPreferencesUtil.getNetCacheResult(getRequestParamsCachedKey(paramsData, url)); |
|
|
Map<String, dynamic> cacheMap = await SharedPreferencesUtil.getNetCacheResult(getRequestParamsCachedKey(paramsData, url)); |
|
|
|
|
|
|
|
|
if (!EmptyUtil.isEmpty(cacheMap) && |
|
|
if (!EmptyUtil.isEmpty(cacheMap) && |
|
@@ -490,11 +511,11 @@ class NetUtil { |
|
|
static String getRequestParamsCachedKey(Map<String, dynamic> map, String path) { |
|
|
static String getRequestParamsCachedKey(Map<String, dynamic> map, String path) { |
|
|
String key; |
|
|
String key; |
|
|
if (EmptyUtil.isEmpty(map)) { |
|
|
if (EmptyUtil.isEmpty(map)) { |
|
|
key= EncodeUtil.generateMd5(path); |
|
|
|
|
|
}else{ |
|
|
|
|
|
key= EncodeUtil.generateMd5(path + map.toString()); |
|
|
|
|
|
|
|
|
key = EncodeUtil.generateMd5(path); |
|
|
|
|
|
} else { |
|
|
|
|
|
key = EncodeUtil.generateMd5(path + map.toString()); |
|
|
} |
|
|
} |
|
|
return key; |
|
|
|
|
|
|
|
|
return key; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 七牛云文件上传 |
|
|
// 七牛云文件上传 |
|
|