Просмотр исходного кода

1、添加一个同步请求方法

tags/0.0.1
PH2 4 лет назад
Родитель
Сommit
8b39bcba3d
1 измененных файлов: 52 добавлений и 35 удалений
  1. +52
    -35
      lib/util/net_util.dart

+ 52
- 35
lib/util/net_util.dart Просмотреть файл

@@ -46,7 +46,7 @@ class NetUtil {
if (_dio == null) {
var setting = await NativeUtil.getSetting();
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;
}
@@ -86,49 +86,39 @@ class NetUtil {

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

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

Response response;
try {
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) {
_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 =
response.data is Map ? response.data : jsonDecode(response.data);
// 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) {
onSuccess(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
// 缓存返回的数据
@@ -186,7 +173,7 @@ class NetUtil {
gravity: ToastGravity.BOTTOM,
);
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) {
if (onError != null) {
@@ -299,6 +286,36 @@ class NetUtil {
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
static String _getRequestParamsCachedKey(
Map<String, dynamic> map, String path) {


Загрузка…
Отмена
Сохранить