Quellcode durchsuchen

1.修改百川SDK,优化请求头获取过程

tags/0.0.12
“yanghuaxuan” vor 3 Jahren
Ursprung
Commit
00e0cb96ce
3 geänderte Dateien mit 72 neuen und 46 gelöschten Zeilen
  1. +57
    -36
      lib/util/net_util.dart
  2. +10
    -5
      lib/util/router_util.dart
  3. +5
    -5
      pubspec.yaml

+ 57
- 36
lib/util/net_util.dart Datei anzeigen

@@ -92,29 +92,51 @@ class NetUtil {
};
}

static Map<String, dynamic> headParam;


/// 同步请求
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 {

var paramsData = {
'postData': params ?? {},
'queryParameters': queryParameters ?? {}
};

var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}};

// 根据请求参数,获取缓存的Key
String cacheKey = getRequestParamsCachedKey(paramsData, path);
// 参数签名 TODO 加密?
// post请求的参数
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;
try {
Dio dio = await NetUtil
.getInstance()
.dio;
Dio dio = await NetUtil.getInstance().dio;
response = await dio.request(
path,
data: !EmptyUtil.isEmpty(bodyParams) ? bodyParams : null,
@@ -171,8 +193,8 @@ class NetUtil {
if (result[GlobalConfig.HTTP_RESPONSE_KEY_CODE]?.toString() == '401000') {
try {
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;
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
String cacheKey = getRequestParamsCachedKey(paramsData, path);
print("缓存Key"+cacheKey);
print("缓存Key" + cacheKey);
// // 读取缓存回调
if (onCache != null) {
await _onCallBackCacheData(onCache, cacheKey);
}

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 解密?
if (isSuccess(result)) {
if (onSuccess != null) {
@@ -243,7 +264,11 @@ class NetUtil {
}

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) {
Logger.error('error: ' + e.toString());
@@ -348,8 +373,7 @@ class NetUtil {
params["os_version"] = iosInfo?.systemVersion?.toString();
// 设备型号 如:iPhone 11pro

params['device_model'] =
EncodeUtil.encodeBase64(iosInfo?.name?.toString() ?? '');
params['device_model'] = EncodeUtil.encodeBase64(iosInfo?.name?.toString() ?? '');
// 设备ID
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));

if (!EmptyUtil.isEmpty(cacheMap) &&
@@ -490,11 +511,11 @@ class NetUtil {
static String getRequestParamsCachedKey(Map<String, dynamic> map, String path) {
String key;
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;
}

// 七牛云文件上传


+ 10
- 5
lib/util/router_util.dart Datei anzeigen

@@ -16,11 +16,7 @@ class RouterUtil {
* data 额外参数
* */
static Future route(SkipModel skipModel, Map<String, dynamic> data, BuildContext context) async {
// skipModel.skipIdentifier="pub.flutter.credit_card";
// if (Application.hasStringMethod(skipModel.skipIdentifier.toString())) {
// Application.doStringMethod(skipModel.skipIdentifier.toString());
// return null;
// }

if (skipModel.skipIdentifier == null || skipModel.skipIdentifier == '') {
print('skipIdentifier 参数不存在,无法跳转页面');
return Future.error('skipIdentifier 参数不存在,无法跳转页面');
@@ -40,6 +36,15 @@ class RouterUtil {
}
}

if (skipModel?.requiredTaobaoAuth == '1') {
UserInfoModel user =
await Provider.of<UserInfoNotifier>(context, listen: false).getUserInfoModel();
if (!user?.isTBAuth ?? false) {
TaobaoAuth.auth(context);
return;
}
}

Widget page = EmptyPage();
if (PageFactory.hasRegisted(skipModel.skipIdentifier)) {
page = PageFactory.create(skipModel.skipIdentifier, data);


+ 5
- 5
pubspec.yaml Datei anzeigen

@@ -41,10 +41,10 @@ dependencies:
# app更新dialogUI(用于IOS,以便统一样式)
flutter_update_dialog: 1.0.0
flutter_alibc:
path: ../zhiying_flutter_alibc
# git:
# ref: 0.0.6
# url: http://192.168.0.138:3000/FnuoOS_ZhiYing/zhiying_flutter_alibc.git
# path: ../zhiying_flutter_alibc
git:
ref: 0.0.7
url: http://192.168.0.138:3000/FnuoOS_ZhiYing/zhiying_flutter_alibc.git
url_launcher: ^5.6.0
#图片预览控件
photo_view: ^0.10.3
@@ -86,7 +86,7 @@ dependencies:
save_image:
git:
url: 'http://192.168.0.138:3000/FnuoOS_ZhiYing/save_image.git'
ref: '0.0.1'
ref: '0.0.2'

dio_cookie_manager: ^1.0.0
#支付宝支付flutterSDK


Laden…
Abbrechen
Speichern