94342c3f
Key
feat: user util
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:wow_english/common/core/const.dart';
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 {
static String token = '';
static saveUser(UserEntity user) {
token = user.token;
SpUtil.getInstance().setData(SpConst.prefsKeyUserInfo, user.toString());
}
static UserEntity? getUser() {
String? userJson = getUserJson();
if (userJson != null && userJson.isNotEmpty) {
var userEntity = UserEntity.fromJson(json.decode(userJson));
// todo 并且在有效期,计算一下, "expireTime": 2592000 倒计时需要手动转换,后面再优化
if (userEntity.token.isNotEmpty) {
token = userEntity.token;
return userEntity;
} else {
// token为空或失效的,清除sp
clearUserSp();
}
}
return null;
}
static saveUserJson(String userJson) {
SpUtil.getInstance().setData(SpConst.prefsKeyUserInfo, userJson);
}
static String? getUserJson() {
return SpUtil.getInstance().get<String>(SpConst.prefsKeyUserInfo);
}
static clearUserSp() {
token = '';
SpUtil.getInstance().remove(SpConst.prefsKeyUserInfo);
}
static logout() {
clearUserSp();
Navigator.of(AppRouter.context).push(AppRouteName.login as Route<Object?>);
}
}
|