|
|
|
@ -67,7 +67,6 @@ class LoginController extends GetxController { |
|
|
|
|
|
|
|
_contactValue.value = contactController.text; |
|
|
|
_formatDisplayContact(); |
|
|
|
_startCountdown(); |
|
|
|
_sendVerificationCode(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -133,7 +132,7 @@ class LoginController extends GetxController { |
|
|
|
_contactValue.value = contactController.text; |
|
|
|
_formatDisplayContact(); |
|
|
|
_viewState.value = LoginViewState.verification; |
|
|
|
_startCountdown(); |
|
|
|
|
|
|
|
_sendVerificationCode(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -185,6 +184,8 @@ class LoginController extends GetxController { |
|
|
|
Future<void> _sendVerificationCode() async { |
|
|
|
// 这里应该是实际发送验证码的逻辑 |
|
|
|
// 为了演示,我们只是模拟发送 |
|
|
|
if (isEmialContactController() || isPhoneContactController()) { |
|
|
|
_startCountdown(); |
|
|
|
var response = await Api.getCode({ |
|
|
|
"addr": contactController.text, |
|
|
|
"vtype": 0, |
|
|
|
@ -194,6 +195,16 @@ class LoginController extends GetxController { |
|
|
|
//短信已发送 |
|
|
|
//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) { |
|
|
|
print("updateVerificationCode=${index}${value}"); |
|
|
|
if (index >= 0 && index < 4) { |
|
|
|
var newList = [..._verificationCode]; |
|
|
|
newList[index] = value; |
|
|
|
@ -217,40 +229,32 @@ class LoginController extends GetxController { |
|
|
|
_verificationCode.value = ['', '', '', '']; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查验证码是否完整 |
|
|
|
bool isVerificationCodeComplete() { |
|
|
|
return !_verificationCode.contains(''); |
|
|
|
// 检查邮箱格式 |
|
|
|
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)); |
|
|
|
} |
|
|
|
|
|
|
|
Future<void> getLoginToken() async { |
|
|
|
// 使用正确语法创建请求头 |
|
|
|
var response = await Api.tokenlogin(); |
|
|
|
if (response != null) { |
|
|
|
if (response?["code"] == null) { |
|
|
|
final user = User.fronTokenJson(response); |
|
|
|
await _storage.write( |
|
|
|
'user', jsonEncode(user.toJson())); // 使用 user.toJson() |
|
|
|
Get.offAllNamed(Routes.pairing); |
|
|
|
} |
|
|
|
} else { |
|
|
|
login(); |
|
|
|
// 检查手机号码格式 |
|
|
|
bool isPhoneContactController() { |
|
|
|
// 检查每个元素是否为单个字母或数字字符 |
|
|
|
final text = contactController.text; |
|
|
|
return RegExp(r'^[0-9]+$').hasMatch(text); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查验证码是否完整 |
|
|
|
bool isVerificationCodeComplete() { |
|
|
|
// 检查每个元素是否为单个字母或数字字符 |
|
|
|
final text = verificationCodeController.text; |
|
|
|
return text.length == 4 && RegExp(r'^[A-Za-z0-9]$').hasMatch(text); |
|
|
|
} |
|
|
|
|
|
|
|
// 登录 |
|
|
|
Future<void> login() async { |
|
|
|
// 暂时关闭验证码验证 |
|
|
|
// if (!isVerificationCodeComplete()) { |
|
|
|
// Get.snackbar( |
|
|
|
// '提示', |
|
|
|
// '请输入完整的验证码', |
|
|
|
// snackPosition: SnackPosition.TOP, |
|
|
|
// backgroundColor: Colors.red.withOpacity(0.1), |
|
|
|
// colorText: Colors.red, |
|
|
|
// ); |
|
|
|
// return; |
|
|
|
// } |
|
|
|
|
|
|
|
_isLoading.value = true; |
|
|
|
|
|
|
|
try { |
|
|
|
@ -260,16 +264,16 @@ class LoginController extends GetxController { |
|
|
|
switch (loginType) { |
|
|
|
case 0 || 1: |
|
|
|
{ |
|
|
|
// if (!isVerificationCodeComplete()) { |
|
|
|
// Get.snackbar( |
|
|
|
// '提示', |
|
|
|
// '请输入完整的验证码', |
|
|
|
// snackPosition: SnackPosition.TOP, |
|
|
|
// backgroundColor: Colors.red.withOpacity(0.1), |
|
|
|
// colorText: Colors.red, |
|
|
|
// ); |
|
|
|
// break; |
|
|
|
// } |
|
|
|
if (!isVerificationCodeComplete()) { |
|
|
|
Get.snackbar( |
|
|
|
'提示', |
|
|
|
'请输入完整的验证码', |
|
|
|
snackPosition: SnackPosition.TOP, |
|
|
|
backgroundColor: Colors.red.withOpacity(0.1), |
|
|
|
colorText: Colors.red, |
|
|
|
); |
|
|
|
break; |
|
|
|
} |
|
|
|
requestLogin({ |
|
|
|
"mail": contactController.text, |
|
|
|
"phone": "", |
|
|
|
@ -425,21 +429,21 @@ class LoginController extends GetxController { |
|
|
|
try { |
|
|
|
print('响应数据: ${response.toString()}'); // 调试输出原始数据 |
|
|
|
|
|
|
|
if (response['code'].toString() == '0') { |
|
|
|
if (response['token'].toString() != '') { |
|
|
|
// 新增:检查 user 字段是否存在且类型正确 |
|
|
|
if (response.containsKey('data') && response['data'] is Map) { |
|
|
|
// if (response.containsKey('data') && response['data'] is Map) { |
|
|
|
final user = User.fromJson(response); |
|
|
|
print('用户初始化成功: ${user.token}'); |
|
|
|
await _storage.write("logintoken", user.token); |
|
|
|
print('用户初始化成功: ${response['token'].toString()}'); |
|
|
|
await _storage.write("logintoken", response['token'].toString()); |
|
|
|
// 存储到 SharedPreferences (仅存储用户数据) |
|
|
|
//final prefs = await GetStorage(); |
|
|
|
await _storage.write( |
|
|
|
'user', jsonEncode(user.toJson())); // 使用 user.toJson() |
|
|
|
|
|
|
|
Get.offAllNamed(Routes.pairing); |
|
|
|
} else { |
|
|
|
throw FormatException('API 响应缺少 user 字段'); |
|
|
|
} |
|
|
|
// } else { |
|
|
|
// throw FormatException('API 响应缺少 user 字段'); |
|
|
|
// } |
|
|
|
} else { |
|
|
|
throw Exception('接口返回错误: ${response['msg']}'); |
|
|
|
} |
|
|
|
|