feat: 优化web

This commit is contained in:
2026-04-23 18:58:13 +08:00
commit 544a2f3428
160 changed files with 27327 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
package handler
import (
"net/http"
"strconv"
"giter.top/smart/internal/iam/service"
"github.com/gin-gonic/gin"
)
type UserHandler struct {
svc service.UserService
}
func NewUserHandler(svc service.UserService) *UserHandler {
return &UserHandler{svc: svc}
}
func (h *UserHandler) Create(c *gin.Context) {
tid := headerTenantID(c)
var req service.CreateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
u, err := h.svc.Create(c.Request.Context(), tid, &req)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, u)
}
func (h *UserHandler) Update(c *gin.Context) {
tid := headerTenantID(c)
uid := c.Param("id")
if uid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
var req service.UpdateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
u, err := h.svc.Update(c.Request.Context(), tid, uid, &req)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, u)
}
func (h *UserHandler) Delete(c *gin.Context) {
tid := headerTenantID(c)
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(), tid, ids); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.Status(http.StatusNoContent)
}
func (h *UserHandler) Get(c *gin.Context) {
tid := headerTenantID(c)
uid := c.Param("id")
if uid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
u, err := h.svc.Get(c.Request.Context(), tid, uid)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, u)
}
func (h *UserHandler) List(c *gin.Context) {
tid := headerTenantID(c)
q := &service.UserListQuery{
Keyword: c.Query("keyword"),
Page: atoiDef(c.Query("page"), 1),
PageSize: atoiDef(c.Query("page_size"), 10),
}
if s := c.Query("dept_id"); s != "" {
q.DeptID = &s
}
if s := c.Query("role_id"); s != "" {
q.RoleID = &s
}
if s := c.Query("status"); s != "" {
v64, err := strconv.ParseInt(s, 10, 16)
if err == nil {
v := int16(v64)
q.Status = &v
}
}
resp, err := h.svc.List(c.Request.Context(), tid, q)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, resp)
}
func (h *UserHandler) DataScope(c *gin.Context) {
uid := headerUserID(c)
if uid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "需要 X-User-ID"})
return
}
ds, err := h.svc.DataScopeForUser(c.Request.Context(), uid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data_scope": ds})
}