Ejemplo n.º 1
0
 public static void Hide()
 {
     _view.Close();
 }
        private async void SignIn(object param)
        {
            LoadingView loadView = new LoadingView(currentView);

            loadView.FriendlyText.Text = "登录中,请稍后";
            try
            {
                loadView.Show();

                //如果允许自动登录,且保存了上一次的Token,则自动登录
                if (IsAutoSignIn && !string.IsNullOrEmpty(LocalProperties.Token))
                {
                    GenericResult <UserInformation> x = await authentication.GetUserInformation();

                    if (x.Success)
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            OnSignInSuccessful();
                            currentView.Close();
                            new MainFrame(x.Result).Show();
                        });
                        return;
                    }
                }

                //如果是验证码登录,且已输入验证码,则验证码登录
                if (IsCodeMode && !string.IsNullOrWhiteSpace(PhoneCode))
                {
                    var x = await authentication.LoginByMessageCode(PhoneInfo, PhoneCode);

                    if (x.Success)
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            OnSignInSuccessful();
                            currentView.Close();
                            new MainFrame(x.Result).Show();
                        });
                        return;
                    }
                }

                //如果密码框中输入了信息,则使用密码框中的密码登录
                if (!IsCodeMode && param is PasswordBox passwordBox && !string.IsNullOrEmpty(passwordBox.Password))
                {
                    string passwordMD5 = Authentication.UserMd5(passwordBox.Password);
                    GenericResult <UserInformation> x = await LoginOperate(passwordMD5);

                    if (x.Success)
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            OnSignInSuccessful();
                            currentView.Close();
                            new MainFrame(x.Result).Show();
                        });
                        if (IsRememberPassword)
                        {
                            LocalProperties.UserName    = PhoneNumber;
                            LocalProperties.CountryCode = Code;
                            LocalProperties.Password    = Authentication.UserMd5(passwordBox.Password);
                        }
                        else
                        {
                            LocalProperties.Password = "";
                        }
                    }
                    else
                    {
                        currentView.Dispatcher.Invoke(() =>
                        {
                            MessageBox.Show(x.Message, "登录失败");
                            currentView.Activate();
                        });
                    }
                    return;
                }

                //如果允许保存密码,且保存了上次登录的密码,且密码框为空,则使用上次保存的密码的md5登录
                if (!IsCodeMode && IsRememberPassword && !string.IsNullOrEmpty(LocalProperties.Password))
                {
                    string passwordMD5 = LocalProperties.Password;
                    GenericResult <UserInformation> x = await LoginOperate(passwordMD5);

                    if (x.Success)
                    {
                        LocalProperties.UserName    = PhoneNumber;
                        LocalProperties.CountryCode = Code;
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            OnSignInSuccessful();
                            currentView.Close();
                            new MainFrame(x.Result).Show();
                        });
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            MessageBox.Show(x.Message, "登录失败");
                            currentView.Activate();
                        });
                        LocalProperties.Password = "";
                        OnPropertyChanged(nameof(PasswordBoxHint));
                    }
                    return;
                }

                Application.Current.Dispatcher.Invoke(() =>
                {
                    MessageBox.Show("要登录,请输入密码");
                    currentView.Activate();
                });
            }
            finally
            {
                loadView.Close();
            }

            async Task <GenericResult <UserInformation> > LoginOperate(string passwordMD5)
            {
                try
                {
                    return(await authentication.LoginByPassword(PhoneNumber, passwordMD5, GetCountryCode()));
                }
                catch (LoginUserTooMuchException ex)
                {
                    GenericResult <OnlineDeviceList> getOnlineDeviceList = await authentication.GetOnlineDeviceList();

                    if (getOnlineDeviceList.Success)
                    {
                        string[] devicesSSID = null;
                        LogoutOthersViewModels logoutOthersViewModels = new LogoutOthersViewModels(getOnlineDeviceList);
                        logoutOthersViewModels.ShowDialog();
                        devicesSSID = logoutOthersViewModels.DevicesSSID;

                        GenericResult <bool?> x = await authentication.LogoutOnlineDevices(devicesSSID);

                        if (x.Result == true)
                        {
                            return(await LoginOperate(passwordMD5));
                        }
                        else
                        {
                            return(ex.Response);
                        }
                    }
                    else
                    {
                        return(ex.Response);
                    }
                }
            }
        }