WIn_RustTools/RustTools/Views/UserPage.xaml.cs

102 lines
2.9 KiB
C#

using System.Net.Http.Json;
using System.Security.Principal;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RustTools.DataList;
using RustTools.muqing;
using RustTools.Themes;
using RustTools.ViewModels;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace RustTools.Views;
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class UserPage : Page
{
public UserSpaceInfo.UserData userInfo { get; set; } = new();
public UserViewModel UserViewModel
{
get; set;
}
public UserPage()
{
UserViewModel = App.GetService<UserViewModel>();
InitializeComponent();
Init();
}
private async void Init()
{
var iniHelper = new IniHelper();
iniHelper.Load(IniHelper.FILE.User);
var account = iniHelper.GetValue(IniHelper.CODE.User, IniHelper.KEY.account);
var token = iniHelper.GetValue(IniHelper.CODE.User, IniHelper.KEY.token);
try
{
var v = await ApiFox.user.loginPc(account, token);
var userSpaceInfo = JsonConvert.DeserializeObject<UserSpaceInfo>(v);
//未登录
if (userSpaceInfo.Code == 0)
{
nulllogin();
}
else
{
await login(account);
}
}
catch
{
nulllogin();
}
}
private void nulllogin()
{
gridview.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
var loginPage = new LoginPage();
loginPage.LoginSucceeded += LoginPage_LoginSucceeded;
view.Children.Add(loginPage);
}
private async void LoginPage_LoginSucceeded(object sender, string account)
{
// 登录成功后,从 Grid 中移除 LoginPage
view.Children.Remove((LoginPage)sender);
await login(account);
}
private async Task login(string account)
{
var v = await ApiFox.user.SpaceInfo(account);
var userSpaceInfo = JsonConvert.DeserializeObject<UserSpaceInfo>(v);
if (userSpaceInfo != null && userSpaceInfo.Code == 0)
{
var data = userSpaceInfo.Data;
if (data == null) { return; }
data.HeadIcon=data.HeadIcon == string.Empty ? "/Assets/tool.png" : data.HeadIcon.Replace("..", wl.api);
userInfo = data;
DataContext = this;
}
gridview.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
private void MenuFlyoutItemLogout_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
var iniHelper = new IniHelper(IniHelper.FILE.User);
iniHelper.Clean();
iniHelper.Save();
nulllogin();
}
}