|
- import 'dart:convert';
-
- import 'package:flutter/material.dart';
- import 'package:flutter_alibc/flutter_alibc.dart';
- import 'package:zhiying_comm/models/user/user_info_model.dart';
- import 'package:zhiying_comm/util/global_config.dart';
- import 'package:zhiying_comm/util/log/let_log.dart';
- import 'package:zhiying_comm/util/shared_prefe_util.dart';
-
- ///
- /// 用户信息Provider
- ///
- class UserInfoNotifier with ChangeNotifier {
- /// 用户信息
- static UserInfoModel _userInfo;
-
- static UserInfoModel get staitcUserInfo {
- return _userInfo;
- }
-
- UserInfoModel get userInfo {
- if (null == _userInfo) {
- getUserInfoModel().then((user) {
- if (user != null) {
- _userInfo = user;
- notifyListeners();
- }
- });
- }
- return _userInfo;
- }
-
- /// 更新淘宝授权
- void updateUserAuth(bool isAuth) {
- if (null == _userInfo) {
- getUserInfoModel().then((user) {
- if (user != null) {
- _userInfo = user;
- _userInfo.isTBAuth = isAuth;
- Logger.log('upate data TBAuth');
- notifyListeners();
- }
- });
- } else {
- _userInfo.isTBAuth = isAuth;
- Logger.log('upate data TBAuth');
- notifyListeners();
- }
- }
-
- /// 更新用户数据
- void setUserInfo(UserInfoModel loginUser) async {
- print('${loginUser.toString()}');
- _userInfo = loginUser;
- // 缓存数据
- await SharedPreferencesUtil.setStringValue(
- GlobalConfig.SHARED_KEY_TOKEN, loginUser.token);
- await SharedPreferencesUtil.setStringValue(
- GlobalConfig.SHARED_KEY_MOBILE, loginUser.mobile);
- await SharedPreferencesUtil.setStringValue(
- GlobalConfig.SHARED_KEY_USER_INFO, jsonEncode(loginUser.toJson()));
- notifyListeners();
- }
-
- /// 退出登陆
- void unLogin() async {
- _userInfo = null;
- // 清除缓存数据
- await SharedPreferencesUtil.setStringValue(
- GlobalConfig.SHARED_KEY_TOKEN, '');
- await SharedPreferencesUtil.setStringValue(
- GlobalConfig.SHARED_KEY_MOBILE, '');
- await SharedPreferencesUtil.setStringValue(
- GlobalConfig.SHARED_KEY_USER_INFO, '');
- // 退出淘宝登录
- FlutterAlibc.loginOut();
- notifyListeners();
- }
-
- /// 获取登陆数据
- Future<UserInfoModel> getUserInfoModel() async {
- if (null != _userInfo) {
- return _userInfo;
- }
- try {
- String userInfoJson = await SharedPreferencesUtil.getStringValue(
- GlobalConfig.SHARED_KEY_USER_INFO);
- if (userInfoJson == null || userInfoJson == '') {
- _userInfo = UserInfoModel();
- } else {
- _userInfo = UserInfoModel.fromJson(jsonDecode(userInfoJson));
- }
- } catch (e) {
- Logger.log(e);
- }
- return _userInfo;
- }
- }
|