123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- import 'package:fis_common/http/options.dart';
- import 'package:fis_jsonrpc/http_pool.dart';
- import 'package:flutter/foundation.dart';
- import 'package:get/get.dart';
- import 'package:vitalapp/architecture/defines.dart';
- import 'package:vitalapp/architecture/utils/prompt_box.dart';
- import 'package:vitalapp/components/dialog_confirm.dart';
- import 'package:vitalapp/managers/interfaces/account.dart';
- import 'package:vitalapp/managers/interfaces/system_setting.dart';
- import 'package:vitalapp/rpc.dart';
- import 'package:vitalapp/store/store.dart';
- import 'package:fis_common/logger/logger.dart';
- import 'state.dart';
- class ServerSettingController4Login extends ServerSettingController {
- @override
- Future<void> handleAfterSubmit(bool saved) async {
- if (saved) {
- Get.back();
- PromptBox.toast("保存成功");
- }
- }
- }
- class ServerSettingController extends FControllerBase {
- final state = ServerSettingState();
- @override
- void onReady() {
- _load();
- super.onReady();
- }
- @protected
- Future<void> handleAfterSubmit(bool saved) async {
- if (saved) {
- await DialogConfirm.show(
- message: "服务地址已变更,需要重新登录",
- disableCancel: true,
- );
- Get.offAllNamed("/login");
- }
- }
- /// 切换已选择的服务
- ///
- /// [index] 索引
- Future<void> switchSavedServer(int index) async {
- final url = state.savedList[index];
- _applyUrl(url);
- Get.back(result: url);
- }
- /// 移除已选择的服务
- ///
- /// [index] 索引
- Future<void> removeSavedServer(int index) async {
- final url = state.savedList[index];
- final hasConfirm = await DialogConfirm.show(
- message: "确定移除此服务地址?\n【$url】",
- width: 400,
- );
- if (hasConfirm) {
- final list = [...state.savedList];
- list.removeAt(index);
- state.savedList = list;
- Store.app.serverOptions = state.savedList;
- await Store.persistent();
- }
- }
- /// 提交修改
- Future<void> submit() async {
- bool saved = false;
- setBusy("正在保存");
- try {
- final uri = await _buildUri();
- if (uri == null) {
- return;
- }
- String url = uri.toString();
- await _sotreUrl(url);
- if (Store.user.isLogOn) {
- final logoutRst = await Get.find<IAccountManager>().logout();
- if (!logoutRst) {
- _toastError("保存失败");
- }
- }
- // 更新RPC地址
- rpc.setServerHost("${uri.host}:${uri.port}", uri.scheme == 'https');
- rpc.clearCache();
- // 重新加载服务配置
- await Get.find<ISystemSettingManager>().getSettings(true);
- saved = true;
- cancelBusy();
- } catch (e) {
- logger.e("ServerSettingController save url error.", e);
- _toastError("保存失败");
- } finally {
- if (!saved) {
- // 给提示一点时间吧
- await Future.delayed(const Duration(milliseconds: 1500));
- }
- handleAfterSubmit(saved);
- }
- }
- Future<Uri?> _buildUri() async {
- RegExp regExp = RegExp(r'\s'); // 匹配空格的正则表达式
- bool hasSpace = regExp.hasMatch(state.host);
- if (hasSpace) {
- _toastError("请输入正确的服务器地址");
- return null;
- }
- final uri = Uri(
- scheme: state.enableSSL ? "https" : "http",
- host: state.host,
- port: state.port,
- );
- final fullAddress = uri.toString();
- if (fullAddress == Store.app.serverUrl) {
- _toastError("服务器地址未发生变更");
- return null;
- }
- final checkResult = await _checkServerValidation(fullAddress);
- if (!checkResult) {
- _toastError("服务器不存在");
- return null;
- }
- return uri;
- }
- void _toastError(String message) {
- cancelBusy();
- PromptBox.toast(message);
- }
- void _load() async {
- final address = Store.app.serverUrl;
- _applyUrl(address);
- state.savedList = Store.app.serverOptions.map((e) => e).toList();
- _sotreUrl(Store.app.serverUrl);
- }
- void _applyUrl(String url) {
- final uri = Uri.parse(url);
- state.enableSSL = uri.scheme.toLowerCase() == "https";
- state.host = uri.host;
- state.port = uri.port;
- }
- /// 检验服务有效性
- Future<bool> _checkServerValidation(String host) async {
- try {
- var response = await jrpcHttpPool.getClient(
- host,
- // timeout: 3000, // 设置了但没生效
- headers: {'content-type': "text/plain"},
- ).post(
- '/IVitalLoginService',
- data:
- '{"jsonrpc":"2.0","method":"CommonLoginAsync","params":[{"AnyAccount":"test","Password":"Symmetry_mk9xah8N","LoginSource":2}],"id":1}',
- options: FHttpScopedOptions(timeout: 3000),
- );
- if (response.data != null) {
- print(response.data);
- return true;
- }
- } catch (e) {
- print("Http unknown error:${e}");
- }
- return false;
- }
- /// 保存到列表
- void _save2List(String url) async {
- if (state.savedList.contains(url)) {
- return;
- }
- final list = [...state.savedList];
- list.add(url);
- state.savedList = list;
- }
- Future<void> _sotreUrl(String url) async {
- _save2List(url);
- Store.app.serverUrl = url;
- Store.app.serverOptions = state.savedList;
- await Store.persistent();
- }
- }
|