import 'package:dating_touchme_app/im/im_manager.dart'; import 'package:dating_touchme_app/oss/oss_manager.dart'; import 'package:get/get.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import '../../model/mine/authentication_data.dart'; import '../../model/mine/user_data.dart'; import '../../network/user_api.dart'; import '../../pages/mine/user_info_page.dart'; import '../../pages/main/main_page.dart'; import '../global.dart'; class UserController extends GetxController { // UserApi实例 late UserApi _userApi; @override void onInit() { super.onInit(); // 从全局依赖中获取UserApi _userApi = Get.find(); } /// 获取环信用户token Future getHxUserToken() async { try { // 调用获取环信用户token接口 final response = await _userApi.getHxUserToken(); // 处理响应 if (response.data.isSuccess) { // 保存token到本地存储 String? token = response.data.data; if (token != null) { // 打印获取的token print('获取环信用户token成功: $token'); IMManager.instance.login(token); return token; } else { SmartDialog.showToast('获取的环信用户token为空'); return null; } } else { SmartDialog.showToast(response.data.message); return null; } } catch (e) { print('获取环信用户token失败: $e'); SmartDialog.showToast('获取环信用户token失败,请重试'); return null; } } /// 获取用户基础信息 Future getBaseUserInfo(String userId) async { try { final response = await _userApi.getBaseUserInfo(userId); if (response.data.isSuccess && response.data.data != null) { // 成功获取基础信息后,调用获取婚姻信息详情接口 await getMarriageInformationDetail(false); } else { SmartDialog.showToast(response.data.message); } } catch (e) { // 获取用户信息失败不影响登录流程 SmartDialog.showToast('获取用户信息失败'); } } /// 获取用户婚姻信息详情 Future getMarriageInformationDetail(bool isMain) async { try { final response = await _userApi.getMarriageInformationDetail(); if (response.data.isSuccess) { // 检查data是否为null或者是空对象 if(response.data.data == null){ if(isMain){ SmartDialog.showToast('转到完善信息'); Get.to(() => UserInfoPage()); } else { Get.offAll(() => UserInfoPage()); } return; } final information = response.data.data!; // if (information.id.isNotEmpty) { // final result = await _userApi.getCertificationList(information.id); // List list = result.data.data!; // final record = list.firstWhere((item) => item.authenticationCode == 0); // information.realAuth = record.status == 1; // } GlobalData().userData = information; if (information.id.isEmpty || information.genderCode.isNaN || information.birthYear == null || information.nickName.isEmpty) { //跳转到完善信息 SmartDialog.showToast('转到完善信息'); // 导航到完善信息页面 if(isMain){ SmartDialog.showToast('转到完善信息'); Get.to(() => UserInfoPage()); } else { Get.offAll(() => UserInfoPage()); } } else if(!isMain){ Get.offAll(MainPage()); } } } catch (e) { // 获取婚姻信息失败不影响登录流程 print('获取婚姻信息异常: $e'); } } }