Blame view

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