public ServerHostSelectViewModel(string selected, Action <ServerHostItem> onOk)
        {
            var data = NTMinerRegistry.GetControlCenterAddresses().ToList();

            if (!data.Contains("127.0.0.1") && !data.Contains(NTKeyword.Localhost))
            {
                data.Add("127.0.0.1");
            }
            if (!data.Contains("server.ntminer.com:3339"))
            {
                data.Add("server.ntminer.com:3339");
            }
            _serverHosts    = data.Select(a => new ServerHostItem(a)).ToList();
            _selectedResult = _serverHosts.FirstOrDefault(a => a.IpOrHost == selected);
            OnOk            = onOk;
            this.Remove     = new DelegateCommand <ServerHostItem>((serverHost) => {
                if (this.ServerHosts.Remove(serverHost))
                {
                    if (NTMinerRegistry.GetControlCenterAddress() == serverHost.IpOrHost)
                    {
                        NTMinerRegistry.SetControlCenterAddress(string.Empty);
                    }
                    this.ServerHosts = this.ServerHosts.ToList();
                }
            });
        }
Esempio n. 2
0
        private void BtnLogin_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(Vm.ServerHost))
            {
                Vm.ShowMessage("服务器地址不能为空");
                return;
            }
            string passwordSha1 = HashUtil.Sha1(Vm.Password);

            NTMinerRegistry.SetControlCenterAddress(Vm.ServerHost);
            var list = NTMinerRegistry.GetControlCenterAddresses();

            if (!list.Contains(Vm.ServerHost))
            {
                list.Insert(0, Vm.ServerHost);
            }
            NTMinerRegistry.SetControlCenterAddresses(list);
            // 内网免登录
            if (Net.IpUtil.IsInnerIp(Vm.ServerHost))
            {
                RpcRoot.SetRpcUser(RpcUser.Empty);
                RpcRoot.SetIsOuterNet(false);
                _isLogined = true;
                this.Close();
                // 回调可能弹窗,弹窗可能有父窗口,父窗口是顶层窗口,如果在this.Close()之前回调
                // 则会导致弹窗的父窗口是本窗口,而本窗口随后立即关闭导致作为子窗口的弹窗也会被关闭。
                _onLoginSuccess?.Invoke();
                return;
            }
            else if (string.IsNullOrEmpty(Vm.LoginName))
            {
                Vm.ShowMessage("没有填写用户名");
                return;
            }
            else if (string.IsNullOrEmpty(Vm.Password))
            {
                Vm.ShowMessage("没有填写密码");
                return;
            }
            RpcRoot.OfficialServer.UserService.LoginAsync(Vm.LoginName, passwordSha1, (response, exception) => {
                if (response == null)
                {
                    Vm.ShowMessage("服务器忙");
                    return;
                }
                if (response.IsSuccess())
                {
                    RpcRoot.SetRpcUser(new RpcUser(response.Data, passwordSha1));
                    RpcRoot.SetIsOuterNet(true);
                    _isLogined = true;
                    UIThread.Execute(() => {
                        this.Close();
                    });
                    // 回调可能弹窗,弹窗可能有父窗口,父窗口是顶层窗口,如果在this.Close()之前回调
                    // 则会导致弹窗的父窗口是本窗口,而本窗口随后立即关闭导致作为子窗口的弹窗也会被关闭。
                    _onLoginSuccess?.Invoke();
                }
                else
                {
                    Vm.ShowMessage(response.ReadMessage(exception));
                }
            });
        }