From 389897b1ed5386c0a7e07900c1aa2ddb1f01285e Mon Sep 17 00:00:00 2001 From: XiMo-210 <2831802697@qq.com> Date: Sun, 6 Aug 2023 00:26:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9Bredis?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=9B=B8=E5=85=B3=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/session/config.go | 2 +- config/session/redis.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/session/config.go b/config/session/config.go index 585f968..5066e59 100644 --- a/config/session/config.go +++ b/config/session/config.go @@ -52,7 +52,7 @@ func getRedisConfig() redisConfig { Info.DB = config.Config.GetInt("redis.db") } if config.Config.IsSet("redis.password") { - Info.DB = config.Config.GetInt("redis.password") + Info.Password = config.Config.GetString("redis.password") } return Info } diff --git a/config/session/redis.go b/config/session/redis.go index 8ea8730..034dad9 100644 --- a/config/session/redis.go +++ b/config/session/redis.go @@ -15,6 +15,6 @@ type redisConfig struct { func setRedis(r *gin.Engine, name string) { Info := getRedisConfig() - store, _ := sessionRedis.NewStore(10, "tcp", Info.Host+":"+Info.Port, "", []byte("secret")) + store, _ := sessionRedis.NewStore(10, "tcp", Info.Host+":"+Info.Port, Info.Password, []byte("secret")) r.Use(sessions.Sessions(name, store)) } From c7a6233ec14e944c2f19b1f7e077479fe1285804 Mon Sep 17 00:00:00 2001 From: XiMo-210 <2831802697@qq.com> Date: Sun, 6 Aug 2023 01:27:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=89=A9=E8=B5=84?= =?UTF-8?q?=E5=80=9F=E7=94=A8=E9=97=AE=E7=AD=94=E6=9D=BF=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../suppliesBorrowController/QA.go | 170 ++++++++++++++++++ app/midwares/checkSuppliesBorrowAdmin.go | 22 +++ app/models/QA.go | 11 ++ app/services/suppliesBorrowService/QA.go | 78 ++++++++ config/database/migrations.go | 7 +- config/router/adminRouter.go | 18 +- config/router/funcRouter.go | 10 +- 7 files changed, 312 insertions(+), 4 deletions(-) create mode 100644 app/controllers/funcControllers/suppliesBorrowController/QA.go create mode 100644 app/midwares/checkSuppliesBorrowAdmin.go create mode 100644 app/models/QA.go create mode 100644 app/services/suppliesBorrowService/QA.go diff --git a/app/controllers/funcControllers/suppliesBorrowController/QA.go b/app/controllers/funcControllers/suppliesBorrowController/QA.go new file mode 100644 index 0000000..d44902f --- /dev/null +++ b/app/controllers/funcControllers/suppliesBorrowController/QA.go @@ -0,0 +1,170 @@ +package suppliesBorrowController + +import ( + "time" + "wejh-go/app/apiException" + "wejh-go/app/models" + "wejh-go/app/services/sessionServices" + "wejh-go/app/services/suppliesBorrowService" + "wejh-go/app/utils" + + "github.com/gin-gonic/gin" +) + +type CreateQAData struct { + Title string `json:"title" binding:"required"` + Content string `json:"content" binding:"required"` +} + +// 创建 QA +func CreateQA(c *gin.Context) { + var data CreateQAData + err := c.ShouldBindJSON(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + publisher := suppliesBorrowService.GetIdentity(c) + err = suppliesBorrowService.CreateQA(models.QA{ + Title: data.Title, + Content: data.Content, + PublishTime: time.Now(), + Publisher: *publisher, + }) + + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, nil) +} + +type GetQAListData struct { + Publisher string `form:"publisher" binding:"required"` +} + +// 用户获取对应发布方的 QA +func GetQAList(c *gin.Context) { + var data GetQAListData + err := c.ShouldBindQuery(&data) + if err != nil { + c.AbortWithError(200, apiException.ParamError) + return + } + + _, err = sessionServices.GetUserSession(c) + if err != nil { + _ = c.AbortWithError(200, apiException.NotLogin) + return + } + + var QAList []models.QA + QAList, err = suppliesBorrowService.GetQAListByPublisher(data.Publisher) + if err != nil { + c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, QAList) +} + +// 管理员获取自己发布的 QA +func GetQAListByAdmin(c *gin.Context) { + admin := suppliesBorrowService.GetIdentity(c) + + var QAList []models.QA + var err error + if *admin == "Admin" { + QAList, err = suppliesBorrowService.GetQAListBySuperAdmin() + } else { + QAList, err = suppliesBorrowService.GetQAListByPublisher(*admin) + } + + if err != nil { + c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, QAList) +} + +type UpdateQAData struct { + ID int `json:"id" binding:"required"` + Title string `json:"title" binding:"required"` + Content string `json:"content" binding:"required"` +} + +// 更新 QA +func UpdateQA(c *gin.Context) { + var data UpdateQAData + err := c.ShouldBindJSON(&data) + if err != nil { + c.AbortWithError(200, apiException.ParamError) + return + } + + pubisher := suppliesBorrowService.GetIdentity(c) + + var QA *models.QA + QA, err = suppliesBorrowService.GetQAbyID(data.ID) + if err != nil { + c.AbortWithError(200, apiException.ServerError) + return + } + + if QA.Publisher != *pubisher && *pubisher != "Admin" { + c.AbortWithError(200, apiException.ServerError) + return + } + + err = suppliesBorrowService.UpdateQA(models.QA{ + ID: data.ID, + Title: data.Title, + Content: data.Content, + PublishTime: time.Now(), + }) + if err != nil { + c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, nil) +} + +type DeleteQAData struct { + ID int `form:"id" binding:"required"` +} + +// 删除 QA +func DeleteQA(c *gin.Context) { + var data DeleteQAData + err := c.ShouldBindQuery(&data) + if err != nil { + c.AbortWithError(200, apiException.ParamError) + return + } + + pubisher := suppliesBorrowService.GetIdentity(c) + + var QA *models.QA + QA, err = suppliesBorrowService.GetQAbyID(data.ID) + if err != nil { + c.AbortWithError(200, apiException.ServerError) + return + } + + if QA.Publisher != *pubisher && *pubisher != "Admin" { + c.AbortWithError(200, apiException.ServerError) + return + } + + err = suppliesBorrowService.DeleteQA(data.ID) + if err != nil { + c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, nil) +} diff --git a/app/midwares/checkSuppliesBorrowAdmin.go b/app/midwares/checkSuppliesBorrowAdmin.go new file mode 100644 index 0000000..daad8d2 --- /dev/null +++ b/app/midwares/checkSuppliesBorrowAdmin.go @@ -0,0 +1,22 @@ +package midwares + +import ( + "wejh-go/app/apiException" + "wejh-go/app/models" + "wejh-go/app/services/sessionServices" + + "github.com/gin-gonic/gin" +) + +func CheckSuppliesBorrowAdmin(c *gin.Context) { + user, err := sessionServices.GetUserSession(c) + if err != nil { + _ = c.AbortWithError(200, apiException.NotLogin) + return + } + if user.Type != models.StudentAffairsCenter && user.Type != models.Admin { + _ = c.AbortWithError(200, apiException.NotAdmin) + return + } + c.Next() +} diff --git a/app/models/QA.go b/app/models/QA.go new file mode 100644 index 0000000..dd11c04 --- /dev/null +++ b/app/models/QA.go @@ -0,0 +1,11 @@ +package models + +import "time" + +type QA struct { + ID int `json:"id"` + Title string `json:"title"` + Content string `json:"content"` + PublishTime time.Time `json:"publish_time" gorm:"type:timestamp;"` + Publisher string `json:"publisher"` +} diff --git a/app/services/suppliesBorrowService/QA.go b/app/services/suppliesBorrowService/QA.go new file mode 100644 index 0000000..0171029 --- /dev/null +++ b/app/services/suppliesBorrowService/QA.go @@ -0,0 +1,78 @@ +package suppliesBorrowService + +import ( + "wejh-go/app/apiException" + "wejh-go/app/models" + "wejh-go/app/services/sessionServices" + "wejh-go/config/database" + + "github.com/gin-gonic/gin" +) + +// 获取身份 +func GetIdentity(c *gin.Context) *string { + user, err := sessionServices.GetUserSession(c) + if err != nil { + c.AbortWithError(200, apiException.NotLogin) + return nil + } + + var identity string + if user.Type == models.StudentAffairsCenter { + identity = "学生事务大厅" + } else if user.Type == models.Admin { + identity = "Admin" + } else { + c.AbortWithError(200, apiException.ServerError) + return nil + } + + return &identity +} + +func CreateQA(QA models.QA) error { + result := database.DB.Create(&QA) + return result.Error +} + +// 根据发布组织获取 QA +func GetQAListByPublisher(publisher string) ([]models.QA, error) { + var QAList []models.QA + result := database.DB.Where("publisher = ?", publisher).Find(&QAList) + if result.Error != nil { + return nil, result.Error + } + return QAList, nil +} + +// 获取所有问答 +func GetQAListBySuperAdmin() ([]models.QA, error) { + var QAList []models.QA + result := database.DB.Order("publisher").Find(&QAList) + if result.Error != nil { + return nil, result.Error + } + return QAList, nil +} + +// 通过 id 获取 QA +func GetQAbyID(id int) (*models.QA, error) { + var QA models.QA + result := database.DB.Where("id = ?", id).First(&QA) + if result.Error != nil { + return nil, result.Error + } + return &QA, nil +} + +// 更新 QA +func UpdateQA(QA models.QA) error { + result := database.DB.Model(&QA).Updates(QA) + return result.Error +} + +// 删除 QA +func DeleteQA(id int) error { + result := database.DB.Delete(models.QA{ID: id}) + return result.Error +} diff --git a/config/database/migrations.go b/config/database/migrations.go index d78adae..be7ef9c 100755 --- a/config/database/migrations.go +++ b/config/database/migrations.go @@ -1,8 +1,9 @@ package database import ( - "gorm.io/gorm" "wejh-go/app/models" + + "gorm.io/gorm" ) func autoMigrate(db *gorm.DB) error { @@ -18,5 +19,7 @@ func autoMigrate(db *gorm.DB) error { &models.LostAndFoundRecord{}, &models.LostKind{}, &models.Notice{}, - &models.LowBatteryQueryRecord{}) + &models.LowBatteryQueryRecord{}, + &models.QA{}, + ) } diff --git a/config/router/adminRouter.go b/config/router/adminRouter.go index 137a0f7..8e4bd74 100755 --- a/config/router/adminRouter.go +++ b/config/router/adminRouter.go @@ -1,11 +1,13 @@ package router import ( - "github.com/gin-gonic/gin" "wejh-go/app/controllers/adminController" "wejh-go/app/controllers/funcControllers/lostAndFoundRecordController" "wejh-go/app/controllers/funcControllers/noticeController" + "wejh-go/app/controllers/funcControllers/suppliesBorrowController" "wejh-go/app/midwares" + + "github.com/gin-gonic/gin" ) // 注册杂项路由 @@ -62,4 +64,18 @@ func adminRouterInit(r *gin.RouterGroup) { notice.PUT("", noticeController.UpdateNotice) } } + + stuAC := r.Group("/stuac", midwares.CheckSuppliesBorrowAdmin) + { + supplies := stuAC.Group("/supplies") + { + qa := supplies.Group("/qa") + { + qa.GET("", suppliesBorrowController.GetQAListByAdmin) + qa.POST("", suppliesBorrowController.CreateQA) + qa.PUT("", suppliesBorrowController.UpdateQA) + qa.DELETE("", suppliesBorrowController.DeleteQA) + } + } + } } diff --git a/config/router/funcRouter.go b/config/router/funcRouter.go index 8978dc7..49cf7f9 100755 --- a/config/router/funcRouter.go +++ b/config/router/funcRouter.go @@ -1,7 +1,6 @@ package router import ( - "github.com/gin-gonic/gin" "wejh-go/app/controllers/funcControllers/canteenController" "wejh-go/app/controllers/funcControllers/customizeHomeController" "wejh-go/app/controllers/funcControllers/lessonController" @@ -9,10 +8,13 @@ import ( "wejh-go/app/controllers/funcControllers/lostAndFoundRecordController" "wejh-go/app/controllers/funcControllers/noticeController" "wejh-go/app/controllers/funcControllers/schoolBusController" + "wejh-go/app/controllers/funcControllers/suppliesBorrowController" "wejh-go/app/controllers/funcControllers/zfController" "wejh-go/app/controllers/yxyController/electricityController" "wejh-go/app/controllers/yxyController/schoolCardController" "wejh-go/app/midwares" + + "github.com/gin-gonic/gin" ) func funcRouterInit(r *gin.RouterGroup) { @@ -77,9 +79,15 @@ func funcRouterInit(r *gin.RouterGroup) { lost.GET("", lostAndFoundRecordController.GetRecords) lost.GET("/kind_list", lostAndFoundRecordController.GetKindList) } + notice := fun.Group("/information", midwares.CheckLogin) { notice.GET("", noticeController.GetNotice) } + + supplies := fun.Group("/supplies", midwares.CheckLogin) + { + supplies.GET("/qa", suppliesBorrowController.GetQAList) + } } }