增加退出登录接口

解决首次登陆卡顿问题
master
pan 4 years ago
parent f363d7966b
commit 4076936680
  1. 16
      src/AccountController.kt
  2. 24
      src/AccountService.kt
  3. 1
      src/MySQL.kt
  4. 5
      src/TestController.kt
  5. 1
      src/Util.kt
  6. 3
      src/Vo.kt

@ -1,12 +1,12 @@
package com.gyf.csams package com.gyf.csams
import io.ktor.application.* import io.ktor.application.*
import io.ktor.features.*
import io.ktor.request.* import io.ktor.request.*
import io.ktor.response.* import io.ktor.response.*
import io.ktor.routing.* import io.ktor.routing.*
import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger(Application::class.java)
fun Application.AccountController() { fun Application.AccountController() {
routing { routing {
@ -45,7 +45,9 @@ fun Application.AccountController() {
route(path = "/login"){ route(path = "/login"){
post{ post{
val userLoginVo= call.receive<UserLoginVo>() val userLoginVo= call.receive<UserLoginVo>()
val tokenResDto:TokenResDto=AccountService.login(userLoginVo,call.request.origin.remoteHost) logger.info("执行登陆")
val tokenResDto:TokenResDto=AccountService.login(userLoginVo,call.request.host())
logger.info("登录请求处理完毕")
call.respond(ApiResponse(message = if(tokenResDto.token!=null) "登陆成功" else "账号或密码错误!!!",body = tokenResDto)) call.respond(ApiResponse(message = if(tokenResDto.token!=null) "登陆成功" else "账号或密码错误!!!",body = tokenResDto))
} }
post(path = "/token"){ post(path = "/token"){
@ -54,6 +56,14 @@ fun Application.AccountController() {
call.respond(ApiResponse(message = if(isValid) "令牌合法" else "令牌不合法",body = isValid)) call.respond(ApiResponse(message = if(isValid) "令牌合法" else "令牌不合法",body = isValid))
} }
} }
post(path = "/logout"){
logger.info("退出登录")
val userLogoutVo=call.receive<UserLogoutVo>()
logger.info("$userLogoutVo")
val flag=AccountService.logout(userLogoutVo.studentId)
call.respond(ApiResponse(message = if(flag) "退出成功" else "退出失败",body = flag))
}
} }
} }
} }

@ -1,14 +1,12 @@
package com.gyf.csams package com.gyf.csams
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.time.ZoneOffset import java.time.ZoneOffset
class AccountService { object AccountService {
companion object {
private val logger = LoggerFactory.getLogger(AccountService::class.java) private val logger = LoggerFactory.getLogger(AccountService::class.java)
/** /**
* 检查学号是否已注册,true=已注册 * 检查学号是否已注册,true=已注册
@ -48,8 +46,6 @@ class AccountService {
return transaction { return transaction {
val user=User.find { Users.studentId eq userLoginVo.studentId }.firstOrNull() val user=User.find { Users.studentId eq userLoginVo.studentId }.firstOrNull()
when { when {
user==null -> { user==null -> {
logger.warn("学号:${userLoginVo.studentId}不存在") logger.warn("学号:${userLoginVo.studentId}不存在")
@ -81,11 +77,25 @@ class AccountService {
*/ */
fun validToken(tokenVo: TokenVo):Boolean{ fun validToken(tokenVo: TokenVo):Boolean{
return transaction { return transaction {
return@transaction !UserToken.find { !UserToken.find {
UserTokens.studentId eq tokenVo.studentId UserTokens.studentId eq tokenVo.studentId
UserTokens.token eq tokenVo.token UserTokens.token eq tokenVo.token
}.empty() }.empty()
} }
} }
fun logout(studentId:String):Boolean{
return transaction {
UserTokens.deleteWhere { UserTokens.studentId eq studentId }>0
} }
} }
fun test(){
logger.info("开始测试")
transaction {
logger.info("查询到个${User.count()}用户")
}
logger.info("结束测试")
}
}

@ -43,5 +43,6 @@ fun initTable(){
val tableList= arrayOf(Users,UserTokens) val tableList= arrayOf(Users,UserTokens)
SchemaUtils.createMissingTablesAndColumns(*tableList) SchemaUtils.createMissingTablesAndColumns(*tableList)
updateComment(*tableList) updateComment(*tableList)
} }
} }

@ -3,7 +3,6 @@ package com.gyf.csams
import io.ktor.application.* import io.ktor.application.*
import io.ktor.response.* import io.ktor.response.*
import io.ktor.routing.* import io.ktor.routing.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger(Application::class.java) private val logger = LoggerFactory.getLogger(Application::class.java)
@ -11,9 +10,7 @@ private val logger = LoggerFactory.getLogger(Application::class.java)
fun Application.TestController(){ fun Application.TestController(){
routing { routing {
get("$ApiPathPrefix/test"){ get("$ApiPathPrefix/test"){
transaction { AccountService.test()
logger.info("查询到个${User.count()}用户")
}
call.respond(ApiResponse(message = "成功连接服务端",body=true)) call.respond(ApiResponse(message = "成功连接服务端",body=true))
} }
} }

@ -27,6 +27,7 @@ fun String.md5(): String {
@Target(AnnotationTarget.CLASS,AnnotationTarget.FIELD) @Target(AnnotationTarget.CLASS,AnnotationTarget.FIELD)
annotation class TableComment(val comment:String) annotation class TableComment(val comment:String)
/** /**
* 表字段添加注释 * 表字段添加注释
* *

@ -35,6 +35,9 @@ data class UserVo(val studentId:String,val name:String)
*/ */
data class UserLoginVo(val studentId: String,val password: String,val device: String) data class UserLoginVo(val studentId: String,val password: String,val device: String)
@Serializable
data class UserLogoutVo(val studentId:String)
data class UserResDto(val password:String) data class UserResDto(val password:String)
data class Token(val token:String,val createTime:Long,val studentId:String) data class Token(val token:String,val createTime:Long,val studentId:String)

Loading…
Cancel
Save