user_model.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:bot_toast/bot_toast.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_hbb/common/hbbs/hbbs.dart';
  6. import 'package:flutter_hbb/models/ab_model.dart';
  7. import 'package:get/get.dart';
  8. import '../common.dart';
  9. import '../utils/http_service.dart' as http;
  10. import 'model.dart';
  11. import 'platform_model.dart';
  12. bool refreshingUser = false;
  13. class UserModel {
  14. final RxString userName = ''.obs;
  15. final RxBool isAdmin = false.obs;
  16. final RxString networkError = ''.obs;
  17. bool get isLogin => userName.isNotEmpty;
  18. WeakReference<FFI> parent;
  19. UserModel(this.parent) {
  20. userName.listen((p0) {
  21. // When user name becomes empty, show login button
  22. // When user name becomes non-empty:
  23. // For _updateLocalUserInfo, network error will be set later
  24. // For login success, should clear network error
  25. networkError.value = '';
  26. });
  27. }
  28. void refreshCurrentUser() async {
  29. if (bind.isDisableAccount()) return;
  30. networkError.value = '';
  31. final token = bind.mainGetLocalOption(key: 'access_token');
  32. if (token == '') {
  33. await updateOtherModels();
  34. return;
  35. }
  36. _updateLocalUserInfo();
  37. final url = await bind.mainGetApiServer();
  38. final body = {
  39. 'id': await bind.mainGetMyId(),
  40. 'uuid': await bind.mainGetUuid()
  41. };
  42. if (refreshingUser) return;
  43. try {
  44. refreshingUser = true;
  45. final http.Response response;
  46. try {
  47. response = await http.post(Uri.parse('$url/api/currentUser'),
  48. headers: {
  49. 'Content-Type': 'application/json',
  50. 'Authorization': 'Bearer $token'
  51. },
  52. body: json.encode(body));
  53. } catch (e) {
  54. networkError.value = e.toString();
  55. rethrow;
  56. }
  57. refreshingUser = false;
  58. final status = response.statusCode;
  59. if (status == 401 || status == 400) {
  60. reset(resetOther: status == 401);
  61. return;
  62. }
  63. final data = json.decode(utf8.decode(response.bodyBytes));
  64. final error = data['error'];
  65. if (error != null) {
  66. throw error;
  67. }
  68. final user = UserPayload.fromJson(data);
  69. _parseAndUpdateUser(user);
  70. } catch (e) {
  71. debugPrint('Failed to refreshCurrentUser: $e');
  72. } finally {
  73. refreshingUser = false;
  74. await updateOtherModels();
  75. }
  76. }
  77. static Map<String, dynamic>? getLocalUserInfo() {
  78. final userInfo = bind.mainGetLocalOption(key: 'user_info');
  79. if (userInfo == '') {
  80. return null;
  81. }
  82. try {
  83. return json.decode(userInfo);
  84. } catch (e) {
  85. debugPrint('Failed to get local user info "$userInfo": $e');
  86. }
  87. return null;
  88. }
  89. _updateLocalUserInfo() {
  90. final userInfo = getLocalUserInfo();
  91. if (userInfo != null) {
  92. userName.value = userInfo['name'];
  93. }
  94. }
  95. Future<void> reset({bool resetOther = false}) async {
  96. await bind.mainSetLocalOption(key: 'access_token', value: '');
  97. await bind.mainSetLocalOption(key: 'user_info', value: '');
  98. if (resetOther) {
  99. await gFFI.abModel.reset();
  100. await gFFI.groupModel.reset();
  101. }
  102. userName.value = '';
  103. }
  104. _parseAndUpdateUser(UserPayload user) {
  105. userName.value = user.name;
  106. isAdmin.value = user.isAdmin;
  107. bind.mainSetLocalOption(key: 'user_info', value: jsonEncode(user));
  108. }
  109. // update ab and group status
  110. static Future<void> updateOtherModels() async {
  111. await Future.wait([
  112. gFFI.abModel.pullAb(force: ForcePullAb.listAndCurrent, quiet: false),
  113. gFFI.groupModel.pull()
  114. ]);
  115. }
  116. Future<void> logOut({String? apiServer}) async {
  117. final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
  118. try {
  119. final url = apiServer ?? await bind.mainGetApiServer();
  120. final authHeaders = getHttpHeaders();
  121. authHeaders['Content-Type'] = "application/json";
  122. await http
  123. .post(Uri.parse('$url/api/logout'),
  124. body: jsonEncode({
  125. 'id': await bind.mainGetMyId(),
  126. 'uuid': await bind.mainGetUuid(),
  127. }),
  128. headers: authHeaders)
  129. .timeout(Duration(seconds: 2));
  130. } catch (e) {
  131. debugPrint("request /api/logout failed: err=$e");
  132. } finally {
  133. await reset(resetOther: true);
  134. gFFI.dialogManager.dismissByTag(tag);
  135. }
  136. }
  137. /// throw [RequestException]
  138. Future<LoginResponse> login(LoginRequest loginRequest) async {
  139. final url = await bind.mainGetApiServer();
  140. final resp = await http.post(Uri.parse('$url/api/login'),
  141. body: jsonEncode(loginRequest.toJson()));
  142. final Map<String, dynamic> body;
  143. try {
  144. body = jsonDecode(utf8.decode(resp.bodyBytes));
  145. } catch (e) {
  146. debugPrint("login: jsonDecode resp body failed: ${e.toString()}");
  147. if (resp.statusCode != 200) {
  148. BotToast.showText(
  149. contentColor: Colors.red, text: 'HTTP ${resp.statusCode}');
  150. }
  151. rethrow;
  152. }
  153. if (resp.statusCode != 200) {
  154. throw RequestException(resp.statusCode, body['error'] ?? '');
  155. }
  156. if (body['error'] != null) {
  157. throw RequestException(0, body['error']);
  158. }
  159. return getLoginResponseFromAuthBody(body);
  160. }
  161. LoginResponse getLoginResponseFromAuthBody(Map<String, dynamic> body) {
  162. final LoginResponse loginResponse;
  163. try {
  164. loginResponse = LoginResponse.fromJson(body);
  165. } catch (e) {
  166. debugPrint("login: jsonDecode LoginResponse failed: ${e.toString()}");
  167. rethrow;
  168. }
  169. if (loginResponse.user != null) {
  170. _parseAndUpdateUser(loginResponse.user!);
  171. }
  172. return loginResponse;
  173. }
  174. static Future<List<dynamic>> queryOidcLoginOptions() async {
  175. try {
  176. final url = await bind.mainGetApiServer();
  177. if (url.trim().isEmpty) return [];
  178. final resp = await http.get(Uri.parse('$url/api/login-options'));
  179. final List<String> ops = [];
  180. for (final item in jsonDecode(resp.body)) {
  181. ops.add(item as String);
  182. }
  183. for (final item in ops) {
  184. if (item.startsWith('common-oidc/')) {
  185. return jsonDecode(item.substring('common-oidc/'.length));
  186. }
  187. }
  188. return ops
  189. .where((item) => item.startsWith('oidc/'))
  190. .map((item) => {'name': item.substring('oidc/'.length)})
  191. .toList();
  192. } catch (e) {
  193. debugPrint(
  194. "queryOidcLoginOptions: jsonDecode resp body failed: ${e.toString()}");
  195. return [];
  196. }
  197. }
  198. }