添加头像
This commit is contained in:
parent
4f8a2bb90c
commit
cdd133eb71
@ -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)
|
||||
}
|
||||
|
||||
@ -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),
|
||||
}
|
||||
|
||||
// 加载数据-医院属性
|
||||
|
||||
@ -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"`
|
||||
}
|
||||
|
||||
@ -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 操作视频状态
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
// 基础数据
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user