controller.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'package:fis_jsonrpc/http_pool.dart';
  2. import 'package:vitalapp/architecture/defines.dart';
  3. import 'package:vitalapp/architecture/utils/prompt_box.dart';
  4. import 'package:vitalapp/rpc.dart';
  5. import 'package:vitalapp/store/store.dart';
  6. import 'state.dart';
  7. class LoginGatewayController extends FControllerBase {
  8. final state = LoginGatewayState();
  9. void onSubmit() async {
  10. try {
  11. RegExp regExp = RegExp(r'\s'); // 匹配空格的正则表达式
  12. bool hasSpace = regExp.hasMatch(state.gateway);
  13. if (hasSpace) {
  14. PromptBox.toast("请输入正确的服务器地址");
  15. return;
  16. }
  17. var checkResult = await _checkServerValidation();
  18. if (!checkResult) {
  19. PromptBox.toast("服务器不存在");
  20. return;
  21. }
  22. final uri = Uri.parse(state.gateway);
  23. final fullAddress = uri.toString();
  24. Store.app.serverUrl = fullAddress;
  25. await Store.persistent();
  26. _setRpc(uri);
  27. PromptBox.toast("保存成功");
  28. } catch (e) {
  29. PromptBox.toast("保存失败");
  30. }
  31. }
  32. @override
  33. void onInit() {
  34. _load();
  35. super.onInit();
  36. }
  37. _setRpc(Uri uri) {
  38. rpc.setServerHost("${uri.host}:${uri.port}", uri.scheme == 'https');
  39. rpc.clearCache();
  40. }
  41. void _load() async {
  42. final address = Store.app.serverUrl;
  43. final uri = Uri.parse(address);
  44. _setRpc(uri);
  45. state.gateway = rpc.currentHostAddress;
  46. }
  47. @override
  48. void onReady() {
  49. //
  50. super.onReady();
  51. }
  52. @override
  53. void onClose() {
  54. _doDispose();
  55. super.onClose();
  56. }
  57. void _doDispose() {}
  58. /// 检验服务有效性
  59. Future<bool> _checkServerValidation() async {
  60. try {
  61. final host = state.gateway;
  62. var response = await jrpcHttpPool.getClient(
  63. host,
  64. timeout: 3000,
  65. headers: {'content-type': "text/plain"},
  66. ).post(
  67. '/IVitalLoginService',
  68. data:
  69. '{"jsonrpc":"2.0","method":"CommonLoginAsync","params":["AnyAccount": "abc123","AnyCode": "","Platform": 1,"LoginSource": 1,"Password": "fb6414d24e3c347d46032b0496f1c4e4"],"id":1}',
  70. );
  71. if (response.data != null) {
  72. print(response.data);
  73. return true;
  74. }
  75. } catch (e) {
  76. print("Http unknown error:${e}");
  77. }
  78. return false;
  79. }
  80. }