基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

100 lines
2.9 KiB

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_alibc/flutter_alibc.dart';
  4. import 'package:zhiying_comm/models/user/user_info_model.dart';
  5. import 'package:zhiying_comm/util/event_util/event_util.dart';
  6. import 'package:zhiying_comm/util/event_util/log_out.dart';
  7. import 'package:zhiying_comm/util/global_config.dart';
  8. import 'package:zhiying_comm/util/log/let_log.dart';
  9. import 'package:zhiying_comm/util/mob_util/mob_util.dart';
  10. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  11. ///
  12. /// 用户信息Provider
  13. ///
  14. class UserInfoNotifier with ChangeNotifier {
  15. /// 用户信息
  16. static UserInfoModel _userInfo;
  17. static UserInfoModel get staitcUserInfo {
  18. return _userInfo;
  19. }
  20. UserInfoModel get userInfo {
  21. if (null == _userInfo) {
  22. getUserInfoModel().then((user) {
  23. if (user != null) {
  24. _userInfo = user;
  25. notifyListeners();
  26. }
  27. });
  28. }
  29. return _userInfo;
  30. }
  31. /// 更新淘宝授权
  32. void updateUserAuth(bool isAuth) {
  33. if (null == _userInfo) {
  34. getUserInfoModel().then((user) {
  35. if (user != null) {
  36. _userInfo = user;
  37. _userInfo.isTBAuth = isAuth;
  38. Logger.log('upate data TBAuth');
  39. notifyListeners();
  40. }
  41. });
  42. } else {
  43. _userInfo.isTBAuth = isAuth;
  44. Logger.log('upate data TBAuth');
  45. notifyListeners();
  46. }
  47. }
  48. /// 更新用户数据
  49. void setUserInfo(UserInfoModel loginUser) async {
  50. print('${loginUser.toString()}');
  51. _userInfo = loginUser;
  52. // 缓存数据
  53. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, loginUser.token);
  54. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, loginUser.mobile);
  55. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, jsonEncode(loginUser.toJson()));
  56. notifyListeners();
  57. }
  58. /// 退出登陆
  59. void unLogin() async {
  60. _userInfo = null;
  61. // 清除缓存数据
  62. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, '');
  63. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, '');
  64. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, '');
  65. // 退出淘宝登录
  66. FlutterAlibc.loginOut();
  67. // 退出QQ登录
  68. MobUtil.cancelQQAuth();
  69. // 退出微信登录
  70. MobUtil.cancelWeChatAuth();
  71. EventUtil.instance.fire(LogOut());
  72. notifyListeners();
  73. }
  74. /// 获取登陆数据
  75. Future<UserInfoModel> getUserInfoModel() async {
  76. if (null != _userInfo) {
  77. return _userInfo;
  78. }
  79. try {
  80. String userInfoJson = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_USER_INFO);
  81. if (userInfoJson == null || userInfoJson == '') {
  82. _userInfo = UserInfoModel();
  83. } else {
  84. _userInfo = UserInfoModel.fromJson(jsonDecode(userInfoJson));
  85. }
  86. } catch (e) {
  87. Logger.log(e);
  88. }
  89. return _userInfo;
  90. }
  91. }