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.
687 lines
25 KiB
687 lines
25 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("/upload/img"){
|
|
val data= call.receiveMultipart()
|
|
try {
|
|
AccountService.uploadHeadImg(data = data.readAllParts())
|
|
call.respond(ApiResponse(message = "上传头像成功",body = true))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "上传头像失败",body = false))
|
|
}
|
|
}
|
|
|
|
post("/refresh"){
|
|
call.withToken<InfoUpdateVo> {
|
|
try {
|
|
log.info("info=${it}")
|
|
call.respond(ApiResponse(message = "个人信息刷新成功",body =AccountService.refreshInfo(vo = 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))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
route("/load/manager"){
|
|
post{
|
|
call.withToken<OnlyToken> {
|
|
call.respond(ApiResponse(message = "部门概况信息加载成功",body=BackgroundService.load()))
|
|
}
|
|
}
|
|
post("/detail"){
|
|
call.withToken<OnlyToken> {
|
|
call.respond(ApiResponse(message = "部门详情加载成功",body=BackgroundService.loadDetail()))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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))
|
|
}
|
|
}
|
|
|
|
post("/hotActivity"){
|
|
call.withToken<OnlyToken> {
|
|
call.respond(ApiResponse(message = "活动轮播图获取成功",
|
|
body=ActivityService.random()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试接口
|
|
*
|
|
*/
|
|
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("/update/exam"){
|
|
call.withToken<AddExamVo> {
|
|
try {
|
|
AssociationService.updateExam(vo=it)
|
|
call.respond(ApiResponse(message = "题库更新成功",body=true))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "题库更新失败",body=false))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/load/exam"){
|
|
call.withToken<SearchExamVo> {
|
|
try {
|
|
call.respond(ApiResponse(message = "加载题库",body = AssociationService.loadExam(vo = it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "题库加载失败",body = null))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
post("/check/apply"){
|
|
call.withToken<SearchExamVo> {
|
|
try {
|
|
val body=AssociationService.applyAssociation(vo = it)
|
|
call.respond(ApiResponse(message = "入团验证信息获取成功",body =body))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "入团验证信息获取失败",body=null))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/create/paper"){
|
|
call.withToken<SearchExamVo> {
|
|
try {
|
|
call.respond(ApiResponse(message = "试卷创建成功,请开始作答",body = AssociationService.createPaper(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "试卷创建失败",body=null))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/apply/paper"){
|
|
call.withToken<AddAnswerVo> {
|
|
try {
|
|
AssociationService.applyAnswer(vo=it)
|
|
call.respond(ApiResponse(message = "提交成功",body = true))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "提交成功",body = false))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
post("/check/join"){
|
|
call.withToken<SearchExamVo> {
|
|
try {
|
|
call.respond(ApiResponse(message = "入团记录查询成功",body=AssociationService.joinAssociation(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "入团记录查询失败",body=null))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/audit/join"){
|
|
call.withToken<AuditJoinVo> {
|
|
try {
|
|
AssociationService.auditJoin(vo = it)
|
|
call.respond(ApiResponse(message = "审核成功",body=true))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "审核失败",body=false))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/show/answer"){
|
|
call.withToken<ShowAnswerVo> {
|
|
try {
|
|
call.respond(ApiResponse(message = "查看答案成功",body = AssociationService.showAnswer(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "查看答案失败",body=null))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
route("/rename"){
|
|
register(RenameService)
|
|
|
|
accept(RenameService)
|
|
|
|
audit(RenameService)
|
|
check(RenameService)
|
|
read(RenameService,Renames)
|
|
}
|
|
|
|
post("/update"){
|
|
call.withToken<UpdateAssociationVo> {
|
|
AssociationService.update(vo=it.associationVo)
|
|
call.respond(ApiResponse(message = "社团更新成功",body = true))
|
|
}
|
|
}
|
|
|
|
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-----")
|
|
}
|
|
|
|
route("/list"){
|
|
post{
|
|
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-----")
|
|
}
|
|
|
|
post("/all"){
|
|
log.info("获取所有社团")
|
|
call.withToken<OnlyToken>{
|
|
try {
|
|
call.respond(ApiResponse(message = "社团列表检索完成",body=AssociationService.loadAll()))
|
|
} 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("/tendency"){
|
|
call.withToken<TendencyVo> {
|
|
try {
|
|
ActivityService.tendency(vo=it)
|
|
call.respond(ApiResponse(message = "${it.type.desc}成功",body = true))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "${it.type.desc}失败",body = false))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/tendency/check"){
|
|
call.withToken<CheckTendencyVo> {
|
|
try {
|
|
call.respond(ApiResponse(message = "活动倾向查询成功",body=ActivityService.checkTendency(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "活动倾向查询失败",body=null))
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
post("/tendency/find"){
|
|
call.withToken<FindTendencyVo> {
|
|
try {
|
|
call.respond(ApiResponse(message = "活动倾向查询成功",body=ActivityService.findTendency(vo=it)))
|
|
} catch (e: Exception) {
|
|
log.error(e)
|
|
call.respond(ApiResponse(message = "活动倾向查询失败",body=null))
|
|
}
|
|
}
|
|
}
|
|
|
|
post("/list/all"){
|
|
call.withToken<OnlyToken> {
|
|
call.respond(ApiResponse(message = "活动信息加载",body = ActivityService.listAll()))
|
|
}
|
|
}
|
|
|
|
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)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |