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.
385 lines
14 KiB
385 lines
14 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.*
|
|
import io.ktor.util.*
|
|
|
|
suspend inline fun <reified T : ClientBaseVo> withToken(call: ApplicationCall, callback: (vo: T) -> Unit) {
|
|
val levelVo = call.receive<T>()
|
|
if (AccountService.validToken(levelVo)) {
|
|
callback(levelVo)
|
|
} else {
|
|
call.respond(Simple.error("token校验失败"))
|
|
}
|
|
}
|
|
|
|
fun Application.StaticController(){
|
|
routing {
|
|
val uploadDir=environment.config.property("ktor.deployment.filePath").getString()
|
|
static(uploadDir) {
|
|
resources(uploadDir)
|
|
}
|
|
}
|
|
}
|
|
|
|
suspend fun loginRes(call: ApplicationCall, body:OwnInfoVo){
|
|
try {
|
|
call.respond(ApiResponse(message = "登陆成功", body = body))
|
|
} catch (e: IllegalArgumentException) {
|
|
call.respond(ApiResponse(message = "${e.message}",body=null))
|
|
}catch (e:Exception){
|
|
call.respond(ApiResponse(message = "发生未知错误,请联系管理员",body=null))
|
|
}
|
|
}
|
|
|
|
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<UserRegVo>()
|
|
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 = ClientType.Foreground.name.toLowerCase()){
|
|
post {
|
|
val userLoginVo = call.receive<UserLoginVo>()
|
|
loginRes(call=call,body = AccountService.login(userLoginVo, call.request.host()))
|
|
}
|
|
post("/token"){
|
|
val tokenVo = call.receive<Token>()
|
|
try {
|
|
val userinfo = AccountService.getUserVo(tokenVo)
|
|
log.info("$userinfo")
|
|
call.respond(ApiResponse(message = "令牌合法", body = userinfo))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "令牌不合法", body = null))
|
|
}
|
|
}
|
|
|
|
}
|
|
route(path = ClientType.Background.name.toLowerCase()){
|
|
post{
|
|
val managerLoginVo = call.receive<ManagerLoginVo>()
|
|
loginRes(call=call,body = AccountService.login(managerLoginVo, call.request.host()))
|
|
}
|
|
post("/token"){
|
|
val tokenVo = call.receive<Token>()
|
|
try {
|
|
val managerInfo = AccountService.getManagerVo(tokenVo)
|
|
log.info("$managerInfo")
|
|
call.respond(ApiResponse(message = "令牌合法", body = managerInfo))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "令牌不合法", body = null))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
post(path = "/logout") {
|
|
log.info("退出登录")
|
|
val onlyToken = call.receive<OnlyToken>()
|
|
log.info("$onlyToken")
|
|
val flag = AccountService.logout(onlyToken)
|
|
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") {
|
|
|
|
log.info("开始上传logo")
|
|
val multipartData = call.receiveMultipart()
|
|
|
|
multipartData.readAllParts().apply {
|
|
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("参数异常")
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
post("/register"){
|
|
log.info("开始提交注册资料")
|
|
withToken<AssociationRegVo>(call = call){
|
|
try {
|
|
AssociationService.register(regVo=it)
|
|
call.respond(ApiResponse(message = "社团注册资料已提交,请等待受理",body = true))
|
|
} catch (e: IllegalArgumentException) {
|
|
call.respond(ApiResponse(message = "社团资料提交失败,请联系系统管理员",body = false))
|
|
} catch (e:Exception){
|
|
call.respond(ApiResponse(message = e.message?:"发生未知错误,请联系系统管理员",body = false))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
|
|
post("/list"){
|
|
log.info("开始查询社团")
|
|
withToken<SearchAssociationVo>(call = call){
|
|
try {
|
|
call.respond(ApiResponse(message = "社团列表检索完成",body=AssociationService.load(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "社团列表检索失败",body=null))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
|
|
post("/accept"){
|
|
log.info("开始受理")
|
|
withToken<AcceptRegAssociation>(call = call){
|
|
try {
|
|
AssociationService.accept(vo=it)
|
|
call.respond(ApiResponse(message = "社团注册资料受理成功",body=true))
|
|
}catch (e:IllegalArgumentException){
|
|
call.respond(ApiResponse(message = "社团资料受理失败,请联系系统管理员",body = false))
|
|
}catch (e:Exception){
|
|
call.respond(Simple.error("发生未知错误,请联系管理员"))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
|
|
post("/audit"){
|
|
log.info("审核列表")
|
|
withToken<OnlyToken>(call = call){
|
|
try {
|
|
call.respond(ApiResponse(message = "社团注册资料获取成功",body=AssociationService.loadAudit(it)))
|
|
}catch (e:IllegalArgumentException){
|
|
call.respond(Simple.error("社团注册资料获取失败"))
|
|
}catch (e:Exception){
|
|
call.respond(Simple.error("发生未知错误,请联系管理员"))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
|
|
post("/check"){
|
|
log.info("审核社团注册资料")
|
|
withToken<CheckRegVo>(call=call){
|
|
try {
|
|
AssociationService.check(it)
|
|
call.respond(ApiResponse(message = "审核结果已保存",body = true))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "审核结果保存失败",body = false))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
|
|
post("/read"){
|
|
log.info("查询用户社团")
|
|
withToken<OnlyToken>(call = call){
|
|
try {
|
|
call.respond(ApiResponse(message = "用户社团查询成功",body=AssociationService.read(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(Simple.error("用户社团查询失败"))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
|
|
post("/load"){
|
|
log.info("社团id查询")
|
|
withToken<ShowAssociationVo>(call = call){
|
|
try {
|
|
call.respond(ApiResponse(message = "社团查询成功",body=AssociationService.load(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(Simple.error("社团查询失败"))
|
|
}
|
|
}
|
|
log.info("----end-----")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* TODO
|
|
*
|
|
*/
|
|
enum class NotificationType{
|
|
System
|
|
}
|
|
|
|
/**
|
|
* 客户端类型
|
|
*
|
|
*/
|
|
enum class ClientType{
|
|
//前台
|
|
Foreground,
|
|
//后台
|
|
Background
|
|
}
|
|
|
|
/**
|
|
* 通知接收者
|
|
*
|
|
*/
|
|
sealed class Receiver{
|
|
protected abstract val id:Int
|
|
abstract val target:ClientType
|
|
}
|
|
|
|
/**
|
|
* 后台客户端接收
|
|
*
|
|
* @property managerId 管理员id
|
|
*/
|
|
data class BackgroundReceiver(val managerId:Int,override val id: Int=managerId, override val target: ClientType=ClientType.Background):Receiver()
|
|
|
|
/**
|
|
* 前台客户端接收
|
|
*
|
|
* @property userId 用户id
|
|
*/
|
|
data class ForegroundReceiver(val userId:Int, override val id:Int=userId, override val target: ClientType=ClientType.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)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |