新增客户端管理功能,包括创建、编辑和删除客户端的API,更新用户管理功能,添加用户创建和编辑页面,优化管理员功能,增强用户和客户端的管理体验。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -11,11 +12,12 @@ import (
|
||||
)
|
||||
|
||||
type AdminHandler struct {
|
||||
adminService *services.AdminService
|
||||
adminService *services.AdminService
|
||||
clientService *services.ClientService
|
||||
}
|
||||
|
||||
func NewAdminHandler(adminService *services.AdminService) *AdminHandler {
|
||||
return &AdminHandler{adminService: adminService}
|
||||
func NewAdminHandler(adminService *services.AdminService, clientService *services.ClientService) *AdminHandler {
|
||||
return &AdminHandler{adminService: adminService, clientService: clientService}
|
||||
}
|
||||
|
||||
func (h *AdminHandler) ShowAdminLogin(c *gin.Context) {
|
||||
@@ -63,20 +65,153 @@ func (h *AdminHandler) ListUsers(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// ListClients 显示客户端列表页面
|
||||
func (h *AdminHandler) ListClients(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
||||
page := 1
|
||||
pageSize := 10
|
||||
|
||||
clients, total, err := h.adminService.ListClients(page, pageSize)
|
||||
// 从查询参数获取分页信息
|
||||
if pageStr := c.Query("page"); pageStr != "" {
|
||||
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
|
||||
page = p
|
||||
}
|
||||
}
|
||||
if pageSizeStr := c.Query("page_size"); pageSizeStr != "" {
|
||||
if ps, err := strconv.Atoi(pageSizeStr); err == nil && ps > 0 {
|
||||
pageSize = ps
|
||||
}
|
||||
}
|
||||
|
||||
// 获取客户端列表
|
||||
clients, total, err := h.clientService.GetClients(page, pageSize)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "admin_clients.html", gin.H{
|
||||
"clients": clients,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
"total": total,
|
||||
"lastPage": int(math.Ceil(float64(total) / float64(pageSize))),
|
||||
})
|
||||
}
|
||||
|
||||
// ShowCreateUser 显示创建用户页面
|
||||
func (h *AdminHandler) ShowCreateUser(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "admin_create_user.html", gin.H{
|
||||
"title": "创建用户",
|
||||
})
|
||||
}
|
||||
|
||||
// HandleCreateUser 处理创建用户请求
|
||||
func (h *AdminHandler) HandleCreateUser(c *gin.Context) {
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
email := c.PostForm("email")
|
||||
|
||||
if username == "" || password == "" || email == "" {
|
||||
c.HTML(http.StatusBadRequest, "admin_create_user.html", gin.H{
|
||||
"title": "创建用户",
|
||||
"error": "用户名、密码和邮箱都不能为空",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
_, err := h.adminService.CreateUser(username, password, email)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "admin_create_user.html", gin.H{
|
||||
"title": "创建用户",
|
||||
"error": "创建用户失败:" + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusFound, "/admin/users")
|
||||
}
|
||||
|
||||
// ShowEditUser 显示编辑用户页面
|
||||
func (h *AdminHandler) ShowEditUser(c *gin.Context) {
|
||||
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{
|
||||
"error": "无效的用户ID",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.adminService.GetUser(uint(userID))
|
||||
if err != nil {
|
||||
c.HTML(http.StatusNotFound, "error.html", gin.H{
|
||||
"error": "用户不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "admin_edit_user.html", gin.H{
|
||||
"title": "编辑用户",
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
|
||||
// HandleEditUser 处理编辑用户请求
|
||||
func (h *AdminHandler) HandleEditUser(c *gin.Context) {
|
||||
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户ID"})
|
||||
return
|
||||
}
|
||||
|
||||
username := c.PostForm("username")
|
||||
email := c.PostForm("email")
|
||||
isActiveStr := c.PostForm("is_active")
|
||||
var isActive *bool
|
||||
if isActiveStr != "" {
|
||||
active := isActiveStr == "true"
|
||||
isActive = &active
|
||||
}
|
||||
|
||||
_, err = h.adminService.UpdateUser(uint(userID), username, email, isActive)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "admin_edit_user.html", gin.H{
|
||||
"title": "编辑用户",
|
||||
"error": "更新用户失败:" + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 如果提供了新密码,则更新密码
|
||||
newPassword := c.PostForm("password")
|
||||
if newPassword != "" {
|
||||
err = h.adminService.UpdateUserPassword(uint(userID), newPassword)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "admin_edit_user.html", gin.H{
|
||||
"title": "编辑用户",
|
||||
"error": "更新密码失败:" + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusFound, "/admin/users")
|
||||
}
|
||||
|
||||
// HandleDeleteUser 处理删除用户请求
|
||||
func (h *AdminHandler) HandleDeleteUser(c *gin.Context) {
|
||||
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户ID"})
|
||||
return
|
||||
}
|
||||
|
||||
err = h.adminService.DeleteUser(uint(userID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除用户失败:" + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusFound, "/admin/users")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user