Blame view

lib/common/core/user_util.dart 2.39 KB
94342c3f   Key   feat: user util
1
2
  import 'dart:convert';
  
aa4e28be   Key   removed: cache_bl...
3
  import 'package:flutter/foundation.dart';
27dad3d1   吴启风   feat:集成友盟统计
4
  import 'package:umeng_common_sdk/umeng_common_sdk.dart';
e12dbc82   Key   user module
5
  import 'package:wow_english/common/core/sp_const.dart';
94342c3f   Key   feat: user util
6
7
8
9
10
  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...
11
12
    static UserEntity? _userEntity;
  
a4d8eaa2   Key   feat: 登录时账户有效性校验
13
14
15
16
17
    static void saveUser(UserEntity? user) {
      if (user == null) {
        _clearUserData();
        return;
      }
aa4e28be   Key   removed: cache_bl...
18
19
      _userEntity = user;
      _saveUserJson(user.toString());
94342c3f   Key   feat: user util
20
21
22
    }
  
    static UserEntity? getUser() {
aa4e28be   Key   removed: cache_bl...
23
24
25
26
27
28
29
30
31
32
33
34
35
      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
36
37
        }
      }
aa4e28be   Key   removed: cache_bl...
38
      return _userEntity;
94342c3f   Key   feat: user util
39
40
    }
  
aa4e28be   Key   removed: cache_bl...
41
    static void _saveUserJson(String userJson) {
94342c3f   Key   feat: user util
42
43
44
      SpUtil.getInstance().setData(SpConst.prefsKeyUserInfo, userJson);
    }
  
aa4e28be   Key   removed: cache_bl...
45
    static String? _getUserJson() {
94342c3f   Key   feat: user util
46
47
48
      return SpUtil.getInstance().get<String>(SpConst.prefsKeyUserInfo);
    }
  
aa4e28be   Key   removed: cache_bl...
49
50
    static void _clearUserData() {
      _userEntity = null;
94342c3f   Key   feat: user util
51
52
53
      SpUtil.getInstance().remove(SpConst.prefsKeyUserInfo);
    }
  
578f1729   吴启风   fix:修复老bug:修改密码后跳...
54
    static void logout([bool showPasswordLoginPage = false]) {
aa4e28be   Key   removed: cache_bl...
55
      _clearUserData();
e55bbdf3   Key   fixed: aliyun oss...
56
57
58
59
60
61
      // 判断下不在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);
      }*/
27dad3d1   吴启风   feat:集成友盟统计
62
      UmengCommonSdk.onProfileSignOff();
48a1b645   吴启风   feat:删除无效日志
63
      pushNamedAndRemoveUntil(AppRouteName.login, (route) => false, arguments: {'showPasswordPage': showPasswordLoginPage});
94342c3f   Key   feat: user util
64
    }
278208b8   吴启风   feat:1、用户访问权限调整;2...
65
  
578775ca   吴启风   feat:课程学习增加vip权限控制
66
67
    // 是否有权限
    static bool hasPermission() {
278208b8   吴启风   feat:1、用户访问权限调整;2...
68
69
70
71
72
73
74
75
76
77
78
      return _userEntity?.valid ?? false;
    }
  
    // 是否登录(token是否有效)
    static bool isLogined() {
      return getUserToken().isNotEmpty;
    }
  
    static String getUserToken() {
      return _userEntity?.token ?? '';
    }
28f20da9   吴启风   feat:针对apple审核对支付...
79
80
81
82
83
  
    // (vip)剩余有效期
    static int getRemainingValidity() {
      return _userEntity?.validDay ?? 0;
    }
94342c3f   Key   feat: user util
84
  }