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

96 lines
2.6 KiB

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