From ba41c6dad48e2b5e081d44f3066ab082f0934987 Mon Sep 17 00:00:00 2001 From: xiangbing Date: Mon, 27 Jan 2025 22:07:25 +0800 Subject: [PATCH] =?UTF-8?q?update=20=20=E6=B7=BB=E5=8A=A0=E7=99=BB?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Api/Base/LoginResponse.cs | 125 +++++++++++++++++++++++++++++++ Common/Api/Base/SystemApi.cs | 15 ++++ Common/Tools/HttpUtils.cs | 99 ++++++++++++++++++++++++ VideoConcat.csproj | 1 + Views/MainWindow.xaml.cs | 22 +++--- 5 files changed, 253 insertions(+), 9 deletions(-) create mode 100644 Common/Api/Base/LoginResponse.cs create mode 100644 Common/Api/Base/SystemApi.cs create mode 100644 Common/Tools/HttpUtils.cs diff --git a/Common/Api/Base/LoginResponse.cs b/Common/Api/Base/LoginResponse.cs new file mode 100644 index 0000000..edf77b1 --- /dev/null +++ b/Common/Api/Base/LoginResponse.cs @@ -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 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(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 } + }, + }; + } +} diff --git a/Common/Api/Base/SystemApi.cs b/Common/Api/Base/SystemApi.cs new file mode 100644 index 0000000..96a0729 --- /dev/null +++ b/Common/Api/Base/SystemApi.cs @@ -0,0 +1,15 @@ + +using VideoConcat.Common.Tools; + +namespace VideoConcat.Common.Api.Base +{ + public class SystemApi + { + public static async Task LoginAsync(string username, string password) + { + HttpService Http = new(); + ApiResponse res = await Http.PostAsync("/api/base/login", new { Username = username, Password = password, Platform = "pc" }); + return res.Data; + } + } +} diff --git a/Common/Tools/HttpUtils.cs b/Common/Tools/HttpUtils.cs new file mode 100644 index 0000000..b15d013 --- /dev/null +++ b/Common/Tools/HttpUtils.cs @@ -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> PostAsync(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.CreateAsync(response); + + if (!apiResponse.IsSuccess) + { + LogUtils.Error($"PostAsync<{typeof(T)}> failed: {apiResponse.Code} {apiResponse.Msg}"); + } + + return apiResponse; + } + catch (TaskCanceledException) + { + return new ApiResponse + { + IsSuccess = false, + Msg = "请求超时", + Code = 408 + }; + } + catch (Exception ex) + { + return new ApiResponse + { + IsSuccess = false, + Msg = ex.Message, + Code = 500 + }; + } + } + } + + public class ApiResponse + { + 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> CreateAsync(HttpResponseMessage response) + { + var result = new ApiResponse + { + IsSuccess = response.IsSuccessStatusCode, + Code = (int)response.StatusCode, + RawContent = await response.Content.ReadAsStringAsync() + }; + + try + { + if (result.IsSuccess) + { + result.Data = JsonSerializer.Deserialize(result.RawContent); + } + else + { + result.Msg = result.RawContent; // 或解析错误结构 + } + } + catch (JsonException ex) + { + result.IsSuccess = false; + result.Msg = $"JSON解析失败: {ex.Message}"; + } + + return result; + } + } +} diff --git a/VideoConcat.csproj b/VideoConcat.csproj index 2e9fd1f..bfd84cf 100644 --- a/VideoConcat.csproj +++ b/VideoConcat.csproj @@ -16,6 +16,7 @@ + diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index 723871c..11c31dc 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -1,4 +1,6 @@ using System.Windows; +using VideoConcat.Common.Api.Base; +using VideoConcat.Common.Tools; namespace VideoConcat.Views { @@ -17,20 +19,22 @@ namespace VideoConcat.Views 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(); Password.Clear(); + WPFDevelopers.Controls.MessageBox.Show("请输入用户名或者密码!"); + return; } - + UserLoginResponse res =await SystemApi.LoginAsync(_userName, _password); + Console.WriteLine(res); } } + }