diff --git a/Common/Api/Base/LoginResponse.cs b/Common/Api/Base/LoginResponse.cs index edf77b1..309238d 100644 --- a/Common/Api/Base/LoginResponse.cs +++ b/Common/Api/Base/LoginResponse.cs @@ -4,9 +4,11 @@ using System.Collections.Generic; using System.Globalization; using Newtonsoft.Json; using Newtonsoft.Json.Converters; +using VideoConcat.Common.Api.Common; namespace VideoConcat.Common.Api.Base { + public partial class UserLoginResponse { [JsonProperty("user")] diff --git a/Common/Api/Base/SystemApi.cs b/Common/Api/Base/SystemApi.cs index 96a0729..aa5786d 100644 --- a/Common/Api/Base/SystemApi.cs +++ b/Common/Api/Base/SystemApi.cs @@ -5,11 +5,12 @@ namespace VideoConcat.Common.Api.Base { public class SystemApi { - public static async Task LoginAsync(string username, string password) + public static async Task> LoginAsync(string username, string password) { - HttpService Http = new(); + HttpHelper Http = new(); ApiResponse res = await Http.PostAsync("/api/base/login", new { Username = username, Password = password, Platform = "pc" }); - return res.Data; + + return res; } } } diff --git a/Common/Api/Common/Common.cs b/Common/Api/Common/Common.cs new file mode 100644 index 0000000..b28ba0f --- /dev/null +++ b/Common/Api/Common/Common.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VideoConcat.Common.Api.Common +{ + public class CommonResponese + { + + [JsonProperty("code")] + public int Code { get; set; } + + [JsonProperty("data")] + public T Data { get; set; } + + [JsonProperty("msg")] + public string Msg { get; set; } + } +} diff --git a/Common/Tools/HttpUtils.cs b/Common/Tools/HttpUtils.cs index b15d013..de1e207 100644 --- a/Common/Tools/HttpUtils.cs +++ b/Common/Tools/HttpUtils.cs @@ -1,59 +1,71 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Net.Http; +using System.Net.Http.Headers; using System.Text; -using System.Text.Json; using System.Threading.Tasks; +using Newtonsoft.Json; namespace VideoConcat.Common.Tools { - public class HttpService + + + // HTTP 请求封装类 + public class HttpHelper { private readonly HttpClient _httpClient; - public HttpService() + public HttpHelper() { + _httpClient = new HttpClient { - //BaseAddress = new Uri("https://admin.xiangbing.vip"), - BaseAddress = new Uri("http://127.0.0.1:8080"), + BaseAddress = new Uri("https://admin.xiangbing.vip") }; + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } - public async Task> PostAsync(string url, object data) + // 发送 GET 请求 + public async Task> GetAsync(string url) { 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 - }; + var response = await _httpClient.GetAsync(url); + response.EnsureSuccessStatusCode(); + var content = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject>(content); } catch (Exception ex) { return new ApiResponse { - IsSuccess = false, - Msg = ex.Message, - Code = 500 + Code = 500, + Msg = $"请求出错: {ex.Message}", + Data = default + }; + } + } + + // 发送 POST 请求 + public async Task> PostAsync(string url, object data) + { + try + { + var json = JsonConvert.SerializeObject(data); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + var response = await _httpClient.PostAsync(url, content); + response.EnsureSuccessStatusCode(); + var responseContent = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject>(responseContent); + } + catch (Exception ex) + { + return new ApiResponse + { + Code = 500, + Msg = $"请求出错: {ex.Message}", + Data = default }; } } @@ -61,39 +73,8 @@ namespace VideoConcat.Common.Tools 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.sln b/VideoConcat.sln index 9ef47fb..81abae2 100644 --- a/VideoConcat.sln +++ b/VideoConcat.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VideoConcat", "VideoConcat.csproj", "{2FF5691C-3184-4B68-944B-C704E64C4E4E}" EndProject -Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "视频拼接", "..\视频拼接\视频拼接.vdproj", "{1444D25A-0A52-4C6A-8FF7-3D1002EA691F}" +Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "视频", "..\视频\视频.vdproj", "{6253EBA0-190A-4A7D-AC55-954172807A46}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,8 +17,8 @@ Global {2FF5691C-3184-4B68-944B-C704E64C4E4E}.Debug|Any CPU.Build.0 = Debug|Any CPU {2FF5691C-3184-4B68-944B-C704E64C4E4E}.Release|Any CPU.ActiveCfg = Release|Any CPU {2FF5691C-3184-4B68-944B-C704E64C4E4E}.Release|Any CPU.Build.0 = Release|Any CPU - {1444D25A-0A52-4C6A-8FF7-3D1002EA691F}.Debug|Any CPU.ActiveCfg = Debug - {1444D25A-0A52-4C6A-8FF7-3D1002EA691F}.Release|Any CPU.ActiveCfg = Release + {6253EBA0-190A-4A7D-AC55-954172807A46}.Debug|Any CPU.ActiveCfg = Debug + {6253EBA0-190A-4A7D-AC55-954172807A46}.Release|Any CPU.ActiveCfg = Release EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index ce98733..b0f6e20 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -24,7 +24,11 @@ - + + + + + diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index 11c31dc..95062c8 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -32,8 +32,16 @@ namespace VideoConcat.Views WPFDevelopers.Controls.MessageBox.Show("请输入用户名或者密码!"); return; } - UserLoginResponse res =await SystemApi.LoginAsync(_userName, _password); - Console.WriteLine(res); + ApiResponse res = await SystemApi.LoginAsync(_userName, _password); + if (res.Code !=0) + { + WPFDevelopers.Controls.MessageBox.Show(res.Msg); + } + else + { + new Video().Show(); + Close(); + } } } diff --git a/视频.ico b/视频.ico index 0079b6d..8922c11 100644 Binary files a/视频.ico and b/视频.ico differ