初始提交
This commit is contained in:
95
bin/util/MyUtils.dart
Normal file
95
bin/util/MyUtils.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import '../model/api_response.dart';
|
||||
import '../model/app_uri_status.dart';
|
||||
|
||||
class MyUtils {
|
||||
static ApiResponse formatResponse(
|
||||
ApiResponse res,
|
||||
String successMsg,
|
||||
String errorMsg,
|
||||
) {
|
||||
if (res.code == HttpStatusCodes.ok) {
|
||||
// 成功但 msg 为空时填默认成功提示
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = successMsg;
|
||||
}
|
||||
} else {
|
||||
// 失败且 msg 为空时填默认失败提示
|
||||
if (res.msg == null || res.msg!.isEmpty) {
|
||||
res.msg = errorMsg;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// 获取最近24小时的开始时间的毫秒数
|
||||
static int getStartOfLast24HoursMilliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime startOfLast24Hours = now.subtract(Duration(hours: 24));
|
||||
return startOfLast24Hours.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取当前时间的毫秒数作为最近24小时的结束时间
|
||||
static int getEndOfLast24HoursMilliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
return now.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
static int getStartRangeFromNDaysAgoToNextDay(int n) {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime start = DateTime(now.year, now.month, now.day - n - 1, 18, 0, 0);
|
||||
DateTime end = start.add(Duration(hours: 24));
|
||||
return start.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
static int getEndRangeFromNDaysAgoToNextDay(int n) {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime start = DateTime(now.year, now.month, now.day - n - 1, 18, 0, 0);
|
||||
DateTime end = start.add(Duration(hours: 24));
|
||||
return end.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取当天前一天的下午18:00的时间戳
|
||||
static int getYesterday18Milliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime yesterday18 = DateTime(now.year, now.month, now.day - 1, 18, 0, 0);
|
||||
return yesterday18.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取当天17:00的时间戳
|
||||
static int getToday17Milliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime today17 = DateTime(now.year, now.month, now.day, 17, 0, 0);
|
||||
return today17.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取前一天的开始时间戳(前一天的00:00:00)
|
||||
static int getStartOfYesterdayMilliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime startOfYesterday =
|
||||
DateTime(now.year, now.month, now.day - 1, 0, 0, 0);
|
||||
return startOfYesterday.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取前一天的结束时间戳(前一天的23:59:59)
|
||||
static int getEndOfYesterdayMilliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime endOfYesterday =
|
||||
DateTime(now.year, now.month, now.day - 1, 23, 59, 59);
|
||||
return endOfYesterday.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取前一天的开始时间戳(前一天的00:00:00)
|
||||
static int getStartOfCurrentMilliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime startOfYesterday = DateTime(now.year, now.month, now.day, 0, 0, 0);
|
||||
return startOfYesterday.millisecondsSinceEpoch;
|
||||
}
|
||||
|
||||
// 获取前一天的结束时间戳(前一天的23:59:59)
|
||||
static int getEndOfCurrentMilliseconds() {
|
||||
DateTime now = DateTime.now();
|
||||
DateTime endOfYesterday =
|
||||
DateTime(now.year, now.month, now.day, 23, 59, 59);
|
||||
return endOfYesterday.millisecondsSinceEpoch;
|
||||
}
|
||||
}
|
||||
102
bin/util/requestWithLog.dart
Normal file
102
bin/util/requestWithLog.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
|
||||
import '../model/api_response.dart';
|
||||
import '../model/app_uri_status.dart';
|
||||
import '../util/MyUtils.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);
|
||||
|
||||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败");
|
||||
try {
|
||||
String? language = "";
|
||||
if (language != null && language.isNotEmpty) {
|
||||
if (queryUrl.contains("?")) {
|
||||
queryUrl += "&lang=$language";
|
||||
} else {
|
||||
queryUrl += "?lang=$language";
|
||||
}
|
||||
}
|
||||
successMsg = successMsg;
|
||||
errorMsg = errorMsg;
|
||||
|
||||
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 || apiResponse.code == 20000) {
|
||||
MyUtils.formatResponse(apiResponse, successMsg, errorMsg);
|
||||
onSuccess?.call(apiResponse);
|
||||
} else {
|
||||
print("[网络请求失败:$response]");
|
||||
apiResponse.msg = responseData['msg'] ?? errorMsg;
|
||||
onFailure?.call(apiResponse);
|
||||
}
|
||||
|
||||
return apiResponse;
|
||||
} else {
|
||||
apiResponse = ApiResponse(code: -1, msg: "失败");
|
||||
onFailure?.call(apiResponse);
|
||||
return apiResponse;
|
||||
}
|
||||
} catch (e) {
|
||||
print("[网络请求异常:$e]");
|
||||
EasyDartModule.logger.error("$logTitle 失败->$e");
|
||||
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';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user