2022-07-27 14:29:47 +08:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-01-06 18:26:19 +08:00
|
|
|
import 'package:flutter_hbb/common/hbbs/hbbs.dart';
|
2022-12-11 21:40:35 +08:00
|
|
|
import 'package:flutter_hbb/common/widgets/peer_tab_page.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';
|
2022-08-03 22:03:31 +08:00
|
|
|
import 'platform_model.dart';
|
2022-07-27 14:29:47 +08:00
|
|
|
|
2022-10-09 18:41:50 +08:00
|
|
|
class UserModel {
|
2022-12-11 21:40:35 +08:00
|
|
|
final RxString userName = ''.obs;
|
|
|
|
final RxString groupName = ''.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);
|
2022-10-09 18:41:50 +08:00
|
|
|
|
|
|
|
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 == '') {
|
|
|
|
await _updateOtherModels();
|
|
|
|
return;
|
|
|
|
}
|
2022-10-09 18:41:50 +08:00
|
|
|
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: {
|
2022-10-22 22:19:14 +08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer $token'
|
2022-10-09 18:41:50 +08:00
|
|
|
},
|
|
|
|
body: json.encode(body));
|
|
|
|
final status = response.statusCode;
|
|
|
|
if (status == 401 || status == 400) {
|
2022-12-11 21:40:35 +08:00
|
|
|
reset();
|
2022-10-09 18:41:50 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-12-11 21:40:35 +08:00
|
|
|
final data = json.decode(response.body);
|
|
|
|
final error = data['error'];
|
|
|
|
if (error != null) {
|
|
|
|
throw error;
|
|
|
|
}
|
2023-01-06 18:26:19 +08:00
|
|
|
|
|
|
|
final user = UserPayload.fromJson(data);
|
|
|
|
await _parseAndUpdateUser(user);
|
2022-10-09 18:41:50 +08:00
|
|
|
} catch (e) {
|
|
|
|
print('Failed to refreshCurrentUser: $e');
|
2022-12-11 21:40:35 +08:00
|
|
|
} finally {
|
|
|
|
await _updateOtherModels();
|
2022-10-09 18:41:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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: '');
|
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 = '';
|
2022-12-11 21:40:35 +08:00
|
|
|
groupName.value = '';
|
|
|
|
statePeerTab.check();
|
2022-10-09 18:41:50 +08:00
|
|
|
}
|
|
|
|
|
2023-01-06 18:26:19 +08:00
|
|
|
Future<void> _parseAndUpdateUser(UserPayload user) async {
|
|
|
|
userName.value = user.name;
|
|
|
|
groupName.value = user.grp;
|
|
|
|
isAdmin.value = user.isAdmin;
|
2022-10-09 18:41:50 +08:00
|
|
|
}
|
2022-07-27 14:29:47 +08:00
|
|
|
|
2022-12-11 21:40:35 +08:00
|
|
|
Future<void> _updateOtherModels() async {
|
|
|
|
await gFFI.abModel.pullAb();
|
|
|
|
await gFFI.groupModel.pull();
|
2022-07-27 14:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> logOut() async {
|
2022-10-09 18:57:38 +08:00
|
|
|
final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
|
2022-12-16 22:18:30 +08:00
|
|
|
try {
|
|
|
|
final url = await bind.mainGetApiServer();
|
2022-12-20 22:36:04 +08:00
|
|
|
await http
|
|
|
|
.post(Uri.parse('$url/api/logout'),
|
|
|
|
body: {
|
|
|
|
'id': await bind.mainGetMyId(),
|
|
|
|
'uuid': await bind.mainGetUuid(),
|
|
|
|
},
|
2023-01-06 18:26:19 +08:00
|
|
|
headers: getHttpHeaders())
|
2022-12-20 22:36:04 +08:00
|
|
|
.timeout(Duration(seconds: 2));
|
|
|
|
} catch (e) {
|
|
|
|
print("request /api/logout failed: err=$e");
|
2022-12-16 22:18:30 +08:00
|
|
|
} finally {
|
|
|
|
await reset();
|
|
|
|
gFFI.dialogManager.dismissByTag(tag);
|
|
|
|
}
|
2022-07-27 14:29:47 +08:00
|
|
|
}
|
|
|
|
|
2023-01-06 18:26:19 +08: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 18:26:19 +08: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 {
|
2023-01-06 18:26:19 +08:00
|
|
|
body = jsonDecode(resp.body);
|
|
|
|
} catch (e) {
|
|
|
|
print("jsonDecode resp body failed: ${e.toString()}");
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw RequestException(resp.statusCode, body['error'] ?? '');
|
2022-07-27 14:29:47 +08:00
|
|
|
}
|
2023-01-06 18:26:19 +08:00
|
|
|
|
|
|
|
final LoginResponse loginResponse;
|
|
|
|
try {
|
|
|
|
loginResponse = LoginResponse.fromJson(body);
|
|
|
|
} catch (e) {
|
|
|
|
print("jsonDecode LoginResponse failed: ${e.toString()}");
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loginResponse.user != null) {
|
|
|
|
await _parseAndUpdateUser(loginResponse.user!);
|
|
|
|
}
|
|
|
|
|
|
|
|
return loginResponse;
|
2022-07-27 14:29:47 +08:00
|
|
|
}
|
|
|
|
}
|