Browse Source

加入验证码邮箱格式验证

weicu
fdp 2 years ago
parent
commit
5e6d35be65
  1. 85
      lib/modules/login/controllers/login_controller.dart

85
lib/modules/login/controllers/login_controller.dart

@ -67,7 +67,6 @@ class LoginController extends GetxController {
_contactValue.value = contactController.text; _contactValue.value = contactController.text;
_formatDisplayContact(); _formatDisplayContact();
_startCountdown();
_sendVerificationCode(); _sendVerificationCode();
} }
@ -133,7 +132,7 @@ class LoginController extends GetxController {
_contactValue.value = contactController.text; _contactValue.value = contactController.text;
_formatDisplayContact(); _formatDisplayContact();
_viewState.value = LoginViewState.verification; _viewState.value = LoginViewState.verification;
_startCountdown();
_sendVerificationCode(); _sendVerificationCode();
} }
@ -185,14 +184,26 @@ class LoginController extends GetxController {
Future<void> _sendVerificationCode() async { Future<void> _sendVerificationCode() async {
// 这里应该是实际发送验证码的逻辑 // 这里应该是实际发送验证码的逻辑
// 为了演示,我们只是模拟发送 // 为了演示,我们只是模拟发送
var response = await Api.getCode({ if (isEmialContactController() || isPhoneContactController()) {
"addr": contactController.text, _startCountdown();
"vtype": 0, var response = await Api.getCode({
}); "addr": contactController.text,
if (response != null) { "vtype": 0,
print("请求后台数据验证码params=${response}"); });
//短信已发送 if (response != null) {
//EasyLoading.showInfo('codeSend'.tr); print("请求后台数据验证码params=${response}");
//短信已发送
//EasyLoading.showInfo('codeSend'.tr);
}
} else {
Get.snackbar(
'提示',
'请正确的手机号码或邮箱',
snackPosition: SnackPosition.TOP,
backgroundColor: Colors.red.withOpacity(0.1),
colorText: Colors.red,
);
return;
} }
} }
@ -205,6 +216,7 @@ class LoginController extends GetxController {
// 更新验证码输入 // 更新验证码输入
void updateVerificationCode(int index, String value) { void updateVerificationCode(int index, String value) {
print("updateVerificationCode=${index}${value}");
if (index >= 0 && index < 4) { if (index >= 0 && index < 4) {
var newList = [..._verificationCode]; var newList = [..._verificationCode];
newList[index] = value; newList[index] = value;
@ -217,25 +229,32 @@ class LoginController extends GetxController {
_verificationCode.value = ['', '', '', '']; _verificationCode.value = ['', '', '', ''];
} }
// 检查邮箱格式
bool isEmialContactController() {
// 检查每个元素是否为单个字母或数字字符
final text = contactController.text;
return (text.length > 5 &&
RegExp(r'^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$')
.hasMatch(text)) ||
(RegExp(r'^[z0-9]$').hasMatch(text));
}
// 检查手机号码格式
bool isPhoneContactController() {
// 检查每个元素是否为单个字母或数字字符
final text = contactController.text;
return RegExp(r'^[0-9]+$').hasMatch(text);
}
// 检查验证码是否完整 // 检查验证码是否完整
bool isVerificationCodeComplete() { bool isVerificationCodeComplete() {
return !_verificationCode.contains(''); // 检查每个元素是否为单个字母或数字字符
final text = verificationCodeController.text;
return text.length == 4 && RegExp(r'^[A-Za-z0-9]$').hasMatch(text);
} }
// 登录 // 登录
Future<void> login() async { Future<void> login() async {
// 暂时关闭验证码验证
// if (!isVerificationCodeComplete()) {
// Get.snackbar(
// '提示',
// '请输入完整的验证码',
// snackPosition: SnackPosition.TOP,
// backgroundColor: Colors.red.withOpacity(0.1),
// colorText: Colors.red,
// );
// return;
// }
_isLoading.value = true; _isLoading.value = true;
try { try {
@ -245,16 +264,16 @@ class LoginController extends GetxController {
switch (loginType) { switch (loginType) {
case 0 || 1: case 0 || 1:
{ {
// if (!isVerificationCodeComplete()) { if (!isVerificationCodeComplete()) {
// Get.snackbar( Get.snackbar(
// '提示', '提示',
// '请输入完整的验证码', '请输入完整的验证码',
// snackPosition: SnackPosition.TOP, snackPosition: SnackPosition.TOP,
// backgroundColor: Colors.red.withOpacity(0.1), backgroundColor: Colors.red.withOpacity(0.1),
// colorText: Colors.red, colorText: Colors.red,
// ); );
// break; break;
// } }
requestLogin({ requestLogin({
"mail": contactController.text, "mail": contactController.text,
"phone": "", "phone": "",

Loading…
Cancel
Save