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.
168 lines
4.8 KiB
168 lines
4.8 KiB
import 'dart:async';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
import 'package:dating_touchme_app/network/user_api.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<UserApi>();
|
|
}
|
|
|
|
// 获取验证码
|
|
Future<void> 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<void> 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 loginData = response.data.data!;
|
|
await storage.write('token', loginData.token);
|
|
// await storage.write('userId', loginData.userId);
|
|
// // 保存用户信息
|
|
// await storage.write('userInfo', loginData.toJson());
|
|
|
|
// 登录成功后获取用户基础信息
|
|
await _getBaseUserInfo(loginData.userId);
|
|
}
|
|
} else {
|
|
SmartDialog.showToast(response.data.message);
|
|
}
|
|
} catch (e) {
|
|
SmartDialog.showToast('网络请求失败,请检查网络连接');
|
|
} finally {
|
|
isLoggingIn.value = false;
|
|
}
|
|
}
|
|
|
|
// 获取用户基础信息
|
|
Future<void> _getBaseUserInfo(String userId) async {
|
|
try {
|
|
final response = await _userApi.getBaseUserInfo(userId);
|
|
|
|
if (response.data.isSuccess && response.data.data != null) {
|
|
// 成功获取基础信息后,调用获取婚姻信息详情接口
|
|
await _getMarriageInformationDetail();
|
|
} else {
|
|
SmartDialog.showToast(response.data.message);
|
|
}
|
|
} catch (e) {
|
|
// 获取用户信息失败不影响登录流程
|
|
SmartDialog.showToast('获取用户信息失败');
|
|
}
|
|
}
|
|
|
|
// 获取用户婚姻信息详情
|
|
Future<void> _getMarriageInformationDetail() async {
|
|
try {
|
|
final response = await _userApi.getMarriageInformationDetail();
|
|
|
|
if (response.data.isSuccess) {
|
|
SmartDialog.showToast('跳转到完善信息');
|
|
// 可以在这里处理用户婚姻信息详情,比如更新UI状态或保存到存储中
|
|
if(response.data.data == null){
|
|
//跳转到完善信息
|
|
SmartDialog.showToast('跳转到完善信息');
|
|
}
|
|
} else {
|
|
// 获取婚姻信息失败不影响登录流程
|
|
}
|
|
} catch (e) {
|
|
// 获取婚姻信息失败不影响登录流程
|
|
}
|
|
}
|
|
}
|