Skip to content

Commit

Permalink
功能: 增加自动登录
Browse files Browse the repository at this point in the history
采用md5_password模式
  • Loading branch information
kengwang committed Feb 21, 2021
1 parent 67328c7 commit 284fca6
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 44 deletions.
9 changes: 9 additions & 0 deletions HyPlayer/Classes/NCSong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

namespace HyPlayer.Classes
{

public struct NCCookie
{
public string name;
public string value;
public string path;
public string domain;
}

public struct NCSong
{
public string sid;
Expand Down
51 changes: 51 additions & 0 deletions HyPlayer/Common.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using NeteaseCloudMusicApi;
Expand All @@ -22,4 +24,53 @@ class Common
public static Frame BaseFrame;
public static Dictionary<string,object> GLOBAL = new Dictionary<string, object>();
}



internal static class Extensions
{
public static byte[] ToByteArrayUtf8(this string value)
{
return Encoding.UTF8.GetBytes(value);
}

public static string ToHexStringLower(this byte[] value)
{
var sb = new StringBuilder();
for (int i = 0; i < value.Length; i++)
sb.Append(value[i].ToString("x2"));
return sb.ToString();
}

public static string ToHexStringUpper(this byte[] value)
{
var sb = new StringBuilder();
for (int i = 0; i < value.Length; i++)
sb.Append(value[i].ToString("X2"));
return sb.ToString();
}

public static string ToBase64String(this byte[] value)
{
return Convert.ToBase64String(value);
}

public static byte[] ComputeMd5(this byte[] value)
{
var md5 = MD5.Create();
return md5.ComputeHash(value);
}

public static byte[] RandomBytes(this Random random, int length)
{
byte[] buffer = new byte[length];
random.NextBytes(buffer);
return buffer;
}

public static string Get(this CookieCollection cookies, string name, string defaultValue)
{
return cookies[name]?.Value ?? defaultValue;
}
}
}
68 changes: 36 additions & 32 deletions HyPlayer/Controls/PlaylistItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,44 @@
Height="300"
Width="250"
d:DesignHeight="300"
d:DesignWidth="250"
CornerRadius="4">
d:DesignWidth="250">
<Grid
Tapped="UIElement_OnTapped">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition
Height="60" />
</Grid.RowDefinitions>
<Image
Grid.RowSpan="2"
Grid.Row="0"
Name="ImageCover"
controls:DockPanel.Dock="Top"
Source="/Assets/icon.png" />
CornerRadius="10">
<Grid
Background="{ThemeResource PlayListItemLuminosity}"
Grid.Row="1"
CornerRadius="4">
<TextBlock
Width="250"
TextWrapping="WrapWholeWords"
x:Name="TextBlockPLAuthor"
Foreground="#FF262626"
Text="歌单创建者"
Margin="10,37,19,9"
FontSize="10" />
<TextBlock
Width="250"
x:Name="TextBlockPLName"
TextWrapping="WrapWholeWords"
Text="歌单名称"
Margin="10,12,10,27"
FontSize="15" />
Tapped="UIElement_OnTapped">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition
Height="60" />
</Grid.RowDefinitions>
<Grid
CornerRadius="10"
Grid.RowSpan="2"
Grid.Row="0">
<Image
Name="ImageCover"
Source="/Assets/icon.png" />
</Grid>
<Grid
Background="{ThemeResource PlayListItemLuminosity}"
Grid.Row="1"
CornerRadius="4">
<TextBlock
Width="250"
TextWrapping="WrapWholeWords"
x:Name="TextBlockPLAuthor"
Foreground="#FF262626"
Text="歌单创建者"
Margin="10,37,19,9"
FontSize="10" />
<TextBlock
Width="250"
x:Name="TextBlockPLName"
TextWrapping="WrapWholeWords"
Text="歌单名称"
Margin="10,12,10,27"
FontSize="15" />
</Grid>
</Grid>
</Grid>
</UserControl>
36 changes: 25 additions & 11 deletions HyPlayer/Pages/BasePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,31 @@ public BasePage()

private async void LoadLoginData()
{
StorageFile sf = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("Settings\\Cookie", Windows.Storage.CreationCollisionOption.ReplaceExisting);
//Common.ncapi = new CloudMusicApi();
var (isOk, json) = await Common.ncapi.RequestAsync(CloudMusicApiProviders.UserAccount);
if (isOk && json["account"].HasValues)
try
{
Common.Logined = true;
Common.LoginedUser.UserName = json["profile"]["nickname"].ToString();
Common.LoginedUser.ImgUrl = json["profile"]["avatarUrl"].ToString();
Common.LoginedUser.uid = json["account"]["id"].ToString();
StorageFile sf = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Settings\\UserPassword");
string txt = await FileIO.ReadTextAsync(sf);
string[] arr = txt.Split("\r\n");
var queries = new Dictionary<string, object>();
var account = arr[0];
var isPhone = Regex.Match(account, "^[0-9]+$").Success;
queries[isPhone ? "phone" : "email"] = account;
queries["md5_password"] = arr[1];
var (isOk, json) = await Common.ncapi.RequestAsync(isPhone ? CloudMusicApiProviders.LoginCellphone : CloudMusicApiProviders.Login, queries);
if (isOk && json["code"].ToString() == "200")
{
Common.Logined = true;
Common.LoginedUser.UserName = json["profile"]["nickname"].ToString();
Common.LoginedUser.ImgUrl = json["profile"]["avatarUrl"].ToString();
Common.LoginedUser.uid = json["account"]["id"].ToString();
TextBlockUserName.Text = json["profile"]["nickname"].ToString();
PersonPictureUser.ProfilePicture =
new BitmapImage(new Uri(json["profile"]["avatarUrl"].ToString()));
}

}
catch (Exception) { }

}

private async void ButtonLogin_OnClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -98,9 +113,8 @@ private async void ButtonLogin_OnClick(object sender, RoutedEventArgs e)
else
{
Common.Logined = true;
//string cookie = JsonConvert.SerializeObject();
StorageFile sf = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("Settings\\Cookie", Windows.Storage.CreationCollisionOption.ReplaceExisting);
//await FileIO.WriteTextAsync(sf, cookie);
StorageFile sf = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("Settings\\UserPassword", Windows.Storage.CreationCollisionOption.ReplaceExisting);
_ = FileIO.WriteTextAsync(sf, account + "\r\n" + TextBoxPassword.Password.ToString().ToByteArrayUtf8().ComputeMd5().ToHexStringLower());
Common.LoginedUser.UserName = json["profile"]["nickname"].ToString();
Common.LoginedUser.ImgUrl = json["profile"]["avatarUrl"].ToString();
Common.LoginedUser.uid = json["account"]["id"].ToString();
Expand Down
2 changes: 1 addition & 1 deletion NeteaseCloudMusicApi

0 comments on commit 284fca6

Please sign in to comment.