Blame view

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