43 lines
1.8 KiB
Plaintext
43 lines
1.8 KiB
Plaintext
/*
|
||||
|
|
* 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)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|