Initial commit for Tripz Admin

This commit is contained in:
Hamza-Ayed
2026-01-21 17:31:10 +03:00
parent 5919554eaa
commit 1da2357124
31 changed files with 1812 additions and 980 deletions

View File

@@ -1,22 +1,144 @@
import 'dart:convert';
import 'dart:io';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import 'package:jwt_decoder/jwt_decoder.dart';
import 'package:secure_string_operations/secure_string_operations.dart';
import '../../constant/api_key.dart';
import '../../constant/box_name.dart';
import '../../constant/char_map.dart';
import '../../constant/info.dart';
import '../../constant/links.dart';
import '../../env/env.dart';
import '../../main.dart';
import '../../print.dart';
import '../../views/widgets/elevated_btn.dart';
import 'device_info.dart';
import 'encrypt_decrypt.dart';
class CRUD {
var dev = '';
getJWT() async {
dev = Platform.isAndroid ? 'android' : 'ios';
var payload = {
'id': 'admin',
'password': AK.passnpassenger,
'aud': '${AK.allowed}$dev',
};
var response1 = await http.post(
Uri.parse(AppLink.loginJwtDriver),
body: payload,
);
if (response1.statusCode == 200) {
final decodedResponse1 = jsonDecode(response1.body);
final jwt = decodedResponse1['jwt'];
await box.write(BoxName.jwt, X.c(X.c(X.c(jwt, cn), cC), cs));
// await AppInitializer().getKey();
}
}
getJwtWallet() async {
String fingerPrint = await DeviceHelper.getDeviceFingerprint();
print('fingerPrint: ${fingerPrint}');
dev = Platform.isAndroid ? 'android' : 'ios';
var payload = {
'id': '1',
'password': AK.passnpassenger,
'aud': '${Env.allowedWallet}$dev',
'fingerPrint': fingerPrint
};
// Log.print('payload: ${payload}');
var response1 = await http.post(
Uri.parse(AppLink.loginWalletAdmin),
body: payload,
);
// Log.print('response.request: ${response1.request}');
// Log.print('response.body: ${response1.body}');
// print(payload);
// Log.print(
// 'jsonDecode(response1.body)["jwt"]: ${jsonDecode(response1.body)['jwt']}');
await box.write(BoxName.hmac, jsonDecode(response1.body)['hmac']);
return jsonDecode(response1.body)['jwt'].toString();
}
Future<dynamic> postWallet(
{required String link, Map<String, dynamic>? payload}) async {
var s = await getJwtWallet();
Log.print('jwt: ${s}');
final hmac = box.read(BoxName.hmac);
Log.print('hmac: ${hmac}');
var url = Uri.parse(link);
Log.print('url: ${url}');
try {
// await LoginDriverController().getJWT();
var response = await http.post(
url,
body: payload,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
'Authorization': 'Bearer $s',
'X-HMAC-Auth': hmac.toString(),
},
);
Log.print('response.request:${response.request}');
Log.print('response.body: ${response.body}');
Log.print('payload:$payload');
if (response.statusCode == 200) {
try {
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == 'success') {
return jsonData;
} else {
return jsonData['status'];
}
} catch (e) {
// addError(e.toString(), 'crud().post - JSON decoding');
return 'failure';
}
} else if (response.statusCode == 401) {
// Specifically handle 401 Unauthorized
var jsonData = jsonDecode(response.body);
if (jsonData['error'] == 'Token expired') {
return 'token_expired'; // Return a specific value for token expiration
} else {
// Other 401 errors
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
return 'failure';
}
} else {
// addError('Non-200 response code: ${response.statusCode}',
// 'crud().post - Other');
return 'failure';
}
} catch (e) {
// addError('HTTP request error: $e', 'crud().post - HTTP');
return 'failure';
}
}
Future<dynamic> get({
required String link,
Map<String, dynamic>? payload,
}) async {
if (box.read(BoxName.jwt) == null) {
await getJWT();
}
bool isTokenExpired = JwtDecoder.isExpired(X
.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
.toString()
.split(AppInformation.addd)[0]);
if (isTokenExpired) {
await getJWT();
}
// await Get.put(LoginDriverController()).getJWT();
var url = Uri.parse(
link,
);
@@ -26,18 +148,148 @@ class CRUD {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
'Authorization':
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}',
'Bearer ${X.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs).toString().split(AppInformation.addd)[0]}'
},
);
Log.print('response: ${response.request}');
// if (response.statusCode == 200) {
var jsonData = jsonDecode(response.body);
if (response.statusCode == 200) {
Log.print('response: ${response.body}');
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == 'success') {
return response.body;
}
if (jsonData['status'] == 'success') {
return response.body;
return jsonData['status'];
} else if (response.statusCode == 401) {
// Specifically handle 401 Unauthorized
var jsonData = jsonDecode(response.body);
if (jsonData['error'] == 'Token expired') {
// Show snackbar prompting to re-login
// await Get.put(LoginDriverController()).getJWT();
// mySnackbarSuccess('please order now'.tr);
return 'token_expired'; // Return a specific value for token expiration
} else {
// Other 401 errors
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
return 'failure';
}
} else {
// addError('Non-200 response code: ${response.statusCode}',
// 'crud().post - Other');
return 'failure';
}
}
return jsonData['status'];
// Future<dynamic> getWallet({
// required String link,
// Map<String, dynamic>? payload,
// }) async {
// var s = await getJwtWallet();
// var url = Uri.parse(
// link,
// );
// var response = await http.post(
// url,
// body: payload,
// headers: {
// "Content-Type": "application/x-www-form-urlencoded",
// 'Authorization': 'Bearer $s'
// },
// );
// if (response.statusCode == 200) {
// var jsonData = jsonDecode(response.body);
// if (jsonData['status'] == 'success') {
// return response.body;
// }
// return jsonData['status'];
// } else if (response.statusCode == 401) {
// // Specifically handle 401 Unauthorized
// var jsonData = jsonDecode(response.body);
// if (jsonData['error'] == 'Token expired') {
// // Show snackbar prompting to re-login
// // await Get.put(LoginDriverController()).getJwtWallet();
// return 'token_expired'; // Return a specific value for token expiration
// } else {
// // Other 401 errors
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
// return 'failure';
// }
// } else {
// addError('Non-200 response code: ${response.statusCode}',
// 'crud().post - Other');
// return 'failure';
// }
// }
Future<dynamic> post(
{required String link, Map<String, dynamic>? payload}) async {
var url = Uri.parse(link);
try {
bool isTokenExpired = JwtDecoder.isExpired(X
.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
.toString()
.split(AppInformation.addd)[0]);
if (isTokenExpired) {
await getJWT();
}
var response = await http.post(
url,
body: payload,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
'Authorization':
'Bearer ${X.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs).toString().split(AppInformation.addd)[0]}'
// 'Authorization': 'Bearer ${box.read(BoxName.jwt)}'
},
);
if (response.statusCode == 200) {
try {
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == 'success') {
return jsonData;
} else {
return jsonData['status'];
}
} catch (e) {
// addError(e.toString(), 'crud().post - JSON decoding');
return 'failure';
}
} else if (response.statusCode == 401) {
// Specifically handle 401 Unauthorized
var jsonData = jsonDecode(response.body);
if (jsonData['error'] == 'Token expired') {
// Show snackbar prompting to re-login
await getJWT();
// MyDialog().getDialog(
// 'Session expired. Please log in again.'.tr,
// '',
// () {
// Get.put(LoginController()).loginUsingCredentials(
// box.read(BoxName.passengerID), box.read(BoxName.email));
// Get.back();
// },
// );
return 'token_expired'; // Return a specific value for token expiration
} else {
// Other 401 errors
// addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401');
return 'failure';
}
} else {
// addError('Non-200 response code: ${response.statusCode}',
// 'crud().post - Other');
return 'failure';
}
} catch (e) {
// addError('HTTP request error: $e', 'crud().post - HTTP');
return 'failure';
}
}
// }
@@ -67,10 +319,7 @@ class CRUD {
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
} else {
print(response.reasonPhrase);
}
} else {}
}
Future<dynamic> getAgoraToken({
@@ -209,15 +458,12 @@ class CRUD {
String imagePathFull =
'${AppLink.server}/card_image/$imagePath-$driverID.jpg';
Log.print('imagePathFull: $imagePathFull');
var request = http.Request(
'POST',
Uri.parse(
'https://eastus.api.cognitive.microsoft.com/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2024-02-01'));
request.body = json.encode({"url": imagePathFull});
Log.print('request.body: ${request.body}');
request.headers.addAll(headers);
Log.print('request.headers: ${request.headers}');
http.StreamedResponse response = await request.send();
@@ -281,49 +527,6 @@ class CRUD {
} else {}
}
Future<dynamic> post({
required String link,
Map<String, dynamic>? payload,
}) async {
try {
var url = Uri.parse(link);
var response = await http.post(
url,
body: payload,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
'Authorization':
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
},
);
Log.print('payload: ${payload}');
var jsonData = jsonDecode(response.body);
Log.print('jsonData: ${jsonData}');
if (response.statusCode == 200) {
if (jsonData['status'] == 'success') {
return response.body;
} else {
return jsonData['status'];
}
} else {
// Log non-fatal errors to Crashlytics
await FirebaseCrashlytics.instance.log("Failed API request :$link");
FirebaseCrashlytics.instance.recordError(
Exception('Error: ${response.statusCode}'),
null,
reason: 'Failed HTTP POST request',
);
return response.statusCode;
}
} catch (e, stackTrace) {
// Log error to Crashlytics
await FirebaseCrashlytics.instance
.recordError(e, stackTrace, reason: 'Exception in post request');
return 'Error occurred';
}
}
Future<dynamic> kazumiSMS({
required String link,
Map<String, dynamic>? payload,
@@ -344,7 +547,6 @@ class CRUD {
if (response.statusCode == 200) {
var responseBody = await response.stream.bytesToString();
var data = json.decode(responseBody);
Log.print('data: ${data}');
return data;
} else {}
}
@@ -366,7 +568,6 @@ class CRUD {
body: body,
headers: headers,
);
Log.print('res: ${res.body}');
}
Future<dynamic> postPayMob({