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

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