controller.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import 'package:fis_common/http/options.dart';
  2. import 'package:fis_jsonrpc/http_pool.dart';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/architecture/defines.dart';
  6. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  7. import 'package:vitalapp/components/dialog_confirm.dart';
  8. import 'package:vitalapp/managers/interfaces/account.dart';
  9. import 'package:vitalapp/managers/interfaces/system_setting.dart';
  10. import 'package:vitalapp/rpc.dart';
  11. import 'package:vitalapp/store/store.dart';
  12. import 'package:fis_common/logger/logger.dart';
  13. import 'state.dart';
  14. class ServerSettingController4Login extends ServerSettingController {
  15. @override
  16. Future<void> handleAfterSubmit(bool saved) async {
  17. if (saved) {
  18. Get.back();
  19. PromptBox.toast("保存成功");
  20. }
  21. }
  22. }
  23. class ServerSettingController extends FControllerBase {
  24. final state = ServerSettingState();
  25. @override
  26. void onReady() {
  27. _load();
  28. super.onReady();
  29. }
  30. @protected
  31. Future<void> handleAfterSubmit(bool saved) async {
  32. if (saved) {
  33. await DialogConfirm.show(
  34. message: "服务地址已变更,需要重新登录",
  35. disableCancel: true,
  36. );
  37. Get.offAllNamed("/login");
  38. }
  39. }
  40. /// 切换已选择的服务
  41. ///
  42. /// [index] 索引
  43. Future<void> switchSavedServer(int index) async {
  44. final url = state.savedList[index];
  45. _applyUrl(url);
  46. Get.back(result: url);
  47. }
  48. /// 移除已选择的服务
  49. ///
  50. /// [index] 索引
  51. Future<void> removeSavedServer(int index) async {
  52. final url = state.savedList[index];
  53. final hasConfirm = await DialogConfirm.show(
  54. message: "确定移除此服务地址?\n【$url】",
  55. width: 400,
  56. );
  57. if (hasConfirm) {
  58. final list = [...state.savedList];
  59. list.removeAt(index);
  60. state.savedList = list;
  61. Store.app.serverOptions = state.savedList;
  62. await Store.persistent();
  63. }
  64. }
  65. /// 提交修改
  66. Future<void> submit() async {
  67. bool saved = false;
  68. setBusy("正在保存");
  69. try {
  70. final uri = await _buildUri();
  71. if (uri == null) {
  72. return;
  73. }
  74. String url = uri.toString();
  75. await _sotreUrl(url);
  76. if (Store.user.isLogOn) {
  77. final logoutRst = await Get.find<IAccountManager>().logout();
  78. if (!logoutRst) {
  79. _toastError("保存失败");
  80. }
  81. }
  82. // 更新RPC地址
  83. rpc.setServerHost("${uri.host}:${uri.port}", uri.scheme == 'https');
  84. rpc.clearCache();
  85. // 重新加载服务配置
  86. await Get.find<ISystemSettingManager>().getSettings(true);
  87. saved = true;
  88. cancelBusy();
  89. } catch (e) {
  90. logger.e("ServerSettingController save url error.", e);
  91. _toastError("保存失败");
  92. } finally {
  93. if (!saved) {
  94. // 给提示一点时间吧
  95. await Future.delayed(const Duration(milliseconds: 1500));
  96. }
  97. handleAfterSubmit(saved);
  98. }
  99. }
  100. Future<Uri?> _buildUri() async {
  101. RegExp regExp = RegExp(r'\s'); // 匹配空格的正则表达式
  102. bool hasSpace = regExp.hasMatch(state.host);
  103. if (hasSpace) {
  104. _toastError("请输入正确的服务器地址");
  105. return null;
  106. }
  107. final uri = Uri(
  108. scheme: state.enableSSL ? "https" : "http",
  109. host: state.host,
  110. port: state.port,
  111. );
  112. final fullAddress = uri.toString();
  113. if (fullAddress == Store.app.serverUrl) {
  114. _toastError("服务器地址未发生变更");
  115. return null;
  116. }
  117. final checkResult = await _checkServerValidation(fullAddress);
  118. if (!checkResult) {
  119. _toastError("服务器不存在");
  120. return null;
  121. }
  122. return uri;
  123. }
  124. void _toastError(String message) {
  125. cancelBusy();
  126. PromptBox.toast(message);
  127. }
  128. void _load() async {
  129. final address = Store.app.serverUrl;
  130. _applyUrl(address);
  131. state.savedList = Store.app.serverOptions.map((e) => e).toList();
  132. _sotreUrl(Store.app.serverUrl);
  133. }
  134. void _applyUrl(String url) {
  135. final uri = Uri.parse(url);
  136. state.enableSSL = uri.scheme.toLowerCase() == "https";
  137. state.host = uri.host;
  138. state.port = uri.port;
  139. }
  140. /// 检验服务有效性
  141. Future<bool> _checkServerValidation(String host) async {
  142. try {
  143. var response = await jrpcHttpPool.getClient(
  144. host,
  145. // timeout: 3000, // 设置了但没生效
  146. headers: {'content-type': "text/plain"},
  147. ).post(
  148. '/IVitalLoginService',
  149. data:
  150. '{"jsonrpc":"2.0","method":"CommonLoginAsync","params":[{"AnyAccount":"test","Password":"Symmetry_mk9xah8N","LoginSource":2}],"id":1}',
  151. options: FHttpScopedOptions(timeout: 3000),
  152. );
  153. if (response.data != null) {
  154. print(response.data);
  155. return true;
  156. }
  157. } catch (e) {
  158. print("Http unknown error:${e}");
  159. }
  160. return false;
  161. }
  162. /// 保存到列表
  163. void _save2List(String url) async {
  164. if (state.savedList.contains(url)) {
  165. return;
  166. }
  167. final list = [...state.savedList];
  168. list.add(url);
  169. state.savedList = list;
  170. }
  171. Future<void> _sotreUrl(String url) async {
  172. _save2List(url);
  173. Store.app.serverUrl = url;
  174. Store.app.serverOptions = state.savedList;
  175. await Store.persistent();
  176. }
  177. }