diff --git a/lib/modules/login/controllers/login_controller.dart b/lib/modules/login/controllers/login_controller.dart index 3b0c7f590..b605931ca 100644 --- a/lib/modules/login/controllers/login_controller.dart +++ b/lib/modules/login/controllers/login_controller.dart @@ -26,22 +26,6 @@ enum LoginViewState { verification // 验证码页面 } -// 用户渠道枚举 -// enum UserChannel { -// UNKNOWN(0), // 未知渠道 -// CHINA_HUAWEI(1), // 中国-华为 -// CHINA_XIAOMI(2), // 中国-小米 -// CHINA_OPPO(3), // 中国-OPPO -// CHINA_VIVO(4), // 中国-VIVO -// CHINA_HONOR(5), // 中国-荣耀 -// OFFICIAL(100), // 官方渠道 -// GLOBAL_APPLE(101), // 全球-苹果 -// GLOBAL_GOOGLE(102); // 全球-谷歌 - -// const UserChannel(this.value); -// final int value; -// } - class LoginController extends GetxController { static LoginController get to => Get.find(); final GetStorage _storage = GetStorage(); @@ -354,9 +338,32 @@ class LoginController extends GetxController { // 获取验证码 void getVerificationCode() { if (contactController.text.isEmpty) { + Get.snackbar( + 'tip'.tr, //提示 + 'pleaseEnterContactInfo'.tr, //请输入手机号或邮箱 + snackPosition: SnackPosition.TOP, + backgroundColor: Colors.red.withOpacity(0.1), + colorText: Colors.red, + ); + return; + } + + // 根据登录类型验证格式 + if (loginType == 0 && !isEmailContactController()) { Get.snackbar( 'tip'.tr, - 'pleaseEnterContactInfo'.tr, + 'invalidEmailFormat'.tr, + snackPosition: SnackPosition.TOP, + backgroundColor: Colors.red.withOpacity(0.1), + colorText: Colors.red, + ); + return; + } + + if (loginType == 1 && !isPhoneContactController()) { + Get.snackbar( + 'tip'.tr, + 'invalidPhoneFormat'.tr, snackPosition: SnackPosition.TOP, backgroundColor: Colors.red.withOpacity(0.1), colorText: Colors.red, @@ -371,40 +378,6 @@ class LoginController extends GetxController { // 使用验证码直接登录 void loginWithVerificationCode() { - //测试期间暂时关闭验证码登录 - // if (contactController.text.isEmpty) { - // Get.snackbar( - // '提示', - // '请输入手机号码或邮箱', - // snackPosition: SnackPosition.TOP, - // backgroundColor: Colors.red.withOpacity(0.1), - // colorText: Colors.red, - // ); - // return; - // } - - // if (verificationCodeController.text.isEmpty) { - // Get.snackbar( - // '提示', - // '请输入验证码', - // snackPosition: SnackPosition.TOP, - // backgroundColor: Colors.red.withOpacity(0.1), - // colorText: Colors.red, - // ); - // return; - // } - - // if (verificationCodeController.text.length < 6) { - // Get.snackbar( - // '提示', - // '请输入完整的验证码', - // snackPosition: SnackPosition.TOP, - // backgroundColor: Colors.red.withOpacity(0.1), - // colorText: Colors.red, - // ); - // return; - // } - _isLoading.value = true; // 模拟验证过程 @@ -481,9 +454,8 @@ class LoginController extends GetxController { // 发送验证码 Future _sendVerificationCode() async { - // 这里应该是实际发送验证码的逻辑 - // 为了演示,我们只是模拟发送 - if (isEmialContactController() || isPhoneContactController()) { + // 实际发送验证码的逻辑 + if (isEmailContactController() || isPhoneContactController()) { _startCountdown(); Logger().d(contactController.text); var response = await Api.getCode({ @@ -497,8 +469,8 @@ class LoginController extends GetxController { } } else { Get.snackbar( - 'tip'.tr, - 'pleaseEnterContactInfo'.tr, + 'tip'.tr, //提示 + 'pleaseEnterContactInfo'.tr, //请输入手机号或邮箱 snackPosition: SnackPosition.TOP, backgroundColor: Colors.red.withOpacity(0.1), colorText: Colors.red, @@ -530,19 +502,39 @@ class LoginController extends GetxController { } // 检查邮箱格式 - bool isEmialContactController() { + bool isEmailContactController() { // 检查每个元素是否为单个字母或数字字符 - final text = contactController.text; - return (text.length > 5 && - RegExp(r'^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$') - .hasMatch(text)); + final text = contactController.text.trim(); + if (text.length < 6) return false; + + // 更严格的邮箱验证 + final emailRegex = + RegExp(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'); + return emailRegex.hasMatch(text); } -// 检查手机号码格式 + // 检查手机号码格式 bool isPhoneContactController() { // 检查每个元素是否为单个字母或数字字符 - final text = contactController.text; - return RegExp(r'^[0-9]+$').hasMatch(text); + final text = contactController.text.trim(); + if (text.isEmpty) return false; + // 根据国家代码验证手机号长度 + // 长度检查 + if (text.length < 7 || text.length > 15) return false; + + // 必须是纯数字 + if (!RegExp(r'^[0-9]+$').hasMatch(text)) return false; + // 特定国家的特殊验证 + switch (_selectedCountryCode.value) { + case '+86': // 中国 + return text.length == 11 && text.startsWith('1'); + case '+1': // 美国/加拿大 + return text.length == 10; + case '+81': // 日本 + return text.length == 10 || text.length == 11; + default: + return true; // 其他国家只做基本验证 + } } // 检查验证码是否完整 @@ -570,7 +562,7 @@ class LoginController extends GetxController { } // 验证邮箱格式 - if (loginType == 0 && !isEmialContactController()) { + if (loginType == 0 && !isEmailContactController()) { Get.snackbar( 'error'.tr, 'invalidEmailFormat'.tr,