diff --git a/src/DownKyi.Core/BiliApi/Users/UserInfo.cs b/src/DownKyi.Core/BiliApi/Users/UserInfo.cs index d6d1a21..cafdbb8 100644 --- a/src/DownKyi.Core/BiliApi/Users/UserInfo.cs +++ b/src/DownKyi.Core/BiliApi/Users/UserInfo.cs @@ -27,8 +27,7 @@ namespace DownKyi.Core.BiliApi.Users UserInfoForNavigationOrigin userInfo = JsonConvert.DeserializeObject(response); if (userInfo == null || userInfo.Data == null) { return null; } - if (userInfo.Data.IsLogin) { return userInfo.Data; } - else { return null; } + return userInfo.Data; } catch (Exception e) { diff --git a/src/DownKyi/ViewModels/ViewIndexViewModel.cs b/src/DownKyi/ViewModels/ViewIndexViewModel.cs index 37aff24..b78e5c5 100644 --- a/src/DownKyi/ViewModels/ViewIndexViewModel.cs +++ b/src/DownKyi/ViewModels/ViewIndexViewModel.cs @@ -229,45 +229,59 @@ namespace DownKyi.ViewModels InputText = string.Empty; } - private UserInfoForNavigation GetUserInfo() + private async Task GetUserInfo() { - // 获取用户信息 - var userInfo = UserInfo.GetUserInfoForNavigation(); - if (userInfo != null) + UserInfoForNavigation userInfo = null; + await Task.Run(new Action(() => { - SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings + // 获取用户信息 + userInfo = UserInfo.GetUserInfoForNavigation(); + if (userInfo != null) { - Mid = userInfo.Mid, - Name = userInfo.Name, - IsLogin = userInfo.IsLogin, - IsVip = userInfo.VipStatus == 1, - ImgKey = userInfo.Wbi.ImgUrl.Split('/').ToList().Last().Split('.')[0], - SubKey = userInfo.Wbi.SubUrl.Split('/').ToList().Last().Split('.')[0], - }); - } - else - { - SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings + SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings + { + Mid = userInfo.Mid, + Name = userInfo.Name, + IsLogin = userInfo.IsLogin, + IsVip = userInfo.VipStatus == 1, + ImgKey = userInfo.Wbi.ImgUrl.Split('/').ToList().Last().Split('.')[0], + SubKey = userInfo.Wbi.SubUrl.Split('/').ToList().Last().Split('.')[0], + }); + } + else { - Mid = -1, - Name = "", - IsLogin = false, - IsVip = false, - }); - } + SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings + { + Mid = -1, + Name = "", + IsLogin = false, + IsVip = false, + }); + } + })); return userInfo; } /// /// 更新用户登录信息 /// - private async void UpdateUserInfo() + private async void UpdateUserInfo(bool isBackgroud = false) { try { + if (isBackgroud) + { + // 获取用户信息 + await GetUserInfo(); + return; + } + LoginPanelVisibility = Visibility.Hidden; + // 获取用户信息 + var userInfo = await GetUserInfo(); + // 检查本地是否存在login文件,没有则说明未登录 if (!File.Exists(StorageManager.GetLogin())) { @@ -277,34 +291,26 @@ namespace DownKyi.ViewModels return; } - await Task.Run(new Action(() => + LoginPanelVisibility = Visibility.Visible; + + if (userInfo != null) { - // 获取用户信息 - var userInfo = GetUserInfo(); - - PropertyChangeAsync(new Action(() => + if (userInfo.Face != null) { - LoginPanelVisibility = Visibility.Visible; + Header = new StorageHeader().GetHeaderThumbnail(userInfo.Mid, userInfo.Name, userInfo.Face, 36, 36); + } + else + { + Header = new BitmapImage(new Uri("pack://application:,,,/Resources/default_header.jpg")); + } + UserName = userInfo.Name; + } + else + { + Header = new BitmapImage(new Uri("pack://application:,,,/Resources/default_header.jpg")); + UserName = null; + } - if (userInfo != null) - { - if (userInfo.Face != null) - { - Header = new StorageHeader().GetHeaderThumbnail(userInfo.Mid, userInfo.Name, userInfo.Face, 36, 36); - } - else - { - Header = new BitmapImage(new Uri("pack://application:,,,/Resources/default_header.jpg")); - } - UserName = userInfo.Name; - } - else - { - Header = new BitmapImage(new Uri("pack://application:,,,/Resources/default_header.jpg")); - UserName = null; - } - })); - })); } catch (Exception e) { @@ -328,6 +334,8 @@ namespace DownKyi.ViewModels string parameter = navigationContext.Parameters.GetValue("Parameter"); if (parameter == null) { + // 其他情况只更新设置的用户信息,不更新UI + UpdateUserInfo(true); return; } @@ -346,10 +354,10 @@ namespace DownKyi.ViewModels { UpdateUserInfo(); } - // 其他情况只更新设置的用户信息,不更新UI else { - GetUserInfo(); + // 其他情况只更新设置的用户信息,不更新UI + UpdateUserInfo(true); } }