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

78 lines
2.1 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 setUserInfo(UserInfoModel loginUser) async {
  29. print('${loginUser.toString()}');
  30. _userInfo = loginUser;
  31. // 缓存数据
  32. await SharedPreferencesUtil.setStringValue(
  33. GlobalConfig.SHARED_KEY_TOKEN, loginUser.token);
  34. await SharedPreferencesUtil.setStringValue(
  35. GlobalConfig.SHARED_KEY_MOBILE, loginUser.mobile);
  36. await SharedPreferencesUtil.setStringValue(
  37. GlobalConfig.SHARED_KEY_USER_INFO, jsonEncode(loginUser.toJson()));
  38. notifyListeners();
  39. }
  40. /// 退出登陆
  41. void unLogin() async {
  42. _userInfo = null;
  43. // 清除缓存数据
  44. await SharedPreferencesUtil.setStringValue(
  45. GlobalConfig.SHARED_KEY_TOKEN, '');
  46. await SharedPreferencesUtil.setStringValue(
  47. GlobalConfig.SHARED_KEY_MOBILE, '');
  48. await SharedPreferencesUtil.setStringValue(
  49. GlobalConfig.SHARED_KEY_USER_INFO, '');
  50. notifyListeners();
  51. }
  52. /// 获取登陆数据
  53. Future<UserInfoModel> getUserInfoModel() async {
  54. if (null != _userInfo) {
  55. return _userInfo;
  56. }
  57. try {
  58. String userInfoJson = await SharedPreferencesUtil.getStringValue(
  59. GlobalConfig.SHARED_KEY_USER_INFO);
  60. if (userInfoJson == null || userInfoJson == '') {
  61. _userInfo = UserInfoModel();
  62. } else {
  63. _userInfo = UserInfoModel.fromJson(jsonDecode(userInfoJson));
  64. }
  65. } catch (e) {
  66. Logger.log(e);
  67. }
  68. return _userInfo;
  69. }
  70. }