feat: 优化web
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"giter.top/smart/internal/iam/repository"
|
||||
"giter.top/smart/internal/iam/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MenuHandler struct {
|
||||
svc service.MenuService
|
||||
}
|
||||
|
||||
func NewMenuHandler(svc service.MenuService) *MenuHandler {
|
||||
return &MenuHandler{svc: svc}
|
||||
}
|
||||
|
||||
func isPlatformAdmin(c *gin.Context) bool {
|
||||
return headerTenantID(c) == entity.PlatformTenantID
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Create(c *gin.Context) {
|
||||
var req service.CreateMenuRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
m, err := h.svc.Create(c.Request.Context(), &req, isPlatformAdmin(c))
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrForbidden) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "仅平台管理员可维护菜单"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, m)
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Update(c *gin.Context) {
|
||||
mid := c.Param("id")
|
||||
if mid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
var req service.UpdateMenuRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
m, err := h.svc.Update(c.Request.Context(), mid, &req, isPlatformAdmin(c))
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrForbidden) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "仅平台管理员可维护菜单"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, m)
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Delete(c *gin.Context) {
|
||||
var ids []string
|
||||
if err := c.ShouldBindJSON(&ids); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.svc.Delete(c.Request.Context(), ids, isPlatformAdmin(c)); err != nil {
|
||||
if errors.Is(err, repository.ErrForbidden) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "仅平台管理员可维护菜单"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Get(c *gin.Context) {
|
||||
mid := c.Param("id")
|
||||
if mid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
m, err := h.svc.Get(c.Request.Context(), mid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, m)
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Tree(c *gin.Context) {
|
||||
var mt *int16
|
||||
if s := c.Query("menu_type"); s != "" {
|
||||
v64, err := strconv.ParseInt(s, 10, 16)
|
||||
if err == nil {
|
||||
v := int16(v64)
|
||||
mt = &v
|
||||
}
|
||||
}
|
||||
tree, err := h.svc.Tree(c.Request.Context(), mt)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, tree)
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Nav(c *gin.Context) {
|
||||
uid := headerUserID(c)
|
||||
if uid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "需要 X-User-ID"})
|
||||
return
|
||||
}
|
||||
tree, err := h.svc.NavForUser(c.Request.Context(), uid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, tree)
|
||||
}
|
||||
|
||||
func (h *MenuHandler) Perms(c *gin.Context) {
|
||||
uid := headerUserID(c)
|
||||
if uid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "需要 X-User-ID"})
|
||||
return
|
||||
}
|
||||
perms, err := h.svc.PermsForUser(c.Request.Context(), uid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"perms": perms})
|
||||
}
|
||||
Reference in New Issue
Block a user