This commit is contained in:
xiangbing 2025-02-08 18:43:27 +08:00
parent ba41c6dad4
commit 0786589239
8 changed files with 89 additions and 71 deletions

View File

@ -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")]

View File

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

View File

@ -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<T>
{
[JsonProperty("code")]
public int Code { get; set; }
[JsonProperty("data")]
public T Data { get; set; }
[JsonProperty("msg")]
public string Msg { get; set; }
}
}

View File

@ -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<ApiResponse<T>> PostAsync<T>(string url, object data)
// 发送 GET 请求
public async Task<ApiResponse<T>> GetAsync<T>(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<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
};
var response = await _httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ApiResponse<T>>(content);
}
catch (Exception ex)
{
return new ApiResponse<T>
{
IsSuccess = false,
Msg = ex.Message,
Code = 500
Code = 500,
Msg = $"请求出错: {ex.Message}",
Data = default
};
}
}
// 发送 POST 请求
public async Task<ApiResponse<T>> PostAsync<T>(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<ApiResponse<T>>(responseContent);
}
catch (Exception ex)
{
return new ApiResponse<T>
{
Code = 500,
Msg = $"请求出错: {ex.Message}",
Data = default
};
}
}
@ -61,39 +73,8 @@ namespace VideoConcat.Common.Tools
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

@ -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

View File

@ -24,7 +24,11 @@
</ResourceDictionary>
</Window.Resources>
<Grid>
<Border>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="39*"/>
<ColumnDefinition Width="461*"/>
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="2">
<StackPanel Orientation="Horizontal">
<StackPanel Width="500">
<StackPanel>

View File

@ -32,8 +32,16 @@ namespace VideoConcat.Views
WPFDevelopers.Controls.MessageBox.Show("请输入用户名或者密码!");
return;
}
UserLoginResponse res =await SystemApi.LoginAsync<UserLoginResponse>(_userName, _password);
Console.WriteLine(res);
ApiResponse<UserLoginResponse> res = await SystemApi.LoginAsync<UserLoginResponse>(_userName, _password);
if (res.Code !=0)
{
WPFDevelopers.Controls.MessageBox.Show(res.Msg);
}
else
{
new Video().Show();
Close();
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 876 KiB