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.
113 lines
4.1 KiB
113 lines
4.1 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
|
|
|
|
suspend inline fun <reified T:BaseVo> withToken(call:ApplicationCall, callback:(vo:T)->Unit){
|
|
val levelVo=call.receive<T>()
|
|
if(AccountService.validToken(levelVo.token)){
|
|
callback(levelVo)
|
|
}else{
|
|
call.respond(ApiResponse(message = "token校验失败",body = null))
|
|
}
|
|
}
|
|
|
|
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 token=AccountService.login(userLoginVo,call.request.host())
|
|
logger.info("登录请求处理完毕")
|
|
call.respond(ApiResponse(message = if(token!=null) "登陆成功" else "账号或密码错误!!!",body = token))
|
|
}
|
|
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))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 主界面接口
|
|
*
|
|
*/
|
|
fun Application.MainController(){
|
|
MainService.register(maxSize = environment.config.property("ktor.deployment.leaveMessage.maxSize").getString().toInt())
|
|
|
|
routing {
|
|
//发送留言
|
|
route("$ApiPathPrefix/main") {
|
|
post("/leaveMessage") {
|
|
withToken<LeaveMessageVo>(call){
|
|
val flag=MainService.createMessage(leaveMessageVo = it)
|
|
call.respond(ApiResponse(message = if(flag) "留言创建成功" else "留言创建失败",body=flag))
|
|
}
|
|
}
|
|
post("/getMessage"){
|
|
withToken<OnlyToken>(call){
|
|
val s=MainService.getAllLeaveMessage()
|
|
call.respond(ApiResponse(message = "留言获取成功",body = s))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fun Application.TestController(){
|
|
routing {
|
|
get("$ApiPathPrefix/test"){
|
|
AccountService.test()
|
|
call.respond(ApiResponse(message = "成功连接服务端",body=true))
|
|
}
|
|
}
|
|
} |