Blame view

lib/common/core/user_util.dart 1.98 KB
94342c3f   Key   feat: user util
1
2
  import 'dart:convert';
  
aa4e28be   Key   removed: cache_bl...
3
  import 'package:flutter/foundation.dart';
94342c3f   Key   feat: user util
4
  import 'package:flutter/material.dart';
e55bbdf3   Key   fixed: aliyun oss...
5
  import 'package:flutter/widgets.dart';
e12dbc82   Key   user module
6
  import 'package:wow_english/common/core/sp_const.dart';
94342c3f   Key   feat: user util
7
8
9
10
11
  import 'package:wow_english/models/user_entity.dart';
  import 'package:wow_english/route/route.dart';
  import 'package:wow_english/utils/sp_util.dart';
  
  class UserUtil {
aa4e28be   Key   removed: cache_bl...
12
13
14
    static UserEntity? _userEntity;
  
    static String get token => _userEntity?.token ?? '';
94342c3f   Key   feat: user util
15
  
a4d8eaa2   Key   feat: 登录时账户有效性校验
16
17
18
19
20
    static void saveUser(UserEntity? user) {
      if (user == null) {
        _clearUserData();
        return;
      }
aa4e28be   Key   removed: cache_bl...
21
22
      _userEntity = user;
      _saveUserJson(user.toString());
94342c3f   Key   feat: user util
23
24
25
    }
  
    static UserEntity? getUser() {
aa4e28be   Key   removed: cache_bl...
26
27
28
29
30
31
32
33
34
35
36
37
38
      if (_userEntity == null) {
        String? userJson = _getUserJson();
        if (userJson != null && userJson.isNotEmpty) {
          try {
            var userEntity = UserEntity.fromJson(json.decode(userJson));
            // todo 并且在有效期,计算一下, "expireTime": 2592000 倒计时需要手动转换,后面再优化
            _userEntity = userEntity;
          } catch (e) {
            if (kDebugMode) {
              print(e);
            }
            _clearUserData();
          }
94342c3f   Key   feat: user util
39
40
        }
      }
aa4e28be   Key   removed: cache_bl...
41
      return _userEntity;
94342c3f   Key   feat: user util
42
43
    }
  
aa4e28be   Key   removed: cache_bl...
44
    static void _saveUserJson(String userJson) {
94342c3f   Key   feat: user util
45
46
47
      SpUtil.getInstance().setData(SpConst.prefsKeyUserInfo, userJson);
    }
  
aa4e28be   Key   removed: cache_bl...
48
    static String? _getUserJson() {
94342c3f   Key   feat: user util
49
50
51
      return SpUtil.getInstance().get<String>(SpConst.prefsKeyUserInfo);
    }
  
aa4e28be   Key   removed: cache_bl...
52
53
    static void _clearUserData() {
      _userEntity = null;
94342c3f   Key   feat: user util
54
55
56
      SpUtil.getInstance().remove(SpConst.prefsKeyUserInfo);
    }
  
089ccd5c   Key   fixed: user util ...
57
    static void logout() {
aa4e28be   Key   removed: cache_bl...
58
      _clearUserData();
e55bbdf3   Key   fixed: aliyun oss...
59
60
61
62
63
64
      // 判断下不在Splash页就跳转
      /*var currentPageName = ModalRoute.of(AppRouter.context)?.settings.name;
      Log.d('当前页面:$currentPageName');
      if (currentPageName != AppRouteName.splash) {
        Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false);
      }*/
99efc573   Key   兼容非json response ...
65
      Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false);
94342c3f   Key   feat: user util
66
67
    }
  }