19
This commit is contained in:
@@ -25,10 +25,16 @@ class DashboardController extends GetxController {
|
||||
var res = await CRUD().get(link: AppLink.getdashbord, payload: {});
|
||||
print('📡 Main dashboard response: $res');
|
||||
|
||||
if (res != 'failure') {
|
||||
var d = jsonDecode(res);
|
||||
print('✅ Decoded main dashboard: ${jsonEncode(d)}');
|
||||
dashbord = d['message'];
|
||||
if (res != 'failure' && res != null) {
|
||||
try {
|
||||
var d = res is String ? jsonDecode(res) : res;
|
||||
print('✅ Decoded main dashboard: ${jsonEncode(d)}');
|
||||
if (d['status'] == 'success' && d['message'] != null) {
|
||||
dashbord = d['message'] is List ? d['message'] : [d['message']];
|
||||
}
|
||||
} catch (e) {
|
||||
print('❌ Error parsing main dashboard: $e');
|
||||
}
|
||||
} else {
|
||||
print('❌ Failed to load main dashboard');
|
||||
}
|
||||
@@ -40,7 +46,7 @@ class DashboardController extends GetxController {
|
||||
);
|
||||
print('💳 Wallet dashboard response: $resPayments');
|
||||
|
||||
if (resPayments != 'failure') {
|
||||
if (resPayments is Map && resPayments['status'] == 'success') {
|
||||
var p = resPayments;
|
||||
print('✅ Decoded wallet dashboard: ${jsonEncode(p)}');
|
||||
|
||||
@@ -48,9 +54,11 @@ class DashboardController extends GetxController {
|
||||
p['message'] is List &&
|
||||
p['message'].isNotEmpty) {
|
||||
dashbord[0].addAll(p['message'][0]);
|
||||
} else if (dashbord.isNotEmpty && p['message'] is Map) {
|
||||
dashbord[0].addAll(p['message']);
|
||||
}
|
||||
} else {
|
||||
print('❌ Failed to load wallet dashboard');
|
||||
print('❌ Failed to load wallet dashboard (or verification required)');
|
||||
}
|
||||
|
||||
// 🔹 Check SMS credit
|
||||
|
||||
81
lib/controller/admin/staff_controller.dart
Normal file
81
lib/controller/admin/staff_controller.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../functions/crud.dart';
|
||||
import '../functions/device_info.dart';
|
||||
import '../../views/widgets/snackbar.dart';
|
||||
|
||||
class StaffController extends GetxController {
|
||||
final CRUD _crud = CRUD();
|
||||
final formKey = GlobalKey<FormState>();
|
||||
|
||||
// التكست كنترولرز
|
||||
final nameController = TextEditingController();
|
||||
final phoneController = TextEditingController();
|
||||
final emailController = TextEditingController();
|
||||
final passwordController = TextEditingController();
|
||||
final birthdateController = TextEditingController();
|
||||
|
||||
String selectedGender = 'Male';
|
||||
String selectedRole = 'service'; // 'admin' or 'service'
|
||||
|
||||
bool isLoading = false;
|
||||
|
||||
Future<void> registerStaff() async {
|
||||
if (!formKey.currentState!.validate()) return;
|
||||
|
||||
isLoading = true;
|
||||
update();
|
||||
|
||||
try {
|
||||
// ملاحظة: لا نأخذ بصمة جهاز الأدمن هنا، بل نتركها فارغة ليقوم الموظف بربطها عند أول دخول له
|
||||
String fingerprint = "";
|
||||
|
||||
var response = await _crud.post(
|
||||
link: AppLink.addStaff,
|
||||
payload: {
|
||||
"name": nameController.text.trim(),
|
||||
"phone": phoneController.text.trim(),
|
||||
"email": emailController.text.trim(),
|
||||
"password": passwordController.text.trim(),
|
||||
"role": selectedRole,
|
||||
"gender": selectedGender,
|
||||
"birthdate": birthdateController.text.trim(),
|
||||
"fingerprint": fingerprint,
|
||||
"site": "main", // القيمة الافتراضية للفرع
|
||||
},
|
||||
);
|
||||
|
||||
if (response != "failure") {
|
||||
mySnackbarSuccess('تمت إضافة الموظف بنجاح');
|
||||
_clearFields();
|
||||
Get.back();
|
||||
} else {
|
||||
mySnackeBarError('فشل في إضافة الموظف. يرجى المحاولة لاحقاً');
|
||||
}
|
||||
} catch (e) {
|
||||
mySnackeBarError('حدث خطأ: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void _clearFields() {
|
||||
nameController.clear();
|
||||
phoneController.clear();
|
||||
emailController.clear();
|
||||
passwordController.clear();
|
||||
birthdateController.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
nameController.dispose();
|
||||
phoneController.dispose();
|
||||
emailController.dispose();
|
||||
passwordController.dispose();
|
||||
birthdateController.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:sefer_admin1/controller/functions/device_info.dart';
|
||||
import 'package:sefer_admin1/views/auth/login_page.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../constant/info.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../../views/admin/admin_home_page.dart';
|
||||
import '../../views/widgets/snackbar.dart';
|
||||
import '../functions/crud.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
|
||||
class OtpHelper extends GetxController {
|
||||
static final String _sendOtpUrl =
|
||||
@@ -73,27 +77,181 @@ class OtpHelper extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> checkAdminLogin() async {
|
||||
final deviceNumber =
|
||||
box.read(BoxName.fingerPrint); // خزّنه عند التشغيل أول مرة
|
||||
final phoneNumber = box.read(BoxName.adminPhone); // عند التحقق من OTP
|
||||
|
||||
if (deviceNumber != null && phoneNumber != null) {
|
||||
/// تسجيل الدخول بكلمة المرور والبصمة
|
||||
Future<bool> loginWithPassword(String password) async {
|
||||
try {
|
||||
final fingerprint = box.read(BoxName.fingerPrint);
|
||||
final response = await CRUD().post(
|
||||
link: _checkAdminLogin,
|
||||
payload: {
|
||||
"device_number": deviceNumber,
|
||||
"phone_number": phoneNumber,
|
||||
'fingerprint': fingerprint,
|
||||
'password': password,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != "failure") {
|
||||
Get.offAll(() => AdminHomePage());
|
||||
if (response != 'failure') {
|
||||
// إذا كان الرد يتطلب OTP (السيرفر يرجعها بداخل message)
|
||||
final msg = response['message'];
|
||||
if (response['status'] == 'otp_required' || (msg is Map && msg['status'] == 'otp_required')) {
|
||||
String phone = (msg is Map ? msg['phone'] : response['phone']) ?? '';
|
||||
_showOtpDialog(phone, password, fingerprint);
|
||||
return false; // ننتظر إكمال الـ OTP
|
||||
}
|
||||
|
||||
// إذا نجح الدخول مباشرة
|
||||
return _handleLoginSuccess(response, password);
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
// سيقوم CRUD بإظهار الخطأ المناسب (مثل "حسابك قيد المراجعة")
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('LOGIN ERROR: $e');
|
||||
mySnackeBarError('حدث خطأ أثناء تسجيل الدخول: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// التحقق من OTP الخاص بتسجيل الدخول
|
||||
Future<void> verifyLoginOtp(
|
||||
String phone, String otp, String password, String fingerprint) async {
|
||||
try {
|
||||
final response = await CRUD().post(
|
||||
link: '${AppLink.server}/Admin/auth/verify_login.php',
|
||||
payload: {
|
||||
'phone': phone,
|
||||
'otp': otp,
|
||||
'fingerprint': fingerprint,
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
bool success = await _handleLoginSuccess(response, password);
|
||||
if (success) {
|
||||
Get.offAll(() => const AdminHomePage());
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print('OTP VERIFY LOGIN ERROR: $e');
|
||||
mySnackeBarError('خطأ في التحقق من الرمز: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _handleLoginSuccess(dynamic response, String password) async {
|
||||
final msg = response['message'];
|
||||
final data = response['admin'] ?? (msg is Map ? msg['admin'] : null);
|
||||
final jwt = response['jwt'] ?? (msg is Map ? msg['jwt'] : null);
|
||||
|
||||
if (jwt != null) {
|
||||
await box.write(BoxName.jwt, c(jwt));
|
||||
}
|
||||
|
||||
if (data != null) {
|
||||
if (data['id'] != null) await box.write(BoxName.driverID, data['id']);
|
||||
if (data['role'] != null) {
|
||||
String role = data['role'].toString().trim();
|
||||
await box.write('admin_role', role);
|
||||
Log.print('Admin role saved: $role');
|
||||
}
|
||||
if (data['phone'] != null) await box.write(BoxName.adminPhone, data['phone']);
|
||||
}
|
||||
|
||||
await box.write(BoxName.phoneVerified, true);
|
||||
await box.write('admin_password', password);
|
||||
|
||||
mySnackbarSuccess('تم تسجيل الدخول بنجاح');
|
||||
return true;
|
||||
}
|
||||
|
||||
void _showOtpDialog(String phone, String password, String fingerprint) {
|
||||
String otpCode = '';
|
||||
Get.defaultDialog(
|
||||
title: 'رمز التحقق',
|
||||
content: Column(
|
||||
children: [
|
||||
Text('تم إرسال رمز التحقق إلى WhatsApp الخاص بك ($phone)'),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
onChanged: (val) => otpCode = val,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'أدخل الرمز هنا',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
textConfirm: 'تحقق',
|
||||
confirmTextColor: Colors.white,
|
||||
onConfirm: () {
|
||||
if (otpCode.length >= 5) {
|
||||
Get.back();
|
||||
verifyLoginOtp(phone, otpCode, password, fingerprint);
|
||||
} else {
|
||||
mySnackeBarError('الرجاء إدخال رمز صحيح');
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static bool _isAutoLoginAttempted = false;
|
||||
|
||||
Future<void> checkAdminLogin() async {
|
||||
final fingerprint = box.read(BoxName.fingerPrint);
|
||||
final password = box.read('admin_password');
|
||||
|
||||
if (fingerprint == null || password == null) {
|
||||
Get.offAll(() => const AdminLoginPage());
|
||||
return;
|
||||
}
|
||||
|
||||
// ─── أولاً: التحقق من وجود توكن JWT صالح ─────────
|
||||
// إذا وُجد توكن غير منتهي الصلاحية → ندخل مباشرة بدون استدعاء login.php
|
||||
final rawJwt = box.read(BoxName.jwt);
|
||||
if (rawJwt != null) {
|
||||
try {
|
||||
String token = r(rawJwt.toString()).split(AppInformation.addd)[0];
|
||||
if (!JwtDecoder.isExpired(token)) {
|
||||
Log.print('Valid JWT found, skipping login.php (no OTP needed)');
|
||||
Get.offAll(() => const AdminHomePage());
|
||||
return;
|
||||
}
|
||||
Log.print('JWT expired, need fresh login');
|
||||
} catch (e) {
|
||||
Log.print('JWT decode failed: \$e, need fresh login');
|
||||
}
|
||||
}
|
||||
|
||||
// ─── ثانياً: لا يوجد توكن صالح → استدعاء login.php ───
|
||||
final response = await CRUD().post(
|
||||
link: _checkAdminLogin,
|
||||
payload: {
|
||||
'fingerprint': fingerprint,
|
||||
'password': password,
|
||||
'is_renewal': '1',
|
||||
},
|
||||
);
|
||||
|
||||
if (response != 'failure') {
|
||||
final msg = response['message'];
|
||||
|
||||
if (response['status'] == 'otp_required' || (msg is Map && msg['status'] == 'otp_required')) {
|
||||
String phone = (msg is Map ? msg['phone'] : response['phone']) ?? '';
|
||||
_showOtpDialog(phone, password, fingerprint);
|
||||
return; // ننتظر إدخال رمز التحقق
|
||||
}
|
||||
|
||||
if (msg is Map && msg['jwt'] != null) {
|
||||
box.write(BoxName.jwt, c(msg['jwt']));
|
||||
if (msg['admin'] != null && msg['admin']['id'] != null) {
|
||||
box.write(BoxName.driverID, msg['admin']['id']);
|
||||
}
|
||||
} else if (response['jwt'] != null) {
|
||||
box.write(BoxName.jwt, c(response['jwt']));
|
||||
}
|
||||
Get.offAll(() => const AdminHomePage());
|
||||
} else {
|
||||
Get.offAll(() => AdminLoginPage());
|
||||
Log.print('Auto-login failed, redirecting to login page');
|
||||
Get.offAll(() => const AdminLoginPage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +262,15 @@ class OtpHelper extends GetxController {
|
||||
box.write(BoxName.fingerPrint, deviceFingerprint);
|
||||
});
|
||||
// تأجيل تنفيذ التنقل حتى تنتهي مرحلة البناء
|
||||
// ⚠️ محاولة واحدة فقط للتسجيل التلقائي لمنع الحلقة اللانهائية
|
||||
Future.microtask(() {
|
||||
if (_isAutoLoginAttempted) {
|
||||
// نحن في حلقة — نتوقف ونذهب لصفحة الدخول مباشرة
|
||||
Log.print('Auto-login already attempted, skipping to login page');
|
||||
return;
|
||||
}
|
||||
_isAutoLoginAttempted = true;
|
||||
|
||||
if (box.read(BoxName.phoneVerified) == true &&
|
||||
box.read(BoxName.adminPhone) != null) {
|
||||
checkAdminLogin();
|
||||
|
||||
@@ -18,11 +18,17 @@ class EmployeeController extends GetxController {
|
||||
|
||||
fetchEmployee() async {
|
||||
var res = await CRUD().get(link: AppLink.getEmployee, payload: {});
|
||||
if (res != 'failure') {
|
||||
employee = jsonDecode(res)['message'];
|
||||
if (res is String && (res == 'failure' || res == 'token_expired')) {
|
||||
Get.snackbar('error', 'Failed to load employees', backgroundColor: AppColor.redColor);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
var jsonData = res is String ? jsonDecode(res) : res;
|
||||
employee = jsonData['message'];
|
||||
update();
|
||||
} else {
|
||||
Get.snackbar('error', '', backgroundColor: AppColor.redColor);
|
||||
} catch (e) {
|
||||
Get.snackbar('error', 'Invalid server response', backgroundColor: AppColor.redColor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -29,22 +29,19 @@ class DeviceHelper {
|
||||
throw UnsupportedError('Unsupported platform');
|
||||
}
|
||||
|
||||
// Extract relevant device information
|
||||
final String deviceId = Platform.isAndroid
|
||||
? deviceData['fingerprint'] ?? 'unknown'
|
||||
: deviceData['identifierForVendor'] ?? 'unknown';
|
||||
String deviceId = 'unknown';
|
||||
if (Platform.isAndroid) {
|
||||
deviceId = deviceData['fingerprint'] ?? 'unknown';
|
||||
} else if (Platform.isIOS) {
|
||||
deviceId = deviceData['identifierForVendor'] ?? 'unknown';
|
||||
} else if (Platform.isMacOS) {
|
||||
deviceId = deviceData['systemGUID'] ?? 'unknown';
|
||||
}
|
||||
|
||||
final String deviceModel = deviceData['model'] ?? 'unknown';
|
||||
final String osVersion = Platform.isAndroid
|
||||
? deviceData['version']['release'] ?? 'unknown'
|
||||
: deviceData['systemVersion'] ?? 'unknown';
|
||||
|
||||
// Log the extracted information
|
||||
|
||||
// Generate and return the encrypted fingerprint
|
||||
final String fingerprint = '${deviceId}_${deviceModel}_$osVersion';
|
||||
|
||||
// print(EncryptionHelper.instance.encryptData(fingerprint));
|
||||
final String fingerprint = '${deviceId}_$deviceModel';
|
||||
print(fingerprint);
|
||||
return (fingerprint);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to generate device fingerprint');
|
||||
|
||||
Reference in New Issue
Block a user