From d5dafd2bcfe7e8c138609a70830f531649d07645 Mon Sep 17 00:00:00 2001 From: "Bob.Song" Date: Fri, 27 Mar 2026 17:55:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hotfix/Api/Controllers/AuthController.cs | 40 +++++++++ .../GameHttpApplicationHandler.cs | 2 +- .../{ => Handler}/GameHttpServicesHandler.cs | 15 ++-- Hotfix/Api/Helper/DingTalkHelper.cs | 63 +++++++++++++ Hotfix/Api/PlayerController.cs | 89 ------------------- Hotfix/Api/PlayerService.cs | 6 -- Hotfix/HTTPHandler/HelloController.cs | 31 ------- Hotfix/HTTPHandler/ProductsController.cs | 21 ----- Hotfix/HTTPHandler/UsersController.cs | 45 ---------- Hotfix/Hotfix.csproj | 1 + Server.sln.DotSettings.user | 1 + 11 files changed, 114 insertions(+), 200 deletions(-) create mode 100644 Hotfix/Api/Controllers/AuthController.cs rename Hotfix/Api/{ => Handler}/GameHttpApplicationHandler.cs (96%) rename Hotfix/Api/{ => Handler}/GameHttpServicesHandler.cs (85%) create mode 100644 Hotfix/Api/Helper/DingTalkHelper.cs delete mode 100644 Hotfix/Api/PlayerController.cs delete mode 100644 Hotfix/Api/PlayerService.cs delete mode 100644 Hotfix/HTTPHandler/HelloController.cs delete mode 100644 Hotfix/HTTPHandler/ProductsController.cs delete mode 100644 Hotfix/HTTPHandler/UsersController.cs diff --git a/Hotfix/Api/Controllers/AuthController.cs b/Hotfix/Api/Controllers/AuthController.cs new file mode 100644 index 0000000..5c0f5c2 --- /dev/null +++ b/Hotfix/Api/Controllers/AuthController.cs @@ -0,0 +1,40 @@ +using System.Threading; +using Fantasy; +using Fantasy.Async; +using Fantasy.Network.HTTP; +using Microsoft.AspNetCore.Mvc; + +namespace NBF; + +[ApiController] +[Route("api/[controller]")] +[ServiceFilter(typeof(SceneContextFilter))] +public class AuthController : ControllerBase +{ + private readonly Scene _scene; + + /// + /// 构造函数依赖注入 + /// + /// + public AuthController(Scene scene) + { + _scene = scene; + } + + [HttpGet("login")] + public async FTask Login() + { + await DingTalkHelper.SendCAPTCHA("123456"); + await FTask.CompletedTask; + return Ok($"Hello from the Fantasy controller! _scene.SceneType:{_scene.SceneType} _scene.SceneType:{_scene.SceneConfigId}"); + } + + [HttpGet("code")] + public async FTask SendCode() + { + await DingTalkHelper.SendCAPTCHA("123456"); + await FTask.CompletedTask; + return Ok($"Hello from the Fantasy controller! _scene.SceneType:{_scene.SceneType} _scene.SceneType:{_scene.SceneConfigId}"); + } +} \ No newline at end of file diff --git a/Hotfix/Api/GameHttpApplicationHandler.cs b/Hotfix/Api/Handler/GameHttpApplicationHandler.cs similarity index 96% rename from Hotfix/Api/GameHttpApplicationHandler.cs rename to Hotfix/Api/Handler/GameHttpApplicationHandler.cs index 478d8e9..0f30702 100644 --- a/Hotfix/Api/GameHttpApplicationHandler.cs +++ b/Hotfix/Api/Handler/GameHttpApplicationHandler.cs @@ -53,7 +53,7 @@ namespace NBF // 5. 自定义响应头 app.Use(async (context, next) => { - context.Response.Headers.Add("X-Game-Server", "Fantasy"); + context.Response.Headers.Add("X-Game-Server", "NBF"); context.Response.Headers.Add("X-Server-Version", "1.0.0"); await next.Invoke(); }); diff --git a/Hotfix/Api/GameHttpServicesHandler.cs b/Hotfix/Api/Handler/GameHttpServicesHandler.cs similarity index 85% rename from Hotfix/Api/GameHttpServicesHandler.cs rename to Hotfix/Api/Handler/GameHttpServicesHandler.cs index 2603b45..c81f46f 100644 --- a/Hotfix/Api/GameHttpServicesHandler.cs +++ b/Hotfix/Api/Handler/GameHttpServicesHandler.cs @@ -7,6 +7,7 @@ using Microsoft.IdentityModel.Tokens; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +using Fantasy; namespace NBF; @@ -27,8 +28,8 @@ public class GameHttpServicesHandler : AsyncEventSystem // 2. 添加全局过滤器 self.MvcBuilder.AddMvcOptions(options => { - options.Filters.Add(); - options.Filters.Add(); + // options.Filters.Add(); + // options.Filters.Add(); }); // 3. 配置 JWT 认证 @@ -76,11 +77,11 @@ public class GameHttpServicesHandler : AsyncEventSystem }); }); - // 6. 注册游戏服务 - self.Builder.Services.AddSingleton(); - self.Builder.Services.AddSingleton(); - self.Builder.Services.AddScoped(); - self.Builder.Services.AddScoped(); + // // 6. 注册游戏服务 + // self.Builder.Services.AddSingleton(); + // self.Builder.Services.AddSingleton(); + // self.Builder.Services.AddScoped(); + // self.Builder.Services.AddScoped(); Log.Info($"[HTTP] 游戏服务配置完成: Scene {self.Scene.SceneConfigId}"); diff --git a/Hotfix/Api/Helper/DingTalkHelper.cs b/Hotfix/Api/Helper/DingTalkHelper.cs new file mode 100644 index 0000000..0c44523 --- /dev/null +++ b/Hotfix/Api/Helper/DingTalkHelper.cs @@ -0,0 +1,63 @@ +using System.Text; +using Fantasy; +using Newtonsoft.Json; + +namespace NBF; + +public static class DingTalkHelper +{ + private static readonly HttpClient _httpClient = new HttpClient(); + + public static async Task SendCAPTCHA(string code) + { + DingTalkMarkdownData dingTalkTestData = new DingTalkMarkdownData + { + markdown = new DingTalkMarkdownItem + { + title = "NB验证码", + text = $"验证码:{code}" + } + }; + await SendMessageAsync("23457f9c93ac0ae909e1cbf8bcfeb7e0573968ac2d4c4b2c3a961b2f0c9247cb", dingTalkTestData); + } + + /// + /// 通用消息发送方法 + /// + /// + /// 消息对象 + /// + private static async Task SendMessageAsync(string token, DingTalkMarkdownData message) + { + try + { + var json = JsonConvert.SerializeObject(message); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + var response = + await _httpClient.PostAsync($"https://oapi.dingtalk.com/robot/send?access_token={token}", content); + var responseContent = await response.Content.ReadAsStringAsync(); + Log.Info($"钉钉={responseContent}"); + // 检查响应是否成功 + return response.IsSuccessStatusCode; + } + catch (Exception ex) + { + Console.WriteLine($"发送飞书消息失败: {ex.Message}"); + return false; + } + } + + [Serializable] + public class DingTalkMarkdownData + { + public string msgtype { get; set; } = "markdown"; + public DingTalkMarkdownItem markdown { get; set; } + } + + [Serializable] + public class DingTalkMarkdownItem + { + public string title { get; set; } + public string text { get; set; } + } +} \ No newline at end of file diff --git a/Hotfix/Api/PlayerController.cs b/Hotfix/Api/PlayerController.cs deleted file mode 100644 index d61f69b..0000000 --- a/Hotfix/Api/PlayerController.cs +++ /dev/null @@ -1,89 +0,0 @@ -using Fantasy; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace NBF.Controllers -{ - [ApiController] - [Route("api/[controller]")] - public class PlayerController : ControllerBase - { - private readonly Scene _scene; - private readonly IPlayerService _playerService; - - public PlayerController(Scene scene, IPlayerService playerService) - { - _scene = scene; - _playerService = playerService; - } - - /// - /// 获取玩家信息(需要认证) - /// - [HttpGet("{playerId}")] - [Authorize(Policy = "Player")] - public async Task GetPlayer(long playerId) - { - var player = await _playerService.GetPlayerAsync(playerId); - if (player == null) - { - return NotFound(new { error = "Player not found" }); - } - - return Ok(new - { - playerId = player.Id, - name = player.Name, - level = player.Level, - exp = player.Exp - }); - } - - /// - /// 登录接口(无需认证) - /// - [HttpPost("login")] - [AllowAnonymous] - public async Task Login([FromBody] LoginRequest request) - { - var (success, token, playerId) = await _playerService.LoginAsync( - request.Username, request.Password); - - if (!success) - { - return Unauthorized(new { error = "Invalid credentials" }); - } - - return Ok(new - { - token = token, - playerId = playerId, - expiresIn = 3600 - }); - } - - /// - /// 管理员接口(需要 Admin 角色) - /// - [HttpPost("ban/{playerId}")] - [Authorize(Policy = "Admin")] - public async Task BanPlayer(long playerId, [FromBody] BanRequest request) - { - await _playerService.BanPlayerAsync(playerId, request.Reason, request.Duration); - - return Ok(new { message = "Player banned successfully" }); - } - } - - public class LoginRequest - { - public string Username { get; set; } - public string Password { get; set; } - } - - public class BanRequest - { - public string Reason { get; set; } - public int Duration { get; set; } // 分钟 - } -} \ No newline at end of file diff --git a/Hotfix/Api/PlayerService.cs b/Hotfix/Api/PlayerService.cs deleted file mode 100644 index 5bfa052..0000000 --- a/Hotfix/Api/PlayerService.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace NBF; - -public class PlayerService : IPlayerService -{ - -} \ No newline at end of file diff --git a/Hotfix/HTTPHandler/HelloController.cs b/Hotfix/HTTPHandler/HelloController.cs deleted file mode 100644 index 1fd9178..0000000 --- a/Hotfix/HTTPHandler/HelloController.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Threading; -using Fantasy.Async; -using Fantasy.Entitas; -using Fantasy.Network.HTTP; -using Microsoft.AspNetCore.Mvc; - -namespace Fantasy; - -[ApiController] -[Route("api/[controller]")] -[ServiceFilter(typeof(SceneContextFilter))] -public class HelloController : ControllerBase -{ - private readonly Scene _scene; - - /// - /// 构造函数依赖注入 - /// - /// - public HelloController(Scene scene) - { - _scene = scene; - } - - [HttpGet("greet")] - public async FTask Greet() - { - Log.Debug($"HelloController Thread.CurrentThread.ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}"); - return Ok($"Hello from the Fantasy controller! _scene.SceneType:{_scene.SceneType} _scene.SceneType:{_scene.SceneConfigId}"); - } -} \ No newline at end of file diff --git a/Hotfix/HTTPHandler/ProductsController.cs b/Hotfix/HTTPHandler/ProductsController.cs deleted file mode 100644 index e28ed64..0000000 --- a/Hotfix/HTTPHandler/ProductsController.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace Fantasy; - -[ApiController] -[Route("api/[controller]")] -public class ProductsController : ControllerBase -{ - [HttpPost] - public IActionResult MiniGame ([FromBody] Product product) - { - // 假设已经保存产品数据 - return Ok("Product created successfully"); - } -} - -public class Product -{ - public int Id { get; set; } - public string Name { get; set; } -} \ No newline at end of file diff --git a/Hotfix/HTTPHandler/UsersController.cs b/Hotfix/HTTPHandler/UsersController.cs deleted file mode 100644 index b0f79c2..0000000 --- a/Hotfix/HTTPHandler/UsersController.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Threading; -using Microsoft.AspNetCore.Mvc; - -namespace Fantasy; - -[ApiController] -[Route("api/[controller]")] -public class UsersController : ControllerBase -{ - private readonly Scene _scene; - - /// - /// 构造函数依赖注入 - /// - /// - public UsersController(Scene scene) - { - _scene = scene; - } - - [HttpGet("{userId}")] - public IActionResult GetUser(int userId) - { - return Ok($"User ID: {userId}"); - } - - [HttpPost("register")] - public IActionResult RegisterUser([FromBody] HttpUser user) - { - return Ok("User registered successfully"); - } - - [HttpGet("greet")] - public IActionResult Greet() - { - Log.Debug($"HelloController Thread.CurrentThread.ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}"); - return Ok($"Hello from the Fantasy controller! _scene.SceneType:{_scene.SceneType} _scene.SceneType:{_scene.SceneConfigId}"); - } -} - -public class HttpUser -{ - public int Id { get; set; } - public string Name { get; set; } -} \ No newline at end of file diff --git a/Hotfix/Hotfix.csproj b/Hotfix/Hotfix.csproj index eeee4d2..724ece9 100644 --- a/Hotfix/Hotfix.csproj +++ b/Hotfix/Hotfix.csproj @@ -13,6 +13,7 @@ + diff --git a/Server.sln.DotSettings.user b/Server.sln.DotSettings.user index 7252f81..20588c1 100644 --- a/Server.sln.DotSettings.user +++ b/Server.sln.DotSettings.user @@ -21,4 +21,5 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded \ No newline at end of file