diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt index 7b7bb6cb8..8890abef1 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt @@ -15,19 +15,17 @@ abstract class VerifyDTO : DTO { data class BindDTO(override val sessionKey: String, val qq: Long) : VerifyDTO() -// 写成data class并继承DTO接口是为了返回时的形式统一 @Serializable -sealed class StateCodeDTO(val code: Int, var msg: String) : DTO { - object Success : StateCodeDTO(0, "success") // 成功 - // object AUTH_WRONG : CodeDTO(1) // AuthKey错误, @see AuthResDTO - object NoBot : StateCodeDTO(2, "指定Bot不存在") - object IllegalSession : StateCodeDTO(3, "Session失效或不存在") - object NotVerifySession : StateCodeDTO(4, "Session未认证") - object NoElement : StateCodeDTO(5, "指定对象不存在") +open class StateCode(val code: Int, var msg: String) { + object Success : StateCode(0, "success") // 成功 + object NoBot : StateCode(2, "指定Bot不存在") + object IllegalSession : StateCode(3, "Session失效或不存在") + object NotVerifySession : StateCode(4, "Session未认证") + object NoElement : StateCode(5, "指定对象不存在") // KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575 @Serializable - class IllegalAccess() : StateCodeDTO(400, "") { // 非法访问 + class IllegalAccess() : StateCode(400, "") { // 非法访问 constructor(msg: String) : this() { this.msg = msg } diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt index f1e6635e9..167846972 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt @@ -27,15 +27,15 @@ fun Application.authModule() { closeSession(it.sessionKey) allSession[it.sessionKey] = AuthedSession(bot, EmptyCoroutineContext) } - call.respondDTO(StateCodeDTO.Success) + call.respondStateCode(StateCode.Success) } catch (e: NoSuchElementException) { - call.respondDTO(StateCodeDTO.NoBot) + call.respondStateCode(StateCode.NoBot) } } miraiVerify("/release") { SessionManager.closeSession(it.sessionKey) - call.respondDTO(StateCodeDTO.Success) + call.respondStateCode(StateCode.Success) } } diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt index c33e2a5b9..09b92aa12 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt @@ -105,19 +105,22 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext ApplicationCall.respondStateCode(code: T, status: HttpStatusCode = HttpStatusCode.OK) + = respondJson(code.toJson(StateCode.serializer()), status) + internal suspend inline fun ApplicationCall.respondDTO(dto: T, status: HttpStatusCode = HttpStatusCode.OK) = respondJson(dto.toJson(), status) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt index 9cc6baf34..6d3be41b0 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt @@ -18,12 +18,12 @@ fun Application.messageModule() { miraiVerify("/sendFriendMessage") { it.session.bot.getFriend(it.target).sendMessage(it.messageChain.toMessageChain()) - call.respondDTO(StateCodeDTO.Success) + call.respondStateCode(StateCode.Success) } miraiVerify("/sendGroupMessage") { it.session.bot.getGroup(it.target).sendMessage(it.messageChain.toMessageChain()) - call.respondDTO(StateCodeDTO.Success) + call.respondStateCode(StateCode.Success) } miraiVerify("/event/message") {