user_service.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:core';
  2. import 'package:domain/entity/response/hotel_info_entity.dart';
  3. import 'package:domain/repository/auth_repository.dart';
  4. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  5. import 'package:get/get.dart';
  6. import 'package:plugin_platform/engine/sp/sp_util.dart';
  7. import 'package:shared/utils/event_bus.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:shared/utils/util.dart';
  10. import '../constants/app_constant.dart';
  11. /// 用户相关的单例服务,保存了Token,UserProfile等信息
  12. class UserService extends GetxService {
  13. static UserService get to => Get.find();
  14. final AuthRepository _authRepository;
  15. UserService(this._authRepository);
  16. //用户登录Token
  17. String? token;
  18. //用户是否已经登录 - 可变字段
  19. Rx<bool> haslogin = false.obs;
  20. //用户详情信息 - 可变字段
  21. Rx<HotelInfoEntity> hotelInfo = HotelInfoEntity().obs;
  22. //极光推送的 registrationId - 可变字段
  23. RxString registrationId = ''.obs;
  24. //当前用户的未读消息数量
  25. RxInt unreadNotificationsCount = 0.obs;
  26. bool get hasToken => token?.isNotEmpty ?? false;
  27. bool get isLogin => haslogin.value;
  28. HotelInfoEntity get getHotelInfo => hotelInfo.value;
  29. String get getRegistrationId => registrationId.value;
  30. int get getUnreadNotificationsCount => unreadNotificationsCount.value;
  31. /// 设置全局的Token,同时更新 hasLogin 的值,赋值时机如下
  32. /*
  33. 1. 在登录或注册成功的时候赋值
  34. 2. 在main.dart中初始化的时候查询是否有Token,如果有的话需要赋值
  35. 3. 退出登录的时候需要赋值
  36. */
  37. void setToken(String? token) {
  38. this.token = token;
  39. if (Utils.isEmpty(token)) {
  40. haslogin.value = false;
  41. SPUtil.remove(AppConstant.storageToken);
  42. } else {
  43. haslogin.value = true;
  44. SPUtil.putString(AppConstant.storageToken, token!);
  45. }
  46. Log.d('UserService =========> 设置Token为:$token');
  47. }
  48. // /// 请求接口获取用户详情
  49. // Future<HotelInfoEntity?> fetchHotelInfo() async {
  50. // //获取到数据
  51. // var result = await _authRepository.fetchHotelInfo();
  52. //
  53. // //处理数据
  54. // if (result.isSuccess) {
  55. // final userProfile = result.data;
  56. // if (userProfile != null) {
  57. // //赋值给Rx对象
  58. // this.hotelInfo.value = userProfile;
  59. // bus.emit(AppConstant.eventHotelInfoRefreshFinish, true);
  60. //
  61. // return userProfile;
  62. // } else {
  63. // bus.emit(AppConstant.eventHotelInfoRefreshFinish, true);
  64. // return null;
  65. // }
  66. // } else {
  67. // SmartDialog.showToast(result.errorMsg ?? '');
  68. // bus.emit(AppConstant.eventHotelInfoRefreshFinish, true);
  69. // return null;
  70. // }
  71. //
  72. // }
  73. /// 处理退出登录之后的数据清除
  74. void handleLogoutParams() {
  75. SPUtil.remove(AppConstant.storageToken);
  76. haslogin.value = false;
  77. hotelInfo.value = HotelInfoEntity();
  78. }
  79. @override
  80. void onInit() {
  81. super.onInit();
  82. String? token = SPUtil.getString(AppConstant.storageToken);
  83. Log.d('UserService - 查询SP token:$token 并赋值' );
  84. setToken(token);
  85. }
  86. }