加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MainWindow.xaml.cs 52.70 KB
一键复制 编辑 原始数据 按行查看 历史
Hamm 提交于 2020-10-27 17:01 . fix: CDN造成的图片不显示BUG
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
using BBBUG.COM.Model;
using Newtonsoft.Json.Linq;
using SuperSocket.ClientEngine;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using WebSocket4Net;
using WMPLib;
namespace BBBUG.COM
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
///
public partial class MainWindow : Window
{
public string roomId = "888";
public string roomPassword = "";
public JObject roomInfo;
public WebSocket wss;
public string websocketUrl = "";
public bool websocketForceStop = false;
public bool websocketConnected = false;
public JObject userInfo;
List<Message> MessageList;
WindowsMediaPlayer audio;
public MainWindow()
{
InitializeComponent();
startAnimation();
//stopAnimation();
if (Https.AccessToken =="" && true)
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
MessageList = new List<Message>();
//
//登录成功 获取房间信息
GetRoomInfoAsync();
GetMyInfo();
GetServerTimeAsync();
audio = new WindowsMediaPlayer();
audio.PlayStateChange += Audio_PlayStateChange;
}
private void Audio_PlayStateChange(int NewState)
{
if(NewState == 1 || NewState == 8)
{
nowSongObject = null;
show_song_name.Content = "歌曲加载中";
show_song_user.Content = "";
}
}
private async Task GetServerTimeAsync()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
};
JObject result = await Https.PostAsync("system/time", postData);
timeDiff = Convert.ToInt32(result["data"]["time"]);
}
private void DeleteSong(object sender, MouseEventArgs args)
{
if (picked_list.SelectedItem != null)
{
DeleteSongAsync((Song)picked_list.SelectedItem);
}
}
private async Task DeleteSongAsync(Song song)
{
//点歌
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"mid",song.mid },
{"room_id", roomId },
};
JObject result = await Https.PostAsync("song/remove", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
getPickedSongData();
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private void DeleteMySong(object sender, MouseEventArgs args)
{
if (my_list.SelectedItem != null)
{
DeleteMySongAsync((Song)my_list.SelectedItem);
}
}
private async Task DeleteMySongAsync(Song song)
{
//点歌
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"mid",song.mid },
{"room_id", roomId },
};
JObject result = await Https.PostAsync("song/deleteMySong", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
getMySongData();
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private void PushSong(object sender, MouseEventArgs args)
{
if (picked_list.SelectedItem != null)
{
PushSongAsync((Song)picked_list.SelectedItem);
}
}
private async Task PushSongAsync(Song song)
{
//点歌
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"mid",song.mid },
{"room_id", roomId },
};
JObject result = await Https.PostAsync("song/push", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
getPickedSongData();
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private void PickMySong(object sender, MouseEventArgs args)
{
if (my_list.SelectedItem != null)
{
PickSongAsync(((Song)(my_list.SelectedItem)));
}
}
private void PickSong(object sender, MouseEventArgs args)
{
if (song_list.SelectedItem != null)
{
PickSongAsync(((Song)(song_list.SelectedItem)));
}
}
private async Task PickSongAsync(Song song)
{
//点歌
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"mid", song.mid },
{"room_id", roomId },
};
JObject result = (JObject)await Https.PostAsync("song/addSong", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private void SearchSong(object sender,MouseEventArgs args)
{
SearchSongAsync();
}
private async Task SearchSongAsync()
{
string keyword = search_song_keyword.Text.Trim();
if (keyword.Length>0)
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"keyword", keyword },
};
JObject result = (JObject)await Https.PostAsync("song/search", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
Action action_update_song = () =>
{
song_list.Items.Clear();
for (int i = 0; i < ((JArray)(result["data"])).Count; i++)
{
song_list.Items.Add(new Song
{
mid = ((JArray)(result["data"]))[i]["mid"].ToString(),
name = ((JArray)(result["data"]))[i]["name"].ToString(),
pic = ((JArray)(result["data"]))[i]["pic"].ToString(),
singer = ((JArray)(result["data"]))[i]["singer"].ToString(),
});
}
};
song_list.Dispatcher.BeginInvoke(action_update_song);
Action action_update_song_no_data = () =>
{
if (song_list.Items.Count > 0)
{
song_list_nodata.Visibility = Visibility.Hidden;
song_list_nodata_tips.Content = "输入关键词搜索想听的歌曲吧";
}
else
{
song_list_nodata.Visibility = Visibility.Visible;
song_list_nodata_tips.Content = "没有搜索到你想要的歌曲,请重试";
}
};
picked_song_nodata.Dispatcher.BeginInvoke(action_update_song_no_data);
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
}
private async Task GetMyInfo()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
};
JObject result = (JObject)await Https.PostAsync("user/getMyInfo", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
//获取房间成功
userInfo = (JObject)result["data"];
Console.WriteLine(userInfo["user_head"].ToString());
my_head_img.Source = BitmapFrame.Create(new Uri(userInfo["user_head"].ToString(), false), BitmapCreateOptions.None, BitmapCacheOption.Default); ;
if (userInfo["myRoom"].ToString().Equals("False"))
{
enter_my_room.Text = "创建房间";
}
else
{
enter_my_room.Text = "我的房间";
}
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
//需要输入密码
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private async Task GetRoomInfoAsync()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"room_id", roomId },
{"room_password", roomPassword },
};
roomInfo = (JObject)await Https.PostAsync("room/getRoomInfo", postData);
if (roomInfo["code"].ToString().Equals(Https.CodeSuccess))
{
//获取房间成功
roomInfo = (JObject)roomInfo["data"];
message_list.Items.Clear();
UpdateRoomUI();
//GetRoomMessageHistory();
GetRoomWebsocketAsync();
}
else if (roomInfo["code"].ToString().Equals(Https.CodeRedirect))
{
//需要输入密码
//GetRoomInfoAsync();
}
else if (roomInfo["code"].ToString().Equals(Https.CodeRedirectForce))
{
//房间封禁
AlertWindow alert = new AlertWindow();
alert.ShowDialog();
//跳转到888房间
roomId = "888";
GetRoomInfoAsync();
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(roomInfo["msg"].ToString());
}
}
private async void UpdateRoomUI()
{
Title = roomInfo["room_name"].ToString();
room_id.Text = roomInfo["room_id"].ToString();
room_title.Text = roomInfo["room_name"].ToString();
}
private async void GetRoomWebsocketAsync()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"channel", roomId },
{"password", roomPassword },
};
JObject result = (JObject)await Https.PostAsync("room/getWebsocketUrl", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
Console.WriteLine("获取连接地址成功");
//获取成功
websocketUrl = "ws://websocket.bbbug.com/?account=" + result["data"]["account"].ToString() + "&channel=" + result["data"]["channel"].ToString() + "&ticket=" + result["data"]["ticket"].ToString();
Console.WriteLine(websocketUrl);
if (websocketConnected)
{
wss.Close();
}
ConnectWebsocket();
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
GetRoomInfoAsync();
}
}
private async Task GetRoomMessageHistory()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"room_id", roomId },
{"room_password", roomPassword },
};
JObject result = (JObject)await Https.PostAsync("message/getMessageList", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
for(int i = ((JArray)result["data"]).Count-1; i >=0; i--)
{
MessageController(((JArray)(result["data"]))[i]["message_content"].ToString());
}
//获取成功
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(roomInfo["msg"].ToString());
}
}
private async Task ConnectWebsocket()
{
while (true)
{
if (!websocketConnected)
{
wss = new WebSocket(websocketUrl);
wss.Opened += websocket_Opened;
wss.Error += websocket_Error;
wss.Closed += websocket_Closed;
wss.MessageReceived += websocket_MessageReceived;
wss.Open();
break;
}
}
}
public void websocketReconnect()
{
Console.WriteLine("链接失败");
}
private void websocket_Opened(object sender, EventArgs e)
{
websocketConnected = true;
Console.WriteLine("链接成功");
}
private void websocket_Error(object sender, ErrorEventArgs e)
{
Console.WriteLine(e.Exception.Message);
websocketReconnect();
websocketConnected = false;
}
private void websocket_Closed(object sender, EventArgs e)
{
websocketReconnect();
websocketConnected = false;
}
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
MessageController(e.Message);
}
JObject nowSongObject;
private void MessageController(string message)
{
message = System.Web.HttpUtility.UrlDecode(message, System.Text.Encoding.UTF8);
JObject result = JObject.Parse(message);
Console.WriteLine(result["type"].ToString());
if (message_list.Items.Count > 100)
{
Action action_update_message = () =>
{
message_list.Items.RemoveAt(0);
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
if (result["type"].ToString().Equals("text"))
{
Action action_update_message = () =>
{
message_list.Items.Add(new Message()
{
message_id = result["message_id"].ToString(),
message_type = result["type"].ToString(),
message_content = System.Web.HttpUtility.UrlDecode(result["content"].ToString(), System.Text.Encoding.UTF8),
user_name = result["user"]["user_name"].ToString(),
user_head = result["user"]["user_head"].ToString().Length < 5 ? "Images/nohead.jpg" : result["user"]["user_head"].ToString(),
fromMe = (int)result["user"]["user_id"] == (int)userInfo["user_id"] ? Visibility.Visible : Visibility.Hidden,
fromOther = (int)result["user"]["user_id"] == (int)userInfo["user_id"] ? Visibility.Hidden : Visibility.Visible,
fromSystem = Visibility.Hidden,
isPicture = result["type"].ToString().Equals("img") ? Visibility.Visible : Visibility.Hidden,
isText = result["type"].ToString().Equals("text") ? Visibility.Visible : Visibility.Hidden,
message_time = GetNowTimeFriendly(result["message_time"].ToString())
});
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
else if (result["type"].ToString().Equals("img"))
{
try {
WebClient client = new WebClient();
HMACSHA1 hmacsha1 = new HMACSHA1();
string fileUrl = getStaticImage(System.Web.HttpUtility.UrlDecode(result["content"].ToString(), System.Text.Encoding.UTF8));
byte[] rstRes = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(fileUrl));
string shaString = System.Web.HttpUtility.UrlEncode(Convert.ToBase64String(rstRes));
if(!System.IO.File.Exists(Environment.CurrentDirectory + "/temp/" + shaString + ".jpg")) {
client.DownloadFile(fileUrl, Environment.CurrentDirectory + "/temp/" + shaString + ".jpg");
}
Action action_update_message = () =>
{
message_list.Items.Add(new Message()
{
message_id = result["message_id"].ToString(),
message_type = result["type"].ToString(),
message_content = Environment.CurrentDirectory + "/temp/" + shaString + ".jpg",
user_name = result["user"]["user_name"].ToString(),
user_head = result["user"]["user_head"].ToString().Length < 5 ? "Images/nohead.jpg" : result["user"]["user_head"].ToString(),
fromMe = (int)result["user"]["user_id"] == (int)userInfo["user_id"] ? Visibility.Visible : Visibility.Hidden,
fromOther = (int)result["user"]["user_id"] == (int)userInfo["user_id"] ? Visibility.Hidden : Visibility.Visible,
fromSystem = Visibility.Hidden,
isPicture = result["type"].ToString().Equals("img") ? Visibility.Visible : Visibility.Hidden,
isText = result["type"].ToString().Equals("text") ? Visibility.Visible : Visibility.Hidden,
message_time = GetNowTimeFriendly(result["message_time"].ToString())
});
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
catch(Exception e)
{
}
}
else if (result["type"].ToString().Equals("system"))
{
//系统消息
Action action_update_message = () =>
{
message_list.Items.Add(new Message()
{
fromMe = Visibility.Hidden,
fromOther = Visibility.Hidden,
fromSystem = Visibility.Visible,
message_content = System.Web.HttpUtility.UrlDecode(result["content"].ToString(), System.Text.Encoding.UTF8),
});
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
else if (result["type"].ToString().Equals("addSong"))
{
//系统消息
Action action_update_message = () =>
{
message_list.Items.Add(new Message()
{
fromMe = Visibility.Hidden,
fromOther = Visibility.Hidden,
fromSystem = Visibility.Visible,
message_content = System.Web.HttpUtility.UrlDecode(((JObject)(result["user"]))["user_name"].ToString(), System.Text.Encoding.UTF8) + " 点了一首 " + ((JObject)(result["song"]))["name"].ToString() + " (" + ((JObject)(result["song"]))["singer"].ToString()+")",
});
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
else if (result["type"].ToString().Equals("push"))
{
//系统消息
Action action_update_message = () =>
{
message_list.Items.Add(new Message()
{
fromMe = Visibility.Hidden,
fromOther = Visibility.Hidden,
fromSystem = Visibility.Visible,
message_content = System.Web.HttpUtility.UrlDecode(((JObject)(result["user"]))["user_name"].ToString(), System.Text.Encoding.UTF8) + " 将 " + ((JObject)(result["song"]))["name"].ToString() +" ("+ ((JObject)(result["song"]))["singer"].ToString() + ") 设为置顶候播",
});
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
else if (result["type"].ToString().Equals("removeSong"))
{
//系统消息
Action action_update_message = () =>
{
message_list.Items.Add(new Message()
{
fromMe = Visibility.Hidden,
fromOther = Visibility.Hidden,
fromSystem = Visibility.Visible,
message_content = System.Web.HttpUtility.UrlDecode(((JObject)(result["user"]))["user_name"].ToString(), System.Text.Encoding.UTF8) + " 移除了歌曲 " + ((JObject)(result["song"]))["name"].ToString() + "("+ ((JObject)(result["song"]))["singer"].ToString() + ")",
});
Decorator decorator = (Decorator)VisualTreeHelper.GetChild(message_list, 0);
ScrollViewer scrollViewer = (ScrollViewer)decorator.Child;
scrollViewer.ScrollToEnd();
};
message_list.Dispatcher.BeginInvoke(action_update_message);
}
else if (result["type"].ToString().Equals("playSong"))
{
nowSongObject = (JObject)result;
Action action_update_song_name = () =>
{
show_song_name.Content = ((JObject)(result["song"]))["name"] + "-" + ((JObject)(result["song"]))["singer"];
};
show_song_name.Dispatcher.BeginInvoke(action_update_song_name);
Action action_update_song_user = () =>
{
show_song_user.Content = "点歌人: " + System.Web.HttpUtility.UrlDecode(((JObject)(result["user"]))["user_name"].ToString(), System.Text.Encoding.UTF8);
};
show_song_user.Dispatcher.BeginInvoke(action_update_song_user);
Action action_update_song_next = () =>
{
if (Convert.ToInt32(userInfo["user_admin"]) == 1 || Convert.ToInt32(userInfo["user_id"]) == Convert.ToInt32(roomInfo["room_user"]) || Convert.ToInt32(userInfo["user_id"]) == Convert.ToInt32(nowSongObject["user"]["user_id"]))
{
button_next_song.ToolTip = "点击切歌";
button_next_song.Source = new BitmapImage(new Uri(@"Images/1_music82.png", UriKind.Relative));
}
else
{
button_next_song.ToolTip = "不喜欢,投一票";
button_next_song.Source = new BitmapImage(new Uri(@"Images/ios-heart-dislike.png", UriKind.Relative));
}
};
button_next_song.Dispatcher.BeginInvoke(action_update_song_next);
int position = GetTimeStamp() - Convert.ToInt32(result["since"] ) - 2;
if (position < 0)
{
position = 0;
}
PlayMusicAsync(((JObject)(result["song"]))["mid"].ToString(),position);
}
}
static int timeDiff = 0;
public static int GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt32(ts.TotalSeconds) - timeDiff;
}
private async Task PlayMusicAsync(string mid,int position)
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
};
HttpResponseMessage result = (HttpResponseMessage)await Https.PostMusicUrl("song/playurl?mid=" +mid , postData);
audio.URL = result.Headers.Location.ToString();
audio.controls.currentPosition = position;
}
private string GetNowTimeFriendly(string timestamps)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = (Convert.ToInt64(timestamps) * 10000000);
TimeSpan toNow = new TimeSpan(lTime);
DateTime targetDt = dtStart.Add(toNow);
return targetDt.ToString("HH:mm");
}
private string getStaticImage(string url)
{
if(url.StartsWith("https://")|| url.StartsWith("https://")){
return url;
}
else
{
return "https://static.bbbug.com/uploads/" + url;
}
}
private void startAnimation()
{
RotateTransform rtf = new RotateTransform();
rtf.CenterX = Convert.ToDouble(12);
rtf.CenterY = Convert.ToDouble(12);
icon_song_player.RenderTransform = rtf;
DoubleAnimation dbAscending = new DoubleAnimation(0, 360, new Duration
(TimeSpan.FromSeconds(10)));
dbAscending.RepeatBehavior = RepeatBehavior.Forever;
rtf.BeginAnimation(RotateTransform.AngleProperty, dbAscending);
}
private void stopAnimation()
{
RotateTransform rtf = new RotateTransform();
rtf.CenterX = Convert.ToDouble(12);
rtf.CenterY = Convert.ToDouble(12);
image_song_picture.RenderTransform = rtf;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
message_list.SelectedIndex = (message_list.Items.Count - 1);
}
private void Border_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}
private void Image_Close_Clicked(object sender, RoutedEventArgs e)
{
Close();
Environment.Exit(0);
}
private void Image_FullScreen_Click(object sender, RoutedEventArgs e)
{
if(WindowState == WindowState.Normal)
{
WindowState = WindowState.Maximized;
}
else
{
WindowState = WindowState.Normal;
}
}
private void Image_MiniScreen_Clicked(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void Button_MessageSend_Clicked(object sender, RoutedEventArgs e)
{
string message = message_input.Text;
if (message.Length > 0)
{
SendMessageToRoom(message, null);
message_input.Text = "";
}
}
private async void SendMessageToRoom(string message,JObject at)
{
//message = System.Web.HttpUtility.UrlEncode(System.Web.HttpUtility.UrlEncode(message, System.Text.Encoding.UTF8), System.Text.Encoding.UTF8);
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"to", roomId },
{"type", "text" },
{"msg", message },
{"where", "channel" },
};
JObject result = (JObject)await Https.PostAsync("message/send", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private void hideAllBox()
{
selectRoomBoxShow = true;
ShowSelectRoomBox();
searchSongBoxShow = true;
ShowSearchSongBox();
pickedSongBoxShow = true;
ShowPickedSongBox();
mySongBoxShow = true;
ShowMySongBox();
emoji_box.Visibility = Visibility.Hidden;
}
private void HideAllBoxClicked(object sender,MouseButtonEventArgs e)
{
hideAllBox();
}
private void ShowSelectRoomBoxClicked(object sender, MouseButtonEventArgs e)
{
pickedSongBoxShow = true;
ShowPickedSongBox();
searchSongBoxShow = true;
ShowSearchSongBox();
mySongBoxShow = true;
ShowMySongBox();
ShowSelectRoomBox();
}
DispatcherTimer selectRoomBoxTimer;
private bool isLoadingRoomList = false;
private void ShowSelectRoomBox()
{
if (isLoadingRoomList)
{
return;
}
selectRoomBoxTimer = new DispatcherTimer();
selectRoomBoxTimer.Interval = new TimeSpan(100000); //时间间隔为20ms
selectRoomBoxTimer.Tick += new EventHandler(selectRoomBoxAnimation);
selectRoomBoxTimer.Start();
}
bool selectRoomBoxShow = false;
public void selectRoomBoxAnimation(object sender, EventArgs e)
{
int width = 350;
isLoadingRoomList = true;
if (!selectRoomBoxShow)
{
if (selectRoomBox.Margin.Right >= 10)
{
selectRoomBoxTimer.Stop();
selectRoomBoxShow = true;
getHotRoomData();
isLoadingRoomList = false;
}
else
{
selectRoomBox.Margin = new Thickness(10, 10, (selectRoomBox.Margin.Right + 30) > 10 ? 10 : (selectRoomBox.Margin.Right + 30), 10);
}
}
else
{
if (selectRoomBox.Margin.Right <= 0 - width - 50)
{
selectRoomBoxTimer.Stop();
selectRoomBoxShow = false;
isLoadingRoomList = false;
}
else
{
selectRoomBox.Margin = new Thickness(10, 10, (selectRoomBox.Margin.Right - 30) < (0 - width - 50) ? (0 - width - 50) : (selectRoomBox.Margin.Right - 30), 10);
}
}
}
private void ShowPickedSongBoxClicked(object sender, MouseButtonEventArgs e)
{
selectRoomBoxShow = true;
ShowSelectRoomBox();
searchSongBoxShow = true;
ShowSearchSongBox();
mySongBoxShow = true;
ShowMySongBox();
ShowPickedSongBox();
}
DispatcherTimer pickedSongBoxTimer;
private bool isLoadingPickedSong = false;
private void ShowPickedSongBox()
{
if (isLoadingPickedSong)
{
return;
}
pickedSongBoxTimer = new DispatcherTimer();
pickedSongBoxTimer.Interval = new TimeSpan(100000); //时间间隔为20ms
pickedSongBoxTimer.Tick += new EventHandler(pickedSongBoxAnimation);
pickedSongBoxTimer.Start();
}
bool pickedSongBoxShow = false;
public void pickedSongBoxAnimation(object sender, EventArgs e)
{
int width = 350;
isLoadingPickedSong = true;
if (!pickedSongBoxShow)
{
if (pickedSongBox.Margin.Right >= 10)
{
pickedSongBoxTimer.Stop();
pickedSongBoxShow = true;
getPickedSongData();
isLoadingPickedSong = false;
}
else
{
pickedSongBox.Margin = new Thickness(10, 10, (pickedSongBox.Margin.Right + 30) > 10 ? 10 : (pickedSongBox.Margin.Right + 30), 10);
}
}
else
{
if (pickedSongBox.Margin.Right <= 0 - width - 50)
{
pickedSongBoxTimer.Stop();
pickedSongBoxShow = false;
isLoadingPickedSong = false;
}
else
{
pickedSongBox.Margin = new Thickness(10, 10, (pickedSongBox.Margin.Right - 30) < (0 - width - 50) ? (0 - width - 50) : (pickedSongBox.Margin.Right - 30), 10);
}
}
}
private void ShowSearchSongBoxClicked(object sender, MouseButtonEventArgs e)
{
selectRoomBoxShow = true;
ShowSelectRoomBox();
pickedSongBoxShow = true;
ShowPickedSongBox();
mySongBoxShow = true;
ShowMySongBox();
ShowSearchSongBox();
}
DispatcherTimer searchSongBoxTimer;
private bool isLoadingSearchSongBox = false;
private void ShowSearchSongBox()
{
if (isLoadingSearchSongBox)
{
return;
}
searchSongBoxTimer = new DispatcherTimer();
searchSongBoxTimer.Interval = new TimeSpan(100000); //时间间隔为20ms
searchSongBoxTimer.Tick += new EventHandler(searchSongBoxAnimation);
searchSongBoxTimer.Start();
}
bool searchSongBoxShow = false;
public void searchSongBoxAnimation(object sender, EventArgs e)
{
int width = 350;
isLoadingSearchSongBox = true;
if (!searchSongBoxShow)
{
if (searchSongBox.Margin.Right >= 10)
{
searchSongBoxTimer.Stop();
searchSongBoxShow = true;
isLoadingSearchSongBox = false;
}
else
{
searchSongBox.Margin = new Thickness(10, 10, (searchSongBox.Margin.Right + 30) > 10 ? 10 : (searchSongBox.Margin.Right + 30), 10);
}
}
else
{
if (searchSongBox.Margin.Right <= 0 - width - 50)
{
searchSongBoxTimer.Stop();
searchSongBoxShow = false;
isLoadingSearchSongBox = false;
}
else
{
searchSongBox.Margin = new Thickness(10, 10, (searchSongBox.Margin.Right - 30) < (0 - width - 50) ? (0 - width - 50) : (searchSongBox.Margin.Right - 30), 10);
}
}
}
private void ShowMySongBoxClicked(object sender, MouseButtonEventArgs e)
{
selectRoomBoxShow = true;
ShowSelectRoomBox();
pickedSongBoxShow = true;
ShowPickedSongBox();
searchSongBoxShow = true;
ShowSearchSongBox();
ShowMySongBox();
}
DispatcherTimer mySongBoxTimer;
private bool isLoadingMySongBox = false;
private void ShowMySongBox()
{
if (isLoadingMySongBox)
{
return;
}
mySongBoxTimer = new DispatcherTimer();
mySongBoxTimer.Interval = new TimeSpan(100000); //时间间隔为20ms
mySongBoxTimer.Tick += new EventHandler(mySongBoxAnimation);
mySongBoxTimer.Start();
}
bool mySongBoxShow = false;
public void mySongBoxAnimation(object sender, EventArgs e)
{
int width = 350;
isLoadingMySongBox = true;
if (!mySongBoxShow)
{
if (mySongBox.Margin.Right >= 10)
{
mySongBoxTimer.Stop();
mySongBoxShow = true;
isLoadingMySongBox = false;
getMySongData();
}
else
{
mySongBox.Margin = new Thickness(10, 10, (mySongBox.Margin.Right + 30) > 10 ? 10 : (mySongBox.Margin.Right + 30), 10);
}
}
else
{
if (mySongBox.Margin.Right <= 0 - width - 50)
{
mySongBoxTimer.Stop();
mySongBoxShow = false;
isLoadingMySongBox = false;
}
else
{
mySongBox.Margin = new Thickness(10, 10, (mySongBox.Margin.Right - 30) < (0 - width - 50) ? (0 - width - 50) : (mySongBox.Margin.Right - 30), 10);
}
}
}
Visibility showDelete = Visibility.Hidden;
public async void getPickedSongData()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"room_id",roomId }
};
JObject result = (JObject)await Https.PostAsync("song/songList", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
Action action_update_song_picked = () =>
{
picked_list.Items.Clear();
for (int i = 0; i < ((JArray)(result["data"])).Count; i++)
{
if (Convert.ToInt32(userInfo["user_admin"]) == 1 || Convert.ToInt32(userInfo["user_id"]) == Convert.ToInt32(roomInfo["room_user"]) || Convert.ToInt32(userInfo["user_id"]) == Convert.ToInt32(result["data"][i]["user"]["user_id"]))
{
showDelete = Visibility.Visible;
}
else
{
showDelete = Visibility.Hidden;
}
picked_list.Items.Add(new Song
{
mid = ((JArray)(result["data"]))[i]["song"]["mid"].ToString(),
name = ((JArray)(result["data"]))[i]["song"]["name"].ToString(),
singer = ((JArray)(result["data"]))[i]["song"]["singer"].ToString(),
pic = ((JArray)(result["data"]))[i]["song"]["pic"].ToString(),
showDelete = showDelete,
pickerName = System.Web.HttpUtility.UrlDecode(((JArray)(result["data"]))[i]["user"]["user_name"].ToString(), System.Text.Encoding.UTF8),
}); ;
}
};
picked_list.Dispatcher.BeginInvoke(action_update_song_picked);
Action action_update_song_picked_no_data = () =>
{
if (picked_list.Items.Count > 0)
{
picked_song_nodata.Visibility = Visibility.Hidden;
}
else
{
picked_song_nodata.Visibility = Visibility.Visible;
}
};
picked_song_nodata.Dispatcher.BeginInvoke(action_update_song_picked_no_data);
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
//需要登录
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
public async void getMySongData()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"room_id",roomId },
//{"order","recent" },
{"per_page","50" }
};
JObject result = (JObject)await Https.PostAsync("song/mySongList", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
Action action_update_song_my = () =>
{
my_list.Items.Clear();
for (int i = 0; i < ((JArray)(result["data"])).Count; i++)
{
my_list.Items.Add(new Song
{
mid = ((JArray)(result["data"]))[i]["mid"].ToString(),
name = ((JArray)(result["data"]))[i]["name"].ToString(),
singer = ((JArray)(result["data"]))[i]["singer"].ToString(),
pic = ((JArray)(result["data"]))[i]["pic"].ToString(),
played = ((JArray)(result["data"]))[i]["played"].ToString(),
}); ;
}
};
my_list.Dispatcher.BeginInvoke(action_update_song_my);
Action action_update_song_my_no_data = () =>
{
if (my_list.Items.Count > 0)
{
my_song_nodata.Visibility = Visibility.Hidden;
}
else
{
my_song_nodata.Visibility = Visibility.Visible;
}
};
my_song_nodata.Dispatcher.BeginInvoke(action_update_song_my_no_data);
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
//需要登录
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
public async void getHotRoomData()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
};
JObject roomList = (JObject)await Https.PostAsync("room/hotRooms", postData);
if (roomList["code"].ToString().Equals(Https.CodeSuccess))
{
//获取房间成功
JArray result = (JArray)roomList["data"];
List<Room> RoomList = new List<Room>();
for (int i = 0; i < result.Count; i++)
{
RoomList.Add(new Room()
{
room_id = result[i]["room_id"].ToString(),
room_name = result[i]["room_name"].ToString(),
room_notice = result[i]["room_notice"].ToString() ?? "房间过于牛逼,于是就不写介绍了。",
room_online = "(" + result[i]["room_online"].ToString() + ")",
user_head = result[i]["user_head"].ToString().Length < 5 ? "Images/nohead.jpg" : result[i]["user_head"].ToString(),
showOnline = (int)result[i]["room_online"] > 0 ? Visibility.Visible : Visibility.Hidden,
user_name = System.Web.HttpUtility.UrlDecode(result[i]["user_name"].ToString(), System.Text.Encoding.UTF8)
});
}
//room_list.Items.Clear();
room_list.ItemsSource = RoomList;
}
else if (roomInfo["code"].ToString().Equals(Https.CodeLogin))
{
//需要登录
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(roomInfo["msg"].ToString());
}
}
private void SelectRoomChanged(object sender, MouseButtonEventArgs e)
{
Room room = (Room)((ListBox)e.Source).SelectedItem;
roomId = room.room_id;
GetRoomInfoAsync();
selectRoomBoxShow = true;
ShowSelectRoomBox();
}
private void MessageInputKeydown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
string message = message_input.Text;
if (message.Length > 0)
{
SendMessageToRoom(message, null);
message_input.Text = "";
}
}
}
private void SearchSongTextBoxFocused(object sender, RoutedEventArgs e)
{
if (search_song_keyword.Text.Equals("输入歌手/歌名/专辑搜索..."))
{
search_song_keyword.Text = "";
}
}
private void SearchSongTextBoxLostFocus(object sender, RoutedEventArgs e)
{
if (search_song_keyword.Text.Trim().Length == 0)
{
search_song_keyword.Text = "输入歌手/歌名/专辑搜索...";
}
}
private void SendMessageTextBoxFocused(object sender, RoutedEventArgs e)
{
if (message_input.Text.Equals("说点什么吧..."))
{
message_input.Text = "";
}
}
private void SendMessageTextBoxLostFocus(object sender, RoutedEventArgs e)
{
if (message_input.Text.Trim().Length == 0)
{
message_input.Text = "说点什么吧...";
}
}
private void PassSong(object sender, MouseButtonEventArgs e)
{
if (nowSongObject != null)
{
PassSongAsync();
}
else
{
wss.Send("getNowSong");
}
}
private async Task PassSongAsync()
{
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"mid",nowSongObject["song"]["mid"].ToString() },
{"room_id",roomId }
};
JObject result = await Https.PostAsync("song/pass", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
if (Convert.ToInt32(userInfo["user_admin"]) == 1 || Convert.ToInt32(userInfo["user_id"]) == Convert.ToInt32(roomInfo["room_user"]) || Convert.ToInt32(userInfo["user_id"]) == Convert.ToInt32(nowSongObject["user"]["user_id"]))
{
getPickedSongData();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
audio.controls.stop();
nowSongObject = null;
}
}
else if (result["code"].ToString().Equals(Https.CodeLogin))
{
LoginWindow login = new LoginWindow();
login.ShowDialog();
}
else
{
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
private void SearchSongTextBoxKeydown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
SearchSongAsync();
}
}
private void MainWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
Environment.Exit(0);
}
private void EnterMyRoomClicked(object sender, MouseButtonEventArgs e)
{
if (enter_my_room.Text.Equals("我的房间"))
{
roomId = userInfo["myRoom"]["room_id"].ToString();
GetRoomInfoAsync();
}
else
{
new AlertWindow().showDialog("创建房间功能即将上线,你可以先在PC端创建房间后再玩耍~");
}
}
private void MyHeadImageClicked(object sender, MouseButtonEventArgs e)
{
new AlertWindow().showDialog("个人中心即将上线,敬请期待!");
}
private void SettingClicked(object sender, MouseButtonEventArgs e)
{
new AlertWindow().showDialog("系统设置正在开发中,敬请期待!");
}
private void ShowEmojiBox(object sender, MouseButtonEventArgs e)
{
if (emoji_box.Visibility == Visibility.Hidden) {
emoji_box.Visibility = Visibility.Visible;
}
else
{
emoji_box.Visibility = Visibility.Hidden;
}
}
private async void EmojiSendClickedAsync(object sender, MouseButtonEventArgs e)
{
string message = "https://cdn.bbbug.com/images/emoji/" + ((Image)e.Source).Source.ToString().Replace("pack://application:,,,/BBBUG音乐聊天室;component/Images/Emojis/", "");
Dictionary<string, string> postData = new Dictionary<string, string>()
{
{"to", roomId },
{"type", "img" },
{"msg", message },
{"resource", message },
{"where", "channel" },
};
JObject result = (JObject)await Https.PostAsync("message/send", postData);
if (result["code"].ToString().Equals(Https.CodeSuccess))
{
}
else
{
//显示错误的提示信息
AlertWindow alert = new AlertWindow();
alert.showDialog(result["msg"].ToString());
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化