This commit is contained in:
xujiang
2025-07-10 18:09:11 +08:00
parent 5cbdc5af73
commit 604b9e59ba
95 changed files with 23709 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package photo
import (
"fmt"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
@ -12,14 +13,37 @@ import (
// 上传照片
func UploadPhotoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UploadPhotoRequest
if err := httpx.Parse(r, &req); err != nil {
// 解析 multipart form
err := r.ParseMultipartForm(32 << 20) // 32MB
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
// 获取文件
file, header, err := r.FormFile("file")
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
defer file.Close()
// 获取表单参数
req := types.UploadPhotoRequest{
Title: r.FormValue("title"),
Description: r.FormValue("description"),
}
// 解析 category_id
if categoryIdStr := r.FormValue("category_id"); categoryIdStr != "" {
var categoryId int64
if _, err := fmt.Sscanf(categoryIdStr, "%d", &categoryId); err == nil {
req.CategoryId = categoryId
}
}
l := photo.NewUploadPhotoLogic(r.Context(), svcCtx)
resp, err := l.UploadPhoto(&req)
resp, err := l.UploadPhoto(&req, file, header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {