using DownKyi.Core.BiliApi.BiliUtils; using DownKyi.Core.BiliApi.Users; using DownKyi.Core.Logging; using DownKyi.Core.Settings; using DownKyi.Core.Settings.Models; using DownKyi.Core.Storage; using DownKyi.Images; using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; using System; using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; namespace DownKyi.ViewModels { public class ViewIndexViewModel : BaseViewModel { public const string Tag = "PageIndex"; #region 页面属性申明 private Visibility loginPanelVisibility; public Visibility LoginPanelVisibility { get => loginPanelVisibility; set => SetProperty(ref loginPanelVisibility, value); } private string userName; public string UserName { get => userName; set => SetProperty(ref userName, value); } private BitmapImage header; public BitmapImage Header { get => header; set => SetProperty(ref header, value); } private VectorImage textLogo; public VectorImage TextLogo { get => textLogo; set => SetProperty(ref textLogo, value); } private string inputText; public string InputText { get => inputText; set => SetProperty(ref inputText, value); } private VectorImage generalSearch; public VectorImage GeneralSearch { get => generalSearch; set => SetProperty(ref generalSearch, value); } private VectorImage settings; public VectorImage Settings { get => settings; set => SetProperty(ref settings, value); } private VectorImage downloadManager; public VectorImage DownloadManager { get => downloadManager; set => SetProperty(ref downloadManager, value); } private VectorImage toolbox; public VectorImage Toolbox { get => toolbox; set => SetProperty(ref toolbox, value); } #endregion public ViewIndexViewModel(IEventAggregator eventAggregator) : base(eventAggregator) { #region 属性初始化 Header = new BitmapImage(new Uri("pack://application:,,,/Resources/default_header.jpg")); TextLogo = LogoIcon.Instance().TextLogo; TextLogo.Fill = DictionaryResource.GetColor("ColorPrimary"); GeneralSearch = ButtonIcon.Instance().GeneralSearch; GeneralSearch.Fill = DictionaryResource.GetColor("ColorPrimary"); Settings = ButtonIcon.Instance().Settings; Settings.Fill = DictionaryResource.GetColor("ColorPrimary"); DownloadManager = ButtonIcon.Instance().DownloadManage; DownloadManager.Fill = DictionaryResource.GetColor("ColorPrimary"); Toolbox = ButtonIcon.Instance().Toolbox; Toolbox.Fill = DictionaryResource.GetColor("ColorPrimary"); #endregion } #region 命令申明 // View加载后执行的事件 public DelegateCommand loadedCommand; public DelegateCommand LoadedCommand => loadedCommand ?? (loadedCommand = new DelegateCommand(ExecuteViewLoaded)); /// /// View加载后执行 /// private void ExecuteViewLoaded() { } // 输入确认事件 public DelegateCommand inputCommand; public DelegateCommand InputCommand => inputCommand ?? (inputCommand = new DelegateCommand(ExecuteInput)); /// /// 处理输入事件 /// private void ExecuteInput() { EnterBili(); } // 登录事件 private DelegateCommand loginCommand; public DelegateCommand LoginCommand => loginCommand ?? (loginCommand = new DelegateCommand(ExecuteLogin)); /// /// 进入登录页面 /// private void ExecuteLogin() { if (UserName == null) { NavigateToView.NavigationView(eventAggregator, ViewLoginViewModel.Tag, Tag, null); } else { // 进入用户空间 var userInfo = SettingsManager.GetInstance().GetUserInfo(); if (userInfo != null && userInfo.Mid != -1) { NavigateToView.NavigationView(eventAggregator, ViewMySpaceViewModel.Tag, Tag, userInfo.Mid); } } } // 进入设置页面 private DelegateCommand settingsCommand; public DelegateCommand SettingsCommand => settingsCommand ?? (settingsCommand = new DelegateCommand(ExecuteSettingsCommand)); /// /// 进入设置页面 /// private void ExecuteSettingsCommand() { NavigateToView.NavigationView(eventAggregator, ViewSettingsViewModel.Tag, Tag, null); } // 进入下载管理页面 private DelegateCommand downloadManagerCommand; public DelegateCommand DownloadManagerCommand => downloadManagerCommand ?? (downloadManagerCommand = new DelegateCommand(ExecuteDownloadManagerCommand)); /// /// 进入下载管理页面 /// private void ExecuteDownloadManagerCommand() { NavigateToView.NavigationView(eventAggregator, ViewDownloadManagerViewModel.Tag, Tag, null); } // 进入工具箱页面 private DelegateCommand toolboxCommand; public DelegateCommand ToolboxCommand => toolboxCommand ?? (toolboxCommand = new DelegateCommand(ExecuteToolboxCommand)); /// /// 进入工具箱页面 /// private void ExecuteToolboxCommand() { NavigateToView.NavigationView(eventAggregator, ViewToolboxViewModel.Tag, Tag, null); } #endregion #region 业务逻辑 /// /// 进入B站链接的处理逻辑, /// 只负责处理输入,并跳转到视频详情页。 /// 不是支持的格式,则进入搜索页面。 /// 支持的格式有: /// av号:av170001, AV170001, https://www.bilibili.com/video/av170001 /// BV号:BV17x411w7KC, https://www.bilibili.com/video/BV17x411w7KC /// 番剧(电影、电视剧)ss号:ss32982, SS32982, https://www.bilibili.com/bangumi/play/ss32982 /// 番剧(电影、电视剧)ep号:ep317925, EP317925, https://www.bilibili.com/bangumi/play/ep317925 /// 番剧(电影、电视剧)md号:md28228367, MD28228367, https://www.bilibili.com/bangumi/media/md28228367 /// 课程ss号:https://www.bilibili.com/cheese/play/ss205 /// 课程ep号:https://www.bilibili.com/cheese/play/ep3489 /// 收藏夹:ml1329019876, ML1329019876, https://www.bilibili.com/medialist/detail/ml1329019876 /// 用户空间:uid928123, UID928123, uid:928123, UID:928123, https://space.bilibili.com/928123 /// private void EnterBili() { if (InputText == null || InputText == string.Empty) { return; } LogManager.Debug(Tag, $"InputText: {InputText}"); // 视频 if (ParseEntrance.IsAvId(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, $"{ParseEntrance.VideoUrl}{InputText.ToLower()}"); } else if (ParseEntrance.IsAvUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, InputText); } else if (ParseEntrance.IsBvId(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, $"{ParseEntrance.VideoUrl}{InputText}"); } else if (ParseEntrance.IsBvUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, InputText); } // 番剧(电影、电视剧) else if (ParseEntrance.IsBangumiSeasonId(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, $"{ParseEntrance.BangumiUrl}{InputText.ToLower()}"); } else if (ParseEntrance.IsBangumiSeasonUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, InputText); } else if (ParseEntrance.IsBangumiEpisodeId(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, $"{ParseEntrance.BangumiUrl}{InputText.ToLower()}"); } else if (ParseEntrance.IsBangumiEpisodeUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, InputText); } else if (ParseEntrance.IsBangumiMediaId(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, $"{ParseEntrance.BangumiMediaUrl}{InputText.ToLower()}"); } else if (ParseEntrance.IsBangumiMediaUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, InputText); } // 课程 else if (ParseEntrance.IsCheeseSeasonUrl(InputText) || ParseEntrance.IsCheeseEpisodeUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewVideoDetailViewModel.Tag, Tag, InputText); } // 用户(参数传入mid) else if (ParseEntrance.IsUserId(InputText)) { NavigateToView.NavigateToViewUserSpace(eventAggregator, Tag, ParseEntrance.GetUserId(InputText)); } else if (ParseEntrance.IsUserUrl(InputText)) { NavigateToView.NavigateToViewUserSpace(eventAggregator, Tag, ParseEntrance.GetUserId(InputText)); } // 收藏夹 else if (ParseEntrance.IsFavoritesId(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewPublicFavoritesViewModel.Tag, Tag, ParseEntrance.GetFavoritesId(InputText)); } else if (ParseEntrance.IsFavoritesUrl(InputText)) { NavigateToView.NavigationView(eventAggregator, ViewPublicFavoritesViewModel.Tag, Tag, ParseEntrance.GetFavoritesId(InputText)); } // TODO 关键词搜索 else { } InputText = string.Empty; } /// /// 更新用户登录信息 /// private async void UpdateUserInfo() { LoginPanelVisibility = Visibility.Hidden; // 检查本地是否存在login文件,没有则说明未登录 if (!File.Exists(StorageManager.GetLogin())) { LoginPanelVisibility = Visibility.Visible; Header = new BitmapImage(new Uri("pack://application:,,,/Resources/default_header.jpg")); UserName = null; return; } await Task.Run(new Action(() => { // 获取用户信息 var userInfo = UserInfo.GetUserInfoForNavigation(); if (userInfo != null) { SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings { Mid = userInfo.Mid, Name = userInfo.Name, IsLogin = userInfo.IsLogin, IsVip = userInfo.VipStatus == 1 }); } else { SettingsManager.GetInstance().SetUserInfo(new UserInfoSettings { Mid = -1, Name = "", IsLogin = false, IsVip = false }); } PropertyChangeAsync(new Action(() => { LoginPanelVisibility = Visibility.Visible; 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; } })); })); } #endregion public override void OnNavigatedTo(NavigationContext navigationContext) { base.OnNavigatedTo(navigationContext); DownloadManager = ButtonIcon.Instance().DownloadManage; DownloadManager.Height = 27; DownloadManager.Width = 32; DownloadManager.Fill = DictionaryResource.GetColor("ColorPrimary"); // 根据传入参数不同执行不同任务 string parameter = navigationContext.Parameters.GetValue("Parameter"); if (parameter == null) { return; } // 启动 if (parameter == "start") { UpdateUserInfo(); } // 从登录页面返回 if (parameter == "login") { UpdateUserInfo(); } // 注销 if (parameter == "logout") { UpdateUserInfo(); } } } }