using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using NCC.Common.Core.Manager; using NCC.Dependency; using NCC.DynamicApiController; namespace NCC.Extend.LqTest { /// /// 测试服务(用于验证接口可用性及自动委派流程) /// [ApiDescriptionSettings(Tag = "绿纤测试服务", Name = "LqTest", Order = 200)] [Route("api/Extend/[controller]")] public class LqTestService : IDynamicApiController, ITransient { private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public LqTestService(IUserManager userManager) { _userManager = userManager; } /// /// 健康检查 / 连通性测试 /// /// /// 用于验证 API 服务是否正常运行,无需登录即可调用(若需鉴权可移除 AllowAnonymous) /// /// 示例请求: /// ```http /// GET /api/Extend/LqTest/Ping /// ``` /// /// 参数说明:无 /// /// 服务状态信息 /// 成功返回服务状态 /// 服务器错误 [HttpGet("Ping")] [AllowAnonymous] public Task Ping() { var result = new { success = true, message = "绿纤 ERP 测试接口正常", timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), service = "LqTestService" }; return Task.FromResult(result); } /// /// Hello World 测试接口 /// /// /// 直接返回 hello world 字符串,用于简单连通性测试 /// /// hello world /// 成功返回 [HttpGet("HelloWorld")] [AllowAnonymous] public Task HelloWorld() { return Task.FromResult(new { message = "hello world" }); } /// /// Echo 回显测试接口 /// /// /// 返回服务器时间与随机数,用于验证接口连通性 /// /// 示例请求: /// ```http /// GET /api/Extend/LqTest/Echo /// ``` /// /// 服务器时间与随机数 /// 成功返回 [HttpGet("Echo")] [AllowAnonymous] public Task Echo() { var random = new Random(); var result = new { message = "echo ok", serverTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), randomValue = random.Next(1, 10000) }; return Task.FromResult(result); } /// /// 带鉴权的测试接口(需登录) /// /// /// 验证当前用户登录态,返回用户相关信息 /// /// 示例请求: /// ```http /// GET /api/Extend/LqTest/WhoAmI /// Authorization: Bearer {token} /// ``` /// /// 当前用户信息 /// 成功返回用户信息 /// 未登录或 token 无效 [HttpGet("WhoAmI")] public async Task WhoAmI() { var userId = _userManager.UserId; var userInfo = await _userManager.GetUserInfo(); var result = new { success = true, userId, userName = userInfo?.userName ?? "未知", account = userInfo?.userAccount ?? "未知", timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; return result; } } }