update 添加登录

This commit is contained in:
xiangbing 2025-01-27 22:07:25 +08:00
parent d3fc2368c8
commit ba41c6dad4
5 changed files with 253 additions and 9 deletions

View File

@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace VideoConcat.Common.Api.Base
{
public partial class UserLoginResponse
{
[JsonProperty("user")]
public User User { get; set; }
[JsonProperty("token")]
public string Token { get; set; }
[JsonProperty("expiresAt")]
public long ExpiresAt { get; set; }
}
public partial class User
{
[JsonProperty("ID")]
public long Id { get; set; }
[JsonProperty("CreatedAt")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("UpdatedAt")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("uuid")]
public Guid Uuid { get; set; }
[JsonProperty("userName")]
public string UserName { get; set; }
[JsonProperty("nickName")]
public string NickName { get; set; }
[JsonProperty("headerImg")]
public Uri HeaderImg { get; set; }
[JsonProperty("authorityId")]
public long AuthorityId { get; set; }
[JsonProperty("authority")]
public Authority Authority { get; set; }
[JsonProperty("authorities")]
public List<Authority> Authorities { get; set; }
[JsonProperty("phone")]
public string Phone { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("enable")]
public long Enable { get; set; }
[JsonProperty("originSetting")]
public object OriginSetting { get; set; }
}
public partial class Authority
{
[JsonProperty("CreatedAt")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("UpdatedAt")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("DeletedAt")]
public object DeletedAt { get; set; }
[JsonProperty("authorityId")]
public long AuthorityId { get; set; }
[JsonProperty("authorityName")]
public string AuthorityName { get; set; }
[JsonProperty("parentId")]
public long ParentId { get; set; }
[JsonProperty("dataAuthorityId")]
public object DataAuthorityId { get; set; }
[JsonProperty("children")]
public object Children { get; set; }
[JsonProperty("menus")]
public object Menus { get; set; }
[JsonProperty("defaultRouter")]
public string DefaultRouter { get; set; }
}
public partial class UserLoginResponse
{
public static UserLoginResponse FromJson(string json)
{
return JsonConvert.DeserializeObject<UserLoginResponse>(json, Converter.Settings);
}
}
public static class Serialize
{
public static string ToJson(this UserLoginResponse self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}

View File

@ -0,0 +1,15 @@

using VideoConcat.Common.Tools;
namespace VideoConcat.Common.Api.Base
{
public class SystemApi
{
public static async Task<UserLoginResponse> LoginAsync<UserLoginResponse>(string username, string password)
{
HttpService Http = new();
ApiResponse<UserLoginResponse> res = await Http.PostAsync<UserLoginResponse>("/api/base/login", new { Username = username, Password = password, Platform = "pc" });
return res.Data;
}
}
}

99
Common/Tools/HttpUtils.cs Normal file
View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace VideoConcat.Common.Tools
{
public class HttpService
{
private readonly HttpClient _httpClient;
public HttpService()
{
_httpClient = new HttpClient
{
//BaseAddress = new Uri("https://admin.xiangbing.vip"),
BaseAddress = new Uri("http://127.0.0.1:8080"),
};
}
public async Task<ApiResponse<T>> PostAsync<T>(string url, object data)
{
try
{
var json = JsonSerializer.Serialize(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync(url, content);
var apiResponse = await ApiResponse<T>.CreateAsync(response);
if (!apiResponse.IsSuccess)
{
LogUtils.Error($"PostAsync<{typeof(T)}> failed: {apiResponse.Code} {apiResponse.Msg}");
}
return apiResponse;
}
catch (TaskCanceledException)
{
return new ApiResponse<T>
{
IsSuccess = false,
Msg = "请求超时",
Code = 408
};
}
catch (Exception ex)
{
return new ApiResponse<T>
{
IsSuccess = false,
Msg = ex.Message,
Code = 500
};
}
}
}
public class ApiResponse<T>
{
public bool IsSuccess { get; set; }
public int Code { get; set; }
public T Data { get; set; }
public string Msg { get; set; }
public string RawContent { get; set; }
public static async Task<ApiResponse<T>> CreateAsync(HttpResponseMessage response)
{
var result = new ApiResponse<T>
{
IsSuccess = response.IsSuccessStatusCode,
Code = (int)response.StatusCode,
RawContent = await response.Content.ReadAsStringAsync()
};
try
{
if (result.IsSuccess)
{
result.Data = JsonSerializer.Deserialize<T>(result.RawContent);
}
else
{
result.Msg = result.RawContent; // 或解析错误结构
}
}
catch (JsonException ex)
{
result.IsSuccess = false;
result.Msg = $"JSON解析失败: {ex.Message}";
}
return result;
}
}
}

View File

@ -16,6 +16,7 @@
<PackageReference Include="log4net" Version="3.0.2" /> <PackageReference Include="log4net" Version="3.0.2" />
<PackageReference Include="MaterialDesignXaml.DialogsHelper" Version="1.0.4" /> <PackageReference Include="MaterialDesignXaml.DialogsHelper" Version="1.0.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.IO.Pipelines" Version="9.0.0" /> <PackageReference Include="System.IO.Pipelines" Version="9.0.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="9.0.0" /> <PackageReference Include="System.Text.Encodings.Web" Version="9.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" /> <PackageReference Include="System.Text.Json" Version="9.0.0" />

View File

@ -1,4 +1,6 @@
using System.Windows; using System.Windows;
using VideoConcat.Common.Api.Base;
using VideoConcat.Common.Tools;
namespace VideoConcat.Views namespace VideoConcat.Views
{ {
@ -17,20 +19,22 @@ namespace VideoConcat.Views
Close(); Close();
} }
private void BtnLogin_Click(object sender, RoutedEventArgs e) private async void BtnLogin_Click(object sender, RoutedEventArgs e)
{ {
if (Username.Text == "admin" && Password.Password == "mA%4ZRKve_kA")
string _userName = Username.Text;
string _password = Password.Password;
if (string.IsNullOrEmpty(_userName) || string.IsNullOrEmpty(_password))
{ {
new Video().Show();
Close();
}
else
{
System.Windows.MessageBox.Show("用户名或者密码错误!");
Username.Clear(); Username.Clear();
Password.Clear(); Password.Clear();
WPFDevelopers.Controls.MessageBox.Show("请输入用户名或者密码!");
return;
}
UserLoginResponse res =await SystemApi.LoginAsync<UserLoginResponse>(_userName, _password);
Console.WriteLine(res);
}
} }
} }
}
}