Explorar el Código

新增缩略图

kobe6258 hace 6 días
padre
commit
5331d2bcaa

+ 28 - 18
controllers/analyst.go

@@ -43,10 +43,6 @@ func (this *AnalystController) UploadImage() {
 	}
 	defer f.Close()
 	ext := path.Ext(h.Filename)
-	//if ext != ".pdf" {
-	//	br.Msg = "文件格式不正确"
-	//	return
-	//}
 	size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
 	if err != nil {
 		size = 15
@@ -55,8 +51,8 @@ func (this *AnalystController) UploadImage() {
 		br.Msg = fmt.Sprintf("头像文件大小不能超过%dK", size)
 		return
 	}
-	dateDir := time.Now().Format("20060102")
-	uploadDir := utils.STATIC_DIR + "ht/" + dateDir
+	dateDir := time.Now().Format(time.DateOnly)
+	uploadDir := utils.STATIC_DIR + "ht/analyst/" + dateDir
 	err = os.MkdirAll(uploadDir, utils.DIR_MOD)
 	if err != nil {
 		br.Msg = "存储目录创建失败"
@@ -72,33 +68,47 @@ func (this *AnalystController) UploadImage() {
 		br.ErrMsg = "头像上传失败,Err:" + err.Error()
 		return
 	}
-	pdfUploadDir := utils.RESOURCE_DIR + "analyst/"
-	savePdfToOssPath := pdfUploadDir + time.Now().Format("200601/20060102/")
+	thumbnailPath, err := utils.ImageResize(fpath, 1200)
+	if err != nil {
+		br.Msg = "头像上传失败"
+		br.ErrMsg = "头像上传失败,Err:" + err.Error()
+		return
+	}
+	ossUploadDir := utils.RESOURCE_DIR + "analyst/"
+	saveOssPath := ossUploadDir + time.Now().Format("200601/20060102/")
 	imgName := utils.GetRandStringNoSpecialChar(28)
-	savePdfToOssPath += imgName
-
+	saveOssPath += imgName
 	defer func() {
 		_ = os.Remove(fpath)
+		_ = os.Remove(thumbnailPath)
 	}()
-
 	ossClient := services.NewOssClient()
 	if ossClient == nil {
 		br.Msg = "头像上传失败"
 		br.ErrMsg = "初始化OSS服务失败"
 		return
 	}
-	imgUrl, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
+	//新增上传缩略图
+
+	thumbnailUrl, err := ossClient.UploadFile("", thumbnailPath, saveOssPath+"_thumbnail")
+	if err != nil {
+		br.Msg = "头像上传失败"
+		br.ErrMsg = "头像上传失败,Err:" + err.Error()
+		return
+	}
+	imgUrl, err := ossClient.UploadFile("", fpath, saveOssPath)
 	if err != nil {
 		br.Msg = "头像上传失败"
 		br.ErrMsg = "头像上传失败,Err:" + err.Error()
 		return
 	}
-
 	base := path.Base(h.Filename)
-	resp := new(response.ReportPdfUploadResp)
-	resp.Url = imgUrl
-	resp.FileName = base
-
+	thumbnailBase := path.Base(h.Filename + "_thumbnail")
+	resp := new(response.HeadImageUploadResp)
+	resp.Url = thumbnailUrl
+	resp.OriginUrl = imgUrl
+	resp.FileName = thumbnailBase
+	resp.OriginFileName = base
 	br.Data = resp
 	br.Msg = "上传成功"
 	br.Ret = 200
@@ -118,7 +128,6 @@ func (this *AnalystController) GetAnalystByName(AnalystName string) {
 		this.ServeJSON()
 	}()
 	analyst, err := models.GetAnalystByName(AnalystName)
-
 	if err != nil {
 		br.Msg = "获取失败"
 		br.ErrMsg = "获取失败,Err:" + err.Error()
@@ -241,6 +250,7 @@ func (this *AnalystController) Edit() {
 	}
 	analyst.Introduction = req.Introduction
 	analyst.Position = req.Position
+	analyst.HeadOriginImgUrl = req.HeadOriginImgUrl
 	analyst.ProfessionalCertificate = req.ProfessionalCertificate
 	analyst.InvestmentCertificate = req.InvestmentCertificate
 	analyst.UpdatedTime = time.Now()

+ 3 - 1
controllers/audio.go

@@ -14,6 +14,7 @@ import (
 	"os"
 	"path"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -49,7 +50,8 @@ func (this *AudioController) UploadAudio() {
 		return
 	}
 	ext := path.Ext(h.Filename)
-	if ext != ".mp3" {
+	lowcaseExt := strings.ToLower(ext)
+	if lowcaseExt != ".mp3" {
 		br.Msg = "音频格式不正确"
 		br.ErrMsg = "音频上传失败,Err:" + err.Error()
 		return

+ 2 - 7
controllers/image.go

@@ -48,13 +48,8 @@ func (this *ImageController) UploadImage() {
 		return
 	}
 	ext := path.Ext(h.Filename)
-	//if ext != ".mp3" {
-	//	br.Msg = "音频格式不正确"
-	//	br.ErrMsg = "音频上传失败,Err:" + err.Error()
-	//	return
-	//}
-	dateDir := time.Now().Format("20060102")
-	uploadDir := utils.STATIC_DIR + "ht/audio" + dateDir
+	dateDir := time.Now().Format(time.DateOnly)
+	uploadDir := utils.STATIC_DIR + "ht/image" + dateDir
 	err = os.MkdirAll(uploadDir, utils.DIR_MOD)
 	if err != nil {
 		br.Msg = "存储目录创建失败"

+ 3 - 0
models/financial_analyst.go

@@ -19,6 +19,7 @@ type CrmFinancialAnalyst struct {
 	HTId                    int           `description:"ht_id"`
 	Name                    string        `description:"姓名"`
 	HeadImgUrl              string        `description:"头像"`
+	HeadOriginImgUrl        string        `description:"原始头像"`
 	Position                string        `description:"职位"`
 	InvestmentCertificate   string        `description:"投资咨询资格证"`
 	ProfessionalCertificate string        `description:"从业资格证"`
@@ -37,6 +38,7 @@ type AnalystView struct {
 	Id                      int    `json:"Id"`
 	Name                    string `json:"Name"`
 	HeadImgURL              string `json:"HeadImgURL"`
+	HeadOriginImgUrl        string `json:"HeadOriginImgUrl"`
 	Position                string `json:"Position"`
 	InvestmentCertificate   string `json:"InvestmentCertificate"`
 	ProfessionalCertificate string `json:"ProfessionalCertificate"`
@@ -56,6 +58,7 @@ func (a *CrmFinancialAnalyst) ToView() *AnalystView {
 		Id:                      a.Id,
 		Name:                    a.Name,
 		HeadImgURL:              a.HeadImgUrl,
+		HeadOriginImgUrl:        a.HeadOriginImgUrl,
 		Position:                a.Position,
 		InvestmentCertificate:   a.InvestmentCertificate,
 		ProfessionalCertificate: a.ProfessionalCertificate,

+ 1 - 0
models/request/analyst.go

@@ -5,6 +5,7 @@ package request
 type AnalystEditReq struct {
 	Id                      int    `description:"用户id"`
 	HeadImgUrl              string `description:"头像地址"`
+	HeadOriginImgUrl        string `description:"原始头像地址"`
 	Introduction            string `description:"简介"`
 	Position                string
 	InvestmentCertificate   string

+ 7 - 0
models/response/analyst.go

@@ -9,3 +9,10 @@ type AnalystListResp struct {
 	List   []*models.AnalystView
 	Paging *paging.PagingItem `description:"分页数据"`
 }
+
+type HeadImageUploadResp struct {
+	Url            string `description:"缩略图URL"`
+	OriginUrl      string `description:"原始图片URL"`
+	FileName       string `description:""`
+	OriginFileName string `description:""`
+}

+ 6 - 6
utils/imageUtils.go

@@ -3,13 +3,14 @@ package utils
 import (
 	"github.com/disintegration/imaging"
 	"image"
+	"image/png"
 	"log"
 	"os"
 	"path/filepath"
 	"strings"
 )
 
-func ImageResize(imageSrc string, width, height int) (string, error) {
+func ImageResize(imageSrc string, maxDimension int) (string, error) {
 	file, err := os.Open(imageSrc)
 	if err != nil {
 		log.Printf("打开图片失败: %v", err)
@@ -25,7 +26,7 @@ func ImageResize(imageSrc string, width, height int) (string, error) {
 	}
 
 	// 调整图片大小
-	resizedImg := imaging.Resize(img, width, height, imaging.Lanczos)
+	resizedImg := imaging.Thumbnail(img, maxDimension, maxDimension, imaging.Lanczos)
 
 	// 获取输入文件的扩展名
 	ext := filepath.Ext(imageSrc)
@@ -45,10 +46,9 @@ func ImageResize(imageSrc string, width, height int) (string, error) {
 	case ".jpg", ".jpeg":
 		err = imaging.Encode(outFile, resizedImg, imaging.JPEG, imaging.JPEGQuality(75))
 	case ".png":
-		err = imaging.Encode(outFile, resizedImg, imaging.PNG, imaging.PNGCompressionLevel(imaging.BestCompression))
-	case ".gif":
-		err = imaging.Encode(outFile, resizedImg, imaging.GIF
-		imaging.GIFQuality(75))
+		err = imaging.Encode(outFile, resizedImg, imaging.PNG, imaging.PNGCompressionLevel(png.BestCompression))
+	//case ".gif":
+	//	err = imaging.Encode(outFile, resizedImg, imaging.GIF, imaging.GIFNumColors(DW))
 	default:
 		log.Printf("不支持的图片格式: %s", ext)
 		return "", err