From 662c6930a0620d8c26af578f600f5712c6647cd8 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Tue, 25 May 2021 05:51:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=95=99=E8=A8=80=E8=A1=A8?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=E7=95=99=E8=A8=80?= =?UTF-8?q?=EF=BC=8C=E8=8E=B7=E5=8F=96=E7=95=99=E8=A8=80=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E5=92=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=AF=B9=E6=8E=A5?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/application.conf | 4 +- src/Application.kt | 1 + src/{AccountController.kt => Controller.kt} | 47 +++++++++++++- src/Dao.kt | 26 +++++++- src/MySQL.kt | 2 +- src/{AccountService.kt => Service.kt} | 69 +++++++++++++++++++-- src/TestController.kt | 17 ----- src/Vo.kt | 22 ++++--- test/ApplicationTest.kt | 9 ++- 9 files changed, 157 insertions(+), 40 deletions(-) rename src/{AccountController.kt => Controller.kt} (62%) rename src/{AccountService.kt => Service.kt} (57%) delete mode 100644 src/TestController.kt diff --git a/resources/application.conf b/resources/application.conf index 2e2b760..f68d123 100644 --- a/resources/application.conf +++ b/resources/application.conf @@ -8,7 +8,9 @@ ktor { jdbcUrl = "jdbc:mysql://localhost:3306/csams" driverClassName = "com.mysql.cj.jdbc.Driver" } - + leaveMessage { + maxSize = 20 + } watch = [ classes ] } application { diff --git a/src/Application.kt b/src/Application.kt index 77a9b84..86e3e99 100644 --- a/src/Application.kt +++ b/src/Application.kt @@ -32,4 +32,5 @@ fun Application.module(testing: Boolean = false) { fun Application.Controller(testing: Boolean = false){ this.AccountController() this.TestController() + this.MainController() } diff --git a/src/AccountController.kt b/src/Controller.kt similarity index 62% rename from src/AccountController.kt rename to src/Controller.kt index 62523ac..12e2041 100644 --- a/src/AccountController.kt +++ b/src/Controller.kt @@ -6,6 +6,15 @@ import io.ktor.response.* import io.ktor.routing.* import org.slf4j.LoggerFactory +suspend inline fun withToken(call:ApplicationCall, callback:(vo:T)->Unit){ + val levelVo=call.receive() + 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() { @@ -46,9 +55,9 @@ fun Application.AccountController() { post{ val userLoginVo= call.receive() logger.info("执行登陆") - val tokenResDto:TokenResDto=AccountService.login(userLoginVo,call.request.host()) + val token=AccountService.login(userLoginVo,call.request.host()) logger.info("登录请求处理完毕") - call.respond(ApiResponse(message = if(tokenResDto.token!=null) "登陆成功" else "账号或密码错误!!!",body = tokenResDto)) + call.respond(ApiResponse(message = if(token!=null) "登陆成功" else "账号或密码错误!!!",body = token)) } post(path = "/token"){ val tokenVo=call.receive() @@ -68,3 +77,37 @@ fun Application.AccountController() { } } +/** + * 主界面接口 + * + */ +fun Application.MainController(){ + MainService.register(maxSize = environment.config.property("ktor.deployment.leaveMessage.maxSize").getString().toInt()) + + routing { + //发送留言 + route("$ApiPathPrefix/main") { + post("/leaveMessage") { + withToken(call){ + val flag=MainService.createMessage(leaveMessageVo = it) + call.respond(ApiResponse(message = if(flag) "留言创建成功" else "留言创建失败",body=flag)) + } + } + post("/getMessage"){ + withToken(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)) + } + } +} \ No newline at end of file diff --git a/src/Dao.kt b/src/Dao.kt index 0ba8d10..dd864f9 100644 --- a/src/Dao.kt +++ b/src/Dao.kt @@ -5,6 +5,8 @@ import org.jetbrains.exposed.dao.IntEntityClass import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.Column +import org.jetbrains.exposed.sql.`java-time`.CurrentDateTime +import org.jetbrains.exposed.sql.`java-time`.CurrentTimestamp import org.jetbrains.exposed.sql.`java-time`.datetime import java.time.LocalDateTime @@ -41,7 +43,7 @@ object UserTokens: IntIdTable(){ val ip:Column = varchar(name="ip",length = 32) @TableComment("令牌创建时间") - val createTime:Column = datetime("create_time").default(LocalDateTime.now()) + val createTime:Column = datetime("create_time").defaultExpression(CurrentTimestamp()) @TableComment("授权设备") val device:Column = varchar(name="device",length = 256) @@ -54,4 +56,24 @@ class UserToken(id:EntityID):IntEntity(id){ var ip by UserTokens.ip var createTime by UserTokens.createTime var device by UserTokens.device -} \ No newline at end of file +} + +@TableComment("留言") +object LeaveMessages:IntIdTable(){ + @TableComment("留言用户") + val studentId:Column = reference("student_id",Users.studentId) + + @TableComment("留言内容") + val message:Column = varchar(name = "message",length = 20) + + @TableComment("留言创建时间") + val createTime:Column = datetime("create_time").defaultExpression(CurrentDateTime()) +} + +class LeaveMessage(id:EntityID):IntEntity(id){ + companion object:IntEntityClass(LeaveMessages) + var studentId by LeaveMessages.studentId + var message by LeaveMessages.message + var createTime by LeaveMessages.createTime +} + diff --git a/src/MySQL.kt b/src/MySQL.kt index d3d0e00..5d9d8aa 100644 --- a/src/MySQL.kt +++ b/src/MySQL.kt @@ -40,7 +40,7 @@ fun Application.MySQL(testing: Boolean = false){ fun initTable(){ transaction { - val tableList= arrayOf(Users,UserTokens) + val tableList= arrayOf(Users,UserTokens,LeaveMessages) SchemaUtils.createMissingTablesAndColumns(*tableList) updateComment(*tableList) diff --git a/src/AccountService.kt b/src/Service.kt similarity index 57% rename from src/AccountService.kt rename to src/Service.kt index 99ddcde..d6a206f 100644 --- a/src/AccountService.kt +++ b/src/Service.kt @@ -42,18 +42,18 @@ object AccountService { * * @param userLoginVo 登陆表单 */ - fun login(userLoginVo: UserLoginVo,_ip:String):TokenResDto{ + fun login(userLoginVo: UserLoginVo,_ip:String):Token?{ return transaction { val user=User.find { Users.studentId eq userLoginVo.studentId }.firstOrNull() when { user==null -> { logger.warn("学号:${userLoginVo.studentId}不存在") - return@transaction TokenResDto(isValid = false) + return@transaction null } userLoginVo.password.md5() != user.password -> { logger.warn("密码:${userLoginVo.password}错误") - return@transaction TokenResDto(isValid = false) + return@transaction null } else -> { val token=UserToken.new{ @@ -62,13 +62,19 @@ object AccountService { device=userLoginVo.device token=listOf(studentId,ip,device).joinToString(separator = ('a' .. 'z').random().toString()).md5() } - return@transaction TokenResDto(isValid = true,token = Token(token = token.token,createTime = token.createTime.toEpochSecond( - ZoneOffset.of("+8")),studentId = token.studentId)) + token.flush() + return@transaction Token(studentId = userLoginVo.studentId,token = token.token, + createTime = token.createTime.toEpochSecond( + ZoneOffset.of("+8")),name=user.name) } } } } + fun validToken(token: Token):Boolean{ + return validToken(TokenVo(token = token.token,studentId = token.studentId)) + } + /** * 令牌校验 * @@ -98,4 +104,55 @@ object AccountService { logger.info("结束测试") } -} \ No newline at end of file +} + +object MainService{ + private val logger = LoggerFactory.getLogger(MainService::class.java) + + var maxSize:Int=20 + + fun register(maxSize:Int){ + this.maxSize=maxSize + } + + /** + * 创建留言信息 + * + * @param leaveMessageVo + * @return + */ + fun createMessage(leaveMessageVo: LeaveMessageVo):Boolean{ + return if(leaveMessageVo.message.isNotEmpty()){ + return transaction { + val count=LeaveMessage.count().toInt() + logger.info("系统留言数:$count,限制数:$maxSize") + if(count>= maxSize){ + LeaveMessage.all().sortedBy { it.createTime }.subList(0, count-maxSize).forEach { + it.delete() + } + + } + logger.info("保存留言:${leaveMessageVo.message}") + LeaveMessage.new { + studentId = leaveMessageVo.token.studentId + message = leaveMessageVo.message + } + logger.info("留言保存成功") + return@transaction true + } + }else{ + logger.info("留言不能为空") + false + } + } + + fun getAllLeaveMessage():List{ + return transaction { + logger.info("获取所有留言") + return@transaction LeaveMessage.all().toList().map { + LeaveMessageFormatVo(message = "${User.find { Users.studentId eq it.studentId }.first().name}说:${it.message}",studentId = it.studentId) + } + } + } +} + diff --git a/src/TestController.kt b/src/TestController.kt deleted file mode 100644 index 0b97d70..0000000 --- a/src/TestController.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.gyf.csams - -import io.ktor.application.* -import io.ktor.response.* -import io.ktor.routing.* -import org.slf4j.LoggerFactory - -private val logger = LoggerFactory.getLogger(Application::class.java) - -fun Application.TestController(){ - routing { - get("$ApiPathPrefix/test"){ - AccountService.test() - call.respond(ApiResponse(message = "成功连接服务端",body=true)) - } - } -} \ No newline at end of file diff --git a/src/Vo.kt b/src/Vo.kt index 1be5dc5..c0a0bac 100644 --- a/src/Vo.kt +++ b/src/Vo.kt @@ -40,13 +40,17 @@ data class UserLogoutVo(val studentId:String) data class UserResDto(val password:String) -data class Token(val token:String,val createTime:Long,val studentId:String) -/** - * 令牌传输 - * - * @property isValid - * @property token - */ -data class TokenResDto(val isValid:Boolean,val token: Token?=null) +data class Token(val token:String,val createTime:Long,val studentId:String,val name:String) + +data class TokenVo(val token:String,val studentId:String) + +sealed class BaseVo{ + abstract val token:Token +} + +data class LeaveMessageVo(val message: String, override val token:Token):BaseVo() + +data class OnlyToken(override val token: Token):BaseVo() +//data class PageVo(val pageSize:Int=10,val page:, override val token: Token):BaseVo() -data class TokenVo(val token:String,val studentId:String) \ No newline at end of file +data class LeaveMessageFormatVo(val message: String,val studentId: String) \ No newline at end of file diff --git a/test/ApplicationTest.kt b/test/ApplicationTest.kt index 2398605..09ebb04 100644 --- a/test/ApplicationTest.kt +++ b/test/ApplicationTest.kt @@ -10,7 +10,6 @@ import io.ktor.config.* import io.ktor.http.* import io.ktor.server.testing.* import org.jetbrains.exposed.sql.transactions.transaction -import java.time.LocalDateTime import kotlin.test.Test import kotlin.test.assertEquals @@ -90,7 +89,13 @@ class ApplicationTest { @Test fun localTime(){ - println(LocalDateTime.now()) + initApp { + transaction { + + } + + + } } /**