refactor: SimApiRequestDelegateFactory/SimApiResultWriter 从 extensions 移到 helpers
- 包 simapi.extensions → simapi.helpers - 新增 interfaces/IBindRequestContext 接口切断 helpers↔controllers 循环依赖 (工厂只依赖接口,不再依赖 SimApiBaseController) - SimApiAuth 注解类与 helpers.SimApiAuth 同名冲突,用 import 别名 SimApiAuthAttribute 解决
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.helpers
|
||||
|
||||
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