Blame view

lib/common/core/user_util.dart 2.38 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
    static UserEntity? _userEntity;
  
a4d8eaa2   Key   feat: 登录时账户有效性校验
14
15
16
17
18
    static void saveUser(UserEntity? user) {
      if (user == null) {
        _clearUserData();
        return;
      }
aa4e28be   Key   removed: cache_bl...
19
20
      _userEntity = user;
      _saveUserJson(user.toString());
94342c3f   Key   feat: user util
21
22
23
    }
  
    static UserEntity? getUser() {
aa4e28be   Key   removed: cache_bl...
24
25
26
27
28
29
30
31
32
33
34
35
36
      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
37
38
        }
      }
aa4e28be   Key   removed: cache_bl...
39
      return _userEntity;
94342c3f   Key   feat: user util
40
41
    }
  
aa4e28be   Key   removed: cache_bl...
42
    static void _saveUserJson(String userJson) {
94342c3f   Key   feat: user util
43
44
45
      SpUtil.getInstance().setData(SpConst.prefsKeyUserInfo, userJson);
    }
  
aa4e28be   Key   removed: cache_bl...
46
    static String? _getUserJson() {
94342c3f   Key   feat: user util
47
48
49
      return SpUtil.getInstance().get<String>(SpConst.prefsKeyUserInfo);
    }
  
aa4e28be   Key   removed: cache_bl...
50
51
    static void _clearUserData() {
      _userEntity = null;
94342c3f   Key   feat: user util
52
53
54
      SpUtil.getInstance().remove(SpConst.prefsKeyUserInfo);
    }
  
578f1729   吴启风   fix:修复老bug:修改密码后跳...
55
    static void logout([bool showPasswordLoginPage = false]) {
aa4e28be   Key   removed: cache_bl...
56
      _clearUserData();
e55bbdf3   Key   fixed: aliyun oss...
57
58
59
60
61
62
      // 判断下不在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);
      }*/
48a1b645   吴启风   feat:删除无效日志
63
      pushNamedAndRemoveUntil(AppRouteName.login, (route) => false, arguments: {'showPasswordPage': showPasswordLoginPage});
94342c3f   Key   feat: user util
64
    }
278208b8   吴启风   feat:1、用户访问权限调整;2...
65
66
67
  
    // 是否有游戏权限
    static bool hasGamePermission() {
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
  }