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

缓存key 问题

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