添加头像

This commit is contained in:
haomingming 2025-12-11 15:34:42 +08:00
parent 4f8a2bb90c
commit cdd133eb71
5 changed files with 105 additions and 19 deletions

View File

@ -3,12 +3,17 @@ package controller
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io"
"math/rand"
"mime/multipart"
"strconv" "strconv"
"time"
"vote-admin-video-api/api/dao" "vote-admin-video-api/api/dao"
"vote-admin-video-api/api/dto" "vote-admin-video-api/api/dto"
"vote-admin-video-api/api/model" "vote-admin-video-api/api/model"
"vote-admin-video-api/api/requests" "vote-admin-video-api/api/requests"
"vote-admin-video-api/api/responses" "vote-admin-video-api/api/responses"
"vote-admin-video-api/extend/aliyun"
"vote-admin-video-api/global" "vote-admin-video-api/global"
"vote-admin-video-api/utils" "vote-admin-video-api/utils"
) )
@ -248,6 +253,7 @@ func (r *Video) PutVideo(c *gin.Context) {
videoAuthor := &model.VideoAuthor{ videoAuthor := &model.VideoAuthor{
VideoId: videoId, VideoId: videoId,
AuthorName: author.AuthorName, AuthorName: author.AuthorName,
AuthorAvatar: author.AuthorAvatar,
HospitalId: hospitalId, HospitalId: hospitalId,
} }
videoAuthor, err = VideoAuthorDao.AddVideoAuthor(tx, videoAuthor) videoAuthor, err = VideoAuthorDao.AddVideoAuthor(tx, videoAuthor)
@ -360,6 +366,7 @@ func (r *Video) AddVideo(c *gin.Context) {
videoAuthor := &model.VideoAuthor{ videoAuthor := &model.VideoAuthor{
VideoId: video.VideoId, VideoId: video.VideoId,
AuthorName: author.AuthorName, AuthorName: author.AuthorName,
AuthorAvatar: author.AuthorAvatar,
HospitalId: hospitalId, HospitalId: hospitalId,
} }
videoAuthor, err = videoAuthorDao.AddVideoAuthor(tx, videoAuthor) videoAuthor, err = videoAuthorDao.AddVideoAuthor(tx, videoAuthor)
@ -435,3 +442,73 @@ func (r *Video) PutVideoStatus(c *gin.Context) {
tx.Commit() tx.Commit()
responses.Ok(c) 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)
}

View File

@ -11,6 +11,7 @@ type VideoAuthorDto struct {
HospitalId string `json:"hospital_id"` // 作者所属id HospitalId string `json:"hospital_id"` // 作者所属id
HospitalName string `json:"hospital_name"` // 作者所属医院 HospitalName string `json:"hospital_name"` // 作者所属医院
AuthorName string `json:"author_name"` // 作者姓名 AuthorName string `json:"author_name"` // 作者姓名
AuthorAvatar string `json:"author_avatar"` // 作者头像
} }
// GetVideoAuthorListDto 列表-分页 // GetVideoAuthorListDto 列表-分页
@ -24,6 +25,7 @@ func GetVideoAuthorListDto(m []*model.VideoAuthor) []*VideoAuthorDto {
AuthorId: fmt.Sprintf("%d", v.AuthorId), AuthorId: fmt.Sprintf("%d", v.AuthorId),
VideoId: fmt.Sprintf("%d", v.VideoId), VideoId: fmt.Sprintf("%d", v.VideoId),
AuthorName: v.AuthorName, AuthorName: v.AuthorName,
AuthorAvatar: v.AuthorAvatar,
HospitalId: fmt.Sprintf("%d", v.HospitalId), HospitalId: fmt.Sprintf("%d", v.HospitalId),
} }

View File

@ -1,9 +1,10 @@
package model package model
import ( import (
"gorm.io/gorm"
"time" "time"
"vote-admin-video-api/global" "vote-admin-video-api/global"
"gorm.io/gorm"
) )
// VideoAuthor 视频-作者表 // VideoAuthor 视频-作者表
@ -11,6 +12,7 @@ type VideoAuthor struct {
AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_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"` 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"` 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"` HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"`
Model Model
BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"` BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"`

View File

@ -35,6 +35,7 @@ type PutVideo struct {
// PutVideoAuthor 修改视频详情-作者 // PutVideoAuthor 修改视频详情-作者
type PutVideoAuthor struct { type PutVideoAuthor struct {
AuthorName string `json:"author_name" form:"author_name" label:"作者姓名" 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"` HospitalId string `json:"hospital_id" form:"hospital_id" label:"作者所属医院id" validate:"required"`
} }
@ -51,6 +52,7 @@ type AddVideo struct {
// AddVideoAuthor 新增视频详情-作者 // AddVideoAuthor 新增视频详情-作者
type AddVideoAuthor struct { type AddVideoAuthor struct {
AuthorName string `json:"author_name" form:"author_name" label:"作者姓名" 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"` HospitalId string `json:"hospital_id" form:"hospital_id" label:"作者所属医院id" validate:"required"`
} }

View File

@ -161,6 +161,9 @@ func privateRouter(r *gin.Engine, api controller.Api) {
// 投票记录列表-视频-分页 // 投票记录列表-视频-分页
videoGroup.GET("/vote/page", api.UserVoteDay.GetVideoVotePage) videoGroup.GET("/vote/page", api.UserVoteDay.GetVideoVotePage)
// 上传作者头像
videoGroup.POST("/author/avatar", api.Video.UploadAuthorAvatar)
} }
// 基础数据 // 基础数据