private async void CheckLoginState()
        {
            try
            {
                if (!_isChecking)
                {
                    return;
                }
                string js_login = "******";

                login_result = await LogintWebView.InvokeScriptAsync("eval", new string[] { js_login });

                if (login_result.Contains("成功"))
                {
                    return;
                }
                if (_doLogin)
                {
                    if (login_result.Contains("错误") || login_result.Contains("失败") || login_result.Contains("不存在") ||
                        login_result.Contains("锁定")) //一系列错误
                    {
                        Tip.Text = "用户名或密码错误!";
                        _doLogin = false;
                        IsPrimaryButtonEnabled = true;
                        Logining.IsActive      = false;
                    }
                }
                await Task.Delay(1000);

                CheckLoginState();
            }
            catch
            {
            }
        }
        /// 点击登录
        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            if (UserName.Text.Equals(""))
            {
                args.Cancel     = true;
                UserName.Header = "请输入用户名!";
                return;
            }
            else if (PassWord.Password.Equals(""))
            {
                args.Cancel     = true;
                PassWord.Header = "请输入密码!";
                return;
            }
            string js = "document.getElementById('input1').setAttribute('value','" + UserName.Text + "');";                  //用户名

            js                    += "document.getElementById('input2').setAttribute('value','" + PassWord.Password + "');"; //密码
            js                    += "document.getElementById('signin').click();";                                           //点击登录
            args.Cancel            = true;
            IsPrimaryButtonEnabled = false;
            Logining.IsActive      = true;
            await LogintWebView.InvokeScriptAsync("eval", new string[] { js });

            _doLogin = true;
            if (!_isChecking)
            {
                _isChecking = true;
                CheckLoginState();
            }
        }
Exemple #3
0
        /// <summary>
        /// 点击登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            args.Cancel            = true;
            Logining.Visibility    = Visibility.Visible;
            IsPrimaryButtonEnabled = false;
            string js = "document.getElementById('input1').setAttribute('value','" + UserName.Text + "');"; //用户名

            js += "document.getElementById('input2').setAttribute('value','" + PassWord.Password + "');";   //密码
            js += "document.getElementById('signin').click();";                                             //点击登录

            Logining.Visibility = Visibility.Visible;
            await LogintWebView.InvokeScriptAsync("eval", new string[] { js });

            bool   login_fail   = false;
            string login_result = "";
            await Task.Run((Action)(async() =>
            {
                while (true)
                {
                    try
                    {
                        string js_login = "******";

                        await LogintWebView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                        {
                            login_result = await LogintWebView.InvokeScriptAsync("eval", new string[] { js_login });
                        }
                                                                );
                        if (login_result.Contains("密码错误") || login_result.Contains("失败") || login_result.Contains("不存在"))
                        {
                            login_fail = true;
                            break;
                        }
                        if (login_result.Contains("成功"))
                        {
                            break;
                        }
                    }
                    catch
                    {
                    }
                }

                await LogintWebView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                {
                    if (login_fail)
                    {
                        Tip.Text = login_result;
                        IsPrimaryButtonEnabled = true;
                        Logining.Visibility = Visibility.Collapsed;
                    }
                });
            }));
        }
        /// <summary>
        /// WebView加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void LogintWebView_LoadCompleted(object sender, NavigationEventArgs e)
        {
            if (e.Uri.AbsoluteUri.StartsWith(_login_url)) //登录页面加载完成
            {
                try
                {
                    string js = "var o = document.getElementById('LoginCaptcha_CaptchaImage');";
                    js += "if(o) o.getAttribute('src');";
                    string capt_src = await LogintWebView.InvokeScriptAsync("eval", new string[] { js });

                    if (capt_src != null && !capt_src.Equals(""))  //有验证码
                    {
                        capt_src          = "http://passport.cnblogs.com" + capt_src;
                        CaptchaImg.Source = new BitmapImage {
                            UriSource = new Uri(capt_src)
                        };
                        CaptchaImg.Visibility       = Visibility.Visible;
                        CaptchaImg_Input.Visibility = Visibility.Visible;
                    }
                }
                finally
                {
                    PrimaryButtonText      = "登录";
                    Logining.IsActive      = false;
                    IsPrimaryButtonEnabled = true;
                }
            }
            else if (e.Uri.AbsoluteUri.Equals(_login_success))  //登录成功  主页加载完成
            {
                _isChecking = false;
                //登录完成
                //加载用户信息
                User = await UserService.GetCurrentUserInfo();

                if (User != null)
                {
                    Hide();
                    if ((bool)RemenberMe.IsChecked)
                    {
                        SaveLoginInfo();
                    }
                }
                else
                {
                    Tip.Text = "用户名密码错误!";
                    IsPrimaryButtonEnabled = true;
                    Logining.Visibility    = Visibility.Collapsed;
                }
            }
        }
        private async void CheckLoginState()
        {
            try
            {
                if (!_isChecking)
                {
                    return;
                }
                string js_login = "******";

                login_result = await LogintWebView.InvokeScriptAsync("eval", new string[] { js_login });

                if (login_result.Contains("成功"))
                {
                    return;
                }
                Tip.Text = login_result;
                if (_doLogin)
                {
                    if (login_result.Contains("错误") || login_result.Contains("失败") || login_result.Contains("不存在") ||
                        login_result.Contains("锁定")) //一系列错误
                    {
                        _doLogin = false;
                        if (CaptchaImg.Visibility == Visibility.Visible)
                        {
                            PrimaryButtonText = "加载验证码...";
                            LogintWebView.Navigate(new Uri(_login_url_redirect));
                            CaptchaImg_Input.Visibility = Visibility.Collapsed;
                            CaptchaImg.Visibility       = Visibility.Collapsed;
                            Logining.IsActive           = true;
                        }
                        else
                        {
                            IsPrimaryButtonEnabled = true;
                            Logining.IsActive      = false;
                        }
                    }
                }
                await Task.Delay(1000);

                CheckLoginState();
            }
            catch
            {
            }
        }
        /// WebView加载完成
        private async void LogintWebView_LoadCompleted(object sender, NavigationEventArgs e)
        {
            if (e.Uri.AbsoluteUri.StartsWith(_login_url)) //登录页面加载完成
            {
                try
                {
                    string js = "var o = document.getElementById('LoginCaptcha_CaptchaImage');";
                    js += "if(o) o.getAttribute('src');";
                    string capt_src = await LogintWebView.InvokeScriptAsync("eval", new string[] { js });
                }
                finally
                {
                    PrimaryButtonText      = "登录";
                    Logining.IsActive      = false;
                    IsPrimaryButtonEnabled = true;
                }
            }
            else if (e.Uri.AbsoluteUri.Equals(_login_success))  //登录成功  主页加载完成
            {
                _isChecking = false;
                //登录完成
                //加载用户信息
                User = await UserService.GetCurrentUserInfo();

                if (User != null)
                {
                    Hide();
                }
                else
                {
                    Tip.Text = "用户名或密码错误!";
                    IsPrimaryButtonEnabled = true;
                    Logining.Visibility    = Visibility.Collapsed;
                }
            }
        }