177 lines
5.5 KiB
Go
177 lines
5.5 KiB
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"ego/internal/serializer"
|
||
|
"ego/internal/service"
|
||
|
"ego/internal/types"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
type SysUserHandler struct {
|
||
|
UserSvc *service.SysUserService
|
||
|
}
|
||
|
|
||
|
// NewSysUserHandler 提供 SysUserHandler 的实例
|
||
|
func NewSysUserHandler(userSvc *service.SysUserService) *SysUserHandler {
|
||
|
return &SysUserHandler{
|
||
|
UserSvc: userSvc,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create 创建用户
|
||
|
// @Summary 创建用户
|
||
|
// @Description 创建新用户
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Param user body model.SysUser true "用户信息"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser [post]
|
||
|
func (h *SysUserHandler) Create(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.Create(c))
|
||
|
}
|
||
|
|
||
|
// DeleteByID 删除用户
|
||
|
// @Summary 删除用户
|
||
|
// @Description 根据ID删除用户
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Param id path string true "用户ID"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/{id} [delete]
|
||
|
func (h *SysUserHandler) DeleteByID(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.DeleteByID(c))
|
||
|
}
|
||
|
|
||
|
// UpdateByID 更新用户
|
||
|
// @Summary 更新用户
|
||
|
// @Description 根据ID更新用户信息
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Param id path string true "用户ID"
|
||
|
// @Param user body model.SysUser true "用户信息"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/{id} [put]
|
||
|
func (h *SysUserHandler) UpdateByID(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.UpdateByID(c))
|
||
|
}
|
||
|
|
||
|
// GetByID 根据ID查询用户
|
||
|
// @Summary 获取用户
|
||
|
// @Description 根据ID获取用户信息
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Param id path string true "用户ID"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/{id} [get]
|
||
|
func (h *SysUserHandler) GetByID(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.GetByID(c))
|
||
|
}
|
||
|
|
||
|
// DeleteByIDs 批量删除用户
|
||
|
// @Summary 批量删除用户
|
||
|
// @Description 根据ID列表批量删除用户
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security ApiKeyAuth
|
||
|
// @Param data body types.Payload true "ID列表"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/batch [delete]
|
||
|
func (h *SysUserHandler) DeleteByIDs(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.DeleteByIDs(c))
|
||
|
}
|
||
|
|
||
|
// GetByCondition 根据条件查询用户
|
||
|
// @Summary 条件查询用户
|
||
|
// @Description 根据条件查询用户列表
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Param query body types.Params true "查询条件"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/condition [post]
|
||
|
func (h *SysUserHandler) GetByCondition(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.GetByCondition(c))
|
||
|
}
|
||
|
|
||
|
// ListByIDs 根据ID列表查询用户
|
||
|
// @Summary 批量查询用户
|
||
|
// @Description 根据ID列表批量查询用户
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Param ids body []string true "用户ID列表"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/list/ids [post]
|
||
|
func (h *SysUserHandler) ListByIDs(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.ListByIDs(c))
|
||
|
}
|
||
|
|
||
|
// UserRegister 用户注册接口
|
||
|
// @Summary 用户注册
|
||
|
// @Description 新用户注册接口
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param user body service.UserRegisterRequest true "用户注册信息"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/user/register [post]
|
||
|
func (h *SysUserHandler) UserRegister(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.Register(c))
|
||
|
}
|
||
|
|
||
|
// UserLogin 用户登录接口
|
||
|
// @Summary 用户登录
|
||
|
// @Description 用户登录接口
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param user body service.UserLoginRequest true "用户登录信息"
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/user/login [post]
|
||
|
func (h *SysUserHandler) UserLogin(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.Login(c))
|
||
|
}
|
||
|
|
||
|
// UserMe 用户详情
|
||
|
// @Summary 获取当前用户信息
|
||
|
// @Description 获取当前登录用户的详细信息
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/me [get]
|
||
|
func (h *SysUserHandler) UserMe(c *gin.Context) {
|
||
|
if user, err := h.UserSvc.CurrentUser(c); err == nil {
|
||
|
c.JSON(http.StatusOK, types.BuildUserResponse(*user))
|
||
|
} else {
|
||
|
c.JSON(http.StatusOK, serializer.Err(http.StatusInternalServerError, "获取用户信息失败!", err))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// UserLogout 用户登出
|
||
|
// @Summary 用户登出
|
||
|
// @Description 用户退出登录
|
||
|
// @Tags 用户管理
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security BearerAuth
|
||
|
// @Success 200 {object} serializer.Response
|
||
|
// @Router /api/v1/sysUser/logout [post]
|
||
|
func (h *SysUserHandler) UserLogout(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, h.UserSvc.UserLogout(c))
|
||
|
}
|