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

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