98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"affiliate_dash/internal/middleware"
|
|
"affiliate_dash/internal/pkg/response"
|
|
"affiliate_dash/internal/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) ListPlatformAdmins(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
|
list, total, err := h.svc.ListPlatformAdmins(page, size)
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.Page(c, list, total, page, size)
|
|
}
|
|
|
|
func (h *UserHandler) ListMerchantAccountGroups(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
|
list, total, err := h.svc.ListMerchantAccountGroups(page, size)
|
|
if err != nil {
|
|
response.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
response.Page(c, list, total, page, size)
|
|
}
|
|
|
|
type createPlatformAdminReq struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Password string `json:"password" binding:"required,min=6"`
|
|
Nickname string `json:"nickname"`
|
|
}
|
|
|
|
func (h *UserHandler) CreatePlatformAdmin(c *gin.Context) {
|
|
var req createPlatformAdminReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
user, err := h.svc.CreatePlatformAdmin(req.Username, req.Password, req.Nickname)
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, user)
|
|
}
|
|
|
|
type updatePlatformAdminReq struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Nickname string `json:"nickname"`
|
|
Password string `json:"password" binding:"omitempty,min=6"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
func (h *UserHandler) UpdatePlatformAdmin(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
var req updatePlatformAdminReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误")
|
|
return
|
|
}
|
|
user, err := h.svc.UpdatePlatformAdmin(uint(id), service.UpdatePlatformAdminInput{
|
|
Username: req.Username,
|
|
Nickname: req.Nickname,
|
|
Password: req.Password,
|
|
Status: req.Status,
|
|
}, middleware.GetUserID(c))
|
|
if err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, user)
|
|
}
|
|
|
|
func (h *UserHandler) DeletePlatformAdmin(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err := h.svc.DeletePlatformAdmin(uint(id), middleware.GetUserID(c)); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, nil)
|
|
}
|