76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Windows;
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
using MaterialDesignThemes.Wpf;
|
|
|
|
namespace Szmedi.AIScriptRunner.ViewModels
|
|
{
|
|
public partial class LoginViewModel : ObservableObject
|
|
{
|
|
[ObservableProperty]
|
|
private string? version;
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(LoginCommand))]
|
|
private string? accountName = string.Empty;
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(LoginCommand))]
|
|
private string? password = string.Empty;
|
|
[ObservableProperty]
|
|
private bool isAuthenticated;
|
|
[ObservableProperty]
|
|
private string? userName;
|
|
[ObservableProperty]
|
|
private string? companyName;
|
|
[ObservableProperty]
|
|
private string? phone;
|
|
|
|
partial void OnIsAuthenticatedChanged(bool oldValue, bool newValue)
|
|
{
|
|
GlobalVariables.IsAuthenticated = newValue;
|
|
}
|
|
|
|
private bool CanLogin()
|
|
{
|
|
return !string.IsNullOrEmpty(AccountName) && !string.IsNullOrEmpty(Password);
|
|
}
|
|
|
|
[RelayCommand(CanExecute = nameof(CanLogin))]
|
|
private async Task LoginAsync()
|
|
{
|
|
var response = await GlobalVariables.AuthenticationService.LoginAsync(AccountName, Password);
|
|
if (response?.Successful == true)
|
|
{
|
|
IsAuthenticated = true;
|
|
AccountName = $"{response.Data?.UserInfoVO.AccountName}";
|
|
UserName = $"{response.Data?.UserInfoVO.NameCn}";
|
|
Phone = $"{response.Data?.UserInfoVO.Phone}";
|
|
CompanyName = $"{response.Data?.UserInfoVO.CompanyNameCn}";
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(response.Message, "登录失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
//注销
|
|
[RelayCommand]
|
|
private void Logout()
|
|
{
|
|
IsAuthenticated = false;
|
|
}
|
|
//注销
|
|
[RelayCommand]
|
|
private void Close(Window window)
|
|
{
|
|
if (window != null)
|
|
{
|
|
window.Hide();
|
|
}
|
|
}
|
|
}
|
|
} |