246 lines
6.2 KiB
Go
246 lines
6.2 KiB
Go
package handlers
|
||
|
||
import (
|
||
"goalfymax-admin/internal/models"
|
||
"goalfymax-admin/internal/services"
|
||
"goalfymax-admin/pkg/utils"
|
||
"strconv"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type GoalfyMaxUserHandler struct {
|
||
service services.GoalfyMaxUserService
|
||
response *utils.Response
|
||
}
|
||
|
||
func NewGoalfyMaxUserHandler(s services.GoalfyMaxUserService) *GoalfyMaxUserHandler {
|
||
return &GoalfyMaxUserHandler{service: s, response: utils.NewResponse()}
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) List(c *gin.Context) {
|
||
var req models.GoalfyMaxUserListRequest
|
||
if err := c.ShouldBindQuery(&req); err != nil {
|
||
h.response.ValidateError(c, err)
|
||
return
|
||
}
|
||
users, total, err := h.service.List(&req)
|
||
if err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, gin.H{"users": users, "total": total, "page": req.Page, "size": req.Size})
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) GetByID(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
user, err := h.service.GetByID(uint(id64))
|
||
if err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, user)
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) Create(c *gin.Context) {
|
||
var req models.GoalfyMaxUserCreateRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
h.response.ValidateError(c, err)
|
||
return
|
||
}
|
||
user, err := h.service.Create(&req)
|
||
if err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, user)
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) Update(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
var req models.GoalfyMaxUserUpdateRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
h.response.ValidateError(c, err)
|
||
return
|
||
}
|
||
user, err := h.service.Update(uint(id64), &req)
|
||
if err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, user)
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) Delete(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
if err := h.service.Delete(uint(id64)); err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, gin.H{"message": "删除成功"})
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) Ban(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
var req models.GoalfyMaxUserBanRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
h.response.ValidateError(c, err)
|
||
return
|
||
}
|
||
|
||
// 从上下文获取当前管理员ID
|
||
adminID := 0
|
||
if userID, exists := c.Get("user_id"); exists {
|
||
switch v := userID.(type) {
|
||
case int:
|
||
adminID = v
|
||
case uint:
|
||
adminID = int(v)
|
||
case string:
|
||
if parsedID, err := strconv.Atoi(v); err == nil {
|
||
adminID = parsedID
|
||
}
|
||
}
|
||
}
|
||
|
||
if err := h.service.Ban(uint(id64), &req, adminID); err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, gin.H{"message": "封禁成功"})
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) Unban(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
if err := h.service.Unban(uint(id64)); err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, gin.H{"message": "解封成功"})
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) AddBalance(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
var req models.GoalfyMaxUserAddBalanceRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
h.response.ValidateError(c, err)
|
||
return
|
||
}
|
||
|
||
// 从上下文获取当前管理员信息
|
||
operatorID := 0
|
||
operatorEmail := "system@goalfy.com"
|
||
if userID, exists := c.Get("user_id"); exists {
|
||
switch v := userID.(type) {
|
||
case int:
|
||
operatorID = v
|
||
case uint:
|
||
operatorID = int(v)
|
||
case string:
|
||
if parsedID, err := strconv.Atoi(v); err == nil {
|
||
operatorID = parsedID
|
||
}
|
||
}
|
||
}
|
||
// 尝试获取用户邮箱(从userInfo中获取)
|
||
if userInfo, exists := c.Get("user"); exists {
|
||
if user, ok := userInfo.(*models.UserInfo); ok && user != nil {
|
||
if user.Email != "" {
|
||
operatorEmail = user.Email
|
||
} else if user.PreferredUsername != "" {
|
||
operatorEmail = user.PreferredUsername + "@goalfy.com"
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取IP地址和UserAgent
|
||
ipAddress := c.ClientIP()
|
||
userAgent := c.Request.UserAgent()
|
||
|
||
if err := h.service.AddBalance(uint(id64), &req, operatorID, operatorEmail, ipAddress, userAgent); err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, gin.H{"message": "增加余额成功"})
|
||
}
|
||
|
||
func (h *GoalfyMaxUserHandler) DeductBalance(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id64, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
h.response.BadRequest(c, "无效的ID")
|
||
return
|
||
}
|
||
var req models.GoalfyMaxUserAddBalanceRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
h.response.ValidateError(c, err)
|
||
return
|
||
}
|
||
|
||
// 从上下文获取当前管理员信息
|
||
operatorID := 0
|
||
operatorEmail := "system@goalfy.com"
|
||
if userID, exists := c.Get("user_id"); exists {
|
||
switch v := userID.(type) {
|
||
case int:
|
||
operatorID = v
|
||
case uint:
|
||
operatorID = int(v)
|
||
case string:
|
||
if parsedID, err := strconv.Atoi(v); err == nil {
|
||
operatorID = parsedID
|
||
}
|
||
}
|
||
}
|
||
// 尝试获取用户邮箱(从userInfo中获取)
|
||
if userInfo, exists := c.Get("user"); exists {
|
||
if user, ok := userInfo.(*models.UserInfo); ok && user != nil {
|
||
if user.Email != "" {
|
||
operatorEmail = user.Email
|
||
} else if user.PreferredUsername != "" {
|
||
operatorEmail = user.PreferredUsername + "@goalfy.com"
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取IP地址和UserAgent
|
||
ipAddress := c.ClientIP()
|
||
userAgent := c.Request.UserAgent()
|
||
|
||
if err := h.service.DeductBalance(uint(id64), &req, operatorID, operatorEmail, ipAddress, userAgent); err != nil {
|
||
h.response.InternalServerError(c, err.Error())
|
||
return
|
||
}
|
||
h.response.Success(c, gin.H{"message": "减少余额成功"})
|
||
}
|