user_service.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'dart:core';
  2. import 'package:domain/entity/response/user_profile.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<UserProfile> userProfile = UserProfile().obs;
  22. //用户密码的类型 - 可变字段
  23. RxString passwordType = ''.obs;
  24. //极光推送的 registrationId - 可变字段
  25. RxString registrationId = ''.obs;
  26. //当前用户的未读消息数量
  27. RxInt unreadNotificationsCount = 0.obs;
  28. bool get hasToken => token?.isNotEmpty ?? false;
  29. bool get isLogin => haslogin.value;
  30. UserProfile get getUserProfile => userProfile.value;
  31. String get getRegistrationId => registrationId.value;
  32. int get getUnreadNotificationsCount => unreadNotificationsCount.value;
  33. /// 设置全局的Token,同时更新haslogin 的值,赋值时机如下
  34. /*
  35. 1. 在登录或注册成功的时候赋值
  36. 2. 在main.dart中初始化的时候查询是否有Token,如果有的话需要赋值
  37. 3. 退出登录的时候需要赋值
  38. */
  39. void setToken(String? token) {
  40. this.token = token;
  41. if (Utils.isEmpty(token)) {
  42. haslogin.value = false;
  43. SPUtil.remove(AppConstant.storageToken);
  44. } else {
  45. haslogin.value = true;
  46. SPUtil.putString(AppConstant.storageToken, token!);
  47. }
  48. Log.d('UserService =========> 设置Token为:$token');
  49. }
  50. /// 请求接口获取用户详情
  51. Future<UserProfile?> fetchUserProfile() async {
  52. //获取到数据
  53. var result = await _authRepository.fetchUserProfile();
  54. //处理数据
  55. if (result.isSuccess) {
  56. final userProfile = result.data;
  57. if (userProfile != null) {
  58. //赋值给Rx对象
  59. this.userProfile.value = userProfile;
  60. passwordType.value = userProfile.passwordType ?? '';
  61. bus.emit(AppConstant.eventProfileRefreshFinish, true);
  62. return userProfile;
  63. } else {
  64. bus.emit(AppConstant.eventProfileRefreshFinish, true);
  65. return null;
  66. }
  67. } else {
  68. SmartDialog.showToast(result.errorMsg ?? '');
  69. bus.emit(AppConstant.eventProfileRefreshFinish, true);
  70. return null;
  71. }
  72. }
  73. /// 处理退出登录之后的数据清除
  74. void handleLogoutParams() {
  75. SPUtil.remove(AppConstant.storageToken);
  76. haslogin.value = false;
  77. userProfile.value = UserProfile();
  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. }