import 'package:dating_touchme_app/controller/discover/room_controller.dart'; import 'package:dating_touchme_app/controller/message/conversation_controller.dart'; import 'package:dating_touchme_app/controller/overlay_controller.dart'; import 'package:dating_touchme_app/extension/ex_widget.dart'; import 'package:dating_touchme_app/generated/assets.dart'; import 'package:dating_touchme_app/im/im_manager.dart'; import 'package:dating_touchme_app/oss/oss_manager.dart'; import 'package:dating_touchme_app/pages/mine/login_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get_storage/get_storage.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; final storage = GetStorage(); @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'); // 等待登录完成 final loginSuccess = await IMManager.instance.login(token); if (loginSuccess) { print('✅ IM登录成功'); } else { print('⚠️ IM登录失败'); } 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; } } bool _checkInformation(UserData information){ if(information.id == null){ return true; } if(information.id!.isEmpty){ return true; } if(information.genderCode == null){ return true; } if(information.genderCode!.isNaN){ return true; } if(information.birthYear == null){ return true; } if(information.nickName == null){ return true; } if(information.nickName!.isEmpty){ return true; } return false; } void logout() async { // 退出直播间 if (Get.isRegistered()) { try { final roomController = Get.find(); await roomController.leaveChannel(); } catch (e) { print('退出直播间失败: $e'); } } // 取消小窗口 if (Get.isRegistered()) { try { final overlayController = Get.find(); overlayController.hide(); // 隐藏直播房间小窗 overlayController.hideVideoCall(); // 隐藏视频通话小窗 } catch (e) { print('取消小窗口失败: $e'); } } // 先退出 IM 登录 if(IMManager.instance.isInitialized){ await IMManager.instance.logout(); } // 清除会话列表和用户信息缓存 if (Get.isRegistered()) { final conversationController = Get.find(); conversationController.clearConversations(); } // App清除本地存储,有待处理, // storage.erase(); storage.remove('userId'); storage.remove('token'); // storage.write('hasAgreedUserAgreement', true); // 清除全局数据 GlobalData().logout(); Get.offAll(() => LoginPage()); } static const statusMap = { 1: '3天', 2: '7天', 3: '15天', 4: '30天', 5: '永久', }; /// 获取用户基础信息 Future getBaseUserInfo(String userId, bool isMain) async { try { final response = await _userApi.getBaseUserInfo(userId); if (response.data.isSuccess && response.data.data != null) { // 成功获取基础信息后,调用获取婚姻信息详情接口 final baseInfo = response.data.data!; if(baseInfo.isForbidden == true){ final storage = GetStorage(); final userId = storage.read('userId'); if (userId != null && userId.isNotEmpty) { logout(); } SmartDialog.dismiss(); SmartDialog.show( onDismiss: (){ SmartDialog.dismiss(); }, builder: (context) { return ClipRRect( borderRadius: BorderRadius.all(Radius.circular(16.w)), child: Material( color: Colors.white, child: Container( width: 311.w, height: 256.w, padding: EdgeInsets.only( top: 18.w, right: 19.w, left: 19.w ), decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ Color(0xFFFFFFFF), Color(0xFFF4F2FF), ], ), ), child: Column( children: [ Image.asset( Assets.imagesLoginWarning, width: 44.w, height: 44.w, ), SizedBox(height: 4.w,), Text( "账号已限制登录", textAlign: TextAlign.center, style: TextStyle( fontSize: 16.w ), ), SizedBox(height: 10.w,), Text( "你的账号涉嫌违规已被系统封禁,如需申诉,请点击右上角联系客服进行反馈。\n" "封禁时间:${statusMap[baseInfo.forbiddenCycle] ?? "未知"}\n" "${baseInfo.forbiddenCycle != 5 ? "解除时间:${baseInfo.forbiddenEndTime}" : ""}", style: TextStyle( fontSize: 13.w ), ), SizedBox(height: 12.w,), Container( width: 128.w, height: 40.w, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(12.w)), color: const Color.fromRGBO(117, 98, 249, 1) ), child: Center( child: Text( "知道了", style: TextStyle( fontSize: 14.w, color: Colors.white ), ), ), ).onTap(() { // 隐藏 overlay SmartDialog.dismiss(); }) ], ), ), ), ); }, ); return ; } final result = await _userApi.getMarriageInformationDetail(); // print(result.data); if (result.data.isSuccess) { if(result.data.data == null){ var information = UserData(); if(baseInfo.liveMatchmaker != null && baseInfo.liveMatchmaker!.enable!){ information.matchmakerFlag = true; information.matchmakerType = baseInfo.liveMatchmaker!.type; } else { information.matchmakerFlag = false; } // information.matchmakerFlag = baseInfo.matchmakerFlag; // if(information.matchmakerFlag!){ // information.matchmakerType = baseInfo.matchmakerType; // } information.realName = baseInfo.realName; information.phone = baseInfo.phone; GlobalData().userData = information; if(isMain){ SmartDialog.showToast('转到完善信息'); Get.to(() => UserInfoPage()); } else { Get.offAll(() => UserInfoPage()); } // await storage.write('userId', GlobalData().userId); return; } final information = result.data.data!; if(baseInfo.liveMatchmaker != null && baseInfo.liveMatchmaker!.enable!){ information.matchmakerFlag = true; information.matchmakerType = baseInfo.liveMatchmaker!.type; information.code = baseInfo.liveMatchmaker!.code; } else { information.matchmakerFlag = false; } information.realName = baseInfo.realName; information.phone = baseInfo.phone; GlobalData().userData = information; // await storage.write('userId', GlobalData().userId); if (_checkInformation(information)) { //跳转到完善信息 SmartDialog.showToast('转到完善信息'); // 导航到完善信息页面 if(isMain){ SmartDialog.showToast('转到完善信息'); Get.to(() => UserInfoPage()); } else { Get.offAll(() => UserInfoPage()); } } else if(!isMain){ await storage.write('userId', userId); Get.offAll(MainPage()); } } } else { SmartDialog.showToast(response.data.message); } } catch (e) { // 获取用户信息失败不影响登录流程 SmartDialog.showToast('获取用户信息失败'); print('获取用户信息失败: $e'); } } }