From f681e20ba9d38fa77f19c94675ebda49d71d9fc2 Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sat, 18 Mar 2023 15:11:58 +0800 Subject: [PATCH 1/7] =?UTF-8?q?debug=E6=97=B6=E5=8F=96=E6=B6=88=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/MainPage.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BiliLite.UWP/MainPage.xaml.cs b/src/BiliLite.UWP/MainPage.xaml.cs index 76a1a660..b99d0e03 100644 --- a/src/BiliLite.UWP/MainPage.xaml.cs +++ b/src/BiliLite.UWP/MainPage.xaml.cs @@ -65,9 +65,9 @@ protected async override void OnNavigatedTo(NavigationEventArgs e) Utils.ShowMessageToast("无法打开链接:" + e.Parameter.ToString()); } } -//#if !DEBUG +#if !DEBUG await Utils.CheckVersion(); -//#endif +#endif } private void MessageCenter_ChangeTitleEvent(object sender, string e) From 489d12b38825e66156059bb43e63bff62bd78b66 Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sat, 18 Mar 2023 15:13:00 +0800 Subject: [PATCH 2/7] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=BB=BA=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/BiliLite.UWP.csproj | 3 ++ .../ObservableCollectionExtension.cs | 27 +++++++++++++ .../Models/Requests/Api/SearchAPI.cs | 11 +++++ .../Models/Responses/SearchSuggestResponse.cs | 14 +++++++ src/BiliLite.UWP/Modules/Home/HomeVM.cs | 14 +++++-- src/BiliLite.UWP/Modules/SearchVM.cs | 12 ++++++ src/BiliLite.UWP/Pages/HomePage.xaml | 8 +++- src/BiliLite.UWP/Pages/HomePage.xaml.cs | 37 +++++++++-------- src/BiliLite.UWP/Pages/SearchPage.xaml | 10 ++++- src/BiliLite.UWP/Pages/SearchPage.xaml.cs | 40 +++++++++++-------- src/BiliLite.UWP/Services/SearchService.cs | 23 +++++++++++ 11 files changed, 160 insertions(+), 39 deletions(-) create mode 100644 src/BiliLite.UWP/Extensions/ObservableCollectionExtension.cs create mode 100644 src/BiliLite.UWP/Models/Responses/SearchSuggestResponse.cs create mode 100644 src/BiliLite.UWP/Services/SearchService.cs diff --git a/src/BiliLite.UWP/BiliLite.UWP.csproj b/src/BiliLite.UWP/BiliLite.UWP.csproj index 59529e4c..09e1f464 100644 --- a/src/BiliLite.UWP/BiliLite.UWP.csproj +++ b/src/BiliLite.UWP/BiliLite.UWP.csproj @@ -130,6 +130,7 @@ + @@ -144,6 +145,7 @@ + @@ -476,6 +478,7 @@ + diff --git a/src/BiliLite.UWP/Extensions/ObservableCollectionExtension.cs b/src/BiliLite.UWP/Extensions/ObservableCollectionExtension.cs new file mode 100644 index 00000000..d7dd6256 --- /dev/null +++ b/src/BiliLite.UWP/Extensions/ObservableCollectionExtension.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace BiliLite.Extensions +{ + public static class ObservableCollectionExtension + { + public static void AddRange(this ObservableCollection collection, IEnumerable range) + { + if (range == null) return; + foreach (var item in range) + { + collection.Add(item); + } + } + + public static void ReplaceRange(this ObservableCollection collection, IEnumerable range) + { + if (range == null) return; + collection.Clear(); + foreach (var item in range) + { + collection.Add(item); + } + } + } +} diff --git a/src/BiliLite.UWP/Models/Requests/Api/SearchAPI.cs b/src/BiliLite.UWP/Models/Requests/Api/SearchAPI.cs index 78865c0f..ff0ba24a 100644 --- a/src/BiliLite.UWP/Models/Requests/Api/SearchAPI.cs +++ b/src/BiliLite.UWP/Models/Requests/Api/SearchAPI.cs @@ -183,5 +183,16 @@ public ApiModel WebSearchTopic(string keyword, int pn = 1, string area = "") } return api; } + + public ApiModel SearchSuggest(string content) + { + var api = new ApiModel() + { + method = RestSharp.Method.Get, + baseUrl = $"https://s.search.bilibili.com/main/suggest", + parameter = $"term={content}&main_ver=v1" + }; + return api; + } } } diff --git a/src/BiliLite.UWP/Models/Responses/SearchSuggestResponse.cs b/src/BiliLite.UWP/Models/Responses/SearchSuggestResponse.cs new file mode 100644 index 00000000..d089d690 --- /dev/null +++ b/src/BiliLite.UWP/Models/Responses/SearchSuggestResponse.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace BiliLite.Models.Responses +{ + public class SearchSuggestResponse + { + public List Tag { get; set; } + } + + public class SearchSuggestResponseTag + { + public string Value { get; set; } + } +} diff --git a/src/BiliLite.UWP/Modules/Home/HomeVM.cs b/src/BiliLite.UWP/Modules/Home/HomeVM.cs index 8ee7d303..e36b35a9 100644 --- a/src/BiliLite.UWP/Modules/Home/HomeVM.cs +++ b/src/BiliLite.UWP/Modules/Home/HomeVM.cs @@ -1,12 +1,8 @@ using BiliLite.Helpers; -using BiliLite.Models; using FontAwesome5; using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; -using Windows.UI.Xaml.Controls; using System.Collections.ObjectModel; namespace BiliLite.Modules @@ -170,6 +166,16 @@ public async Task LoginUserCard() } + private ObservableCollection m_suggestSearchContents; + + public ObservableCollection SuggestSearchContents + { + get => m_suggestSearchContents; + set { + m_suggestSearchContents = value; + DoPropertyChanged("SuggestSearchContents"); + } + } } public class HomeNavItem : IModules { diff --git a/src/BiliLite.UWP/Modules/SearchVM.cs b/src/BiliLite.UWP/Modules/SearchVM.cs index b49540a8..196bcd2b 100644 --- a/src/BiliLite.UWP/Modules/SearchVM.cs +++ b/src/BiliLite.UWP/Modules/SearchVM.cs @@ -60,6 +60,7 @@ public bool ShowLoadMore get { return _ShowLoadMore; } set { _ShowLoadMore = value; DoPropertyChanged("ShowLoadMore"); } } + public async virtual void Refresh() { HasData = false; @@ -161,6 +162,17 @@ public ISearchVM SelectItem set { _SelectItem = value; } } + private ObservableCollection m_suggestSearchContents; + + public ObservableCollection SuggestSearchContents + { + get => m_suggestSearchContents; + set + { + m_suggestSearchContents = value; + DoPropertyChanged("SuggestSearchContents"); + } + } } public class SearchVideoVM : ISearchVM { diff --git a/src/BiliLite.UWP/Pages/HomePage.xaml b/src/BiliLite.UWP/Pages/HomePage.xaml index 0e42cf64..baeff87f 100644 --- a/src/BiliLite.UWP/Pages/HomePage.xaml +++ b/src/BiliLite.UWP/Pages/HomePage.xaml @@ -25,7 +25,13 @@ PaneDisplayMode="Top" IsBackButtonVisible="Collapsed" > - + diff --git a/src/BiliLite.UWP/Pages/HomePage.xaml.cs b/src/BiliLite.UWP/Pages/HomePage.xaml.cs index 26e1364a..72827f09 100644 --- a/src/BiliLite.UWP/Pages/HomePage.xaml.cs +++ b/src/BiliLite.UWP/Pages/HomePage.xaml.cs @@ -1,24 +1,12 @@ -using BiliLite.Helpers; +using BiliLite.Extensions; +using BiliLite.Helpers; using BiliLite.Models.Common; using BiliLite.Modules; -using BiliLite.Pages.Home; using BiliLite.Services; using Microsoft.Toolkit.Uwp.Connectivity; using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.ApplicationModel; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; // https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板 @@ -169,7 +157,7 @@ private async void SearchBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBo return; } - if (await MessageCenter.HandelUrl(SearchBox.Text)) + if (await MessageCenter.HandelUrl(args.QueryText)) { return; } @@ -178,10 +166,10 @@ private async void SearchBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBo { icon = Symbol.Find, page = typeof(SearchPage), - title = "搜索:" + SearchBox.Text, + title = "搜索:" + args.QueryText, parameters = new SearchParameter() { - keyword = SearchBox.Text, + keyword = args.QueryText, searchType = SearchType.Video } }); @@ -318,5 +306,20 @@ private void btnOpenDynamic_Click(object sender, RoutedEventArgs e) } }); } + + private async void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) + { + if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput) return; + var text = sender.Text; + var suggestSearchContents = await new SearchService().GetSearchSuggestContents(text); + if (homeVM.SuggestSearchContents == null) + { + homeVM.SuggestSearchContents = new System.Collections.ObjectModel.ObservableCollection(suggestSearchContents); + } + else + { + homeVM.SuggestSearchContents.ReplaceRange(suggestSearchContents); + } + } } } diff --git a/src/BiliLite.UWP/Pages/SearchPage.xaml b/src/BiliLite.UWP/Pages/SearchPage.xaml index 2a6e6119..febf3f3f 100644 --- a/src/BiliLite.UWP/Pages/SearchPage.xaml +++ b/src/BiliLite.UWP/Pages/SearchPage.xaml @@ -401,7 +401,15 @@ 香港地区 台湾地区 - + diff --git a/src/BiliLite.UWP/Pages/SearchPage.xaml.cs b/src/BiliLite.UWP/Pages/SearchPage.xaml.cs index b0979fa5..7f56014f 100644 --- a/src/BiliLite.UWP/Pages/SearchPage.xaml.cs +++ b/src/BiliLite.UWP/Pages/SearchPage.xaml.cs @@ -4,19 +4,9 @@ using BiliLite.Modules; using BiliLite.Services; using Microsoft.UI.Xaml.Controls; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板 @@ -96,29 +86,32 @@ protected override void OnNavigatedTo(NavigationEventArgs e) } pivot.SelectedIndex = (int)par.searchType; } + private async void txtKeyword_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) { - if (string.IsNullOrEmpty( txtKeyword.Text)) + var queryText = args.QueryText; + if (string.IsNullOrEmpty(queryText)) { Utils.ShowMessageToast("关键字不能为空啊,喂(#`O′)"); return; } - if (await MessageCenter.HandelUrl(txtKeyword.Text)) + if (await MessageCenter.HandelUrl(queryText)) { return; } - txtKeyword.Text = txtKeyword.Text.TrimStart('@'); + queryText = queryText.TrimStart('@'); foreach (var item in searchVM.SearchItems) { - item.Keyword = txtKeyword.Text; - item.Area= searchVM.Area.area; + item.Keyword = queryText; + item.Area = searchVM.Area.area; item.Page = 1; item.HasData = false; } searchVM.SelectItem.Refresh(); - ChangeTitle("搜索:"+txtKeyword.Text); + ChangeTitle("搜索:" + queryText); } + public void ChangeTitle(string title) { if ((this.Parent as Frame).Parent is TabViewItem) @@ -262,6 +255,21 @@ private void cbArea_SelectionChanged(object sender, SelectionChangedEventArgs e) searchVM.SelectItem.Refresh(); } } + + private async void txtKeyword_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) + { + if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput) return; + var text = sender.Text; + var suggestSearchContents = await new SearchService().GetSearchSuggestContents(text); + if (searchVM.SuggestSearchContents == null) + { + searchVM.SuggestSearchContents = new System.Collections.ObjectModel.ObservableCollection(suggestSearchContents); + } + else + { + searchVM.SuggestSearchContents.ReplaceRange(suggestSearchContents); + } + } } public class SearchDataTemplateSelector : DataTemplateSelector { diff --git a/src/BiliLite.UWP/Services/SearchService.cs b/src/BiliLite.UWP/Services/SearchService.cs new file mode 100644 index 00000000..7356e049 --- /dev/null +++ b/src/BiliLite.UWP/Services/SearchService.cs @@ -0,0 +1,23 @@ +using BiliLite.Extensions; +using BiliLite.Models.Requests.Api; +using BiliLite.Models.Responses; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BiliLite.Services +{ + public class SearchService + { + public async Task> GetSearchSuggestContents(string text) + { + if (string.IsNullOrWhiteSpace(text)) return null; + var suggestSearchResult = await new SearchAPI().SearchSuggest(text).Request(); + if (!suggestSearchResult.status) return null; + var suggestSearch = await suggestSearchResult.GetResult(); + if (suggestSearch == null) return null; + var suggestSearchContent = suggestSearch.result.Tag.Select(x => x.Value); + return suggestSearchContent.ToList(); + } + } +} From 1b27e6bf8c5737a4e630f31213eb09998845d747 Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sat, 18 Mar 2023 15:25:11 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=8F=98=E6=9B=B4=E6=8E=A8=E8=8D=90?= =?UTF-8?q?=E9=A1=B5=E5=90=8C=E6=97=B6=E6=98=BE=E7=A4=BA=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E5=92=8Cup=E4=B8=BB=E5=90=8D=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/Modules/Home/RecommendVM.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/BiliLite.UWP/Modules/Home/RecommendVM.cs b/src/BiliLite.UWP/Modules/Home/RecommendVM.cs index 08a50a23..df982fc6 100644 --- a/src/BiliLite.UWP/Modules/Home/RecommendVM.cs +++ b/src/BiliLite.UWP/Modules/Home/RecommendVM.cs @@ -349,11 +349,7 @@ public string bottomText { return desc_button.text; } - if (card_goto=="live") - { - return args.up_name; - } - return ""; + return args.up_name; } } From 6b9a4c9b94b8db36b97eea00c082e76db5375abe Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Mon, 3 Apr 2023 17:01:59 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/App.xaml.cs | 1 + src/BiliLite.UWP/Assets/Text/help.md | 4 +- src/BiliLite.UWP/BiliLite.UWP.csproj | 8 ++ .../Extensions/ExceptionExtensions.cs | 16 ++++ .../Extensions/StringExtensions.cs | 11 +++ .../Models/Requests/BiliRequest.cs | 10 ++- src/BiliLite.UWP/Modules/IModules.cs | 10 +-- .../Player/Playurl/BiliPayUrlRequest.cs | 2 +- src/BiliLite.UWP/Services/LogHelper.cs | 80 ++++++++++++------- src/BiliLite.UWP/Services/LogService.cs | 49 ++++++++++++ src/BiliLite.UWP/log4net.config | 19 +++++ 11 files changed, 168 insertions(+), 42 deletions(-) create mode 100644 src/BiliLite.UWP/Extensions/ExceptionExtensions.cs create mode 100644 src/BiliLite.UWP/Services/LogService.cs create mode 100644 src/BiliLite.UWP/log4net.config diff --git a/src/BiliLite.UWP/App.xaml.cs b/src/BiliLite.UWP/App.xaml.cs index 0a9d930e..2a5d732a 100644 --- a/src/BiliLite.UWP/App.xaml.cs +++ b/src/BiliLite.UWP/App.xaml.cs @@ -49,6 +49,7 @@ public App() FFmpegInteropLogging.SetLogLevel(LogLevel.Info); FFmpegInteropLogging.SetLogProvider(this); SqlHelper.InitDB(); + LogHelper.Init(); this.Suspending += OnSuspending; } private void RegisterExceptionHandlingSynchronizationContext() diff --git a/src/BiliLite.UWP/Assets/Text/help.md b/src/BiliLite.UWP/Assets/Text/help.md index 00c14a10..b2e3fbb8 100644 --- a/src/BiliLite.UWP/Assets/Text/help.md +++ b/src/BiliLite.UWP/Assets/Text/help.md @@ -131,8 +131,8 @@ Webp图片扩展安装地址:[ms-windows-store://pdp/?productid=9PG2DK419DRG](ms ### 日志文件 -路径:`%USERPROFILE%\AppData\Local\Packages\5421.502643927C6AD_wp9dg7z2zqgtj\LocalState\log` +路径:`%USERPROFILE%\AppData\Local\Packages\5422.502643927C6AD_vn31kc91nth4r\LocalState\log` -为了你的账号安全,发送日志时请一定要移除access_key=...里的内容 +为了你的账号安全,发送日志时请一定要确保access_key/access_token/csrf=...里的内容已清除或已经被修改为{hasValue} [点击打开日志存放目录](OpenLog) diff --git a/src/BiliLite.UWP/BiliLite.UWP.csproj b/src/BiliLite.UWP/BiliLite.UWP.csproj index 09e1f464..ef5946a8 100644 --- a/src/BiliLite.UWP/BiliLite.UWP.csproj +++ b/src/BiliLite.UWP/BiliLite.UWP.csproj @@ -129,6 +129,7 @@ + @@ -279,6 +280,7 @@ + @@ -599,6 +601,9 @@ Designer + + PreserveNewest + @@ -879,6 +884,9 @@ 3.19.4 + + 2.0.15 + 6.2.10 diff --git a/src/BiliLite.UWP/Extensions/ExceptionExtensions.cs b/src/BiliLite.UWP/Extensions/ExceptionExtensions.cs new file mode 100644 index 00000000..50c185b3 --- /dev/null +++ b/src/BiliLite.UWP/Extensions/ExceptionExtensions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BiliLite.Extensions +{ + public static class ExceptionExtensions + { + public static bool IsNetworkError(this Exception ex) + { + return ex.HResult == -2147012867 || ex.HResult == -2147012889; + } + } +} diff --git a/src/BiliLite.UWP/Extensions/StringExtensions.cs b/src/BiliLite.UWP/Extensions/StringExtensions.cs index 71d34522..02947331 100644 --- a/src/BiliLite.UWP/Extensions/StringExtensions.cs +++ b/src/BiliLite.UWP/Extensions/StringExtensions.cs @@ -143,6 +143,17 @@ public static RichTextBlock ToRichTextBlock(this string txt, JObject emote) } } + public static string ProtectValues(this string url, params string[] keys) + { + foreach (string key in keys) + { + string pattern = $@"({key}=)([^&]*)"; + string replacement = $"$1{{hasValue}}"; + url = Regex.Replace(url, pattern, replacement); + } + return url; + } + #region Private methods /// diff --git a/src/BiliLite.UWP/Models/Requests/BiliRequest.cs b/src/BiliLite.UWP/Models/Requests/BiliRequest.cs index 2db18089..dfba6b27 100644 --- a/src/BiliLite.UWP/Models/Requests/BiliRequest.cs +++ b/src/BiliLite.UWP/Models/Requests/BiliRequest.cs @@ -143,13 +143,21 @@ private HttpResults ConstructRedirectResults(IFlurlResponse response) return httpResults; } + private async Task LogRequest() + { + var body = ""; + if (m_body != null) + body = await m_body?.ReadAsStringAsync(); + LogHelper.Log($"http request: [{m_method}]{m_url} {body}", LogType.INFO); + } + public async Task Send() { + await LogRequest(); IFlurlResponse response = null; HttpResults httpResults; try { - if (m_method == HttpMethod.Get) { response = await m_request.GetAsync(); diff --git a/src/BiliLite.UWP/Modules/IModules.cs b/src/BiliLite.UWP/Modules/IModules.cs index 53b8bb9c..0d411cad 100644 --- a/src/BiliLite.UWP/Modules/IModules.cs +++ b/src/BiliLite.UWP/Modules/IModules.cs @@ -1,13 +1,9 @@ -using BiliLite.Models; +using BiliLite.Extensions; +using BiliLite.Models; using BiliLite.Models.Common; using BiliLite.Services; using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.UI.Xaml.Controls; namespace BiliLite.Modules { @@ -15,7 +11,7 @@ public class IModules : INotifyPropertyChanged { public virtual ReturnModel HandelError(Exception ex) { - if (LogHelper.IsNetworkError(ex)) + if (ex.IsNetworkError()) { return new ReturnModel() { diff --git a/src/BiliLite.UWP/Modules/Player/Playurl/BiliPayUrlRequest.cs b/src/BiliLite.UWP/Modules/Player/Playurl/BiliPayUrlRequest.cs index 78d1f3bb..b1375ca7 100644 --- a/src/BiliLite.UWP/Modules/Player/Playurl/BiliPayUrlRequest.cs +++ b/src/BiliLite.UWP/Modules/Player/Playurl/BiliPayUrlRequest.cs @@ -14,6 +14,7 @@ using BiliLite.gRPC.Api; using Proto.Reply; using BiliLite.Models.Requests.Api; +using BiliLite.Services; namespace BiliLite.Modules.Player.Playurl { @@ -688,7 +689,6 @@ private async Task GetPlayUrlUseWebApi(PlayInfo playInf } catch (Exception ex) { - return BiliPlayUrlQualitesInfo.Failure(ex.Message); } diff --git a/src/BiliLite.UWP/Services/LogHelper.cs b/src/BiliLite.UWP/Services/LogHelper.cs index 51624712..3c7f3455 100644 --- a/src/BiliLite.UWP/Services/LogHelper.cs +++ b/src/BiliLite.UWP/Services/LogHelper.cs @@ -1,13 +1,14 @@ -using BiliLite.Models.Common; +using BiliLite.Extensions; +using BiliLite.Models.Common; using NLog; using NLog.Config; using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; +using System.Globalization; +using System.IO; +using System.Runtime.CompilerServices; using System.Threading.Tasks; - +using Windows.Storage; namespace BiliLite.Services { @@ -15,26 +16,54 @@ public class LogHelper { public static LoggingConfiguration config; public static Logger logger = LogManager.GetCurrentClassLogger(); - public static void Log(string message, LogType type, Exception ex = null) + + public static void Init() + { + config = new LoggingConfiguration(); + Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; + var logfile = new NLog.Targets.FileTarget() + { + Name = "logfile", + CreateDirs = true, + FileName = storageFolder.Path + @"\log\" + DateTime.Now.ToString("yyyyMMdd") + ".log", + Layout = "${longdate}|${level:uppercase=true}|${logger}|${threadid}|${message}|${exception:format=Message,StackTrace}" + }; + config.AddRule(LogLevel.Info, LogLevel.Info, logfile); + config.AddRule(LogLevel.Debug, LogLevel.Debug, logfile); + config.AddRule(LogLevel.Error, LogLevel.Error, logfile); + config.AddRule(LogLevel.Fatal, LogLevel.Fatal, logfile); + LogManager.Configuration = config; + // todo: add await + DeleteFile(storageFolder.Path + @"\log\"); + } + + public static async Task DeleteFile(string path) { - if (config == null) + string pattern = "yyyyMMdd"; + int days = 7; + var folder = await StorageFolder.GetFolderFromPathAsync(path); + + var files = await folder.GetFilesAsync(); + + foreach (var file in files) { - config = new LoggingConfiguration(); - Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; - var logfile = new NLog.Targets.FileTarget() + var fileName = file.DisplayName; + if (!DateTimeOffset.TryParseExact(fileName, pattern, CultureInfo.InvariantCulture, DateTimeStyles.None, out var fileDate)) { - Name = "logfile", - CreateDirs = true, - FileName = storageFolder.Path + @"\log\" + DateTime.Now.ToString("yyyyMMdd") + ".log", - Layout = "${longdate}|${level:uppercase=true}|${logger}|${threadid}|${message}|${exception:format=Message,StackTrace}" - }; - config.AddRule(LogLevel.Info, LogLevel.Info, logfile); - config.AddRule(LogLevel.Debug, LogLevel.Debug, logfile); - config.AddRule(LogLevel.Error, LogLevel.Error, logfile); - config.AddRule(LogLevel.Fatal, LogLevel.Fatal, logfile); - LogManager.Configuration = config; + continue; + } + if (fileDate < DateTimeOffset.Now.AddDays(-days)) + { + File.Delete(file.Path); + } } + } + + public static void Log(string message, LogType type, Exception ex = null, [CallerMemberName] string methodName = null) + { Debug.WriteLine("[" + LogType.INFO.ToString() + "]" + message); + message = $"[{methodName}]{message}"; + message = message.ProtectValues("access_key", "csrf", "access_token"); switch (type) { case LogType.INFO: @@ -53,16 +82,5 @@ public static void Log(string message, LogType type, Exception ex = null) break; } } - public static bool IsNetworkError(Exception ex) - { - if (ex.HResult == -2147012867 || ex.HResult == -2147012889) - { - return true; - } - { - return false; - } - } - } } diff --git a/src/BiliLite.UWP/Services/LogService.cs b/src/BiliLite.UWP/Services/LogService.cs new file mode 100644 index 00000000..de57766e --- /dev/null +++ b/src/BiliLite.UWP/Services/LogService.cs @@ -0,0 +1,49 @@ +using BiliLite.Models.Common; +using log4net; +using log4net.Config; +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.CompilerServices; + +namespace BiliLite.Services +{ + public class LogService + { + public static ILog logger = LogManager.GetLogger(typeof(LogHelper)); + + public static void Log(string message, LogType type, Exception ex = null, [CallerMemberName] string methodName = null) + { + if (!LogManager.GetRepository().Configured) + { + var logRepository = LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); + } + + Debug.WriteLine("[" + LogType.INFO.ToString() + "]" + message); + + switch (type) + { + case LogType.INFO: + logger.Info(message); + break; + case LogType.DEBUG: + logger.Debug(message); + break; + case LogType.ERROR: + logger.Error(message, ex); + break; + case LogType.FATAL: + logger.Fatal(message, ex); + break; + default: + break; + } + } + public static void Init() + { + var logRepository = LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly()); + XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); + } + } +} \ No newline at end of file diff --git a/src/BiliLite.UWP/log4net.config b/src/BiliLite.UWP/log4net.config new file mode 100644 index 00000000..fc5c393e --- /dev/null +++ b/src/BiliLite.UWP/log4net.config @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 7bf9897fc5388fc3591eeeefacc962a746d601fd Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Thu, 6 Apr 2023 20:01:51 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/Helpers/SettingHelper.cs | 18 +++++++++++++++++ src/BiliLite.UWP/Pages/SettingPage.xaml | 15 ++++++++++++++ src/BiliLite.UWP/Pages/SettingPage.xaml.cs | 23 ++++++++++++++++++++++ src/BiliLite.UWP/Services/LogHelper.cs | 17 ++++++++++++---- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/BiliLite.UWP/Helpers/SettingHelper.cs b/src/BiliLite.UWP/Helpers/SettingHelper.cs index 6bd462f4..64c1548a 100644 --- a/src/BiliLite.UWP/Helpers/SettingHelper.cs +++ b/src/BiliLite.UWP/Helpers/SettingHelper.cs @@ -535,5 +535,23 @@ public class Download public const string DEFAULT_VIDEO_TYPE = "DownloadDefaultVideoType"; } + + public class Other + { + /// + /// 自动清理日志文件 + /// + public const string AUTO_CLEAR_LOG_FILE = "autoClearLogFile"; + + /// + /// 自动清理多少天前的日志文件 + /// + public const string AUTO_CLEAR_LOG_FILE_DAY = "autoClearLogFileDay"; + + /// + /// 保护日志敏感信息 + /// + public const string PROTECT_LOG_INFO = "protectLogInfo"; + } } } diff --git a/src/BiliLite.UWP/Pages/SettingPage.xaml b/src/BiliLite.UWP/Pages/SettingPage.xaml index b42965b4..41995bd5 100644 --- a/src/BiliLite.UWP/Pages/SettingPage.xaml +++ b/src/BiliLite.UWP/Pages/SettingPage.xaml @@ -516,6 +516,21 @@ + + + 其他 + + + + 自动清理日志文件 + + 自动清理几天前的日志 + + 保护日志中敏感信息 + + + + 帮助 diff --git a/src/BiliLite.UWP/Pages/SettingPage.xaml.cs b/src/BiliLite.UWP/Pages/SettingPage.xaml.cs index a823e03f..1eac0c46 100644 --- a/src/BiliLite.UWP/Pages/SettingPage.xaml.cs +++ b/src/BiliLite.UWP/Pages/SettingPage.xaml.cs @@ -39,6 +39,7 @@ public SettingPage() LoadDanmu(); LoadLiveDanmu(); LoadDownlaod(); + LoadOther(); } private void LoadUI() { @@ -651,6 +652,28 @@ private void LoadDownlaod() }); } + private void LoadOther() + { + //自动清理日志文件 + swAutoClearLogFile.IsOn = SettingHelper.GetValue(SettingHelper.Other.AUTO_CLEAR_LOG_FILE, true); + swAutoClearLogFile.Toggled += new RoutedEventHandler((e, args) => + { + SettingHelper.SetValue(SettingHelper.Other.AUTO_CLEAR_LOG_FILE, swAutoClearLogFile.IsOn); + }); + //自动清理多少天前的日志文件 + numAutoClearLogDay.Value = SettingHelper.GetValue(SettingHelper.Other.AUTO_CLEAR_LOG_FILE_DAY, 7); + numAutoClearLogDay.ValueChanged += ((e, args) => + { + SettingHelper.SetValue(SettingHelper.Other.AUTO_CLEAR_LOG_FILE_DAY, numAutoClearLogDay.Value); + }); + //保护日志敏感信息 + swProtectLogInfo.IsOn = SettingHelper.GetValue(SettingHelper.Other.PROTECT_LOG_INFO, true); + swProtectLogInfo.Toggled += ((e, args) => + { + SettingHelper.SetValue(SettingHelper.Other.PROTECT_LOG_INFO, swProtectLogInfo.IsOn); + }); + } + private void ExceptHomeNavItems() { List list = new List(); diff --git a/src/BiliLite.UWP/Services/LogHelper.cs b/src/BiliLite.UWP/Services/LogHelper.cs index 3c7f3455..b389f7a4 100644 --- a/src/BiliLite.UWP/Services/LogHelper.cs +++ b/src/BiliLite.UWP/Services/LogHelper.cs @@ -1,4 +1,5 @@ using BiliLite.Extensions; +using BiliLite.Helpers; using BiliLite.Models.Common; using NLog; using NLog.Config; @@ -17,6 +18,10 @@ public class LogHelper public static LoggingConfiguration config; public static Logger logger = LogManager.GetCurrentClassLogger(); + private static bool IsAutoClearLogFile => SettingHelper.GetValue(SettingHelper.Other.AUTO_CLEAR_LOG_FILE, true); + private static int AutoClearLogFileDay => SettingHelper.GetValue(SettingHelper.Other.AUTO_CLEAR_LOG_FILE_DAY, 7); + private static bool IsProtectLogInfo => SettingHelper.GetValue(SettingHelper.Other.PROTECT_LOG_INFO, true); + public static void Init() { config = new LoggingConfiguration(); @@ -33,14 +38,17 @@ public static void Init() config.AddRule(LogLevel.Error, LogLevel.Error, logfile); config.AddRule(LogLevel.Fatal, LogLevel.Fatal, logfile); LogManager.Configuration = config; - // todo: add await - DeleteFile(storageFolder.Path + @"\log\"); + if (IsAutoClearLogFile) + { + // todo: add await + DeleteFile(storageFolder.Path + @"\log\"); + } } public static async Task DeleteFile(string path) { string pattern = "yyyyMMdd"; - int days = 7; + int days = AutoClearLogFileDay; var folder = await StorageFolder.GetFolderFromPathAsync(path); var files = await folder.GetFilesAsync(); @@ -63,7 +71,8 @@ public static void Log(string message, LogType type, Exception ex = null, [Calle { Debug.WriteLine("[" + LogType.INFO.ToString() + "]" + message); message = $"[{methodName}]{message}"; - message = message.ProtectValues("access_key", "csrf", "access_token"); + if(IsProtectLogInfo) + message = message.ProtectValues("access_key", "csrf", "access_token"); switch (type) { case LogType.INFO: From 7b2f9ebe73b9d719a3f19933d0db8551b4f4f20b Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Thu, 6 Apr 2023 22:49:21 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=8D=E9=A6=88?= =?UTF-8?q?=E9=82=AE=E7=AE=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/Pages/SettingPage.xaml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BiliLite.UWP/Pages/SettingPage.xaml b/src/BiliLite.UWP/Pages/SettingPage.xaml index 41995bd5..961b6e5e 100644 --- a/src/BiliLite.UWP/Pages/SettingPage.xaml +++ b/src/BiliLite.UWP/Pages/SettingPage.xaml @@ -553,6 +553,7 @@ Logo绘制 : xiaoyaomengo 反馈 Github Discussions : https://github.com/ywmoyue/biliuwp-lite/discussions + 邮箱(请在发邮件时附带Github Discussion贴子链接) : ruamuyan@outlook.com 说明 这是一个第三方客户端,应用所有数据来源均来自哔哩哔哩。 本程序仅供学习交流编程技术使用。 From 9a87f5dabc368c5081a5a6694d26fde78d83d51d Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Thu, 6 Apr 2023 22:49:36 +0800 Subject: [PATCH 7/7] update --- document/new_version.json | 6 +++--- src/BiliLite.Packages/Package.appxmanifest | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/document/new_version.json b/document/new_version.json index aa9499b7..c3bb7a5c 100644 --- a/document/new_version.json +++ b/document/new_version.json @@ -1,7 +1,7 @@ { - "version": "4.5.3", - "version_num": 40503, - "version_desc": "更新内容:\r\n\r\n* 支持视频详情页右侧标题可右键复制\r\n\r\n* 修复少部分视频打不开问题\r\n\r\n\r\n 如果无法打开下载地址,请访问:https://github.com/ywmoyue/biliuwp-lite/releases", + "version": "4.5.4", + "version_num": 40504, + "version_desc": "更新内容:\r\n\r\n* 首页推荐视频同时显示标签和up主名字\r\n\r\n* 添加搜索建议\r\n\r\n* 优化日志输出\r\n\r\n\r\n 如果无法打开下载地址,请访问:https://github.com/ywmoyue/biliuwp-lite/releases", "url": "https://github.com/ywmoyue/biliuwp-lite/releases", "download_url": "https://github.com/ywmoyue/biliuwp-lite/releases" } \ No newline at end of file diff --git a/src/BiliLite.Packages/Package.appxmanifest b/src/BiliLite.Packages/Package.appxmanifest index 85ed108e..95ac0afe 100644 --- a/src/BiliLite.Packages/Package.appxmanifest +++ b/src/BiliLite.Packages/Package.appxmanifest @@ -14,7 +14,7 @@ + Version="4.5.4.0" />