提交 a5222045 authored 作者: yueweilu's avatar yueweilu

缓存key 问题

上级 f1271eae
...@@ -38,9 +38,11 @@ class HttpService extends GetxService { ...@@ -38,9 +38,11 @@ class HttpService extends GetxService {
} else{ } else{
headers['Authorization'] = ''; headers['Authorization'] = '';
} }
Map<String,dynamic>? tempParams = {};
if (params != null) { if (params != null) {
params.addAll(headers); tempParams.addAll(params);
headers['Sign'] = SignTool.createSign(params!); tempParams.addAll(headers);
headers['Sign'] = SignTool.createSign(tempParams);
} }
else { else {
headers['Sign'] = SignTool.createSign(headers); headers['Sign'] = SignTool.createSign(headers);
...@@ -57,7 +59,7 @@ class HttpService extends GetxService { ...@@ -57,7 +59,7 @@ class HttpService extends GetxService {
CancelToken? cancelToken, CancelToken? cancelToken,
bool excludeToken = false, bool excludeToken = false,
bool showLoading = false, bool showLoading = false,
bool cacheEnabled = false, bool cacheEnabled = true,
}) async { }) async {
if (showLoading) CustomToast.loading(); if (showLoading) CustomToast.loading();
try { try {
...@@ -235,13 +237,13 @@ class _RequestInterceptor extends Interceptor { ...@@ -235,13 +237,13 @@ class _RequestInterceptor extends Interceptor {
case DioExceptionType.badResponse: case DioExceptionType.badResponse:
final response = err.response; final response = err.response;
final statusCode = response?.statusCode; final statusCode = response?.statusCode;
print('************* ${response?.data}'); Console.log('************* ${response?.data}');
print('******statusCode******* $statusCode'); Console.log('******statusCode******* $statusCode');
// Console.log(response?.data); // Console.log(response?.data);
var msg = '服务器错误'; var msg = '服务器错误';
switch (statusCode) { switch (statusCode) {
case 403: case 403:
print('----------403---------access_token-------------------------${UserStore.to.accessToken}--------------------'); Console.log('----------403---------access_token-------------------------${UserStore.to.accessToken}--------------------');
msg = '$statusCode - Unauthorized'; msg = '$statusCode - Unauthorized';
final newToken = await refreshToken(); final newToken = await refreshToken();
if (newToken != null) { if (newToken != null) {
...@@ -269,7 +271,7 @@ class _RequestInterceptor extends Interceptor { ...@@ -269,7 +271,7 @@ class _RequestInterceptor extends Interceptor {
} else { } else {
UserStore.to.logout(); UserStore.to.logout();
CustomToast.fail('登录已失效,请重新登录'); CustomToast.fail('登录已失效,请重新登录');
print('-------------------access_token-------------------------${UserStore.to.accessToken}--------------------'); Console.log('-------------------access_token-------------------------${UserStore.to.accessToken}--------------------');
} }
break; break;
...@@ -337,13 +339,14 @@ class _CacheInterceptor extends Interceptor { ...@@ -337,13 +339,14 @@ class _CacheInterceptor extends Interceptor {
@override @override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async { void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
final cacheEnabled = options.extra?['cacheEnabled'] ?? false; final cacheEnabled = options.extra['cacheEnabled'] ?? false;
final status = await Tools.checkCurrentNetStatus(); final status = await Tools.checkCurrentNetStatus();
if (cacheEnabled && !status) { if (cacheEnabled && !status) {
// 在发起请求之前,检查缓存是否存在有效数据 // 在发起请求之前,检查缓存是否存在有效数据
final cachedFile = await DefaultCacheManager().getFileFromCache(options.uri.toString()); String cacheKey = options.data.isEmpty ? options.uri.toString() : '${options.uri.toString()}?${options.data}';
final cachedFile = await DefaultCacheManager().getFileFromCache(cacheKey);
if (cachedFile != null && cachedFile.validTill.isAfter(DateTime.now())) { if (cachedFile != null && cachedFile.validTill.isAfter(DateTime.now())) {
Console.log('--------------使用缓存数据------------------'); Console.log('-获取数据cachedFile------------------------------$cachedFile-');
// 如果缓存有效,直接返回缓存数据 // 如果缓存有效,直接返回缓存数据
final cachedData = await cachedFile.file.readAsBytes(); final cachedData = await cachedFile.file.readAsBytes();
final decodedData = utf8.decode(cachedData); // 将字节列表解码为字符串 final decodedData = utf8.decode(cachedData); // 将字节列表解码为字符串
...@@ -371,7 +374,7 @@ class _CacheInterceptor extends Interceptor { ...@@ -371,7 +374,7 @@ class _CacheInterceptor extends Interceptor {
final requestBody = response.requestOptions.data.toString(); final requestBody = response.requestOptions.data.toString();
// 将 GET 请求的参数和请求体参数拼接成缓存的键 // 将 GET 请求的参数和请求体参数拼接成缓存的键
final cacheKey = requestBody.isEmpty ? url : '$url?$requestBody'; final cacheKey = requestBody.isEmpty ? url : '$url?$requestBody';
// Console.log('----------cacheKey-----------------------$cacheKey'); Console.log('----------cacheKey-----------------------$cacheKey');
// 将响应数据转换为字符串,并将其编码为字节列表 // 将响应数据转换为字符串,并将其编码为字节列表
List<int> bytes = utf8.encode(jsonEncode(response.data)); List<int> bytes = utf8.encode(jsonEncode(response.data));
Uint8List uint8List = Uint8List.fromList(bytes); Uint8List uint8List = Uint8List.fromList(bytes);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论