refactor: 提取统一响应封装 SimApiResultWriter
- dispatchResult 复用 SimApiResultWriter(IActionResult/SimApiBaseResponse/String/Unit/其他 五分支) - @SimApiAuth 鉴权执行(checkSimApiAuth:401/类型403/checker 遍历) - @OriginResponse 跳过封装原样输出
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
||||
* 移植自 C# 项目 SimApi(E:\simcu\simapi-net),遵循 MIT 许可证。
|
||||
*/
|
||||
|
||||
package simapi.extensions
|
||||
|
||||
import soulsoft_web_http.*
|
||||
import soulsoft_web_mvc.core.*
|
||||
import simapi.communications.*
|
||||
|
||||
/**
|
||||
* 统一响应封装工具(对齐 C# SimApiResponseFilter 的包装分支)。
|
||||
* 供 SimApiRequestDelegateFactory 与内置路由委托复用:
|
||||
* - SimApiBaseResponse(含子类)→ 原样输出
|
||||
* - String → SimApiResponse<String>(data 为字符串)
|
||||
* - Unit(void)→ SimApiBaseResponse()({code:200, message:成功})
|
||||
* - 其他对象(DTO/数组/动态结构)→ SimApiDataResponse(data 内嵌为对象)
|
||||
*/
|
||||
public class SimApiResultWriter {
|
||||
private init() {}
|
||||
|
||||
public static func write(context: HttpContext, actionResult: Any): Unit {
|
||||
if (let result: IActionResult <- actionResult) {
|
||||
// 显式返回 IActionResult(如 ContentResult)→ 原样
|
||||
result.invoke(context)
|
||||
} else if (let result: SimApiBaseResponse <- actionResult) {
|
||||
// 已是 SimApiBaseResponse(含子类)→ 原样输出
|
||||
ObjectResult<Any>(result).invoke(context)
|
||||
} else if (let result: String <- actionResult) {
|
||||
// String → SimApiResponse<String>(data 为字符串)
|
||||
ObjectResult<Any>(SimApiResponse<String>(result)).invoke(context)
|
||||
} else if (let result: Unit <- actionResult) {
|
||||
// void/无返回 → SimApiBaseResponse()({code:200, message:成功})
|
||||
context.response.writeAsJson(SimApiBaseResponse())
|
||||
} else {
|
||||
// 其他对象(DTO/数组/动态结构)→ SimApiDataResponse
|
||||
// data 由 SimApiDataResponse.serializeObject 内嵌为对象
|
||||
ObjectResult<Any>(SimApiDataResponse(actionResult)).invoke(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user