You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.1 KiB
120 lines
3.1 KiB
4 years ago
|
import 'package:akuCommunity/utils/logger/logger_data.dart';
|
||
4 years ago
|
import 'package:akuCommunity/utils/network/base_model.dart';
|
||
4 years ago
|
import 'package:bot_toast/bot_toast.dart';
|
||
|
import 'package:dio/dio.dart';
|
||
|
|
||
|
import 'package:akuCommunity/constants/api.dart';
|
||
4 years ago
|
import 'package:logger/logger.dart';
|
||
4 years ago
|
|
||
|
class NetUtil {
|
||
|
Dio _dio;
|
||
4 years ago
|
Logger _logger;
|
||
4 years ago
|
static final NetUtil _netUtil = NetUtil._internal();
|
||
|
|
||
|
factory NetUtil() => _netUtil;
|
||
|
|
||
4 years ago
|
Dio get dio => _dio;
|
||
|
|
||
4 years ago
|
NetUtil._internal() {
|
||
4 years ago
|
_logger = Logger(
|
||
|
printer: PrettyPrinter(
|
||
|
methodCount: 2,
|
||
|
errorMethodCount: 4,
|
||
|
));
|
||
4 years ago
|
BaseOptions options = BaseOptions(
|
||
4 years ago
|
baseUrl: '${API.host}/app',
|
||
4 years ago
|
connectTimeout: API.networkTimeOut,
|
||
|
receiveTimeout: API.networkTimeOut,
|
||
|
sendTimeout: API.networkTimeOut,
|
||
|
headers: {},
|
||
|
);
|
||
|
if (_dio == null) _dio = Dio(options);
|
||
|
}
|
||
|
|
||
4 years ago
|
///call auth after login
|
||
|
auth(String token) {
|
||
|
_logger.d('setToken@$token');
|
||
|
_dio.options.headers.putIfAbsent('App-Admin-Token', () => token);
|
||
|
}
|
||
|
|
||
4 years ago
|
Future<BaseModel> get(
|
||
|
String path, {
|
||
|
Map<String, dynamic> params,
|
||
4 years ago
|
bool showMessage = false,
|
||
4 years ago
|
}) async {
|
||
|
try {
|
||
|
Response res = await _dio.get(path, queryParameters: params);
|
||
4 years ago
|
_logger.v({
|
||
|
'path': res.request.path,
|
||
|
'header': res.request.headers,
|
||
|
'params': res.request.queryParameters,
|
||
|
'data': res.data,
|
||
|
});
|
||
|
LoggerData.addData(res);
|
||
4 years ago
|
BaseModel baseModel = BaseModel.fromJson(res.data);
|
||
|
_parseRequestError(baseModel, showMessage: showMessage);
|
||
|
return baseModel;
|
||
|
} on DioError catch (e) {
|
||
|
_parseErr(e);
|
||
|
}
|
||
|
return BaseModel.err();
|
||
|
}
|
||
|
|
||
|
Future<BaseModel> post(
|
||
|
String path, {
|
||
|
Map<String, dynamic> params,
|
||
|
bool showMessage = false,
|
||
|
}) async {
|
||
|
try {
|
||
|
Response res = await _dio.post(path, data: params);
|
||
|
_logger.v({
|
||
|
'path': res.request.path,
|
||
|
'header': res.request.headers,
|
||
|
'params': res.request.queryParameters,
|
||
|
'data': res.data,
|
||
|
});
|
||
|
LoggerData.addData(res);
|
||
|
BaseModel baseModel = BaseModel.fromJson(res.data);
|
||
|
_parseRequestError(baseModel, showMessage: showMessage);
|
||
|
|
||
|
return baseModel;
|
||
4 years ago
|
} on DioError catch (e) {
|
||
|
_parseErr(e);
|
||
|
}
|
||
|
return BaseModel.err();
|
||
|
}
|
||
|
|
||
|
_parseErr(DioError err) {
|
||
4 years ago
|
_logger.v({
|
||
|
'type': err.type.toString(),
|
||
|
'message': err.message,
|
||
|
});
|
||
|
LoggerData.addData(err);
|
||
4 years ago
|
_makeToast(String message) {
|
||
4 years ago
|
BotToast.showText(text: '$message\_${err?.response?.statusCode ?? ''}');
|
||
4 years ago
|
}
|
||
|
|
||
|
switch (err.type) {
|
||
|
case DioErrorType.CONNECT_TIMEOUT:
|
||
|
case DioErrorType.SEND_TIMEOUT:
|
||
|
case DioErrorType.RECEIVE_TIMEOUT:
|
||
|
_makeToast('连接超时');
|
||
|
break;
|
||
|
case DioErrorType.RESPONSE:
|
||
|
_makeToast('服务器出错');
|
||
|
break;
|
||
|
case DioErrorType.CANCEL:
|
||
|
break;
|
||
|
case DioErrorType.DEFAULT:
|
||
|
_makeToast('未知错误');
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
_parseRequestError(BaseModel model, {bool showMessage = false}) {
|
||
|
if (!model.status || showMessage) {
|
||
|
BotToast.showText(text: model.message);
|
||
|
}
|
||
4 years ago
|
}
|
||
|
}
|