feat(admin): 添加用户上传图片功能及相关页面

- 新增用户添加页面路由 /user/add 和文件上传处理路由 /user/doUpload
- 实现文件上传表单页面 admin/useradd.html
- 在 UserController 中添加 Add 方法渲染上传页面
- 在 UserController 中添加 DoUpload 方法处理文件上传及保存
- 在中间件添加 c.Next() 确保请求链继续执行
This commit is contained in:
2026-02-23 15:11:03 +08:00
parent ae36d59b68
commit a81fbb724e
4 changed files with 65 additions and 1 deletions

View File

@@ -1,6 +1,11 @@
package admin
import "github.com/gin-gonic/gin"
import (
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
type UserController struct {
BaseController
@@ -10,6 +15,38 @@ func (con UserController) Index(c *gin.Context) {
con.Success(c)
}
func (con UserController) Add(c *gin.Context) {
c.HTML(200, "admin/useradd", gin.H{})
}
func (con UserController) Show(c *gin.Context) {
c.String(200, "管理员用户详情")
}
func (con UserController) DoUpload(c *gin.Context) {
username := c.PostForm("username")
file, err := c.FormFile("face")
if err != nil {
c.JSON(400, gin.H{"error": "获取文件失败: " + err.Error()})
return
}
uploadDir := "./upload"
if err := os.MkdirAll(uploadDir, os.ModePerm); err != nil {
c.JSON(500, gin.H{"error": "创建目录失败: " + err.Error()})
return
}
dst := filepath.Join(uploadDir, file.Filename)
if err := c.SaveUploadedFile(file, dst); err != nil {
c.JSON(500, gin.H{"error": "保存文件失败: " + err.Error()})
return
}
c.JSON(200, gin.H{
"success": true,
"username": username,
"dst": dst,
})
}