191 lines
4.6 KiB
Go
191 lines
4.6 KiB
Go
package adminhandler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
func (h *Handler) ListUsers(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "0"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("size", "50"))
|
|
list, total, err := h.svc.ListUsers(c.Request.Context(), c.Query("email"), page, size)
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, gin.H{
|
|
"content": list, "totalElements": total,
|
|
"page": page, "size": size, "appCode": "spark",
|
|
})
|
|
}
|
|
|
|
func (h *Handler) UserByEmail(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
out, err := h.svc.UserByEmail(c.Request.Context(), c.Query("email"))
|
|
if err != nil {
|
|
if errors.Is(err, errs.ErrInvalidArgument) {
|
|
Fail(c, http.StatusBadRequest, "email 必填")
|
|
return
|
|
}
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, out)
|
|
}
|
|
|
|
func (h *Handler) UserByID(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
u, err := h.svc.UserByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
Fail(c, http.StatusNotFound, "用户不存在")
|
|
return
|
|
}
|
|
OK(c, u)
|
|
}
|
|
|
|
func (h *Handler) UserByNickname(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
list, err := h.svc.UsersByNickname(c.Request.Context(), c.Query("nickname"))
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, gin.H{"items": list, "count": len(list)})
|
|
}
|
|
|
|
func (h *Handler) UserByDevice(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
list, err := h.svc.UsersByDevice(c.Request.Context(), c.Query("deviceId"))
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, gin.H{"items": list, "count": len(list)})
|
|
}
|
|
|
|
func (h *Handler) UserByIP(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
ip := c.Query("ip")
|
|
list, err := h.svc.UsersByIP(c.Request.Context(), ip)
|
|
if err != nil {
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OK(c, gin.H{"items": list, "count": len(list), "ip": ip})
|
|
}
|
|
|
|
type emailActionReq struct {
|
|
Email string `json:"email"`
|
|
Reason string `json:"reason"`
|
|
Value *bool `json:"value"`
|
|
Banned *bool `json:"banned"`
|
|
Nickname string `json:"nickname"`
|
|
}
|
|
|
|
func (h *Handler) BanByEmail(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
var req emailActionReq
|
|
_ = c.ShouldBindJSON(&req)
|
|
email := firstNonEmpty(req.Email, c.Query("email"))
|
|
banned := true
|
|
if req.Banned != nil {
|
|
banned = *req.Banned
|
|
} else if req.Value != nil {
|
|
banned = *req.Value
|
|
}
|
|
if err := h.svc.BanByEmail(c.Request.Context(), email, banned); err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
Fail(c, http.StatusNotFound, "用户不存在")
|
|
return
|
|
}
|
|
if errors.Is(err, errs.ErrInvalidArgument) {
|
|
Fail(c, http.StatusBadRequest, "email 必填")
|
|
return
|
|
}
|
|
Fail(c, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
OKMsg(c, "已更新封禁状态", gin.H{"email": email, "banned": banned})
|
|
}
|
|
|
|
func (h *Handler) ResetNicknameByEmail(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
var req emailActionReq
|
|
_ = c.ShouldBindJSON(&req)
|
|
email := firstNonEmpty(req.Email, c.Query("email"))
|
|
nick := req.Nickname
|
|
if err := h.svc.ResetNicknameByEmail(c.Request.Context(), email, nick); err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
Fail(c, http.StatusNotFound, "用户不存在")
|
|
return
|
|
}
|
|
Fail(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if nick == "" {
|
|
nick = "用户"
|
|
}
|
|
OKMsg(c, "已重置昵称", gin.H{"email": email, "nickname": nick})
|
|
}
|
|
|
|
func (h *Handler) ResetAvatarByEmail(c *gin.Context) {
|
|
if !h.ready(c) {
|
|
return
|
|
}
|
|
var req emailActionReq
|
|
_ = c.ShouldBindJSON(&req)
|
|
email := firstNonEmpty(req.Email, c.Query("email"))
|
|
if err := h.svc.ResetAvatarByEmail(c.Request.Context(), email); err != nil {
|
|
if errors.Is(err, errs.ErrNotFound) {
|
|
Fail(c, http.StatusNotFound, "用户不存在")
|
|
return
|
|
}
|
|
Fail(c, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
OKMsg(c, "已重置头像", gin.H{"email": email})
|
|
}
|
|
|
|
func (h *Handler) ResetPasswordByEmail(c *gin.Context) {
|
|
Fail(c, http.StatusBadRequest, "Spark V3 使用邮箱验证码登录,无密码可重置")
|
|
}
|
|
|
|
func (h *Handler) WhitelistByEmail(c *gin.Context) {
|
|
OKMsg(c, "V3 暂无账号白名单字段,已忽略", gin.H{"note": "请在 Java ops lists 维护"})
|
|
}
|
|
|
|
func (h *Handler) MarkAbnormalByEmail(c *gin.Context) {
|
|
OKMsg(c, "V3 暂无异常标记字段,已忽略", gin.H{"note": "可用封禁代替"})
|
|
}
|
|
|
|
func firstNonEmpty(a, b string) string {
|
|
if strings.TrimSpace(a) != "" {
|
|
return strings.TrimSpace(a)
|
|
}
|
|
return strings.TrimSpace(b)
|
|
}
|