You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
csamsserver/src/AccountController.kt

70 lines
2.8 KiB

package com.gyf.csams
import io.ktor.application.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger(Application::class.java)
fun Application.AccountController() {
routing {
route(path = "$ApiPathPrefix/account"){
route(path="/register") {
/**
* 检测学号是否已注册
*/
get(path = "/checkId") {
val studentId = call.request.queryParameters["studentId"]
if (studentId?.isNotEmpty() == true) {
if (AccountService.registered(studentId)) {
call.respond(ApiResponse(message = "学号已注册", body = true))
} else {
call.respond(ApiResponse(message = "学号可注册", body = false))
}
} else {
call.respond(Simple.error("学号检测失败,请联系管理员"))
}
}
/**
* 注册账号
*/
post {
val userVo = call.receive<UserVo>()
val userResDto = AccountService.register(userVo)
if (userResDto != null) {
call.respond(ApiResponse(message = "注册成功", body = userResDto))
} else {
call.respond(Simple.error("注册失败"))
}
}
}
route(path = "/login"){
post{
val userLoginVo= call.receive<UserLoginVo>()
logger.info("执行登陆")
val tokenResDto:TokenResDto=AccountService.login(userLoginVo,call.request.host())
logger.info("登录请求处理完毕")
call.respond(ApiResponse(message = if(tokenResDto.token!=null) "登陆成功" else "账号或密码错误!!!",body = tokenResDto))
}
post(path = "/token"){
val tokenVo=call.receive<TokenVo>()
val isValid=AccountService.validToken(tokenVo)
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))
}
}
}
}