logo和名称替换
This commit is contained in:
@@ -5,13 +5,13 @@ import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio/io.dart';
|
||||
import 'package:hgmj/config/config.dart';
|
||||
import 'package:hgmj/hj_utils/text_util.dart';
|
||||
import 'package:hgmj/tools_base/debug_log.dart';
|
||||
import 'package:hgmj/tools_base/net/base_resp_bean.dart';
|
||||
import 'package:hgmj/tools_base/net/http_response_interceptor.dart';
|
||||
import 'package:hgmj/tools_base/net/net_code.dart';
|
||||
import 'package:hgmj/tools_base/net/net_manager.dart';
|
||||
import 'package:wydj/config/config.dart';
|
||||
import 'package:wydj/hj_utils/text_util.dart';
|
||||
import 'package:wydj/tools_base/debug_log.dart';
|
||||
import 'package:wydj/tools_base/net/base_resp_bean.dart';
|
||||
import 'package:wydj/tools_base/net/http_response_interceptor.dart';
|
||||
import 'package:wydj/tools_base/net/net_code.dart';
|
||||
import 'package:wydj/tools_base/net/net_manager.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../../track_event_manager/device_service.dart';
|
||||
@@ -56,7 +56,8 @@ abstract class HttpManger {
|
||||
if (TextUtil.isNotEmpty(serverTimeS)) {
|
||||
_serverTime = DateTime.parse(serverTimeS!);
|
||||
_diffTimeInSeconds = DateTime.now().difference(_serverTime!).inSeconds;
|
||||
debugLog("============>server diff from local in seconds:$_diffTimeInSeconds");
|
||||
debugLog(
|
||||
"============>server diff from local in seconds:$_diffTimeInSeconds");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,37 +96,50 @@ abstract class HttpManger {
|
||||
}) async {
|
||||
// 给默认值兜底:若 response 非 null 但 data 不是 Map/BaseRespBean(null/String/List),
|
||||
// 下面 resultData 不会被赋值,late 变量访问会抛 LateInitializationError
|
||||
BaseRespBean resultData = BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
BaseRespBean resultData =
|
||||
BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
Response? response;
|
||||
try {
|
||||
response = await dio.request(url, queryParameters: queryParameters, options: options, data: data, cancelToken: cancelToken);
|
||||
response = await dio.request(url,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
data: data,
|
||||
cancelToken: cancelToken);
|
||||
} on DioException catch (e) {
|
||||
debugLog('DioException $e');
|
||||
if (e.type == DioExceptionType.cancel) {
|
||||
resultData = BaseRespBean(Code.LOCAL_CANCEL_REQUEST, msg: '请求已经取消~,请重试', data: null);
|
||||
resultData = BaseRespBean(Code.LOCAL_CANCEL_REQUEST,
|
||||
msg: '请求已经取消~,请重试', data: null);
|
||||
} else if (e.type == DioExceptionType.connectionTimeout ||
|
||||
e.type == DioExceptionType.receiveTimeout ||
|
||||
e.type == DioExceptionType.sendTimeout) {
|
||||
resultData = BaseRespBean(Code.NETWORK_TIMEOUT, msg: '网络连接超时~', data: null);
|
||||
resultData =
|
||||
BaseRespBean(Code.NETWORK_TIMEOUT, msg: '网络连接超时~', data: null);
|
||||
}
|
||||
final connectivityResult = await Connectivity().checkConnectivity();
|
||||
if (connectivityResult == ConnectivityResult.none) {
|
||||
//没有网络
|
||||
resultData = BaseRespBean(Code.LOCAL_NO_NETWORK, msg: '暂无网络,请检查网络设置', data: null);
|
||||
resultData = BaseRespBean(Code.LOCAL_NO_NETWORK,
|
||||
msg: '暂无网络,请检查网络设置', data: null);
|
||||
} else {
|
||||
resultData = BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常,请重新试试~', data: null);
|
||||
resultData =
|
||||
BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常,请重新试试~', data: null);
|
||||
}
|
||||
} on SocketException catch (e) {
|
||||
resultData = BaseRespBean(Code.NETWORK_TIMEOUT, msg: '网络连接超时~', data: null);
|
||||
resultData =
|
||||
BaseRespBean(Code.NETWORK_TIMEOUT, msg: '网络连接超时~', data: null);
|
||||
debugLog('SocketException $e');
|
||||
} on HttpException catch (e) {
|
||||
resultData = BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
resultData =
|
||||
BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
debugLog('HttpException $e');
|
||||
} on FormatException catch (e) {
|
||||
debugLog('FormatException $e');
|
||||
resultData = BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
resultData =
|
||||
BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
} catch (e) {
|
||||
resultData = BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
resultData =
|
||||
BaseRespBean(Code.NETWORK_ERROR, msg: '网络异常请稍后再试~', data: null);
|
||||
debugLog(e);
|
||||
}
|
||||
|
||||
@@ -141,7 +155,8 @@ abstract class HttpManger {
|
||||
final data_ = resultData.data;
|
||||
if (data_ is Map) {
|
||||
try {
|
||||
resultData.data = jsonTransformation.call(Map<String, dynamic>.from(data_));
|
||||
resultData.data =
|
||||
jsonTransformation.call(Map<String, dynamic>.from(data_));
|
||||
} catch (e) {
|
||||
print('jsonTransformation error $e');
|
||||
resultData.data = null;
|
||||
@@ -178,14 +193,16 @@ abstract class HttpManger {
|
||||
var adapter = DefaultHttpClientAdapter();
|
||||
|
||||
adapter.onHttpClientCreate = (client) {
|
||||
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
|
||||
client.badCertificateCallback =
|
||||
(X509Certificate cert, String host, int port) => true;
|
||||
return client;
|
||||
};
|
||||
dio.httpClientAdapter = adapter;
|
||||
}
|
||||
|
||||
// 获取统一的请求头
|
||||
Future<Options> generateRequestOption(String apiUrl, {Options? options, required HttpMethod method}) async {
|
||||
Future<Options> generateRequestOption(String apiUrl,
|
||||
{Options? options, required HttpMethod method}) async {
|
||||
//调用方可以自带 options(比如 X-Request-ID 这种单接口的头),公共头往上加,method 一律以本次请求为准
|
||||
options ??= Options();
|
||||
options.method = method.method;
|
||||
@@ -203,7 +220,11 @@ abstract class HttpManger {
|
||||
options.headers?["api_version"] = "1.0.0";
|
||||
options.headers?["device"] = Platform.operatingSystem;
|
||||
Uri? baseUri = Uri.tryParse(baseUrl);
|
||||
Uri targetUri = Uri(scheme: baseUri?.scheme, host: baseUri?.host, port: baseUri?.port, path: baseUri!.path + apiUrl);
|
||||
Uri targetUri = Uri(
|
||||
scheme: baseUri?.scheme,
|
||||
host: baseUri?.host,
|
||||
port: baseUri?.port,
|
||||
path: baseUri!.path + apiUrl);
|
||||
options.headers?["x-api-key"] = await _sign(targetUri.path);
|
||||
options.headers?["sid"] = TrackSessionManager().currentSid;
|
||||
options.headers?["DeviceModel"] = DeviceInfoService.model;
|
||||
@@ -226,15 +247,20 @@ abstract class HttpManger {
|
||||
var digest = sha1Encrypt.convert(bytes);
|
||||
return 'timestamp=$timestamp;sign=${digest.toString()};nonce=${signObj['nonce']}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class _HttpManagerImp extends HttpManger {
|
||||
@override
|
||||
Future<BaseRespBean> fetchResponseByDELETE(String url,
|
||||
{Map<String, dynamic>? param, Options? options, Function(Map<String, dynamic> json)? jsonTransformation}) async {
|
||||
final reqOptions = await generateRequestOption(url, options: options, method: HttpMethod.delete);
|
||||
return await _requestByUrl(url, options: reqOptions, data: param, jsonTransformation: jsonTransformation);
|
||||
{Map<String, dynamic>? param,
|
||||
Options? options,
|
||||
Function(Map<String, dynamic> json)? jsonTransformation}) async {
|
||||
final reqOptions = await generateRequestOption(url,
|
||||
options: options, method: HttpMethod.delete);
|
||||
return await _requestByUrl(url,
|
||||
options: reqOptions,
|
||||
data: param,
|
||||
jsonTransformation: jsonTransformation);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -244,8 +270,12 @@ class _HttpManagerImp extends HttpManger {
|
||||
Options? options,
|
||||
Function(Map<String, dynamic> json)? jsonTransformation,
|
||||
}) async {
|
||||
final reqOptions = await generateRequestOption(url, options: options, method: HttpMethod.get);
|
||||
return await _requestByUrl(url, options: reqOptions, queryParameters: param, jsonTransformation: jsonTransformation);
|
||||
final reqOptions = await generateRequestOption(url,
|
||||
options: options, method: HttpMethod.get);
|
||||
return await _requestByUrl(url,
|
||||
options: reqOptions,
|
||||
queryParameters: param,
|
||||
jsonTransformation: jsonTransformation);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -255,18 +285,24 @@ class _HttpManagerImp extends HttpManger {
|
||||
Options? options,
|
||||
Function(Map<String, dynamic> json)? jsonTransformation,
|
||||
}) async {
|
||||
final reqOptions = await generateRequestOption(url, options: options, method: HttpMethod.post);
|
||||
return await _requestByUrl(url, options: reqOptions, data: param, jsonTransformation: jsonTransformation);
|
||||
final reqOptions = await generateRequestOption(url,
|
||||
options: options, method: HttpMethod.post);
|
||||
return await _requestByUrl(url,
|
||||
options: reqOptions,
|
||||
data: param,
|
||||
jsonTransformation: jsonTransformation);
|
||||
}
|
||||
|
||||
Future<BaseRespBean> fetchDetectLineResponse(String url, {Options? options, CancelToken? cancelToken}) async {
|
||||
Future<BaseRespBean> fetchDetectLineResponse(String url,
|
||||
{Options? options, CancelToken? cancelToken}) async {
|
||||
options ??= Options(
|
||||
method: HttpMethod.get.method,
|
||||
sendTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
headers: {},
|
||||
);
|
||||
final result = await _requestByUrl(url, options: options, cancelToken: cancelToken);
|
||||
final result =
|
||||
await _requestByUrl(url, options: options, cancelToken: cancelToken);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:hgmj/tools_base/debug_log.dart';
|
||||
import 'package:hgmj/tools_base/toast.dart';
|
||||
import 'package:wydj/tools_base/debug_log.dart';
|
||||
import 'package:wydj/tools_base/toast.dart';
|
||||
|
||||
import '../../config/config.dart';
|
||||
import '../../hj_utils/text_util.dart';
|
||||
@@ -19,7 +19,8 @@ class HttpRespInterceptor {
|
||||
|
||||
static Future<dynamic> handleResponse(Response response) async {
|
||||
if (response.statusCode != 200) {
|
||||
return Future.error(ApiException(response.statusCode, "statusCode is not 200"));
|
||||
return Future.error(
|
||||
ApiException(response.statusCode, "statusCode is not 200"));
|
||||
}
|
||||
BaseRespBean? baseResp;
|
||||
if (response.data is Map) {
|
||||
@@ -27,7 +28,8 @@ class HttpRespInterceptor {
|
||||
} else if (response.data is String) {
|
||||
baseResp = BaseRespBean.fromJson(json.decode(response.data));
|
||||
} else {
|
||||
return Future.error(ApiException(Code.PARSE_DATE_ERROR, Lang.PARSE_DATE_ERROR));
|
||||
return Future.error(
|
||||
ApiException(Code.PARSE_DATE_ERROR, Lang.PARSE_DATE_ERROR));
|
||||
}
|
||||
|
||||
int? code = baseResp.code;
|
||||
@@ -65,7 +67,9 @@ class HttpRespInterceptor {
|
||||
|
||||
/// 展示提示
|
||||
if (code != Code.SUCCESS) {
|
||||
if (TextUtil.isEmpty(baseResp.msg) && TextUtil.isEmpty(baseResp.tip) && response.statusCode != 200) {
|
||||
if (TextUtil.isEmpty(baseResp.msg) &&
|
||||
TextUtil.isEmpty(baseResp.tip) &&
|
||||
response.statusCode != 200) {
|
||||
showToast("服务器错误");
|
||||
} else {
|
||||
if (!TextUtil.isEmpty(baseResp.tip)) {
|
||||
@@ -75,7 +79,8 @@ class HttpRespInterceptor {
|
||||
}
|
||||
}
|
||||
}
|
||||
debugLog('path:${response.requestOptions.baseUrl}${response.requestOptions.path}');
|
||||
debugLog(
|
||||
'path:${response.requestOptions.baseUrl}${response.requestOptions.path}');
|
||||
debugLog('param: ${response.requestOptions.queryParameters}');
|
||||
debugLog('data: ${response.requestOptions.data}');
|
||||
debugLog('head: ${response.requestOptions.headers}');
|
||||
|
||||
@@ -2,17 +2,17 @@ import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hgmj/config/address.dart';
|
||||
import 'package:hgmj/config/config.dart';
|
||||
import 'package:hgmj/hj_utils/light_model.dart';
|
||||
import 'package:hgmj/hj_utils/store_keys.dart';
|
||||
import 'package:hgmj/tools_base/debug_log.dart';
|
||||
import 'package:hgmj/tools_base/net/aes_util.dart';
|
||||
import 'package:hgmj/tools_base/net/base_resp_bean.dart';
|
||||
import 'package:hgmj/tools_base/net/curl_util.dart';
|
||||
import 'package:hgmj/tools_base/net/http_manager.dart';
|
||||
import 'package:hgmj/tools_base/net/net_code.dart';
|
||||
import 'package:hgmj/tools_base/toast.dart';
|
||||
import 'package:wydj/config/address.dart';
|
||||
import 'package:wydj/config/config.dart';
|
||||
import 'package:wydj/hj_utils/light_model.dart';
|
||||
import 'package:wydj/hj_utils/store_keys.dart';
|
||||
import 'package:wydj/tools_base/debug_log.dart';
|
||||
import 'package:wydj/tools_base/net/aes_util.dart';
|
||||
import 'package:wydj/tools_base/net/base_resp_bean.dart';
|
||||
import 'package:wydj/tools_base/net/curl_util.dart';
|
||||
import 'package:wydj/tools_base/net/http_manager.dart';
|
||||
import 'package:wydj/tools_base/net/net_code.dart';
|
||||
import 'package:wydj/tools_base/toast.dart';
|
||||
|
||||
/// 统一处理网络响应:解析 BaseRespBean、按业务码分发、密文解密、错误提示
|
||||
class HttpResponseInterceptor extends InterceptorsWrapper {
|
||||
@@ -25,7 +25,8 @@ class HttpResponseInterceptor extends InterceptorsWrapper {
|
||||
// curl/响应体仅 debug 下生成:generateCurl 会 json.encode 整个请求体、
|
||||
// '$data' 会序列化整个响应,无条件求值在 release 也跑、纯属浪费
|
||||
if (kDebugMode) {
|
||||
debugLog('$tag curl ====== ${CurlUtil.generateCurl(response.requestOptions)}');
|
||||
debugLog(
|
||||
'$tag curl ====== ${CurlUtil.generateCurl(response.requestOptions)}');
|
||||
debugLog('==========response==========\n${response.data}');
|
||||
}
|
||||
handleResponse(response);
|
||||
@@ -55,12 +56,14 @@ class HttpResponseInterceptor extends InterceptorsWrapper {
|
||||
try {
|
||||
baseResp = BaseRespBean.fromJson(json.decode(response.data));
|
||||
} catch (e) {
|
||||
baseResp = BaseRespBean(Code.PARSE_DATE_ERROR, msg: Lang.PARSE_DATE_ERROR);
|
||||
baseResp =
|
||||
BaseRespBean(Code.PARSE_DATE_ERROR, msg: Lang.PARSE_DATE_ERROR);
|
||||
}
|
||||
} else if (response.data is BaseRespBean) {
|
||||
baseResp = response.data;
|
||||
} else {
|
||||
baseResp = BaseRespBean(Code.PARSE_DATE_ERROR, msg: Lang.PARSE_DATE_ERROR);
|
||||
baseResp =
|
||||
BaseRespBean(Code.PARSE_DATE_ERROR, msg: Lang.PARSE_DATE_ERROR);
|
||||
}
|
||||
// 同步下服务器时间
|
||||
httpManager.setServerTime(baseResp.time);
|
||||
|
||||
@@ -8,7 +8,7 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:convert/convert.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:hgmj/tools_base/debug_log.dart';
|
||||
import 'package:wydj/tools_base/debug_log.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
import 'dio_cli.dart';
|
||||
|
||||
@@ -4,8 +4,8 @@ import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio/io.dart';
|
||||
import 'package:hgmj/tools_base/debug_log.dart';
|
||||
import 'package:hgmj/tools_base/net/http_manager.dart';
|
||||
import 'package:wydj/tools_base/debug_log.dart';
|
||||
import 'package:wydj/tools_base/net/http_manager.dart';
|
||||
|
||||
import '../../config/address.dart';
|
||||
import '../../hj_utils/light_model.dart';
|
||||
@@ -56,7 +56,8 @@ class NetManager {
|
||||
Future<String> userAgent() => DeviceInfoService.getCachedUserAgent();
|
||||
|
||||
/// 强制重新生成并写回缓存(扫码登录、设备切换等场景)。
|
||||
Future<String> refreshUserAgent() => DeviceInfoService.getCachedUserAgent(deviceId: DeviceInfoService.deviceId);
|
||||
Future<String> refreshUserAgent() => DeviceInfoService.getCachedUserAgent(
|
||||
deviceId: DeviceInfoService.deviceId);
|
||||
|
||||
/// 清除ua
|
||||
Future clearUserAgent() => DeviceInfoService.clearCachedUserAgent();
|
||||
@@ -81,7 +82,8 @@ class NetManager {
|
||||
if (TextUtil.isNotEmpty(serverTimeS)) {
|
||||
_serverTime = DateTime.parse(serverTimeS!);
|
||||
_diffTimeInSeconds = DateTime.now().difference(_serverTime!).inSeconds;
|
||||
debugLog("============>server diff from local in seconds:$_diffTimeInSeconds");
|
||||
debugLog(
|
||||
"============>server diff from local in seconds:$_diffTimeInSeconds");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +108,8 @@ Dio createDio({BaseOptions? options}) {
|
||||
var adapter = DefaultHttpClientAdapter();
|
||||
// var adapter = DefaultHttpClientAdapter();
|
||||
adapter.onHttpClientCreate = (client) {
|
||||
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
|
||||
client.badCertificateCallback =
|
||||
(X509Certificate cert, String host, int port) => true;
|
||||
return client;
|
||||
};
|
||||
dio.httpClientAdapter = adapter;
|
||||
|
||||
Reference in New Issue
Block a user