119 lines
3.7 KiB
Dart
119 lines
3.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
import 'package:ef/ef.dart';
|
|
import 'package:vbvs_app/common/color/appConstants.dart';
|
|
import 'package:vbvs_app/common/color/app_uri_status.dart';
|
|
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
|
import 'package:vbvs_app/common/util/MyUtils.dart';
|
|
import 'package:vbvs_app/enum/APPPackageType.dart';
|
|
import 'package:vbvs_app/model/api_response.dart';
|
|
|
|
Future<ApiResponse> requestWithLog({
|
|
required String logTitle,
|
|
required MyHttpMethod method,
|
|
required String queryUrl,
|
|
Map<String, dynamic>? data,
|
|
String successMsg = "操作成功",
|
|
String errorMsg = "操作失败",
|
|
void Function(ApiResponse res)? onSuccess,
|
|
void Function(ApiResponse res)? onFailure,
|
|
}) async {
|
|
EasyDartModule.logger.info(logTitle);
|
|
DailyLogUtils.writeLog(logTitle);
|
|
|
|
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
|
try {
|
|
String? language = "";
|
|
if (AppConstants().ent_type == APPPackageType.MHT.code) {
|
|
if (mhLanguageController.selectLanguage != null) {
|
|
language = mhLanguageController.selectLanguage.value!.language_code;
|
|
}
|
|
} else {
|
|
if (languageController.selectLanguage != null) {
|
|
language = languageController.selectLanguage.value!.language_code;
|
|
}
|
|
}
|
|
|
|
if (language != null && language.isNotEmpty) {
|
|
if (queryUrl.contains("?")) {
|
|
queryUrl += "&lang=$language";
|
|
} else {
|
|
queryUrl += "?lang=$language";
|
|
}
|
|
}
|
|
successMsg = successMsg.tr;
|
|
errorMsg = errorMsg.tr;
|
|
|
|
var response;
|
|
|
|
switch (method) {
|
|
case MyHttpMethod.get:
|
|
response = await EasyDartModule.dio.get(queryUrl);
|
|
break;
|
|
case MyHttpMethod.post:
|
|
response = data != null
|
|
? await EasyDartModule.dio.post(queryUrl, data: jsonEncode(data))
|
|
: await EasyDartModule.dio.post(queryUrl);
|
|
break;
|
|
case MyHttpMethod.put:
|
|
response = data != null
|
|
? await EasyDartModule.dio.put(queryUrl, data: jsonEncode(data))
|
|
: await EasyDartModule.dio.put(queryUrl);
|
|
break;
|
|
case MyHttpMethod.delete:
|
|
response = data != null
|
|
? await EasyDartModule.dio.delete(queryUrl, data: jsonEncode(data))
|
|
: await EasyDartModule.dio.delete(queryUrl);
|
|
break;
|
|
}
|
|
|
|
if (response != null) {
|
|
final responseData =
|
|
response.data is String ? jsonDecode(response.data) : response.data;
|
|
|
|
apiResponse = ApiResponse.fromJson(responseData, (object) => object,
|
|
rawResponse: response);
|
|
|
|
if (apiResponse.code == HttpStatusCodes.ok) {
|
|
MyUtils.formatResponse(apiResponse, successMsg, errorMsg);
|
|
onSuccess?.call(apiResponse);
|
|
} else {
|
|
apiResponse.msg = responseData['msg'] ?? errorMsg;
|
|
EasyDartModule.logger
|
|
.error("$logTitle ${responseData['msg'] ?? errorMsg}");
|
|
onFailure?.call(apiResponse);
|
|
}
|
|
return apiResponse;
|
|
} else {
|
|
apiResponse = ApiResponse(code: -1, msg: "失败".tr);
|
|
EasyDartModule.logger.error("$logTitle 失败");
|
|
onFailure?.call(apiResponse);
|
|
return apiResponse;
|
|
}
|
|
} catch (e) {
|
|
EasyDartModule.logger.error("$logTitle 失败->$e");
|
|
DailyLogUtils.writeError("$logTitle 失败->$e");
|
|
apiResponse.msg = e.toString();
|
|
onFailure?.call(apiResponse);
|
|
return apiResponse;
|
|
}
|
|
}
|
|
|
|
enum MyHttpMethod { get, post, put, delete }
|
|
|
|
extension HttpMethodExtension on MyHttpMethod {
|
|
String get name {
|
|
switch (this) {
|
|
case MyHttpMethod.get:
|
|
return 'GET';
|
|
case MyHttpMethod.post:
|
|
return 'POST';
|
|
case MyHttpMethod.put:
|
|
return 'PUT';
|
|
case MyHttpMethod.delete:
|
|
return 'DELETE';
|
|
}
|
|
}
|
|
}
|