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';
|
|
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);
|
|
var httpClient = HttpClient();
|
httpClient.connectionTimeout = const Duration(seconds: 20);
|
var uri = Uri(
|
scheme: "http",
|
host: "api.location.izzql.com",
|
path: api,
|
port: 8090,
|
queryParameters: params);
|
|
// var uri = Uri(
|
// scheme: "http",
|
// host: "192.168.3.122",
|
// path: api,
|
// port: 8082,
|
// queryParameters: params);
|
|
print("uri:$uri");
|
|
if (onStart != null) {
|
onStart();
|
}
|
HttpRequestResult requestResult;
|
|
try {
|
HttpClientRequest request = await httpClient.postUrl(uri);
|
var response = await request.close();
|
if (response.statusCode == HttpStatus.ok) {
|
String result = await response.transform(const Utf8Decoder()).join();
|
requestResult = HttpRequestResult(true, jsonDecode(result));
|
} else {
|
requestResult = HttpRequestResult(false, null, msg: "网络请求失败");
|
}
|
} on TimeoutException catch (_) {
|
requestResult = HttpRequestResult(false, null, msg: "网络请求超时");
|
} on SocketException catch (_) {
|
Fluttertoast.showToast(msg: "网络请求出错");
|
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;
|
}
|
}
|