Skip to content

Commit

Permalink
支持发送带图片评论,自己发送的评论点右键可删除
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmoyue committed Jun 28, 2024
1 parent c86b7c7 commit 848cd84
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/BiliLite.UWP/BiliLite.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
<Compile Include="Models\Common\Danmaku\BiliDanmakuItem.cs" />
<Compile Include="Models\Common\Dynamic\DynamicUgcSeasonCardModel.cs" />
<Compile Include="Models\Common\Player\PlaySpeedMenu.cs" />
<Compile Include="Models\Common\UploadFileInfo.cs" />
<Compile Include="Models\Common\UserDynamic\DynamicPicture.cs" />
<Compile Include="Models\Common\UserDynamic\DynLiveInfo.cs" />
<Compile Include="Models\Common\UserDynamic\IUserDynamicCommands.cs" />
<Compile Include="Models\Common\UserDynamic\NavDynArticle.cs" />
Expand All @@ -196,6 +198,7 @@
<Compile Include="Pages\ISavablePage.cs" />
<Compile Include="Services\Biz\MediaListService.cs" />
<Compile Include="Services\PlaySpeedMenuService.cs" />
<Compile Include="ViewModels\Comment\SendCommentDialogViewModel.cs" />
<Compile Include="ViewModels\Rank\RankRegionViewModel.cs" />
<Compile Include="ViewModels\Settings\EditPlaySpeedMenuViewModel.cs" />
<Compile Include="ViewModels\User\FavoriteItemViewModel.cs" />
Expand Down
7 changes: 3 additions & 4 deletions src/BiliLite.UWP/Controls/CommentControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
</Flyout>

<MenuFlyout x:Key="menu">
<MenuFlyoutItem Click="BtnDeleteComment_Click" Visibility="{Binding showDelete}">删除</MenuFlyoutItem>
<MenuFlyoutItem>举报</MenuFlyoutItem>
<MenuFlyoutItem Click="BtnDeleteComment_Click" Visibility="{Binding ShowDelete}">删除</MenuFlyoutItem>
<MenuFlyoutItem Click="MenuFlyoutItem_OnClick">举报</MenuFlyoutItem>
</MenuFlyout>
<DataTemplate x:Key="commentTemplate" x:DataType="comment:CommentViewModel">
<Grid>
Expand Down Expand Up @@ -135,7 +135,7 @@
</Border>
</Grid>

<StackPanel Orientation="Horizontal" Margin="4 0" Grid.Column="1" >
<StackPanel ContextFlyout="{StaticResource menu}" Orientation="Horizontal" Margin="4 0" Grid.Column="1" >
<TextBlock VerticalAlignment="Center" Visibility="{x:Bind ShowTop}" FontSize="14" Margin="0 0 8 0" Foreground="DeepPink">[置顶]</TextBlock>
<TextBlock Text="{x:Bind Path=Member.Uname}"
Foreground="{x:Bind Path=Member.Vip.VipType,Converter={StaticResource colorSelct},ConverterParameter=2}" VerticalAlignment="Center"></TextBlock>
Expand Down Expand Up @@ -183,7 +183,6 @@
Text="{x:Bind Path=Member.UserSailing.Cardbg.Fan.NumDesc,Mode=OneWay}"></TextBlock>
</StackPanel>
</StackPanel>
<AppBarButton Visibility="Collapsed" Style="{StaticResource AppBarButtonNoChevronStyle}" Flyout="{StaticResource ResourceKey=menu}" HorizontalAlignment="Right" Icon="More" Width="48" ></AppBarButton>
</StackPanel>

<TextBlock Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" FontSize="12" Foreground="Gray" Margin="4 0" VerticalAlignment="Top" ToolTipService.ToolTip="{x:Bind Path=Ctime,Converter={StaticResource Datetime}}">
Expand Down
5 changes: 5 additions & 0 deletions src/BiliLite.UWP/Controls/CommentControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ private void CommentControl_OnSizeChanged(object sender, SizeChangedEventArgs e)
}
}

private void MenuFlyoutItem_OnClick(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}

#endregion

#region Public Methods
Expand Down
52 changes: 46 additions & 6 deletions src/BiliLite.UWP/Dialogs/SendCommentDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
mc:Ignorable="d"
Title="发表评论"
xmlns:modules="using:BiliLite.Modules"
xmlns:fa="using:FontAwesome5"
xmlns:viewModels="using:BiliLite.ViewModels"
xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:userDynamic="using:BiliLite.Models.Common.UserDynamic"
PrimaryButtonText="发送"
SecondaryButtonText="取消"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
Expand Down Expand Up @@ -51,12 +54,49 @@
</Flyout>
</ContentDialog.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBox MinHeight="120" Padding="8 8 8 36" TextWrapping="Wrap" x:Name="txt_Comment" AcceptsReturn="True" PlaceholderText="说点什么吧..."></TextBox>
<Button x:Name="btnOpenFace" Click="btnOpenFace_Click" Width="36" VerticalAlignment="Bottom" Background="Transparent" Height="36" Foreground="Gray">
<StackPanel Orientation="Horizontal">
<FontIcon FontFamily="Segoe MDL2 Assets" FontSize="14" Glyph="&#xE170;" />

</StackPanel>
</Button>
<StackPanel VerticalAlignment="Bottom" Orientation="Horizontal">
<Button x:Name="btnOpenFace" Click="btnOpenFace_Click" Width="36" VerticalAlignment="Bottom" Background="Transparent" Height="36" Foreground="Gray">
<StackPanel Orientation="Horizontal">
<FontIcon FontFamily="Segoe MDL2 Assets" FontSize="14" Glyph="&#xE170;" />
</StackPanel>
</Button>
<Button x:Name="BtnUploadDraw" Click="BtnUploadDraw_OnClick" Width="36" VerticalAlignment="Bottom" Background="Transparent" Height="36" Foreground="Gray">
<StackPanel Orientation="Horizontal">
<fa:FontAwesome Icon="Regular_FileImage" FontSize="14"></fa:FontAwesome>
</StackPanel>
</Button>
</StackPanel>
<StackPanel Grid.Row="1"
Visibility="{x:Bind m_viewModel.ShowPictures,Mode=OneWay}">
<toolkit:AdaptiveGridView
StretchContentForSingleRow="False"
OneRowModeEnabled="True"
ItemHeight="80"
ItemsSource="{x:Bind Path=m_viewModel.Pictures,Mode=OneWay}"
SelectionMode="None"
IsItemClickEnabled="False">
<toolkit:AdaptiveGridView.ItemTemplate>
<DataTemplate x:DataType="userDynamic:DynamicPicture">
<Grid
Background="Transparent">
<toolkit:ImageEx
Source="{x:Bind ImageUrl}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
PlaceholderSource="ms-appx:///Assets/Thumbnails/Placeholde.png"></toolkit:ImageEx>
<Button HorizontalAlignment="Right"
VerticalAlignment="Top"
x:Name="BtnRemovePicture"
Click="BtnRemovePicture_OnClick">x</Button>
</Grid>
</DataTemplate>
</toolkit:AdaptiveGridView.ItemTemplate>
</toolkit:AdaptiveGridView>
</StackPanel>
</Grid>
</ContentDialog>
60 changes: 59 additions & 1 deletion src/BiliLite.UWP/Dialogs/SendCommentDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
using BiliLite.Models.Requests.Api;
using System;
using System.IO;
using System.Linq;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using BiliLite.Extensions;
using BiliLite.ViewModels;
using static BiliLite.Models.Requests.Api.CommentApi;
using Microsoft.Extensions.DependencyInjection;
using Windows.Storage;
using BiliLite.Models.Common;
using BiliLite.Models.Common.UserDynamic;
using BiliLite.Models.Exceptions;
using BiliLite.Services;
using BiliLite.ViewModels.Comment;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“内容对话框”项模板

Expand All @@ -17,8 +26,12 @@ public sealed partial class SendCommentDialog : ContentDialog
readonly EmoteViewModel emoteVM;
readonly string oid;
readonly CommentType commentType;
private static readonly ILogger _logger = GlobalLogger.FromCurrentType();
private readonly SendCommentDialogViewModel m_viewModel;

public SendCommentDialog(string oid, CommentType commentType)
{
m_viewModel = App.ServiceProvider.GetRequiredService<SendCommentDialogViewModel>();
emoteVM = App.ServiceProvider.GetService<EmoteViewModel>();
this.InitializeComponent();
commentApi = new CommentApi();
Expand All @@ -37,7 +50,7 @@ private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, Conten
{
IsPrimaryButtonEnabled = false;
var text = txt_Comment.Text;
var result = await commentApi.AddComment(oid, commentType, text).Request();
var result = await commentApi.AddComment(oid, commentType, text, m_viewModel.Pictures.ToList()).Request();
var data = await result.GetData<object>();
if (data.code == 0)
{
Expand Down Expand Up @@ -78,5 +91,50 @@ private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
txt_Comment.Text += (e.ClickedItem as EmotePackageItemModel).text.ToString();
}

private async void BtnUploadDraw_OnClick(object sender, RoutedEventArgs e)
{
try
{
var filePicker = new FileOpenPicker();
filePicker.FileTypeFilter.Add(".jpg");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
var file = await filePicker.PickSingleFileAsync();
if (file == null) return;
using var openFile = await file.OpenAsync(FileAccessMode.Read);
using var stream = openFile.AsStreamForRead();
var bin = new byte[stream.Length];

await stream.ReadAsync(bin, 0, bin.Length);
var fileInfo = new UploadFileInfo()
{
Data = bin,
FileName = file.Name,
};

var api = commentApi.UploadDraw(fileInfo);
var result = await api.Request();
if (!result.status)
throw new CustomizedErrorException(result.message);
var uploadDrawResult = await result.GetData<DynamicPicture>();
if (!uploadDrawResult.success)
throw new CustomizedErrorException(uploadDrawResult.message);
m_viewModel.AddPicture(uploadDrawResult.data);
}
catch (Exception ex)
{
_logger.Error(ex.Message, ex);
Notify.ShowMessageToast(ex.Message);
}
}

private void BtnRemovePicture_OnClick(object sender, RoutedEventArgs e)
{
if (sender is Button { DataContext: DynamicPicture picture })
{
m_viewModel.RemovePicture(picture);
}
}
}
}
4 changes: 2 additions & 2 deletions src/BiliLite.UWP/Extensions/ApiModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public static async Task<HttpResults> Request(this ApiModel api, [CallerMemberNa
{
if (api.need_cookie)
{
return await api.url.PostHttpResultsWithCookie(api.body, api.headers, api.ExtraCookies);
return await api.url.PostHttpResultsWithCookie(api.body, api.FormData, api.headers, api.ExtraCookies);
}

return await api.url.PostHttpResultsAsync(api.body, api.headers);
return await api.url.PostHttpResultsAsync(api.body, api.FormData, api.headers);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/BiliLite.UWP/Extensions/StringHttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ public static async Task<HttpResults> GetRedirectHttpResultsWithWebCookie(this s
/// <param name="headers"></param>
/// <param name="cookies"></param>
/// <returns></returns>
public static async Task<HttpResults> GetHttpResultsWithWebCookie(this string url, IDictionary<string, string> headers = null, IDictionary<string,string> extraCookies = null)
public static async Task<HttpResults> GetHttpResultsWithWebCookie(this string url, IDictionary<string, string> headers = null, IDictionary<string, string> extraCookies = null)
{
try
{
var cookies = await GetCookies();

if (extraCookies != null)
{
foreach(var kvp in extraCookies.ToList())
foreach (var kvp in extraCookies.ToList())
{
cookies.Add(kvp.Key, kvp.Value);
}
Expand Down Expand Up @@ -196,19 +196,19 @@ public static async Task<string> GetString(this string url, IDictionary<string,
/// <param name="cookie"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static async Task<HttpResults> PostHttpResultsAsync(this string url, string body, IDictionary<string, string> headers = null, IDictionary<string, string> cookies = null)
public static async Task<HttpResults> PostHttpResultsAsync(this string url, string body, Dictionary<string, object> formBody, IDictionary<string, string> headers = null, IDictionary<string, string> cookies = null)
{
Debug.WriteLine("POST:" + url + "\r\nBODY:" + body);
var biliRequestBuilder = new BiliRequestBuilder(url)
.SetHeaders(headers)
.SetCookies(cookies)
.SetPostBody(body);
.SetPostBody(body, formBody);
var biliRequest = biliRequestBuilder.Build();
var httpResult = await biliRequest.Send();
return httpResult;
}

public static async Task<HttpResults> PostHttpResultsWithCookie(this string url, string body, IDictionary<string, string> headers = null, IDictionary<string, string> extraCookies = null)
public static async Task<HttpResults> PostHttpResultsWithCookie(this string url, string body, Dictionary<string, object> formBody, IDictionary<string, string> headers = null, IDictionary<string, string> extraCookies = null)
{
try
{
Expand All @@ -218,7 +218,7 @@ public static async Task<HttpResults> PostHttpResultsWithCookie(this string url,
if (cookies == null || cookies.Count == 0)
{
//访问一遍bilibili.com
var getCookieResult = await Constants.BILIBILI_DOMAIN.GetHttpResultsAsync();
var getCookieResult = await Constants.BILIBILI_DOMAIN.GetHttpResultsAsync();
cookieService.Cookies = getCookieResult.cookies;
}
cookies = cookieService.Cookies;
Expand All @@ -232,7 +232,7 @@ public static async Task<HttpResults> PostHttpResultsWithCookie(this string url,
}
}

return await url.PostHttpResultsAsync(body, headers, cookiesCollection);
return await url.PostHttpResultsAsync(body, formBody, headers, cookiesCollection);
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/BiliLite.UWP/Extensions/ViewModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static IServiceCollection AddViewModels(this IServiceCollection services)
services.AddTransient<CollectedPageViewModel>();
services.AddTransient<RankViewModel>();
services.AddTransient<EditPlaySpeedMenuViewModel>();
services.AddTransient<SendCommentDialogViewModel>();
return services;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/BiliLite.UWP/Models/Common/UploadFileInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BiliLite.Models.Common
{
public class UploadFileInfo
{
public string FileName { get; set; }

public byte[] Data { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/BiliLite.UWP/Models/Common/UserDynamic/DynamicPicture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace BiliLite.Models.Common.UserDynamic
{
public class DynamicPicture
{
[JsonProperty("image_height")]
public double ImageHeight { get; set; }

[JsonProperty("image_width")]
public double ImageWidth { get; set; }

[JsonProperty("image_url")]
public string ImageUrl { get; set; }

[JsonProperty("img_size")]
public double ImgSize { get; set; }
}
}
47 changes: 45 additions & 2 deletions src/BiliLite.UWP/Models/Requests/Api/CommentApi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using BiliLite.Extensions;
using System.Collections.Generic;
using System.Linq;
using BiliLite.Models.Common;
using BiliLite.Services;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using BiliLite.Models.Common.UserDynamic;

namespace BiliLite.Models.Requests.Api
{
Expand Down Expand Up @@ -136,7 +139,8 @@ public ApiModel DeleteComment(string oid, string rpid, int type)
// api.body += ApiHelper.GetSign(api.body, appKey);
return api;
}
public ApiModel AddComment(string oid, CommentType type, string message)

public ApiModel AddComment(string oid, CommentType type, string message, List<DynamicPicture> pictures = null)
{
var csrf = m_cookieService.GetCSRFToken();
var api = new ApiModel()
Expand All @@ -146,6 +150,45 @@ public ApiModel AddComment(string oid, CommentType type, string message)
body = $"&oid={oid}&type={(int)type}&message={Uri.EscapeDataString(message)}&csrf={csrf}",
need_cookie = true,
};

if (pictures != null && pictures.Any())
{
var pictureList = new List<object>();
foreach (var picture in pictures)
{
pictureList.Add(new
{
img_src = picture.ImageUrl,
img_width = picture.ImageWidth,
img_height = picture.ImageHeight,
img_size = picture.ImgSize
});
}

var picturesArgs = JsonConvert.SerializeObject(pictureList);
api.body += $"&pictures={picturesArgs}";
}

//api.body += ApiHelper.GetSign(api.body, appKey);
return api;
}

public ApiModel UploadDraw(UploadFileInfo file)
{
var csrf = m_cookieService.GetCSRFToken();
var api = new ApiModel()
{
method = RestSharp.Method.Post,
baseUrl = $"{ApiHelper.API_BASE_URL}/x/dynamic/feed/draw/upload_bfs",
FormData = new Dictionary<string, object>()
{
{ "file_up", file },
{ "biz", "new_dyn" },
{ "category", "daily" },
{ "csrf", csrf }
},
need_cookie = true,
};
//api.body += ApiHelper.GetSign(api.body, appKey);
return api;
}
Expand Down
Loading

0 comments on commit 848cd84

Please sign in to comment.