185 lines
5.2 KiB
Dart
185 lines
5.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'BaseEntity.dart';
|
|
import 'DioLogInterceptor.dart';
|
|
import 'NetworkConfig.dart';
|
|
|
|
enum RequestMethod {
|
|
Post,
|
|
Get,
|
|
}
|
|
|
|
const RequestMethodValues = {
|
|
RequestMethod.Get: "GET",
|
|
RequestMethod.Post: "POST",
|
|
};
|
|
|
|
class RequestCenter {
|
|
factory RequestCenter() => _getInstance();
|
|
|
|
static RequestCenter get instance => _getInstance();
|
|
static RequestCenter? _instance;
|
|
|
|
Dio? _dio, _dioLog;
|
|
|
|
final dio = Dio();
|
|
|
|
void setupDio() {
|
|
dio.options.baseUrl = 'https://openapi.shhuanmeng.com/';
|
|
dio.interceptors.add(InterceptorsWrapper(
|
|
onRequest: (options, handler) {
|
|
// 可以在这里添加其他请求配置
|
|
return handler.next(options);
|
|
},
|
|
onResponse: (response, handler) {
|
|
// 处理响应
|
|
return handler.next(response);
|
|
},
|
|
onError: (DioError e, handler) {
|
|
// 处理错误
|
|
return handler.next(e);
|
|
},
|
|
));
|
|
}
|
|
|
|
|
|
RequestCenter._internal() {
|
|
setup();
|
|
setupDio();
|
|
}
|
|
|
|
static RequestCenter _getInstance() {
|
|
_instance ??= RequestCenter._internal();
|
|
//域名不一致,重新初始化
|
|
if (_instance!._dio != null &&
|
|
NetworkConfig.ServerDomain_Online != _instance!._dio!.options.baseUrl) {
|
|
_instance!._dio!.options.baseUrl = NetworkConfig.ServerDomain_Online;
|
|
}
|
|
return _instance!;
|
|
}
|
|
|
|
void setup() {
|
|
// 初始化
|
|
if (null == _dio) {
|
|
_dio = Dio(BaseOptions(
|
|
baseUrl: NetworkConfig.ServerDomain_Online,
|
|
sendTimeout: const Duration(seconds: 10),
|
|
receiveTimeout: const Duration(seconds: 20),
|
|
connectTimeout: const Duration(seconds: 20),
|
|
contentType: Headers.jsonContentType,
|
|
responseType: ResponseType.json));
|
|
_dio!.interceptors.add(DioLogInterceptor());
|
|
/* _dio.interceptors.add(new ResponseInterceptors());*/
|
|
_dioLog = Dio(BaseOptions(
|
|
sendTimeout: const Duration(seconds: 10),
|
|
receiveTimeout: const Duration(seconds: 20),
|
|
connectTimeout: const Duration(seconds: 20),
|
|
contentType: Headers.jsonContentType,
|
|
responseType: ResponseType.json));
|
|
//_dioLog!.interceptors.add(DioLogInterceptor());
|
|
|
|
// (_dio?.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
|
|
// client.findProxy = (uri) {
|
|
// return "PROXY 192.168.1.11:8888";
|
|
// };
|
|
// //抓Https包设置
|
|
// client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
|
|
// };
|
|
|
|
}
|
|
}
|
|
|
|
// 网络请求默认为post
|
|
Future<BaseEntity?> request(
|
|
path,
|
|
Map<String, dynamic> parameters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
try {
|
|
// Map<String, dynamic> general = {
|
|
// "deviceNumber": NetworkConfig.deviceID,
|
|
// };
|
|
// parameters.addAll(general);
|
|
//签名加密
|
|
Map<String, dynamic> parametersSign = sign(parameters);
|
|
Response response = await _dio!.post(path, data: parametersSign,
|
|
options: Options(
|
|
headers: {
|
|
'Authorization': "Bearer ${NetworkConfig.token}",
|
|
"Channel":NetworkConfig.Channel,
|
|
"Platform":NetworkConfig.Platform,
|
|
"Version":NetworkConfig.Version,
|
|
"Language":NetworkConfig.Language,
|
|
},
|
|
));
|
|
BaseEntity entity = BaseEntity.PlayfromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} catch (e) {
|
|
error(ErrorEntity(code: -1, message: "$e"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> requestGet(
|
|
path,
|
|
Map<String, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
try {
|
|
//FormData formData = FormData.fromMap(parmeters);
|
|
Response response = await _dio!.get(path, queryParameters: parmeters,
|
|
options: Options(
|
|
headers: {
|
|
'Authorization': "Bearer ${NetworkConfig.token}"
|
|
},
|
|
));
|
|
BaseEntity entity = BaseEntity.PlayfromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} catch (e) {
|
|
error(ErrorEntity(code: -1, message: "$e"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
Map<String, dynamic> sign(Map<String, dynamic> parmeters) {
|
|
List<String> keys = parmeters.keys.toList();
|
|
// key排序
|
|
keys.sort((a, b) {
|
|
List<int> al = a.codeUnits;
|
|
List<int> bl = b.codeUnits;
|
|
for (int i = 0; i < al.length; i++) {
|
|
if (bl.length <= i) return 1;
|
|
if (al[i] > bl[i]) {
|
|
return 1;
|
|
} else if (al[i] < bl[i]) return -1;
|
|
}
|
|
return 0;
|
|
});
|
|
Map<String, dynamic> treeMap = Map();
|
|
keys.forEach((element) {
|
|
treeMap[element] = parmeters[element];
|
|
});
|
|
String data = "";
|
|
for (dynamic str in treeMap.values) {
|
|
if (str != null) {
|
|
data = data + str.toString();
|
|
}
|
|
}
|
|
treeMap["sign"] = generateMd5(data + NetworkConfig.signKey);
|
|
return treeMap;
|
|
}
|
|
|
|
String generateMd5(String data) {
|
|
return md5.convert(utf8.encode(data)).toString();
|
|
}
|
|
}
|