基础库
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

108 linhas
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. notifyListeners();
  59. }
  60. /// 退出登陆
  61. void unLogin() async {
  62. _userInfo = null;
  63. // 清除缓存数据
  64. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, '');
  65. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, '');
  66. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, '');
  67. // 退出淘宝登录
  68. FlutterAlibc.loginOut();
  69. // 退出QQ登录
  70. MobUtil.cancelQQAuth();
  71. // 退出微信登录
  72. MobUtil.cancelWeChatAuth();
  73. if(!reFresh){
  74. reFresh=true;
  75. Timer(Duration(milliseconds: 1000), (){
  76. EventUtil.instance.fire(LogOut());
  77. });
  78. }
  79. notifyListeners();
  80. }
  81. /// 获取登陆数据
  82. Future<UserInfoModel> getUserInfoModel() async {
  83. if (null != _userInfo) {
  84. return _userInfo;
  85. }
  86. try {
  87. String userInfoJson = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_USER_INFO);
  88. if (userInfoJson == null || userInfoJson == '') {
  89. _userInfo = UserInfoModel();
  90. } else {
  91. _userInfo = UserInfoModel.fromJson(jsonDecode(userInfoJson));
  92. }
  93. } catch (e) {
  94. Logger.log(e);
  95. }
  96. return _userInfo;
  97. }
  98. }