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

user_info_model_notifier.dart 2.8 KiB

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