48 lines
1.1 KiB
Dart
48 lines
1.1 KiB
Dart
import 'package:app/data/local/storage.dart';
|
|
import 'package:app/request/api/user_api.dart';
|
|
import 'package:app/request/dto/user/user_info_dto.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
class UserStore extends ChangeNotifier {
|
|
UserInfoDto? userInfo;
|
|
String token = "";
|
|
|
|
///设置用户数据
|
|
Future<void> asyncUserInfo() async {
|
|
if (token.isNotEmpty) {
|
|
var res = await getUserInfoApi();
|
|
await Storage.set("user_info", res.toJson());
|
|
setUserInfo();
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
///获取用户数据
|
|
Future<void> setUserInfo() async {
|
|
var info = await Storage.get("user_info");
|
|
if (info != null) {
|
|
userInfo = UserInfoDto.fromJson(info);
|
|
}
|
|
}
|
|
|
|
///设置token
|
|
Future<void> setToken(String value) async {
|
|
token = value;
|
|
await Storage.set('token', token);
|
|
}
|
|
|
|
///获取token
|
|
static Future<String> getToken() async {
|
|
return await Storage.get("token") ?? '';
|
|
}
|
|
|
|
///退出登录
|
|
Future<void> logout() async {
|
|
logoutApi();
|
|
await Storage.remove('token');
|
|
await Storage.remove('user_info');
|
|
token = '';
|
|
notifyListeners();
|
|
}
|
|
}
|