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.

60 lines
1.6 KiB

2 years ago
import 'package:cloud_car_internal/models/user/user_info_model.dart';
import 'package:cloud_car_internal/utils/api_client.dart';
import 'package:cloud_car_internal/utils/hive_util.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import '../constants/api/api.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 {
var appBox = await HiveUtil().openAppBox;
if (appBox.containsKey('token')) {
final token = appBox.get('token') as String;
_isLogin = true;
apiClient.setToken(token);
await updateUserInfo();
return true;
} else {
_isLogin = false;
return false;
}
}
Future setToken(String token, ) async {
var appBox = await HiveUtil().openAppBox;
apiClient.setToken(token);
await appBox.put('token', token);
_isLogin = true;
//每次打开app更新用户信息
await updateUserInfo();
}
Future logout() async {
var appBox = await HiveUtil().openAppBox;
apiClient.clearToken();
_isLogin = false;
await appBox.delete('token');
}
Future updateUserInfo() async {
// var base = await apiClient.request(API.user.userInfo);
// if (base.code == 0) {
// _userInfo = UserInfoModel.fromJson(base.data);
// } else {
// CloudToast.show(base.msg);
// _userInfo = UserInfoModel.fail;
// }
notifyListeners();
}
}