You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
6.5 KiB
246 lines
6.5 KiB
import 'package:get/get.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../../../core/routes/app_routes.dart';
|
|
|
|
enum LoginViewState {
|
|
welcome, // 欢迎页面
|
|
verification // 验证码页面
|
|
}
|
|
|
|
class LoginController extends GetxController {
|
|
static LoginController get to => Get.find();
|
|
|
|
// 状态管理
|
|
final _viewState = LoginViewState.welcome.obs;
|
|
final _isLoading = false.obs;
|
|
final _countdownSeconds = 0.obs;
|
|
final _contactValue = ''.obs;
|
|
final _displayContact = ''.obs;
|
|
final _verificationCode = ['', '', '', ''].obs;
|
|
|
|
// Getters
|
|
LoginViewState get viewState => _viewState.value;
|
|
bool get isLoading => _isLoading.value;
|
|
int get countdownSeconds => _countdownSeconds.value;
|
|
String get contactValue => _contactValue.value;
|
|
String get displayContact => _displayContact.value;
|
|
List<String> get verificationCode => _verificationCode;
|
|
|
|
// 文本控制器
|
|
final contactController = TextEditingController();
|
|
final verificationCodeController = TextEditingController();
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_viewState.value = LoginViewState.welcome;
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
contactController.dispose();
|
|
verificationCodeController.dispose();
|
|
super.onClose();
|
|
}
|
|
|
|
// 获取验证码
|
|
void getVerificationCode() {
|
|
if (contactController.text.isEmpty) {
|
|
Get.snackbar(
|
|
'提示',
|
|
'请输入手机号码或邮箱',
|
|
snackPosition: SnackPosition.TOP,
|
|
backgroundColor: Colors.red.withOpacity(0.1),
|
|
colorText: Colors.red,
|
|
);
|
|
return;
|
|
}
|
|
|
|
_contactValue.value = contactController.text;
|
|
_formatDisplayContact();
|
|
_startCountdown();
|
|
_sendVerificationCode();
|
|
}
|
|
|
|
// 使用验证码直接登录
|
|
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;
|
|
|
|
// 模拟验证过程
|
|
Future.delayed(const Duration(seconds: 2), () {
|
|
_isLoading.value = false;
|
|
// 验证成功,跳转到配对页面
|
|
Get.offAllNamed(Routes.pairing);
|
|
});
|
|
}
|
|
|
|
// 切换到验证码页面
|
|
void showVerificationView() {
|
|
if (contactController.text.isEmpty) {
|
|
Get.snackbar(
|
|
'提示',
|
|
'请输入手机号码或邮箱',
|
|
snackPosition: SnackPosition.TOP,
|
|
backgroundColor: Colors.red.withOpacity(0.1),
|
|
colorText: Colors.red,
|
|
);
|
|
return;
|
|
}
|
|
|
|
_contactValue.value = contactController.text;
|
|
_formatDisplayContact();
|
|
_viewState.value = LoginViewState.verification;
|
|
_startCountdown();
|
|
_sendVerificationCode();
|
|
}
|
|
|
|
// 返回欢迎页面
|
|
void showWelcomeView() {
|
|
_viewState.value = LoginViewState.welcome;
|
|
_resetVerificationCode();
|
|
}
|
|
|
|
// 格式化显示的联系方式(隐藏部分字符)
|
|
void _formatDisplayContact() {
|
|
String contact = _contactValue.value;
|
|
if (contact.contains('@')) { // 邮箱
|
|
final parts = contact.split('@');
|
|
if (parts[0].length > 3) {
|
|
_displayContact.value = parts[0].substring(0, 3) + '****@' + parts[1];
|
|
} else {
|
|
_displayContact.value = contact;
|
|
}
|
|
} else if (RegExp(r'^\d+$').hasMatch(contact)) { // 手机号
|
|
if (contact.length > 7) {
|
|
_displayContact.value = '+86 ' + contact.substring(0, 3) + '****' + contact.substring(contact.length - 4);
|
|
} else {
|
|
_displayContact.value = contact;
|
|
}
|
|
} else {
|
|
_displayContact.value = contact;
|
|
}
|
|
}
|
|
|
|
// 开始倒计时
|
|
void _startCountdown() {
|
|
_countdownSeconds.value = 60;
|
|
Future.doWhile(() async {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
_countdownSeconds.value--;
|
|
return _countdownSeconds.value > 0;
|
|
});
|
|
}
|
|
|
|
// 发送验证码
|
|
void _sendVerificationCode() {
|
|
// 这里应该是实际发送验证码的逻辑
|
|
// 为了演示,我们只是模拟发送
|
|
}
|
|
|
|
// 重新发送验证码
|
|
void resendVerificationCode() {
|
|
if (_countdownSeconds.value > 0) return;
|
|
_startCountdown();
|
|
_sendVerificationCode();
|
|
}
|
|
|
|
// 更新验证码输入
|
|
void updateVerificationCode(int index, String value) {
|
|
if (index >= 0 && index < 4) {
|
|
var newList = [..._verificationCode];
|
|
newList[index] = value;
|
|
_verificationCode.value = newList;
|
|
}
|
|
}
|
|
|
|
// 重置验证码
|
|
void _resetVerificationCode() {
|
|
_verificationCode.value = ['', '', '', ''];
|
|
}
|
|
|
|
// 检查验证码是否完整
|
|
bool isVerificationCodeComplete() {
|
|
return !_verificationCode.contains('');
|
|
}
|
|
|
|
// 登录
|
|
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 {
|
|
// 模拟验证过程
|
|
await Future.delayed(const Duration(seconds: 2));
|
|
|
|
// 验证成功,跳转到配对页面
|
|
Get.offAllNamed(Routes.pairing);
|
|
} catch (e) {
|
|
Get.snackbar(
|
|
'错误',
|
|
'登录失败,请重试',
|
|
snackPosition: SnackPosition.TOP,
|
|
backgroundColor: Colors.red.withOpacity(0.1),
|
|
colorText: Colors.red,
|
|
);
|
|
} finally {
|
|
_isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
// 使用密码登录
|
|
void usePasswordLogin() {
|
|
// 这里可以实现密码登录的逻辑
|
|
Get.snackbar(
|
|
'提示',
|
|
'密码登录功能尚未实现',
|
|
snackPosition: SnackPosition.TOP,
|
|
backgroundColor: Colors.blue.withOpacity(0.1),
|
|
colorText: Colors.blue,
|
|
);
|
|
}
|
|
}
|