You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../constants/api.dart';
import '../model/login_info_model.dart';
import '../model/network/api_client.dart';
import '../model/user_info_model.dart';
import '../utils/hive_store.dart';
import '../utils/toast/cloud_toast.dart';
class UserProvider extends ChangeNotifier {
bool _isLogin = false;
bool get isLogin => _isLogin;
late UserInfoModel _userInfo;
UserInfoModel get userInfo => _userInfo;
Future<bool> init() async {
if (HiveStore.appBox?.containsKey('token') ?? false) {
final token = HiveStore.appBox?.get('token') as String;
_isLogin = true;
apiClient.setToken(token);
await updateUserInfo();
return true;
} else {
_isLogin = false;
return false;
}
}
Future setToken(String token, {User? user}) async {
apiClient.setToken(token);
await HiveStore.appBox!.put('token', token);
_isLogin = true;
//每次打开app更新用户信息
await updateUserInfo();
}
Future logout() async {
apiClient.clearToken();
_isLogin = false;
await HiveStore.appBox?.delete('token');
}
Future updateUserInfo() async {
var base = await apiClient.request(API.manager.info);
if (base.code == 0) {
_userInfo = UserInfoModel.fromJson(base.data);
} else {
CloudToast.show(base.msg);
_userInfo = UserInfoModel.fail;
}
notifyListeners();
}
}