import 'dart:async'; import 'package:dating_touchme_app/controller/global.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import '../../network/user_api.dart'; import 'user_controller.dart'; class LoginController extends GetxController { // 手机号输入 final phoneNumber = ''.obs; // 验证码输入 final verificationCode = ''.obs; // 是否正在发送验证码 final isSendingCode = false.obs; // 倒计时秒数 final countdownSeconds = 0.obs; // 是否正在登录中 final isLoggingIn = false.obs; // 从GetX依赖注入中获取UserApi实例 late UserApi _userApi; // GetStorage实例,用于存储token等信息 final storage = GetStorage(); @override void onInit() { super.onInit(); // 从全局依赖中获取UserApi _userApi = Get.find(); } // 获取验证码 Future getVerificationCode() async { // 验证手机号格式 if (phoneNumber.value.isEmpty || phoneNumber.value.length != 11) { SmartDialog.showToast('请输入正确的手机号'); return; } isSendingCode.value = true; try { // 构建请求参数 final params = { 'purpose': 1, // 认证 'verifiableAccount': phoneNumber.value, 'verifiableAccountType': 1, // 手机 }; // 调用UserApi中的验证码接口 final response = await _userApi.getVerificationCode(params); // 处理响应 if (response.data.isSuccess) { // 生产环境移除打印,可考虑使用正式的日志框架 // print('验证码发送成功'); // 开始倒计时 startCountdown(); } else { SmartDialog.showToast(response.data.message); } } catch (e) { SmartDialog.showToast('网络请求失败,请重试'); } finally { isSendingCode.value = false; } } // 开始倒计时 void startCountdown() { countdownSeconds.value = 60; Timer.periodic(const Duration(seconds: 1), (timer) { countdownSeconds.value--; if (countdownSeconds.value <= 0) { timer.cancel(); } }); } // 清除错误信息 - 由于使用SmartDialog,此方法不再需要 // void clearErrorMessage() {} // 登录方法 Future login() async { // 验证输入 if (phoneNumber.value.isEmpty || phoneNumber.value.length != 11) { SmartDialog.showToast('请输入正确的手机号'); return; } if (verificationCode.value.isEmpty) { SmartDialog.showToast('请输入验证码'); return; } isLoggingIn.value = true; try { // 构建登录请求参数 final params = { 'account': phoneNumber.value, 'accountType': 2, // 手机号类型 'captcha': verificationCode.value, }; // 调用登录接口 final response = await _userApi.login(params); // 处理响应 if (response.data.isSuccess) { // 保存token和用户信息 if (response.data.data != null) { final result = response.data.data!; GlobalData().userId = result.userId; GlobalData().qnToken = result.token; await storage.write('token', result.token); // await storage.write('userId', result.userId); // 登录成功后获取用户信息 await _handleUserInfoRetrieval(result.userId); } } else { SmartDialog.showToast(response.data.message); } } catch (e) { SmartDialog.showToast('网络请求失败,请检查网络连接'); } finally { isLoggingIn.value = false; } } // 使用UserController中的方法获取用户信息 Future _handleUserInfoRetrieval(String userId) async { try { // 检查并注册UserController UserController userController; if (Get.isRegistered()) { userController = Get.find(); } else { userController = Get.put(UserController()); } // 使用UserController获取基础信息 await userController.getBaseUserInfo(userId, false); } catch (e) { // 获取用户信息失败不影响登录流程 print('获取用户信息异常: $e'); } } }