后端: Go(Gin+GORM+MySQL+Redis) - 17张业务表(yz_pw_前缀), 自动迁移 - JWT认证(3小时) + 盐+MD5密码 + 图形验证码 - 班级CRUD/加入(8人姓名验证/邀请码)/审核/30天自动清理 - 系统配置(16项)/菜单管理(动态路由)/数据统计 - 文件MD5去重/数据隔离/账号封禁/敏感词DFA检测 前端: Vue3+Vite+Element Plus+Less+ECharts+FontAwesome - 动态路由(数据库菜单驱动) - 蓝白配色, H5响应式 - 登录/注册/忘记密码/个人中心 - 班级创建向导/详情/加入/列表 - 管理后台: 审核/配置/菜单/统计/用户/敏感词 数据库: MySQL 10.31.100.3:3306/photowall 管理员: hero920103 / 920103
158 lines
3.9 KiB
Go
158 lines
3.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"photowall/internal/model"
|
|
"photowall/internal/service"
|
|
"photowall/pkg/response"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type SchoolHandler struct {
|
|
svc *service.SchoolService
|
|
}
|
|
|
|
func NewSchoolHandler(svc *service.SchoolService) *SchoolHandler {
|
|
return &SchoolHandler{svc: svc}
|
|
}
|
|
|
|
// List GET /api/schools
|
|
func (h *SchoolHandler) List(c *gin.Context) {
|
|
var q service.SchoolQuery
|
|
if err := c.ShouldBindQuery(&q); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
result, err := h.svc.List(&q)
|
|
if err != nil {
|
|
response.Internal(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
// Create POST /api/schools
|
|
func (h *SchoolHandler) Create(c *gin.Context) {
|
|
var school model.School
|
|
if err := c.ShouldBindJSON(&school); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.svc.Create(&school); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.Created(c, school)
|
|
}
|
|
|
|
// Get GET /api/schools/:id
|
|
func (h *SchoolHandler) Get(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
school, err := h.svc.GetByID(uint(id))
|
|
if err != nil {
|
|
response.NotFound(c, err.Error())
|
|
return
|
|
}
|
|
// 附带学院列表
|
|
colleges, _ := h.svc.ListColleges(uint(id))
|
|
response.OK(c, gin.H{"school": school, "colleges": colleges})
|
|
}
|
|
|
|
// Update PUT /api/schools/:id
|
|
func (h *SchoolHandler) Update(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var body map[string]interface{}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
// 只允许更新部分字段
|
|
updates := map[string]interface{}{}
|
|
if v, ok := body["name"]; ok {
|
|
updates["name"] = v
|
|
}
|
|
if v, ok := body["name_en"]; ok {
|
|
updates["name_en"] = v
|
|
}
|
|
if v, ok := body["school_type"]; ok {
|
|
updates["school_type"] = v
|
|
}
|
|
if v, ok := body["address"]; ok {
|
|
updates["address"] = v
|
|
}
|
|
if err := h.svc.Update(uint(id), updates); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
// Delete DELETE /api/schools/:id
|
|
func (h *SchoolHandler) Delete(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.svc.Delete(uint(id)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "删除成功"})
|
|
}
|
|
|
|
// ---- 学院 ----
|
|
|
|
// ListColleges GET /api/schools/:id/colleges
|
|
func (h *SchoolHandler) ListColleges(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
list, err := h.svc.ListColleges(uint(id))
|
|
if err != nil {
|
|
response.Internal(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, list)
|
|
}
|
|
|
|
// CreateCollege POST /api/schools/:id/colleges
|
|
func (h *SchoolHandler) CreateCollege(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var body struct {
|
|
Name string `json:"name" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.BadRequest(c, "学院名称不能为空")
|
|
return
|
|
}
|
|
college, err := h.svc.CreateCollege(uint(id), body.Name)
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.Created(c, college)
|
|
}
|
|
|
|
// UpdateCollege PUT /api/colleges/:id
|
|
func (h *SchoolHandler) UpdateCollege(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var body struct {
|
|
Name string `json:"name" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.BadRequest(c, "学院名称不能为空")
|
|
return
|
|
}
|
|
if err := h.svc.UpdateCollege(uint(id), body.Name); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
// DeleteCollege DELETE /api/colleges/:id
|
|
func (h *SchoolHandler) DeleteCollege(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.svc.DeleteCollege(uint(id)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"message": "删除成功"})
|
|
}
|