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/Controller.kt

261 lines
8.6 KiB

package com.gyf.csams
import io.ktor.application.*
import io.ktor.http.content.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
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(Simple.error("token校验失败"))
}
}
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("注册失败"))
}
}
}
//TODO 封装前后台登录逻辑
route(path = "/login") {
route(path = ReceiverType.Foreground.name.toLowerCase()){
post {
val userLoginVo = call.receive<UserLoginVo>()
val token = AccountService.login(userLoginVo, call.request.host())
call.respond(ApiResponse(message = if (token != null) "登陆成功" else "账号或密码错误!!!", body = token))
}
post("/token"){
val tokenVo = call.receive<Token>()
val isValid = AccountService.validToken(tokenVo)
call.respond(ApiResponse(message = if (isValid) "令牌合法" else "令牌不合法", body = isValid))
}
}
route(path = ReceiverType.Background.name.toLowerCase()){
post{
val managerLoginVo = call.receive<ManagerLoginVo>()
val token = AccountService.login(managerLoginVo, call.request.host())
call.respond(ApiResponse(message = if (token != null) "登陆成功" else "账号或密码错误!!!", body = token))
}
post("/token"){
val tokenVo = call.receive<Token>()
val isValid = AccountService.validManagerToken(tokenVo)
call.respond(ApiResponse(message = if (isValid) "令牌合法" else "令牌不合法", body = isValid))
}
}
}
post(path = "/logout") {
environment.log.info("退出登录")
val userLogoutVo = call.receive<UserLogoutVo>()
environment.log.info("$userLogoutVo")
val flag = AccountService.logout(userLogoutVo.userId)
call.respond(ApiResponse(message = if (flag) "退出成功" else "退出失败", body = flag))
}
}
}
}
/**
* 主界面接口
*
*/
fun Application.MainController() {
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))
}
}
}
fun Application.AssociationController() {
routing {
route("$ApiPathPrefix/association") {
post("/uploadLogo") {
val multipartData = call.receiveMultipart()
multipartData.readAllParts().apply {
environment.log.info("part size=$size")
if (size == 4) {
val result=FileService.storeFile(this)
call.respond(ApiResponse(message = if(result.isNullOrEmpty()) "文件上传失败" else
"成功上传${result.size}个文件",body = result))
} else {
call.respond(Simple.error("参数异常"))
throw IllegalArgumentException("参数异常")
}
}
}
post("/register"){
withToken<RegAssociationVo>(call = call){
val flag=AssociationService.register(vo=it)
call.respond(ApiResponse(message = if(flag) "社团注册资料已保存,请等待受理" else "社团注册资料保存失败",body=flag))
}
}
}
}
}
/**
* TODO
*
*/
enum class NotificationType{
System
}
/**
* 通知接收客户端
*
*/
enum class ReceiverType{
//前台
Foreground,
//后台
Background
}
/**
* 通知接收者
*
*/
sealed class Receiver{
protected abstract val id:Int
abstract val target:ReceiverType
}
/**
* 后台客户端接收
*
* @property managerId 管理员id
*/
data class BackgroundReceiver(val managerId:Int,override val id: Int=managerId, override val target: ReceiverType=ReceiverType.Background):Receiver()
/**
* 前台客户端接收
*
* @property userId 用户id
*/
data class ForegroundReceiver(val userId:Int, override val id:Int=userId, override val target: ReceiverType=ReceiverType.Foreground):Receiver()
sealed class BaseNotification{
abstract val type:NotificationType
abstract val title:String
abstract val receiver:Receiver
}
/**
* 系统通知
*
* @property type
* @property title
*/
sealed class SysNotificationVo<T>:BaseNotification(){
override val type: NotificationType=NotificationType.System
abstract val content:T
}
/**
* 注册社团通知
*
* @property title
*/
data class RegisterNotification(override val title: String="注册社团通知", override val content: RegAssociationDto,
override val receiver: ForegroundReceiver
):SysNotificationVo<RegAssociationDto>()
data class SimpleNotification(
override val type: NotificationType=NotificationType.System,
override val title: String="test",
override val receiver: Receiver=BackgroundReceiver(managerId = randomNum().toInt())
):BaseNotification()
fun Application.NotificationController(){
routing {
route("${ApiPathPrefix}/notification") {
post("/pull") {
withToken<NotificationDto>(call) {
val notificationList = NotificationService.pull(vo = it)
call.respond(
ApiResponse(
message = if (notificationList.isEmpty()) "没有最新通知" else "获取${notificationList.size}条最新通知",
body = notificationList
)
)
}
}
post("/count"){
withToken<NotificationDto>(call) {
call.respond(ApiResponse(message = "统计未读通知",body = NotificationService.count(it)))
}
}
post("/list"){
withToken<NotificationDto>(call){
call.respond(ApiResponse(message = "获取通知列表",body=NotificationService.list(it)))
}
}
}
}
}