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

51 lines
1.7 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. UserInfoModel _userInfo;
  13. /// 更新用户数据
  14. void setUserInfo(UserInfoModel loginUser) async {
  15. print('${loginUser.toString()}');
  16. this._userInfo = loginUser;
  17. // 缓存数据
  18. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, loginUser.token);
  19. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, loginUser.mobile);
  20. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, jsonEncode(loginUser.toJson()));
  21. notifyListeners();
  22. }
  23. /// 退出登陆
  24. void unLogin() async {
  25. this._userInfo = null;
  26. // 清除缓存数据
  27. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, '');
  28. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, '');
  29. await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, '');
  30. notifyListeners();
  31. }
  32. /// 获取登陆数据
  33. Future<UserInfoModel> getUserInfoModel() async {
  34. if (null != _userInfo) {
  35. return _userInfo;
  36. }
  37. try {
  38. String userInfoJson = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_USER_INFO);
  39. _userInfo = UserInfoModel.fromJson(jsonDecode(userInfoJson));
  40. } catch (e) {
  41. Logger.log(e);
  42. }
  43. return _userInfo;
  44. }
  45. }