From cdd133eb71cb5fc5e017b7e69bb277b1bb41b52b Mon Sep 17 00:00:00 2001 From: haomingming Date: Thu, 11 Dec 2025 15:34:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controller/Video.go | 89 +++++++++++++++++++++++++++++++++++++--- api/dto/VideoAuthor.go | 10 +++-- api/model/VideoAuthor.go | 12 +++--- api/requests/Video.go | 10 +++-- api/router/router.go | 3 ++ 5 files changed, 105 insertions(+), 19 deletions(-) diff --git a/api/controller/Video.go b/api/controller/Video.go index aac3ecc..cbcb674 100644 --- a/api/controller/Video.go +++ b/api/controller/Video.go @@ -3,12 +3,17 @@ package controller import ( "fmt" "github.com/gin-gonic/gin" + "io" + "math/rand" + "mime/multipart" "strconv" + "time" "vote-admin-video-api/api/dao" "vote-admin-video-api/api/dto" "vote-admin-video-api/api/model" "vote-admin-video-api/api/requests" "vote-admin-video-api/api/responses" + "vote-admin-video-api/extend/aliyun" "vote-admin-video-api/global" "vote-admin-video-api/utils" ) @@ -246,9 +251,10 @@ func (r *Video) PutVideo(c *gin.Context) { } videoAuthor := &model.VideoAuthor{ - VideoId: videoId, - AuthorName: author.AuthorName, - HospitalId: hospitalId, + VideoId: videoId, + AuthorName: author.AuthorName, + AuthorAvatar: author.AuthorAvatar, + HospitalId: hospitalId, } videoAuthor, err = VideoAuthorDao.AddVideoAuthor(tx, videoAuthor) if err != nil { @@ -358,9 +364,10 @@ func (r *Video) AddVideo(c *gin.Context) { } videoAuthor := &model.VideoAuthor{ - VideoId: video.VideoId, - AuthorName: author.AuthorName, - HospitalId: hospitalId, + VideoId: video.VideoId, + AuthorName: author.AuthorName, + AuthorAvatar: author.AuthorAvatar, + HospitalId: hospitalId, } videoAuthor, err = videoAuthorDao.AddVideoAuthor(tx, videoAuthor) if err != nil { @@ -435,3 +442,73 @@ func (r *Video) PutVideoStatus(c *gin.Context) { tx.Commit() responses.Ok(c) } + +// UploadAuthorAvatar 上传作者头像 +func (r *Video) UploadAuthorAvatar(c *gin.Context) { + file, err := c.FormFile("file") + if err != nil { + responses.FailWithMessage("文件错误", c) + return + } + + f, err := file.Open() + if err != nil { + responses.FailWithMessage("文件错误", c) + return + } + defer func(f multipart.File) { + err := f.Close() + if err != nil { + fmt.Println(err) + } + }(f) + + // 读取文件内容到字节切片 + fileBytes, err := io.ReadAll(f) + if err != nil { + responses.FailWithMessage("文件错误", c) + return + } + + // 验证文件类型(只允许图片) + fileType := "jpg" + if file.Filename != "" { + fileType = utils.GetExtension(file.Filename) + if fileType == "" { + fileType = "jpg" + } + } + + // 验证是否为图片格式 + allowedTypes := map[string]bool{ + "jpg": true, + "jpeg": true, + "png": true, + "gif": true, + "webp": true, + } + if !allowedTypes[fileType] { + responses.FailWithMessage("只支持图片格式(jpg、jpeg、png、gif、webp)", c) + return + } + + // 生成OSS路径 + now := time.Now() + dateTimeString := now.Format("20060102150405") + rand.New(rand.NewSource(time.Now().UnixNano())) + ossPath := "static/author_avatar/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType + + // 上传到OSS + _, err = aliyun.PutObjectByte(ossPath, fileBytes) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 返回上传后的URL + result := make(map[string]interface{}) + result["url"] = utils.AddOssDomain("/" + ossPath) + result["path"] = "/" + ossPath + + responses.OkWithData(result, c) +} diff --git a/api/dto/VideoAuthor.go b/api/dto/VideoAuthor.go index cd5c50c..1615fb6 100644 --- a/api/dto/VideoAuthor.go +++ b/api/dto/VideoAuthor.go @@ -11,6 +11,7 @@ type VideoAuthorDto struct { HospitalId string `json:"hospital_id"` // 作者所属id HospitalName string `json:"hospital_name"` // 作者所属医院 AuthorName string `json:"author_name"` // 作者姓名 + AuthorAvatar string `json:"author_avatar"` // 作者头像 } // GetVideoAuthorListDto 列表-分页 @@ -21,10 +22,11 @@ func GetVideoAuthorListDto(m []*model.VideoAuthor) []*VideoAuthorDto { if len(m) > 0 { for i, v := range m { response := &VideoAuthorDto{ - AuthorId: fmt.Sprintf("%d", v.AuthorId), - VideoId: fmt.Sprintf("%d", v.VideoId), - AuthorName: v.AuthorName, - HospitalId: fmt.Sprintf("%d", v.HospitalId), + AuthorId: fmt.Sprintf("%d", v.AuthorId), + VideoId: fmt.Sprintf("%d", v.VideoId), + AuthorName: v.AuthorName, + AuthorAvatar: v.AuthorAvatar, + HospitalId: fmt.Sprintf("%d", v.HospitalId), } // 加载数据-医院属性 diff --git a/api/model/VideoAuthor.go b/api/model/VideoAuthor.go index 9c19a3e..e30dbae 100644 --- a/api/model/VideoAuthor.go +++ b/api/model/VideoAuthor.go @@ -1,17 +1,19 @@ package model import ( - "gorm.io/gorm" "time" "vote-admin-video-api/global" + + "gorm.io/gorm" ) // VideoAuthor 视频-作者表 type VideoAuthor struct { - AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_id"` - VideoId int64 `gorm:"column:video_id;type:bigint(19);comment:视频id;NOT NULL" json:"video_id"` - AuthorName string `gorm:"column:author_name;type:varchar(100);comment:作者姓名" json:"author_name"` - HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"` + AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_id"` + VideoId int64 `gorm:"column:video_id;type:bigint(19);comment:视频id;NOT NULL" json:"video_id"` + AuthorName string `gorm:"column:author_name;type:varchar(100);comment:作者姓名" json:"author_name"` + AuthorAvatar string `gorm:"column:author_avatar;type:varchar(255);comment:作者头像" json:"author_avatar"` + HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"` Model BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"` } diff --git a/api/requests/Video.go b/api/requests/Video.go index 86d12c4..48aea96 100644 --- a/api/requests/Video.go +++ b/api/requests/Video.go @@ -34,8 +34,9 @@ type PutVideo struct { // PutVideoAuthor 修改视频详情-作者 type PutVideoAuthor struct { - AuthorName string `json:"author_name" form:"author_name" label:"作者姓名" validate:"required"` - HospitalId string `json:"hospital_id" form:"hospital_id" label:"作者所属医院id" validate:"required"` + AuthorName string `json:"author_name" form:"author_name" label:"作者姓名" validate:"required"` + AuthorAvatar string `json:"author_avatar" form:"author_avatar" label:"作者头像"` + HospitalId string `json:"hospital_id" form:"hospital_id" label:"作者所属医院id" validate:"required"` } // AddVideo 新增视频详情 @@ -50,8 +51,9 @@ type AddVideo struct { // AddVideoAuthor 新增视频详情-作者 type AddVideoAuthor struct { - AuthorName string `json:"author_name" form:"author_name" label:"作者姓名" validate:"required"` - HospitalId string `json:"hospital_id" form:"hospital_id" label:"作者所属医院id" validate:"required"` + AuthorName string `json:"author_name" form:"author_name" label:"作者姓名" validate:"required"` + AuthorAvatar string `json:"author_avatar" form:"author_avatar" label:"作者头像"` + HospitalId string `json:"hospital_id" form:"hospital_id" label:"作者所属医院id" validate:"required"` } // PutVideoStatus 操作视频状态 diff --git a/api/router/router.go b/api/router/router.go index 8e2b3e2..6f0b21b 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -161,6 +161,9 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 投票记录列表-视频-分页 videoGroup.GET("/vote/page", api.UserVoteDay.GetVideoVotePage) + + // 上传作者头像 + videoGroup.POST("/author/avatar", api.Video.UploadAuthorAvatar) } // 基础数据