115 lines
3.1 KiB
Dart
115 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:wallpaperx/common/components/easy_loading.dart';
|
|
import 'package:wallpaperx/common/http/http.dart';
|
|
import 'package:wallpaperx/common/utils/log_print.dart';
|
|
import 'package:wallpaperx/firebase/firebase_analytics_manager.dart';
|
|
|
|
///网络请求工具类
|
|
class HttpUtil {
|
|
HttpUtil._();
|
|
|
|
static final http = Http();
|
|
static Dio dio = http.dio;
|
|
|
|
static void get(
|
|
String url,
|
|
Function callback, {
|
|
Map<String, dynamic>? headerMap,
|
|
Map<String, dynamic>? params,
|
|
ResponseType? responseType,
|
|
Function? errorCallback,
|
|
bool? isBackResponse = false, //是否返回Response类型数据
|
|
bool? showLoading, //是否返回Response类型数据
|
|
}) async {
|
|
try {
|
|
Map<String, dynamic> headers = {};
|
|
if (params != null) {
|
|
params["use_whitelist"] = true;
|
|
LogPrint.d("params: ${params.toString()}");
|
|
}
|
|
Response response;
|
|
response = await dio.get(
|
|
url,
|
|
queryParameters: params,
|
|
options: Options(
|
|
responseType: responseType ?? ResponseType.json,
|
|
headers: headers,
|
|
method: "get"),
|
|
);
|
|
callback(isBackResponse == false ? response.toString() : response);
|
|
} catch (e) {
|
|
errorCallback!(e);
|
|
dismiss(dismiss: true);
|
|
LogPrint.d("e: $e");
|
|
showError("Server error", show: showLoading ?? true);
|
|
}
|
|
}
|
|
|
|
// post
|
|
static void post(
|
|
String url,
|
|
Function callback, {
|
|
Map<String, dynamic>? headerMap,
|
|
Map<String, dynamic>? params,
|
|
ResponseType? responseType,
|
|
Function? errorCallback,
|
|
bool? isBackResponse = false,
|
|
}) async {
|
|
try {
|
|
Map<String, dynamic> headers = {};
|
|
if (params != null) {
|
|
LogPrint.d("e: ${params.toString()}");
|
|
}
|
|
Response response;
|
|
response = await dio.post(url,
|
|
data: params,
|
|
options: Options(
|
|
responseType: responseType ?? ResponseType.json,
|
|
headers: headers,
|
|
method: "post"),
|
|
onSendProgress: (x, y) {});
|
|
callback(isBackResponse == false ? response.toString() : response);
|
|
} catch (e) {
|
|
errorCallback!(e);
|
|
LogPrint.d("e: $e");
|
|
showError("Server error");
|
|
dismiss(dismiss: true);
|
|
}
|
|
}
|
|
|
|
static void postFileImg(
|
|
String url,
|
|
Function callback, {
|
|
Map<String, dynamic>? headerMap,
|
|
required File file,
|
|
ResponseType? responseType,
|
|
Function? errorCallback,
|
|
bool? isBackResponse = false,
|
|
}) async {
|
|
try {
|
|
Map<String, dynamic> headers = {};
|
|
Response response;
|
|
|
|
String base64Image = base64Encode(file.readAsBytesSync());
|
|
FormData formData = FormData.fromMap({
|
|
"file": base64Image,
|
|
});
|
|
response = await dio.post(
|
|
url,
|
|
data: formData,
|
|
options: Options(
|
|
headers: headers,
|
|
method: "post",
|
|
contentType: "multipart/form-data",
|
|
),
|
|
);
|
|
callback(isBackResponse == false ? response.toString() : response);
|
|
} catch (e) {
|
|
LogPrint.d("e: $e");
|
|
}
|
|
}
|
|
}
|