app-starlock/star_lock/lib/network/api_provider_base.dart
2023-11-18 10:38:13 +08:00

89 lines
2.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../tools/toast.dart';
import 'package:get/get.dart';
import '../appRouters.dart';
import 'api.dart';
import 'request_interceptor.dart';
import 'request_interceptor_log.dart';
import 'response_interceptor.dart';
import 'response_interceptor_log.dart';
class BaseProvider extends GetConnect with Api {
@override
void onInit() {
httpClient.baseUrl = baseUrl;
httpClient.addRequestModifier(requestInterceptor);
httpClient.addResponseModifier(responseInterceptor);
httpClient.addRequestModifier(requestLogInterceptor);
httpClient.addResponseModifier(responseLogInterceptor);
httpClient.timeout = 15.seconds;
super.onInit();
}
@override
Future<Response<T>> post<T>(String? url, body,
{String? contentType,
Map<String, String>? headers,
Map<String, dynamic>? query,
Decoder<T>? decoder,
Progress? uploadProgress,
bool? isUnShowLoading = false}) async {
// print("post: url:${url} body:${body} contentType:${contentType} headers:${headers} query:${query}");
if (isUnShowLoading == false) EasyLoading.show();
print('星锁请求body体为${body}');
var res = await super.post(url, body,
contentType: contentType,
headers: headers,
query: query,
decoder: decoder,
uploadProgress: uploadProgress);
if (res.body == null) {
if (EasyLoading.isShow) EasyLoading.dismiss(animation: true);
var rs = {
"errorMsg": "Network Error!",
"errorCode": -1,
"data": null,
"description": "表示成功或是。"
};
return Response(
request: res.request,
statusCode: -1,
bodyString: res.bodyString,
bodyBytes: res.bodyBytes,
body: rs as T,
statusText: res.statusText,
);
}else{
}
// print('得到的数据======>bodyString:${res.bodyString} body:${res.body} bodyBytes:${res.bodyBytes} status:${res.status} statusText:${res.statusText} statusCode:${res.statusCode}');
getDataResult(res.body);
return res;
}
getDataResult(T) {
switch (T["errorCode"]) {
case 403:
Get.offNamedUntil(Routers.starLockLoginPage, (route) => false);
break;
case 500:
Toast.show(msg: "服务器错误");
break;
case 421:
case 422:
case 430: //与葛工约定弹出ErrorMsg
Toast.show(msg: T["errorMsg"]);
break;
//已单独处理,暂不做全局弹窗
// case 425:
// Toast.show(msg: "用户不存在");
// break;
case 10001:
Toast.show(msg: "数据不存在");
break;
}
}
}