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/main/kotlin/com/gyf/csams/Controller.kt

486 lines
17 KiB

package com.gyf.csams
import com.gyf.csams.AuditController.accept
import com.gyf.csams.AuditController.audit
import com.gyf.csams.AuditController.check
import com.gyf.csams.AuditController.read
import com.gyf.csams.AuditController.register
import com.gyf.csams.module.*
import io.ktor.application.*
import io.ktor.http.content.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.util.*
import org.jetbrains.exposed.dao.id.IntIdTable
import java.util.*
/**
* 令牌校验
*
* @param T
* @param callback
*/
suspend inline fun <reified T: ClientBaseVo> ApplicationCall.withToken(callback: (vo: T) -> Unit) {
val levelVo = receive<T>()
if (AccountService.validToken(levelVo)) {
callback(levelVo)
} else {
respond(Simple.error("token校验失败"))
}
}
/**
* 静态资源映射路径配置
*
*/
fun Application.StaticController(){
routing {
val uploadDir=environment.config.property("ktor.deployment.filePath").getString()
static(uploadDir) {
resources(uploadDir)
}
}
}
/**
* 帐号接口
*
*/
fun Application.AccountController() {
suspend fun loginRes(call: ApplicationCall, body:BaseLoginVo){
try {
call.respond(ApiResponse(message = "登陆成功", body = AccountService.login(body, call.request.host())))
} catch (e: IllegalArgumentException) {
call.respond(ApiResponse(message = "帐号或密码错误",body=null))
}catch (e:Exception){
call.respond(ApiResponse(message = "发生未知错误,请联系管理员",body=null))
}
}
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("注册失败"))
}
}
}
post("/refresh"){
call.withToken<OnlyToken> {
try {
call.respond(ApiResponse(message = "个人信息获取成功",body =AccountService.refreshInfo(_token = it) ))
} catch (e: Exception) {
log.error(e)
call.respond(Simple.error(message = "个人信息获取失败"))
}
}
}
//TODO 封装前后台登录逻辑
route(path = "/login") {
route(path = ClientType.Foreground.name.lowercase(Locale.getDefault())){
post {
val userLoginVo = call.receive<UserLoginVo>()
loginRes(call=call,body = userLoginVo)
}
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.lowercase(Locale.getDefault())){
post{
val managerLoginVo = call.receive<ManagerLoginVo>()
loginRes(call=call,body = managerLoginVo)
}
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") {
call.withToken<LeaveMessageVo> {
val flag = MainService.createMessage(leaveMessageVo = it)
call.respond(ApiResponse(message = if (flag) "留言创建成功" else "留言创建失败", body = flag))
}
}
post("/getMessage") {
call.withToken<OnlyToken> {
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))
}
}
}
/**
* 审核接口
*/
object AuditController{
/**
* 前台提交资料
*
* @param A
* @param E
* @param service
*/
inline fun <reified A:ClientBaseVo,reified E:AuditService<A,*,*>> Route.register(service:E){
applicationEngineEnvironment {
post("/register"){
log.info("开始提交${service.dataType}")
call.withToken<A>{
try {
service.register(vo = it)
call.respond(ApiResponse(message = "${service.dataType}已提交,请等待受理",body = true))
} catch (e: Exception) {
log.error(e)
call.respond(ApiResponse(message = "${service.dataType}提交失败", body = false))
}
}
log.info("----end-----")
}
}
}
/**
* 后台查看提交资料
*
* @param E
* @param service
*/
inline fun <reified E:AuditService<*,*,*>> Route.audit(service: E){
applicationEngineEnvironment {
post("/audit"){
log.info("获取${service.dataType}审核记录")
call.withToken<OnlyToken>{
try {
call.respond(ApiResponse(message = "${service.dataType}获取成功",body=service.audit(it)))
}catch (e:Exception){
log.error(e)
call.respond(Simple.error("${service.dataType}获取失败"))
}
}
log.info("----end-----")
}
}
}
/**
* 后台受理提交资料
*
* @param A
* @param E
* @param service
*/
inline fun <reified A:ClientBaseVo,reified E:AuditService<A,*,*>> Route.accept(service:E){
applicationEngineEnvironment {
post("/accept"){
log.info("开始受理${service.dataType}")
call.withToken<AcceptVo>{
try {
service.accept(vo=it)
call.respond(ApiResponse(message = "${service.dataType}受理成功",body=true))
}catch (e:Exception){
log.error(e)
call.respond(ApiResponse(message = "${service.dataType}受理失败",body = false))
}
}
log.info("----end-----")
}
}
}
/**
* 后台审核提交资料
*
* @param A
* @param E
* @param service
*/
inline fun <reified A:ClientBaseVo,reified E:AuditService<A,*,*>> Route.check(service:E){
applicationEngineEnvironment {
post("/check"){
log.info("审核${service.dataType}")
call.withToken<CheckVo>{
try {
service.check(it)
call.respond(ApiResponse(message = "${service.dataType}审核结果已保存",body = true))
} catch (e: Exception) {
log.error(e)
call.respond(ApiResponse(message = "${service.dataType}审核结果保存失败",body = false))
}
}
log.info("----end-----")
}
}
}
inline fun <reified A:IntIdTable,reified E:AuditService<*,*,*>> Route.read(service:E,table:A){
applicationEngineEnvironment {
post("/read"){
log.info("查询${service.dataType}审核进度")
call.withToken<OnlyToken>{
try {
call.respond(ApiResponse(message = "${service.dataType}审核进度查询成功",body=service.read(vo=it,table = table)))
} catch (e: Exception) {
log.error(e)
call.respond(Simple.error("${service.dataType}审核进度查询失败"))
}
}
log.info("----end-----")
}
}
}
}
/**
* 社团接口
*
*/
fun Application.AssociationController() {
routing {
route("$ApiPathPrefix/association") {
register(AssociationService)
accept(AssociationService)
audit(AssociationService)
check(AssociationService)
read(AssociationService,Associations)
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("/list"){
log.info("开始查询社团")
call.withToken<SearchAssociationVo>{
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-----")
}
route("/load"){
post{
log.info("社团id查询")
call.withToken<ShowAssociationVo>{
try {
call.respond(ApiResponse(message = "社团查询成功",body=AssociationService.load(vo=it)))
} catch (e: Exception) {
log.error(e)
call.respond(Simple.error("社团查询失败"))
}
}
log.info("----end-----")
}
post("/members"){
log.info("加载社团成员")
call.withToken<QueryAssociationMembers>{
try {
call.respond(ApiResponse(message = "社团成员加载成功",body=AssociationService.load(vo=it)))
} catch (e: Exception) {
log.error(e)
call.respond(Simple.error("社团成员加载失败"))
}
}
}
}
}
}
}
/**
* 活动接口
*
*/
fun Application.ActivityController(){
routing {
route("$ApiPathPrefix/activity") {
register(ActivityService)
audit(ActivityService)
accept(ActivityService)
check(ActivityService)
read(ActivityService,Activities)
post("/load"){
val searchActivityVo=call.receive<SearchActivityVo>()
call.respond(ApiResponse(message = "活动列表加载成功",body = ActivityService.load(vo=searchActivityVo)))
}
post("/show"){
val showActivityVo=call.receive<ShowActivityVo>()
call.respond(ApiResponse(message = "活动详情加载成功",body = ActivityService.show(vo=showActivityVo)))
}
route("/photo"){
post{
val searchActivityPhotoVo=call.receive<SearchActivityPhotoVo>()
call.respond(ApiResponse(message = "活动相册加载成功",body = ActivityService.load(searchActivityPhotoVo)))
}
post("upload"){
log.info("开始上传活动照片")
val multipartData = call.receiveMultipart()
multipartData.readAllParts().apply {
log.info("part size=$size")
ActivityService.upload(this)
call.respond(ApiResponse(message = "照片上传成功",body = true))
}
log.info("----end-----")
}
}
route("/comment"){
post{
val searchCommentVo=call.receive<SearchCommentVo>()
call.respond(ApiResponse(message = "评论加载成功",body=ActivityService.loadComment(searchCommentVo)))
}
post("/send"){
val sendBBSVo=call.receive<SendBBSVo>()
try {
ActivityService.sendComment(sendBBSVo)
call.respond(ApiResponse(message = "评论发送成功",body=true))
} catch (e: Exception) {
log.error(e)
call.respond(ApiResponse(message = "评论发送失败",body=true))
}
}
}
}
}
}
/**
* 通知接口
*
*/
fun Application.NotificationController(){
routing {
route("${ApiPathPrefix}/notification") {
post("/pull") {
call.withToken<NotificationDto>{
val notificationList = NotificationService.pull(vo = it)
call.respond(
ApiResponse(
message = if (notificationList.isEmpty()) "没有最新通知" else "获取${notificationList.size}条最新通知",
body = notificationList
)
)
}
}
post("/count"){
call.withToken<NotificationDto> {
call.respond(ApiResponse(message = "统计未读通知",body = NotificationService.count(it)))
}
}
post("/list"){
call.withToken<NotificationDto>{
call.respond(ApiResponse(message = "获取通知列表",body=NotificationService.list(it)))
}
}
}
}
}