562 lines
18 KiB
Dart
562 lines
18 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:dio/io.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;
|
|
|
|
static String signKey = "5159d59637fe1b79ba789e87443e8282";
|
|
|
|
Dio? _dio, _dioLog;
|
|
|
|
final dio = Dio();
|
|
|
|
void setupDio() {
|
|
dio.options.baseUrl = 'https://api.minimax.chat/v1/text/chatcompletion_pro?GroupId=1810530961268412805';
|
|
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());
|
|
}
|
|
}
|
|
|
|
// 网络请求默认为post
|
|
Future<BaseEntity?> request(
|
|
path,
|
|
Map<Object, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
try {
|
|
//FormData formData = FormData.fromMap(parmeters);
|
|
Response response = await _dio!.post(path, data: parmeters);
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} catch (e) {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> OpenAiTTS(
|
|
String parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
) async {
|
|
try {
|
|
final Dio dio = Dio();
|
|
const String apiKey =
|
|
'hk-fgecry10000349243fbf9fa42fdd30895ab0d3784d710997';
|
|
const String apiUrl = 'https://api.openai-hk.com/v1/audio/speech';
|
|
|
|
final response = await dio.post(
|
|
apiUrl,
|
|
options: Options(
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer $apiKey',
|
|
},
|
|
),
|
|
data: json.encode({
|
|
"model": "tts-1",
|
|
"input": "Today is a wonderful day to build something people love!",
|
|
"voice": "nova",
|
|
"response_format": "mp3",
|
|
"speed": 1.0
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final Map<String, dynamic> responseData = response.data;
|
|
// 从响应数据中获取 TTS 的具体内容(假设是 'audio' 字段)
|
|
final String audioContent = responseData['audio'];
|
|
print('TTS audio content: $audioContent');
|
|
} else {}
|
|
BaseEntity entity = BaseEntity.TTSfromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} on DioError catch (e) {
|
|
print('Error: ${e.response?.data}');
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> ttsRequest(
|
|
Map<String, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
) async {
|
|
try {
|
|
print('Request: $parmeters');
|
|
|
|
final dio = Dio();
|
|
const url = 'http://101.43.19.200:83/tts'; // 代理配置的路径
|
|
final headers = {
|
|
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
};
|
|
/*final encodedData = parmeters.entries
|
|
.map((e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
|
|
.join('&');*/
|
|
String encodedData = parmeters.entries
|
|
.map((entry) =>
|
|
'${Uri.encodeComponent(entry.key)}=${Uri.encodeComponent(entry.value)}')
|
|
.join('&');
|
|
final response = await dio.post(url,
|
|
data: encodedData, options: Options(headers: headers));
|
|
print('Response: ${response.data}');
|
|
BaseEntity entity = BaseEntity.TTSfromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} catch (e) {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> sendRequest(
|
|
Map<Object, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
) async {
|
|
try {
|
|
print('Request: $parmeters');
|
|
/*final Dio dio = Dio();
|
|
const String apiKey =
|
|
'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';
|
|
const String apiUrl = 'https://claude.1372350891.workers.dev/v1/messages';
|
|
|
|
final response = await dio.post(
|
|
apiUrl,
|
|
options: Options(
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'x-api-key': apiKey,
|
|
'anthropic-version': '2023-06-01',
|
|
},
|
|
),
|
|
// data: json.encode(parmeters),
|
|
);*/
|
|
|
|
final response = await dio.post('/messages',
|
|
data: parmeters,
|
|
options: Options(
|
|
contentType: Headers.jsonContentType,
|
|
responseType: ResponseType.json,
|
|
headers: {
|
|
'x-api-key':
|
|
'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',
|
|
'anthropic-version': "2023-06-01"
|
|
},
|
|
));
|
|
print('Response: ${response.data}');
|
|
|
|
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} on DioError catch (e) {
|
|
print('Error: ${e.response?.data}');
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> sendRequest2(
|
|
Map<Object, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
) async {
|
|
try {
|
|
print('Request: $parmeters');
|
|
final response = await dio.post('',
|
|
data: parmeters,
|
|
options: Options(
|
|
contentType: Headers.jsonContentType,
|
|
responseType: ResponseType.json,
|
|
headers: {
|
|
/* 'x-api-key':
|
|
'sk-OvOa1b25ba5913525c34b1e9c032f888ce337f21567VFtWh',
|
|
'anthropic-version': '2023-06-01',*/
|
|
"Authorization": "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJHcm91cE5hbWUiOiLlvKDlsZU4MjI4IiwiVXNlck5hbWUiOiLlvKDlsZU4MjI4IiwiQWNjb3VudCI6IiIsIlN1YmplY3RJRCI6IjE4MTA1MzA5NjEyNzI2MDcxNzQiLCJQaG9uZSI6IjE3NjIxMjUxMzcyIiwiR3JvdXBJRCI6IjE4MTA1MzA5NjEyNjg0MTI4MDUiLCJQYWdlTmFtZSI6IiIsIk1haWwiOiIiLCJDcmVhdGVUaW1lIjoiMjAyNC0wNy0yOSAyMTo1NTo0NSIsImlzcyI6Im1pbmltYXgifQ.C5qPhBvQo6M0Bf_prE-szprwLS5VGw4UV93ADmjwXb0Fno9To4mZM37id6EIKMOx9c-8NHN5IINodjV7pWMX8IB7onc1DBrDjSWIUNBPeHmaKPdtpTVItHsbLZWtGrCLLs8wIGqRoqmbgSIRENSVRTWyTsgUuT8UQmRdnNbObMqupz67usUnP_ddxUnW6tTXGumoz8vof43xaBTFO3-gdlmw9q9JdqfSTOkGHWhrn-oC4-JgvtBZMP6rXp3YK6Da4OQQMr5uzFdXVNHhpwySxGucAxE-KHwGewJXN-JVUvCkbUu9ozgu9IjYuk2Z3HmzBagFHbsx9t-2rDiCkO436g",
|
|
"Content-Type": "application/json"
|
|
},
|
|
));
|
|
print('Response: ${response.data}');
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} on DioError catch (e) {
|
|
print('Error: ${e.response?.data}');
|
|
}
|
|
}
|
|
|
|
//特殊处理网络请求默认为post
|
|
Future<BaseEntity?> request1(
|
|
path,
|
|
Map<String, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
Map<String, dynamic> headers = {
|
|
"AppId": NetworkConfig.AppId,
|
|
"userId": NetworkConfig.userId,
|
|
"Version": NetworkConfig.Version,
|
|
"Language": NetworkConfig.Language
|
|
};
|
|
parmeters.addAll(headers);
|
|
Map<String, dynamic> parmetersSign = sign(parmeters);
|
|
try {
|
|
/*由于服务器是表单结构*/
|
|
FormData formData = FormData.fromMap(parmetersSign);
|
|
Response response = await _dio!.post(path, data: formData);
|
|
if (response != null && response.statusCode == 200) {
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} else {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> requestLog(
|
|
path,
|
|
Map<String, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
Map<String, dynamic> headers = {
|
|
"AppId": NetworkConfig.AppId,
|
|
/*"BossId": NetworkConfig.BossId,*/
|
|
"userId": NetworkConfig.userId,
|
|
"Version": NetworkConfig.Version,
|
|
"Language": NetworkConfig.Language
|
|
};
|
|
parmeters.addAll(headers);
|
|
Map<String, dynamic> parmetersSign = sign(parmeters);
|
|
try {
|
|
/*由于服务器是表单结构*/
|
|
FormData formData = FormData.fromMap(parmetersSign);
|
|
Response response = await _dioLog!.post(path, data: formData);
|
|
if (response != null && response.statusCode == 200) {
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} else {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 网络请求默认为post
|
|
Future<BaseEntity?> requestPay(
|
|
path,
|
|
Map<String, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
Map<String, dynamic> headers = {
|
|
"AppId": NetworkConfig.AppId,
|
|
/*"BossId": NetworkConfig.BossId,*/
|
|
"userId": NetworkConfig.userId,
|
|
"Version": NetworkConfig.Version,
|
|
"Language": NetworkConfig.Language
|
|
};
|
|
parmeters.addAll(headers);
|
|
Map<String, dynamic> parmetersSign = sign(parmeters);
|
|
try {
|
|
FormData formData = FormData.fromMap(parmetersSign);
|
|
Response response = await _dio!.post(path, data: formData);
|
|
if (response != null && response.statusCode == 200) {
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
success(entity);
|
|
return entity;
|
|
} else {
|
|
error(ErrorEntity(code: -1, message: "未知错误"));
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
ErrorEntity(code: -1, message: "未知错误");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 网络请求默认为post
|
|
Future<BaseEntity?> requestComfyUI1(
|
|
formData,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
try {
|
|
var response = await _dio!.post(
|
|
"http://192.168.1.103:8188${NetworkConfig.uploadImg}",
|
|
data: formData,
|
|
options: Options(
|
|
contentType: Headers.jsonContentType,
|
|
responseType: ResponseType.json,
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
},
|
|
),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
BaseEntity entity = BaseEntity.PlayfromComfyUi(response.data);
|
|
success(entity);
|
|
return entity;
|
|
}
|
|
} catch (e) {
|
|
print("e" + e.toString());
|
|
//error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<BaseEntity?> requestComfyUI2(
|
|
formData,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
try {
|
|
var response = await _dio!.post(
|
|
"http://${NetworkConfig.serverAddress}${NetworkConfig.prompt}",
|
|
data: formData,
|
|
options: Options(
|
|
contentType: Headers.jsonContentType,
|
|
responseType: ResponseType.json,
|
|
),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
BaseEntity entity = BaseEntity.PlayfromComfyUi(response.data);
|
|
success(entity);
|
|
return entity;
|
|
}
|
|
} catch (e) {
|
|
print("prompt:$e");
|
|
//error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 网络请求默认为post (网页)
|
|
Future<BaseEntity?> requestWeb(
|
|
path,
|
|
Map<String, dynamic> parmeters,
|
|
Function(BaseEntity dataEntity) success,
|
|
Function(ErrorEntity errorEntity) error,
|
|
{RequestMethod? method}) async {
|
|
Map<String, dynamic> headers = {
|
|
"AppId": NetworkConfig.AppId,
|
|
/*"BossId": NetworkConfig.BossId,*/
|
|
"UserId": NetworkConfig.userId,
|
|
"Version": NetworkConfig.Version,
|
|
"Language": NetworkConfig.Language
|
|
};
|
|
parmeters.addAll(headers);
|
|
//签名加密
|
|
Map<String, dynamic> parmetersSign = sign(parmeters);
|
|
try {
|
|
/*由于服务器是表单结构*/
|
|
FormData formData = FormData.fromMap(parmetersSign);
|
|
Response response = await _dio!.post(path, data: formData);
|
|
if (response != null && response.statusCode == 200) {
|
|
BaseEntity entity = BaseEntity.fromJson(response.data);
|
|
Map<String, dynamic> parmeters = {
|
|
"AppId": NetworkConfig.AppId,
|
|
/*"BossId": NetworkConfig.BossId,*/
|
|
"UserId": NetworkConfig.userId,
|
|
"Token": entity.message
|
|
};
|
|
Map<String, dynamic> p1 = sign(parmeters);
|
|
parmeters.addAll(p1);
|
|
//data 信息添加用户信息
|
|
entity.data = gettoken(parmeters);
|
|
success(entity);
|
|
|
|
return entity;
|
|
} else {
|
|
error(ErrorEntity(code: -1, message: "Network Anomaly"));
|
|
return null;
|
|
}
|
|
} catch (e) {
|
|
ErrorEntity(code: -1, message: "Network Anomaly");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 捕获异常的错误信息
|
|
ErrorEntity _getErrorMsg(DioError error) {
|
|
switch (error.type) {
|
|
case DioErrorType.cancel:
|
|
{
|
|
return ErrorEntity(code: -1, message: "请求取消");
|
|
}
|
|
break;
|
|
case DioErrorType.connectionTimeout:
|
|
{
|
|
return ErrorEntity(code: -1, message: "连接超时");
|
|
}
|
|
break;
|
|
case DioErrorType.sendTimeout:
|
|
{
|
|
return ErrorEntity(code: -1, message: "请求超时");
|
|
}
|
|
break;
|
|
case DioErrorType.receiveTimeout:
|
|
{
|
|
return ErrorEntity(code: -1, message: "响应超时");
|
|
}
|
|
break;
|
|
case DioErrorType.badResponse:
|
|
{
|
|
try {
|
|
int errCode = error.response!.statusCode!;
|
|
String errorMsg = error.response!.statusMessage!;
|
|
return ErrorEntity(code: errCode, message: errorMsg);
|
|
} on Exception catch (_) {
|
|
return ErrorEntity(code: -1, message: "Network Anomaly");
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
return ErrorEntity(code: -1, message: "Network Anomaly");
|
|
}
|
|
}
|
|
}
|
|
|
|
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.userToken);
|
|
return treeMap;
|
|
}
|
|
|
|
String gettoken(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 = "";
|
|
treeMap.forEach((key, value) {
|
|
if (data == "") {
|
|
data = key + "=" + value.toString();
|
|
} else {
|
|
data = data + "&" + key + "=" + value.toString();
|
|
}
|
|
});
|
|
return data;
|
|
}
|
|
|
|
String generateMd5(String data) {
|
|
return md5.convert(utf8.encode(data)).toString();
|
|
}
|
|
}
|