2026-08-16 12:46:15 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
|
|
|
|
|
* 移植自 C# 项目 SimApi(E:\simcu\simapi-net),遵循 MIT 许可证。
|
|
|
|
|
|
* Controllers/SimApiCommonController:通用内置路由。
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-08-18 04:23:07 +08:00
|
|
|
|
package simcu::simapi.controllers
|
2026-08-16 12:46:15 +08:00
|
|
|
|
|
|
|
|
|
|
import std.collection.*
|
|
|
|
|
|
import soulsoft_web_mvc.annotations.*
|
2026-08-18 04:23:07 +08:00
|
|
|
|
import simcu::simapi.communications.*
|
|
|
|
|
|
import simcu::simapi.configurations.*
|
|
|
|
|
|
import simcu::simapi.helpers.*
|
2026-08-16 12:46:15 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通用控制器:错误反馈、WebConfig、用户信息。
|
|
|
|
|
|
* 对齐 C# 的 SimApiCommonController。
|
|
|
|
|
|
* 控制器直接返回 SimApiBaseResponse / SimApiResponse<T>(对齐 C# SimApiBaseResponse<T>)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class SimApiCommonController <: SimApiBaseController {
|
|
|
|
|
|
private let _options: SimApiOptions
|
|
|
|
|
|
|
|
|
|
|
|
public init(options: SimApiOptions) {
|
|
|
|
|
|
this._options = options
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* GET /exception/{code}:错误反馈页面(始终注册)。
|
2026-08-16 23:44:20 +08:00
|
|
|
|
* 对齐 C# ExceptionHandler:抛 SimApiException,由异常中间件统一输出。
|
2026-08-16 12:46:15 +08:00
|
|
|
|
*/
|
|
|
|
|
|
@HttpGet["exception/{code}"]
|
2026-08-16 23:44:20 +08:00
|
|
|
|
public func exceptionHandler(@FromRoute code: Int64): Unit {
|
|
|
|
|
|
SimApiError.error(code: code)
|
2026-08-16 12:46:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST/GET /config:给前端的自定义信息(含版本)。
|
|
|
|
|
|
* 对齐 C# WebConfigRoute 默认值 /config(SimApiCommonController.WebConfig,[HttpPost, HttpGet] 无路径,
|
|
|
|
|
|
* 由 MapControllerRoute(pattern=WebConfigRoute) 注册;soulsoft 约定路由不支持 defaults,故用特性路由直接对齐路径)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
@HttpGet["/config"]
|
|
|
|
|
|
public func webConfig(): HashMap<String, Any> {
|
|
|
|
|
|
webConfigMap()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@HttpPost["/config"]
|
|
|
|
|
|
public func webConfigPost(): HashMap<String, Any> {
|
|
|
|
|
|
webConfigMap()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST /user/info:获取已登录用户信息(需登录)。
|
|
|
|
|
|
* 返回 SimApiLoginItem 由 SimApiResponseFilter 自动封装(data 为登录信息对象)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
@HttpPost["/user/info"]
|
|
|
|
|
|
public func userInfo(): SimApiLoginItem {
|
|
|
|
|
|
requireLogin()
|
|
|
|
|
|
loginInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private func versionsMap(): HashMap<String, Any> {
|
|
|
|
|
|
var map = HashMap<String, Any>()
|
|
|
|
|
|
map["SimApi"] = SimApiUtil.simApiVersion
|
|
|
|
|
|
map["App"] = SimApiUtil.appVersion
|
|
|
|
|
|
map
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private func webConfigMap(): HashMap<String, Any> {
|
|
|
|
|
|
var map = HashMap<String, Any>()
|
|
|
|
|
|
for ((key, value) in _options.webConfig) {
|
|
|
|
|
|
map[key] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_options.webConfigIncludeVersion) {
|
|
|
|
|
|
map["Versions"] = versionsMap()
|
|
|
|
|
|
}
|
|
|
|
|
|
map
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|