基础库
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.
 
 
 
 
 

109 lines
3.1 KiB

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