示例#1
0
        /// <summary>
        /// The function to load data and navigate to the next page.
        /// </summary>
        private async void BeginLoading(string username, string password)
        {
            try
            {
                var token = await Auth.AuthorizeAsync(username, password, null);

                Data.TmpData.CurrentAuth = token;
                Frame.Navigate(typeof(MainPage));
                Debug.Write("Access token: ");
                Debug.WriteLine(token.Tokens.AccessToken);
            }
            catch
            {
                status.Visibility = Visibility.Visible;
                ProgressBarVisualHelper.SetYFHelperVisibility(ring, false);
            }
        }
        /// <summary>
        /// The function to load data and navigate to the next page.
        /// </summary>
        private async void BeginLoading(string username, string password)
        {
            try
            {
                Pixeez.AuthResult token = default;
                async Task 正常加载tokenAsync()
                {
                    token = await Auth.AuthorizeAsync(username, password, null, Data.AppDataHelper.GetDeviceId());
                }
                if (Data.AppDataHelper.ContainKey(Data.AppDataHelper.RefreshTokenKey))
                {
                    token = Newtonsoft.Json.JsonConvert.DeserializeObject <Pixeez.AuthResult>(Data.AppDataHelper.GetValue(Data.AppDataHelper.RefreshTokenKey).ToString());
                    if (username == token.Key.Username && password == token.Key.Password)
                    {
                        //不使用密码认证
                        if (DateTime.UtcNow >= token.Key.KeyExpTime)
                        {
                            //token 已过期
                            try
                            {
                                token = await Auth.AuthorizeAsync(username, password, token.Authorize.RefreshToken, Data.AppDataHelper.GetDeviceId());
                            }
                            catch
                            {
                                Debug.WriteLine("使用 refresh token 登录失败");
#if DEBUG
                                if (Debugger.IsAttached)
                                {
                                    Debugger.Break();
                                }
#endif
                                await 正常加载tokenAsync();
                            }
                        }
                    }
                    else
                    {
                        await 正常加载tokenAsync();
                    }
                }
                else
                {
                    await 正常加载tokenAsync();
                }


                Data.TmpData.CurrentAuth = token;
                Data.AppDataHelper.SetValue(Data.AppDataHelper.RefreshTokenKey, Newtonsoft.Json.JsonConvert.SerializeObject(token));
                var leftwidth = Data.AppDataHelper.GetValue("leftwidth");
                if (leftwidth != null)
                {
                    Data.TmpData.waterflowwidth = (int)leftwidth;
                }
                else
                {
                    Data.AppDataHelper.SetValue("leftwidth", Data.TmpData.waterflowwidth);
                }
                var leftcolum = Data.AppDataHelper.GetValue("leftcolum");
                if (leftcolum != null)
                {
                    Data.TmpData.waterflowcolumnum = (int)leftcolum;
                }
                else
                {
                    Data.AppDataHelper.SetValue("leftcolum", Data.TmpData.waterflowcolumnum);
                }
                Frame.Navigate(typeof(MainPage));
                Debug.Write("Access token: ");
                Debug.WriteLine(token.Tokens.AccessToken);
            }
            catch
            {
                Data.AppDataHelper.Remove(Data.AppDataHelper.RefreshTokenKey);
                status.Visibility = Visibility.Visible;
                ProgressBarVisualHelper.SetYFHelperVisibility(ring, false);
            }
        }