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 '../../network/user_api.dart'; import '../../pages/mine/user_info_page.dart'; import '../../pages/main/main_page.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或者是空对象 final information = response.data.data; if (information == null || information.id.isEmpty || information.genderCode.isNaN || information.birthYear == null || information.nickName.isEmpty) { //跳转到完善信息 SmartDialog.showToast('转到完善信息'); // 导航到完善信息页面 Get.offAll(() => UserInfoPage()); } else if(!isMain){ Get.offAll(MainPage()); } } else { // 获取婚姻信息失败不影响登录流程 } } catch (e) { // 获取婚姻信息失败不影响登录流程 print('获取婚姻信息异常: $e'); } } }