mirror of
https://github.com/rafi1212122/BLHX.Server.git
synced 2025-12-12 22:44:36 +01:00
accounts db & http sdk db impl
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using BLHX.Server.Sdk;
|
||||
using BLHX.Server.Common.Database;
|
||||
using BLHX.Server.Sdk;
|
||||
using BLHX.Server.SDK.Models;
|
||||
|
||||
namespace BLHX.Server.SDK.Controllers
|
||||
{
|
||||
@@ -6,7 +8,43 @@ namespace BLHX.Server.SDK.Controllers
|
||||
{
|
||||
public static void Register(WebApplication app)
|
||||
{
|
||||
app.MapPost("user/create", (HttpContext ctx, UserCreateRequest req) =>
|
||||
{
|
||||
var account = DBManager.AccountContext.Accounts.SingleOrDefault(x => x.DeviceId == req.DeviceId);
|
||||
|
||||
UserCreateResponse rsp = new() { Result = 0, IsNew = 0 };
|
||||
if (account is null)
|
||||
{
|
||||
account = new(req.DeviceId, Guid.NewGuid().ToString());
|
||||
DBManager.AccountContext.Add(account);
|
||||
DBManager.AccountContext.SaveChanges();
|
||||
rsp.IsNew = 1;
|
||||
}
|
||||
rsp.Uid = account.Uid;
|
||||
rsp.Token = account.Token;
|
||||
|
||||
return ctx.Response.WriteAsJsonAsync(rsp);
|
||||
});
|
||||
|
||||
app.MapPost("user/login", (HttpContext ctx, UserLoginRequest req) =>
|
||||
{
|
||||
var account = DBManager.AccountContext.Accounts.SingleOrDefault(x => x.Uid == req.Uid);
|
||||
|
||||
if (account is not null && account.Token == req.Token)
|
||||
{
|
||||
return ctx.Response.WriteAsJsonAsync(new UserLoginResponse()
|
||||
{
|
||||
AccessToken = account.Token,
|
||||
Birth = null,
|
||||
ChannelId = req.StoreId,
|
||||
CurrentTimestampMs = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
|
||||
KrKmcStatus = 2,
|
||||
Result = 0,
|
||||
TransCode = "NULL"
|
||||
});
|
||||
}
|
||||
return ctx.Response.WriteAsJsonAsync(new BaseResponse() { Result = 1 });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
10
BLHX.Server.SDK/Models/Base.cs
Normal file
10
BLHX.Server.SDK/Models/Base.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BLHX.Server.SDK.Models
|
||||
{
|
||||
class BaseResponse
|
||||
{
|
||||
[JsonPropertyName("result")]
|
||||
public int Result { get; set; }
|
||||
}
|
||||
}
|
||||
43
BLHX.Server.SDK/Models/User.cs
Normal file
43
BLHX.Server.SDK/Models/User.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using BLHX.Server.Sdk;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BLHX.Server.SDK.Models
|
||||
{
|
||||
record UserCreateRequest(string ChannelId, string DeviceId) : BindableFormRequest<UserCreateRequest>;
|
||||
record UserLoginRequest(uint Uid, string StoreId, string DeviceId, string Platform, string Token) : BindableFormRequest<UserLoginRequest>;
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
class UserLoginResponse : BaseResponse
|
||||
{
|
||||
[JsonPropertyName("accessToken")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonPropertyName("birth")]
|
||||
public dynamic? Birth { get; set; }
|
||||
|
||||
[JsonPropertyName("channelId")]
|
||||
public string ChannelId { get; set; }
|
||||
|
||||
[JsonPropertyName("current_timestamp_ms")]
|
||||
public long CurrentTimestampMs { get; set; }
|
||||
|
||||
[JsonPropertyName("kr_kmc_status")]
|
||||
public int KrKmcStatus { get; set; }
|
||||
|
||||
[JsonPropertyName("transcode")]
|
||||
public string TransCode { get; set; }
|
||||
}
|
||||
|
||||
class UserCreateResponse : BaseResponse
|
||||
{
|
||||
[JsonPropertyName("uid")]
|
||||
public uint Uid { get; set; }
|
||||
|
||||
[JsonPropertyName("token")]
|
||||
public string Token { get; set; }
|
||||
|
||||
[JsonPropertyName("isNew")]
|
||||
public int IsNew { get; set; }
|
||||
}
|
||||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
using BLHX.Server.Common.Utils;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BLHX.Server.Sdk
|
||||
{
|
||||
public class SDKServer
|
||||
{
|
||||
static readonly Logger c = new(nameof(SDKServer), ConsoleColor.Green);
|
||||
public static readonly Logger c = new(nameof(SDKServer), ConsoleColor.Green);
|
||||
static Task? runTask = null;
|
||||
|
||||
public static void Main(string[] args)
|
||||
@@ -13,11 +17,15 @@ namespace BLHX.Server.Sdk
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
// Disables default logger
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Services.AddMvcCore();
|
||||
builder.Services.AddAntiforgery();
|
||||
|
||||
var app = builder.Build();
|
||||
app.Urls.Add("http://*:80");
|
||||
app.Urls.Add("https://*:443");
|
||||
|
||||
app.UseStatusCodePages();
|
||||
app.UseAntiforgery();
|
||||
app.UseMiddleware<RequestLoggingMiddleware>();
|
||||
|
||||
foreach (Type controller in Assembly.GetExecutingAssembly().GetTypes().Where(p => typeof(IRegisterable).IsAssignableFrom(p) && !p.IsInterface))
|
||||
@@ -63,4 +71,46 @@ namespace BLHX.Server.Sdk
|
||||
{
|
||||
public abstract static void Register(WebApplication app);
|
||||
}
|
||||
|
||||
public static class BindingExtensions
|
||||
{
|
||||
public static async Task<T?> BindFromForm<T>(this HttpContext httpContext)
|
||||
{
|
||||
var serviceProvider = httpContext.RequestServices;
|
||||
var factory = serviceProvider.GetRequiredService<IModelBinderFactory>();
|
||||
var metadataProvider = serviceProvider.GetRequiredService<IModelMetadataProvider>();
|
||||
|
||||
var metadata = metadataProvider.GetMetadataForType(typeof(T));
|
||||
var modelBinder = factory.CreateBinder(new()
|
||||
{
|
||||
Metadata = metadata
|
||||
});
|
||||
|
||||
var context = new DefaultModelBindingContext
|
||||
{
|
||||
ModelMetadata = metadata,
|
||||
ModelName = string.Empty,
|
||||
ValueProvider = new FormValueProvider(
|
||||
BindingSource.Form,
|
||||
httpContext.Request.Form,
|
||||
CultureInfo.InvariantCulture
|
||||
),
|
||||
ActionContext = new ActionContext(
|
||||
httpContext,
|
||||
new RouteData(),
|
||||
new ActionDescriptor()),
|
||||
ModelState = new ModelStateDictionary()
|
||||
};
|
||||
await modelBinder.BindModelAsync(context);
|
||||
return (T?)context.Result.Model;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract record BindableFormRequest<TSelf>
|
||||
{
|
||||
public static async ValueTask<TSelf?> BindAsync(HttpContext httpContext, ParameterInfo parameter)
|
||||
{
|
||||
return await httpContext.BindFromForm<TSelf>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user