171 lines
5.6 KiB
Go
171 lines
5.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
adminservice "wx_service/internal/admin/service"
|
|
"wx_service/internal/model"
|
|
)
|
|
|
|
type updatePasswordRequest struct {
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
NewPassword string `json:"new_password" binding:"required"`
|
|
}
|
|
|
|
type updateProfileRequest struct {
|
|
DisplayName string `json:"display_name" binding:"required"`
|
|
Email string `json:"email"`
|
|
Phone string `json:"phone"`
|
|
Timezone string `json:"timezone"`
|
|
}
|
|
|
|
type updateSystemConfigRequest struct {
|
|
SiteName string `json:"site_name" binding:"required"`
|
|
AllowRegister bool `json:"allow_register"`
|
|
LoginFailLimit int `json:"login_fail_limit"`
|
|
DefaultPageSize int `json:"default_page_size"`
|
|
AuditLogRetentionDays int `json:"audit_log_retention_days"`
|
|
}
|
|
|
|
func (h *Handler) GetSettings(c *gin.Context) {
|
|
claims, ok := CurrentAdminClaims(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.GetAdminSettings(c.Request.Context(), claims.AdminID)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, adminservice.ErrAdminUserNotFound):
|
|
c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "admin not found"))
|
|
case errors.Is(err, adminservice.ErrInvalidInput):
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid admin id"))
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "load admin settings failed"))
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Success(data))
|
|
}
|
|
|
|
func (h *Handler) UpdateProfile(c *gin.Context) {
|
|
claims, ok := CurrentAdminClaims(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
|
return
|
|
}
|
|
|
|
var req updateProfileRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid request payload"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.UpdateAdminProfile(c.Request.Context(), claims.AdminID, adminservice.UpdateAdminProfileInput{
|
|
DisplayName: req.DisplayName,
|
|
Email: req.Email,
|
|
Phone: req.Phone,
|
|
Timezone: req.Timezone,
|
|
})
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, adminservice.ErrAdminUserNotFound):
|
|
c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "admin not found"))
|
|
case errors.Is(err, adminservice.ErrInvalidInput):
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "display_name required and email format must be valid"))
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "update admin profile failed"))
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Success(data))
|
|
}
|
|
|
|
func (h *Handler) GetSystemConfig(c *gin.Context) {
|
|
if _, ok := CurrentAdminClaims(c); !ok {
|
|
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.GetSystemConfig(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "load system config failed"))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Success(data))
|
|
}
|
|
|
|
func (h *Handler) UpdateSystemConfig(c *gin.Context) {
|
|
if _, ok := CurrentAdminClaims(c); !ok {
|
|
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
|
return
|
|
}
|
|
|
|
var req updateSystemConfigRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid request payload"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.UpdateSystemConfig(c.Request.Context(), adminservice.SystemConfigPayload{
|
|
SiteName: req.SiteName,
|
|
AllowRegister: req.AllowRegister,
|
|
LoginFailLimit: req.LoginFailLimit,
|
|
DefaultPageSize: req.DefaultPageSize,
|
|
AuditLogRetentionDays: req.AuditLogRetentionDays,
|
|
})
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, adminservice.ErrInvalidInput):
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid system config payload"))
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "update system config failed"))
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Success(data))
|
|
}
|
|
|
|
func (h *Handler) UpdatePassword(c *gin.Context) {
|
|
claims, ok := CurrentAdminClaims(c)
|
|
if !ok {
|
|
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "unauthorized"))
|
|
return
|
|
}
|
|
|
|
var req updatePasswordRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "invalid request payload"))
|
|
return
|
|
}
|
|
|
|
err := h.svc.UpdatePassword(c.Request.Context(), claims.AdminID, req.OldPassword, req.NewPassword)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, adminservice.ErrAdminUserNotFound):
|
|
c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "admin not found"))
|
|
case errors.Is(err, adminservice.ErrInvalidCredentials):
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "当前密码错误"))
|
|
case errors.Is(err, adminservice.ErrPasswordPolicyViolation):
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "新密码至少6位且不能与当前密码相同"))
|
|
case errors.Is(err, adminservice.ErrInvalidInput):
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "old_password/new_password are required"))
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "update password failed"))
|
|
}
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Success(gin.H{
|
|
"message": "密码更新成功",
|
|
}))
|
|
}
|