Weller 4 years ago
parent
commit
888e53ea51
1 changed files with 54 additions and 38 deletions
  1. +54
    -38
      lib/util/net_util.dart

+ 54
- 38
lib/util/net_util.dart View File

@@ -46,7 +46,7 @@ class NetUtil {
if (_dio == null) { if (_dio == null) {
var setting = await NativeUtil.getSetting(); var setting = await NativeUtil.getSetting();
String domain = setting['domain']; //'http://www.hairuyi.com/'; String domain = setting['domain']; //'http://www.hairuyi.com/';
_config(domain, proxyUrl: '192.168.0.112:8888');
// _config(domain, proxyUrl: '192.168.0.66:8866');
} }
return _dio; return _dio;
} }
@@ -64,7 +64,7 @@ class NetUtil {
headers: {'device': 'wx_applet', 'Platform': 'wx_applet'}, headers: {'device': 'wx_applet', 'Platform': 'wx_applet'},
)); ));
_dio.interceptors.add(_NetInterceptors()); _dio.interceptors.add(_NetInterceptors());
_dio.interceptors.add(LogInterceptor());
// _dio.interceptors.add(LogInterceptor());


const bool inProduction = const bool.fromEnvironment("dart.vm.product"); const bool inProduction = const bool.fromEnvironment("dart.vm.product");
if (proxyUrl != null && proxyUrl != '' && !inProduction) { if (proxyUrl != null && proxyUrl != '' && !inProduction) {
@@ -86,49 +86,39 @@ class NetUtil {


/// 同步请求 /// 同步请求
static Future<dynamic> post(String path, static Future<dynamic> post(String path,
{Map<String, dynamic> params}) async {
{Map<String, dynamic> params, NetMethod method = NetMethod.POST}) async {
if (params == null) { if (params == null) {
params = {}; params = {};
} }

// 根据请求参数,获取缓存的Key
String cacheKey = _getRequestParamsCachedKey(params, path);
// 参数签名 TODO 加密?
Map<String, dynamic> sign = await signParams(params); Map<String, dynamic> sign = await signParams(params);

Response response; Response response;
try { try {
Dio dio = await NetUtil.getInstance().dio; Dio dio = await NetUtil.getInstance().dio;
response = await dio.post(path, data: sign);
response = await dio.request(path, data: sign, options: Options(method: enumToString(method)));
} on DioError catch (e) { } on DioError catch (e) {
_formatError(e); _formatError(e);
// Logger.error(e);
}
if (response == null) {
return null;
} }


if (response.statusCode != 200) {
// 请求错误
const bool inProduction = const bool.fromEnvironment("dart.vm.product");
if (!inProduction) {
Fluttertoast.showToast(msg: response.statusMessage);
try {
var result = response.data is Map ? response.data : jsonDecode(response.data);
// TODO 解密?
if (isSuccess(result)) {
// 缓存成功的数据
_setCallBackCacheData(cacheKey, response.data is Map ? jsonEncode(response.data) : response.data);
return result;
// 缓存返回的数据
} }
// log.e(response.statusMessage);
return null;
}

var result = response.data;

if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
GlobalConfig.RESPONSE_SUCCESS_CODE ||
result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
'${GlobalConfig.RESPONSE_SUCCESS_CODE}') {
Logger.error(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]);


Fluttertoast.showToast(
msg: result[GlobalConfig.HTTP_RESPONSE_KEY_MSG],
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
);
Logger.error('error: ' + result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]);
Fluttertoast.showToast(msg: result[GlobalConfig.HTTP_RESPONSE_KEY_MSG], toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM,);
return result;
} catch (e) {
return null;
} }
return result;
} }


/// 异步请求 /// 异步请求
@@ -166,10 +156,7 @@ class NetUtil {
var result = var result =
response.data is Map ? response.data : jsonDecode(response.data); response.data is Map ? response.data : jsonDecode(response.data);
// TODO 解密? // TODO 解密?
if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
GlobalConfig.RESPONSE_SUCCESS_CODE ||
result[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
'${GlobalConfig.RESPONSE_SUCCESS_CODE}') {
if (isSuccess(result)) {
if (onSuccess != null) { if (onSuccess != null) {
onSuccess(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); onSuccess(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
// 缓存返回的数据 // 缓存返回的数据
@@ -186,7 +173,7 @@ class NetUtil {
gravity: ToastGravity.BOTTOM, gravity: ToastGravity.BOTTOM,
); );
if (onError != null) { if (onError != null) {
onError(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());
@@ -288,8 +275,7 @@ class NetUtil {
(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == (cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
GlobalConfig.RESPONSE_SUCCESS_CODE || GlobalConfig.RESPONSE_SUCCESS_CODE ||
cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] == cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
'${GlobalConfig.RESPONSE_SUCCESS_CODE}') &&
null != cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]) {
'${GlobalConfig.RESPONSE_SUCCESS_CODE}') && !EmptyUtil.isEmpty(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
onCache(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); onCache(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
return; return;
} }
@@ -301,6 +287,36 @@ class NetUtil {
SharedPreferencesUtil.setNetCacheResult(cacheKey, value); SharedPreferencesUtil.setNetCacheResult(cacheKey, value);
} }


/// 根据请求参数,获取缓存的数据
static Future<dynamic> getRequestCachedData(String url, {Map<String, dynamic> params}) async{
Map<String, dynamic> cacheMap = await SharedPreferencesUtil.getNetCacheResult(_getRequestParamsCachedKey(params, url));
if (!EmptyUtil.isEmpty(cacheMap) &&
cacheMap.containsKey(GlobalConfig.HTTP_RESPONSE_KEY_CODE) &&
(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
GlobalConfig.RESPONSE_SUCCESS_CODE ||
cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
'${GlobalConfig.RESPONSE_SUCCESS_CODE}') && !EmptyUtil.isEmpty(cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
return cacheMap[GlobalConfig.HTTP_RESPONSE_KEY_DATA];
}
return null;
}

/// 判断后台返回是否成功
static bool isSuccess(Map<String, dynamic> data){
try {
if (!EmptyUtil.isEmpty(data) &&
(data[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
GlobalConfig.RESPONSE_SUCCESS_CODE ||
data[GlobalConfig.HTTP_RESPONSE_KEY_CODE] ==
'${GlobalConfig.RESPONSE_SUCCESS_CODE}')) {
return true;
}
}catch(e){
return false;
}
return false;
}

/// 根据请求参数,获取缓存的Key /// 根据请求参数,获取缓存的Key
static String _getRequestParamsCachedKey( static String _getRequestParamsCachedKey(
Map<String, dynamic> map, String path) { Map<String, dynamic> map, String path) {


Loading…
Cancel
Save