Files
rustdesk/flutter/lib/models/user_model.dart
T

178 lines
4.9 KiB
Dart
Raw Normal View History

2022-07-27 14:29:47 +08:00
import 'dart:async';
import 'dart:convert';
2023-06-22 10:04:16 +08:00
import 'package:flutter/material.dart';
2023-01-06 19:26:19 +09:00
import 'package:flutter_hbb/common/hbbs/hbbs.dart';
2022-07-27 14:29:47 +08:00
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
2022-09-27 17:52:36 +08:00
import '../common.dart';
2022-07-27 14:29:47 +08:00
import 'model.dart';
import 'platform_model.dart';
2022-07-27 14:29:47 +08:00
2023-07-01 10:19:59 +08:00
bool refreshingUser = false;
class UserModel {
2022-12-11 21:40:35 +08:00
final RxString userName = ''.obs;
final RxBool isAdmin = false.obs;
2022-07-27 14:29:47 +08:00
WeakReference<FFI> parent;
2022-12-11 21:40:35 +08:00
UserModel(this.parent);
void refreshCurrentUser() async {
2022-11-10 21:25:12 +08:00
final token = bind.mainGetLocalOption(key: 'access_token');
2022-12-11 21:40:35 +08:00
if (token == '') {
2023-06-21 18:28:52 +08:00
await updateOtherModels();
2022-12-11 21:40:35 +08:00
return;
}
2023-06-21 11:39:48 +08:00
_updateLocalUserInfo();
final url = await bind.mainGetApiServer();
final body = {
'id': await bind.mainGetMyId(),
'uuid': await bind.mainGetUuid()
};
2023-07-01 10:19:59 +08:00
if (refreshingUser) return;
try {
2023-07-01 10:19:59 +08:00
refreshingUser = true;
final response = await http.post(Uri.parse('$url/api/currentUser'),
headers: {
2022-10-22 22:19:14 +08:00
'Content-Type': 'application/json',
'Authorization': 'Bearer $token'
},
body: json.encode(body));
2023-07-01 10:19:59 +08:00
refreshingUser = false;
final status = response.statusCode;
if (status == 401 || status == 400) {
2022-12-11 21:40:35 +08:00
reset();
return;
}
final data = json.decode(utf8.decode(response.bodyBytes));
2022-12-11 21:40:35 +08:00
final error = data['error'];
if (error != null) {
throw error;
}
2023-01-06 19:26:19 +09:00
final user = UserPayload.fromJson(data);
2023-06-21 11:32:50 +08:00
_parseAndUpdateUser(user);
} catch (e) {
2023-06-22 08:25:29 +08:00
debugPrint('Failed to refreshCurrentUser: $e');
2022-12-11 21:40:35 +08:00
} finally {
2023-07-01 10:19:59 +08:00
refreshingUser = false;
2023-06-21 18:28:52 +08:00
await updateOtherModels();
}
}
2023-06-21 11:32:50 +08:00
static Map<String, dynamic>? getLocalUserInfo() {
2023-06-22 08:25:29 +08:00
final userInfo = bind.mainGetLocalOption(key: 'user_info');
if (userInfo == '') {
return null;
}
2023-06-21 11:32:50 +08:00
try {
2023-06-22 08:25:29 +08:00
return json.decode(userInfo);
2023-06-21 11:32:50 +08:00
} catch (e) {
2023-06-22 22:33:54 +08:00
debugPrint('Failed to get local user info "$userInfo": $e');
2023-06-21 11:32:50 +08:00
}
return null;
}
_updateLocalUserInfo() {
final userInfo = getLocalUserInfo();
if (userInfo != null) {
userName.value = userInfo['name'];
}
}
2022-12-11 21:40:35 +08:00
Future<void> reset() async {
2022-10-22 22:19:14 +08:00
await bind.mainSetLocalOption(key: 'access_token', value: '');
2023-06-21 23:58:13 +08:00
await bind.mainSetLocalOption(key: 'user_info', value: '');
2022-12-11 21:40:35 +08:00
await gFFI.abModel.reset();
await gFFI.groupModel.reset();
2022-10-22 22:19:14 +08:00
userName.value = '';
}
2023-06-21 11:32:50 +08:00
_parseAndUpdateUser(UserPayload user) {
2023-01-06 19:26:19 +09:00
userName.value = user.name;
isAdmin.value = user.isAdmin;
}
2022-07-27 14:29:47 +08:00
2023-06-21 18:28:52 +08:00
// update ab and group status
static Future<void> updateOtherModels() async {
2023-06-21 16:04:52 +08:00
await Future.wait([gFFI.abModel.pullAb(), gFFI.groupModel.pull()]);
2022-07-27 14:29:47 +08:00
}
Future<void> logOut() async {
2022-10-09 19:57:38 +09:00
final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
try {
final url = await bind.mainGetApiServer();
2023-01-28 22:02:42 +09:00
final authHeaders = getHttpHeaders();
authHeaders['Content-Type'] = "application/json";
await http
.post(Uri.parse('$url/api/logout'),
2023-01-28 22:02:42 +09:00
body: jsonEncode({
'id': await bind.mainGetMyId(),
'uuid': await bind.mainGetUuid(),
2023-01-28 22:02:42 +09:00
}),
headers: authHeaders)
.timeout(Duration(seconds: 2));
} catch (e) {
2023-06-22 08:25:29 +08:00
debugPrint("request /api/logout failed: err=$e");
} finally {
await reset();
gFFI.dialogManager.dismissByTag(tag);
}
2022-07-27 14:29:47 +08:00
}
2023-01-06 19:26:19 +09:00
/// throw [RequestException]
Future<LoginResponse> login(LoginRequest loginRequest) async {
2022-07-27 14:29:47 +08:00
final url = await bind.mainGetApiServer();
2023-01-06 19:26:19 +09:00
final resp = await http.post(Uri.parse('$url/api/login'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode(loginRequest.toJson()));
final Map<String, dynamic> body;
2022-07-27 14:29:47 +08:00
try {
body = jsonDecode(utf8.decode(resp.bodyBytes));
2023-01-06 19:26:19 +09:00
} catch (e) {
2023-06-22 08:25:29 +08:00
debugPrint("login: jsonDecode resp body failed: ${e.toString()}");
2023-01-06 19:26:19 +09:00
rethrow;
2022-07-27 14:29:47 +08:00
}
2023-01-06 19:26:19 +09:00
if (resp.statusCode != 200) {
throw RequestException(resp.statusCode, body['error'] ?? '');
}
2023-06-22 22:33:54 +08:00
if (body['error'] != null) {
throw RequestException(0, body['error']);
}
2023-01-06 19:26:19 +09:00
2023-06-21 11:32:50 +08:00
return getLoginResponseFromAuthBody(body);
}
LoginResponse getLoginResponseFromAuthBody(Map<String, dynamic> body) {
2023-01-06 19:26:19 +09:00
final LoginResponse loginResponse;
try {
loginResponse = LoginResponse.fromJson(body);
} catch (e) {
2023-06-22 08:25:29 +08:00
debugPrint("login: jsonDecode LoginResponse failed: ${e.toString()}");
2023-01-06 19:26:19 +09:00
rethrow;
}
if (loginResponse.user != null) {
2023-06-21 11:32:50 +08:00
_parseAndUpdateUser(loginResponse.user!);
2023-01-06 19:26:19 +09:00
}
return loginResponse;
2022-07-27 14:29:47 +08:00
}
2023-06-14 15:17:20 +08:00
static Future<List<dynamic>> queryLoginOptions() async {
try {
final url = await bind.mainGetApiServer();
2023-06-29 06:25:17 +08:00
if (url.trim().isEmpty) return [];
final resp = await http.get(Uri.parse('$url/api/login-options'));
2023-06-14 15:17:20 +08:00
return jsonDecode(resp.body);
} catch (e) {
2023-06-22 10:04:16 +08:00
debugPrint(
"queryLoginOptions: jsonDecode resp body failed: ${e.toString()}");
2023-06-14 15:17:20 +08:00
return [];
}
}
2022-07-27 14:29:47 +08:00
}