mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-05-12 10:56:11 +02:00
fix abModel multi request and state didn't refresh bug
This commit is contained in:
parent
672d5f31d0
commit
8d23c11312
11 changed files with 111 additions and 230 deletions
|
@ -9,11 +9,65 @@ import '../common.dart';
|
|||
import 'model.dart';
|
||||
import 'platform_model.dart';
|
||||
|
||||
class UserModel extends ChangeNotifier {
|
||||
class UserModel {
|
||||
var userName = "".obs;
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
UserModel(this.parent);
|
||||
UserModel(this.parent) {
|
||||
refreshCurrentUser();
|
||||
}
|
||||
|
||||
void refreshCurrentUser() async {
|
||||
await getUserName();
|
||||
final token = await bind.mainGetLocalOption(key: "access_token");
|
||||
if (token == '') return;
|
||||
final url = await bind.mainGetApiServer();
|
||||
final body = {
|
||||
'id': await bind.mainGetMyId(),
|
||||
'uuid': await bind.mainGetUuid()
|
||||
};
|
||||
try {
|
||||
final response = await http.post(Uri.parse('$url/api/currentUser'),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer $token"
|
||||
},
|
||||
body: json.encode(body));
|
||||
final status = response.statusCode;
|
||||
if (status == 401 || status == 400) {
|
||||
resetToken();
|
||||
return;
|
||||
}
|
||||
await _parseResp(response.body);
|
||||
} catch (e) {
|
||||
print('Failed to refreshCurrentUser: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void resetToken() async {
|
||||
await bind.mainSetLocalOption(key: "access_token", value: "");
|
||||
await bind.mainSetLocalOption(key: "user_info", value: "");
|
||||
userName.value = "";
|
||||
}
|
||||
|
||||
Future<String> _parseResp(String body) async {
|
||||
final data = json.decode(body);
|
||||
final error = data['error'];
|
||||
if (error != null) {
|
||||
return error!;
|
||||
}
|
||||
final token = data['access_token'];
|
||||
if (token != null) {
|
||||
await bind.mainSetLocalOption(key: "access_token", value: token);
|
||||
}
|
||||
final info = data['user'];
|
||||
if (info != null) {
|
||||
final value = json.encode(info);
|
||||
await bind.mainSetOption(key: "user_info", value: value);
|
||||
userName.value = info["name"];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
Future<String> getUserName() async {
|
||||
if (userName.isNotEmpty) {
|
||||
|
@ -29,6 +83,7 @@ class UserModel extends ChangeNotifier {
|
|||
}
|
||||
|
||||
Future<void> logOut() async {
|
||||
// TODO show toast
|
||||
debugPrint("start logout");
|
||||
final url = await bind.mainGetApiServer();
|
||||
final _ = await http.post(Uri.parse("$url/api/logout"),
|
||||
|
@ -44,7 +99,6 @@ class UserModel extends ChangeNotifier {
|
|||
]);
|
||||
parent.target?.abModel.clear();
|
||||
userName.value = "";
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> login(String userName, String pass) async {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue