| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  | import 'dart:async'; | 
					
						
							|  |  |  | import 'dart:convert'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 19:41:50 +09: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 19:41:50 +09: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 19:41:50 +09: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 19:41:50 +09: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 19:41:50 +09: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; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       await _parseUserInfo(data); | 
					
						
							| 
									
										
										
										
											2022-10-09 19:41:50 +09:00
										 |  |  |     } catch (e) { | 
					
						
							|  |  |  |       print('Failed to refreshCurrentUser: $e'); | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |     } finally { | 
					
						
							|  |  |  |       await _updateOtherModels(); | 
					
						
							| 
									
										
										
										
											2022-10-09 19:41:50 +09: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: ''); | 
					
						
							|  |  |  |     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 = ''; | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |     groupName.value = ''; | 
					
						
							|  |  |  |     statePeerTab.check(); | 
					
						
							| 
									
										
										
										
											2022-10-09 19:41:50 +09:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |   Future<void> _parseUserInfo(dynamic userinfo) async { | 
					
						
							|  |  |  |     bind.mainSetLocalOption(key: 'user_info', value: jsonEncode(userinfo)); | 
					
						
							|  |  |  |     userName.value = userinfo['name'] ?? ''; | 
					
						
							|  |  |  |     groupName.value = userinfo['grp'] ?? ''; | 
					
						
							|  |  |  |     isAdmin.value = userinfo['is_admin'] == true; | 
					
						
							| 
									
										
										
										
											2022-10-09 19:41:50 +09: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 19:57:38 +09:00
										 |  |  |     final tag = gFFI.dialogManager.showLoading(translate('Waiting')); | 
					
						
							| 
									
										
										
										
											2022-12-16 23:18:30 +09:00
										 |  |  |     try { | 
					
						
							|  |  |  |       final url = await bind.mainGetApiServer(); | 
					
						
							|  |  |  |       final _ = await http.post(Uri.parse('$url/api/logout'), | 
					
						
							|  |  |  |           body: { | 
					
						
							|  |  |  |             'id': await bind.mainGetMyId(), | 
					
						
							|  |  |  |             'uuid': await bind.mainGetUuid(), | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           headers: await getHttpHeaders()); | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       await reset(); | 
					
						
							|  |  |  |       gFFI.dialogManager.dismissByTag(tag); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<Map<String, dynamic>> login(String userName, String pass) async { | 
					
						
							|  |  |  |     final url = await bind.mainGetApiServer(); | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2022-10-22 22:19:14 +08:00
										 |  |  |       final resp = await http.post(Uri.parse('$url/api/login'), | 
					
						
							|  |  |  |           headers: {'Content-Type': 'application/json'}, | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  |           body: jsonEncode({ | 
					
						
							| 
									
										
										
										
											2022-10-22 22:19:14 +08:00
										 |  |  |             'username': userName, | 
					
						
							|  |  |  |             'password': pass, | 
					
						
							|  |  |  |             'id': await bind.mainGetMyId(), | 
					
						
							|  |  |  |             'uuid': await bind.mainGetUuid() | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  |           })); | 
					
						
							|  |  |  |       final body = jsonDecode(resp.body); | 
					
						
							|  |  |  |       bind.mainSetLocalOption( | 
					
						
							| 
									
										
										
										
											2022-10-22 22:19:14 +08:00
										 |  |  |           key: 'access_token', value: body['access_token'] ?? ''); | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |       await _parseUserInfo(body['user']); | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  |       return body; | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							| 
									
										
										
										
											2022-10-22 22:19:14 +08:00
										 |  |  |       return {'error': '$err'}; | 
					
						
							| 
									
										
										
										
											2022-12-11 21:40:35 +08:00
										 |  |  |     } finally { | 
					
						
							|  |  |  |       await _updateOtherModels(); | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |