You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.4 KiB
Go
167 lines
4.4 KiB
Go
1 month ago
|
package user
|
||
|
|
||
|
import (
|
||
|
"cls/internal/application/user"
|
||
|
domainUser "cls/internal/domain/user"
|
||
|
middleware "cls/internal/infrastructure/middleware/auth"
|
||
|
"cls/internal/interfaces"
|
||
|
"cls/pkg/jwtfx"
|
||
|
"cls/pkg/logger"
|
||
|
"cls/pkg/util/page"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
// UserHandler 处理请求阶段的逻辑
|
||
|
type UserHandler struct {
|
||
|
service *user.UserService
|
||
|
authMiddleware *middleware.AuthMiddleware
|
||
|
log logger.Logger
|
||
|
}
|
||
|
|
||
|
var _ interfaces.Handler = (*UserHandler)(nil)
|
||
|
|
||
|
func NewUserHandler(userService *user.UserService, authMiddleware *middleware.AuthMiddleware, logger logger.New) *UserHandler {
|
||
|
return &UserHandler{
|
||
|
service: userService,
|
||
|
authMiddleware: authMiddleware,
|
||
|
log: logger("cls:interfaces:user"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (u *UserHandler) RegisterRouters(router gin.IRouter) {
|
||
|
userGroup := router.Group("/user")
|
||
|
{
|
||
|
userGroup.GET("/profile", u.getProfile) // 获取个人信息
|
||
|
userGroup.POST("/set-password", u.getProfile) // 设置密码
|
||
|
userGroup.GET("/gift-count", u.giftCount) // 获取个人信息
|
||
|
userGroup.GET("/list", u.listUsers) // 获取用户列表
|
||
|
userGroup.POST("/disable/:uid", u.disable) // 禁用用户
|
||
|
userGroup.POST("/enable/:uid", u.enable) // 启用用户
|
||
|
userGroup.GET("/unlock-times", u.unlockTimes)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (u *UserHandler) giftCount(c *gin.Context) {
|
||
|
ePhone, err := u.authMiddleware.DecodeToken(c)
|
||
|
if err != nil {
|
||
|
u.log.Error(err.Error())
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
count, err := u.service.GetUserGiftCount(ePhone)
|
||
|
if err != nil {
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
} else {
|
||
|
c.AbortWithStatusJSON(http.StatusOK, count)
|
||
|
}
|
||
|
}
|
||
|
func (u *UserHandler) unlockTimes(c *gin.Context) {
|
||
|
u2 := &domainUser.User{}
|
||
|
err := c.ShouldBindJSON(u2)
|
||
|
if err != nil {
|
||
|
u.log.Error(err.Error())
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
err = u.service.SetPassword(u2.Phone, u2.Password)
|
||
|
if err != nil {
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
} else {
|
||
|
c.AbortWithStatus(http.StatusOK)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (u *UserHandler) setPassword(c *gin.Context) {
|
||
|
u2 := &domainUser.User{}
|
||
|
err := c.ShouldBindJSON(u2)
|
||
|
if err != nil {
|
||
|
u.log.Error(err.Error())
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
err = u.service.SetPassword(u2.Phone, u2.Password)
|
||
|
if err != nil {
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
} else {
|
||
|
c.AbortWithStatus(http.StatusOK)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// getProfile 获取个人信息
|
||
|
func (u *UserHandler) getProfile(c *gin.Context) {
|
||
|
has := jwtfx.IsGuest(c)
|
||
|
if has {
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// 使用新的 DecodeToken 方法,直接传入 gin.Context
|
||
|
ePhone, err := u.authMiddleware.DecodeToken(c)
|
||
|
if err != nil {
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
userInfo, err := u.service.GetUserProfileByPhone(ePhone)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, userInfo)
|
||
|
}
|
||
|
|
||
|
// listUsers 获取用户列表
|
||
|
func (u *UserHandler) listUsers(c *gin.Context) {
|
||
|
p := &page.Page{}
|
||
|
if err := c.ShouldBindQuery(p); err != nil {
|
||
|
u.log.Errorf("解析用户列表分页参数失败: %v", err)
|
||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := u.service.ListUsers(p, map[string]string{
|
||
|
"search_like_name": c.Query("search_like_name"),
|
||
|
"search_eq_org_id": c.Query("search_eq_org_id")}); err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, p)
|
||
|
}
|
||
|
|
||
|
// disable 禁用用户
|
||
|
func (u *UserHandler) disable(c *gin.Context) {
|
||
|
uid, err := strconv.ParseInt(c.Param("uid"), 10, 64)
|
||
|
if err != nil {
|
||
|
u.log.Error(err.Error())
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
if err := u.service.DisableUser(uint64(uid)); err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{"message": "用户已禁用"})
|
||
|
}
|
||
|
|
||
|
// enable 启用用户
|
||
|
func (u *UserHandler) enable(c *gin.Context) {
|
||
|
uid, err := strconv.ParseInt(c.Param("uid"), 10, 64)
|
||
|
if err != nil {
|
||
|
u.log.Error(err.Error())
|
||
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
if err := u.service.EnableUser(uint64(uid)); err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{"message": "用户已启用"})
|
||
|
}
|