import 'dart:async';
|
import 'dart:convert';
|
import 'dart:io';
|
|
import 'package:device_info/device_info.dart';
|
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
import 'package:fluttertoast/fluttertoast.dart';
|
import '../utils/string_util.dart';
|
import '../utils/user_util.dart';
|
import '../utils/app_util.dart';
|
import '../utils/global.dart';
|
import '../utils/encrypt_util.dart';
|
import '../ui/widget/dialog.dart';
|
import '../utils/ui_utils.dart';
|
import '../model/common/http_model.dart';
|
import 'package:dio/dio.dart';
|
import 'package:dio/adapter.dart';
|
|
typedef OnHttpRequestFinish = void Function(HttpRequestResult result);
|
|
typedef OnHttpRequestStart = void Function();
|
|
showLoading(BuildContext context) {
|
//先丢失焦点
|
FocusScope.of(context).unfocus();
|
//开启加载框
|
DialogUtil.showDialog(context, LoadingDialog(""));
|
}
|
|
dismissDialog(BuildContext context) {
|
Navigator.pop(context);
|
}
|
|
class HttpUtil {
|
static AndroidDeviceInfo? _androidInfo;
|
static IosDeviceInfo? _iosInfo;
|
|
static _getSign(Map<String, dynamic> params) {
|
List list = [];
|
//签名
|
params.forEach((key, value) {
|
list.add("$key=$value");
|
});
|
//排序
|
list.sort();
|
String signStr = "";
|
list.forEach((element) {
|
signStr += element + "&";
|
});
|
|
signStr += "@#\$^234AB&c&fg&.==888";
|
|
return EncryptUtil.MD5(signStr);
|
}
|
|
static Future<Map<String, dynamic>> getBaseParams(
|
Map<String, dynamic>? params) async {
|
params ??= {};
|
|
if (Platform.isAndroid) {
|
if (_androidInfo == null) {
|
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
_androidInfo = await deviceInfo.androidInfo;
|
}
|
} else if (Platform.isIOS) {
|
if (_iosInfo == null) {
|
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
_iosInfo = await deviceInfo.iosInfo;
|
}
|
}
|
|
params["version"] = (await AppUtil.getVersionCode()).toString();
|
|
print("androidInfo:${_androidInfo!.version}");
|
|
//添加附加参数
|
params["timestamp"] = DateTime.now().millisecondsSinceEpoch.toString();
|
params["platform"] = Platform.isAndroid ? "android" : "ios";
|
params["packages"] = "com.dw.zzql";
|
if (Platform.isAndroid) {
|
if (Global.utdId != null) {
|
params["utdId"] = Global.utdId;
|
}
|
params["osVersion"] = _androidInfo!.version.release;
|
} else if (Platform.isIOS) {
|
params["device"] = _iosInfo!.identifierForVendor;
|
}
|
|
if (Global.channel != null) {
|
params["channel"] = Global.channel;
|
}
|
|
params["sign"] = _getSign(params);
|
|
return params;
|
}
|
|
static Future<HttpRequestResult> baseRequest(
|
String api, Map<String, dynamic> params, OnHttpRequestStart? onStart,
|
{bool notifyError = false}) async {
|
// params ??= {};
|
params = await getBaseParams(params);
|
|
if (onStart != null) {
|
onStart();
|
}
|
HttpRequestResult requestResult;
|
|
try {
|
var dio = Dio()
|
..options = BaseOptions(
|
baseUrl: "http://api.hanju.goxcw.com:8089/BuWan",
|
connectTimeout: 20000,
|
receiveTimeout: 1000 * 60,
|
contentType: "application/x-www-form-urlencoded");
|
//设置代理
|
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
|
(HttpClient client) {
|
client.findProxy = (uri) {
|
return 'PROXY 192.168.3.122:8888';
|
};
|
client.badCertificateCallback =
|
(X509Certificate cert, String host, int port) => true;
|
};
|
|
// FormData formData = FormData.fromMap(params);
|
var response = await dio.post(
|
api,
|
data: params,
|
onSendProgress: (int sent, int total) {
|
print('$sent $total');
|
},
|
);
|
if (response.statusCode == HttpStatus.ok) {
|
String result = response.data.toString();
|
print("网络请求结果:$result");
|
requestResult = HttpRequestResult(true, jsonDecode(result));
|
} else {
|
requestResult = HttpRequestResult(false, null, msg: "网络请求失败");
|
}
|
} on DioError catch (_) {
|
if (_.type == DioErrorType.connectTimeout ||
|
_.type == DioErrorType.receiveTimeout ||
|
_.type == DioErrorType.sendTimeout) {
|
requestResult = HttpRequestResult(false, null, msg: "网络请求超时");
|
} else {
|
requestResult = HttpRequestResult(false, null, msg: "网络请求出错");
|
}
|
} catch (e) {
|
requestResult = HttpRequestResult(false, null, msg: "网络请求出错");
|
}
|
if (notifyError && !requestResult.success) {
|
ToastUtil.toast(requestResult.msg!);
|
}
|
return requestResult;
|
}
|
}
|
|
class UserApiUtil {
|
///验证码发送
|
static Future<Map<String, dynamic>?> sendSMS(
|
BuildContext context, String phone) async {
|
var result =
|
await HttpUtil.baseRequest("/api/v1/sms/sendSMS", {"phone": phone}, () {
|
showLoading(context);
|
});
|
dismissDialog(context);
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
|
static Future<Map<String, dynamic>?> uploadPushRegId(
|
BuildContext context, String regId) async {
|
var uid = await UserUtil.getUid();
|
var params = {"regId": regId};
|
if (uid != null) {
|
params["uid"] = uid.toString();
|
}
|
|
var result = await HttpUtil.baseRequest(
|
"/api/v1/user/uploadPushRegId", params, () {});
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
|
///登录
|
static Future<Map<String, dynamic>?> login(
|
BuildContext context, String phone, String vcode, String token) async {
|
Map<String, dynamic> params = {};
|
params["phone"] = phone;
|
if (!StringUtil.isNullOrEmpty(vcode)) {
|
params["vcode"] = vcode;
|
}
|
|
if (!StringUtil.isNullOrEmpty(token)) {
|
params["token"] = token;
|
}
|
|
var result =
|
await HttpUtil.baseRequest("/api/v1/user/loginPhone", params, () {
|
showLoading(context);
|
}, notifyError: true);
|
dismissDialog(context);
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
|
///获取用户信息
|
static Future<Map<String, dynamic>?> getUserInfo(
|
BuildContext context, int uid) async {
|
Map<String, dynamic> params = {};
|
params["uid"] = uid.toString();
|
var result =
|
await HttpUtil.baseRequest("/api/v1/user/getUserInfo", params, () {});
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
|
///退出登录
|
static Future<Map<String, dynamic>?> logout(
|
BuildContext context, int? uid) async {
|
Map<String, dynamic> params = {};
|
params["uid"] = uid.toString();
|
var result =
|
await HttpUtil.baseRequest("/api/v1/user/logout", params, () {});
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
}
|
|
class FeedBackApiUtil {
|
///查阅向我求救的SOS
|
static Future<Map<String, dynamic>?> advice(
|
BuildContext context, String? type, String content) async {
|
var uid = await UserUtil.getUid();
|
Map<String, dynamic> params = {};
|
if (uid != null) {
|
params["uid"] = uid.toString();
|
}
|
|
if (type != null) {
|
params["type"] = type;
|
}
|
|
params["content"] = content;
|
var result =
|
await HttpUtil.baseRequest("/api/v1/feedback/advice", params, () {
|
showLoading(context);
|
}, notifyError: true);
|
|
dismissDialog(context);
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
}
|
|
class ConfigApiUtil {
|
///查阅向我求救的SOS
|
static Future<Map<String, dynamic>?> getConfig() async {
|
var result = await HttpUtil.baseRequest(
|
"/api/v1/config/getConfig", {}, () {},
|
notifyError: true);
|
if (result.success) {
|
return result.data;
|
}
|
return null;
|
}
|
}
|