Browse Source

no message

xingzai 11 months ago
parent
commit
9831730d94
5 changed files with 117 additions and 28 deletions
  1. 1 0
      models/cygx_yanxuan_special_user.go
  2. 32 0
      services/cygx_yanxuan_special.go
  3. 29 22
      services/html2Img.go
  4. 51 6
      services/oss.go
  5. 4 0
      services/task.go

+ 1 - 0
models/cygx_yanxuan_special_user.go

@@ -46,6 +46,7 @@ type CygxYanxuanSpecialAuthorItem struct {
 	LatestPublishDate    string                              // 最近更新时间
 	IsFollow             int                                 // 是否已关注 1已关注 0 未关注
 	HasChangeHeadImg     int                                 // 是否更换过默认头像 1是,0否
+	MomentsImg           string                              `description:"分享到朋友圈的封面图片"`
 	YanxuanSpecialCenter *CygxYanxuanSpecialCenterAuthorResp // 研选专栏文章内容
 }
 

+ 32 - 0
services/cygx_yanxuan_special.go

@@ -1,6 +1,7 @@
 package services
 
 import (
+	"context"
 	"errors"
 	"fmt"
 	"hongze/hongze_cygx/models"
@@ -469,3 +470,34 @@ func GetYanxuanSpecialCollectMap(userId int) (mapResp map[int]bool, err error) {
 	}
 	return
 }
+
+//func init() {
+//	UpdateYanxuanSpecialAuthoMomentsImg()
+//}
+
+// 更新研选专栏作者朋友圈封面图片,并删除老的图片
+// func UpdateYanxuanSpecialAuthoMomentsImg() {
+func UpdateYanxuanSpecialAuthoMomentsImg(cont context.Context) (err error) {
+	//var err error
+	//time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
+	defer func() {
+		if err != nil {
+			fmt.Println("err:", err)
+			go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error(), 3)
+		}
+	}()
+	var condition string
+	var pars []interface{}
+	condition += ` AND  a.nick_name <> ''   `
+	condition += `	ORDER BY latest_publish_time DESC`
+	list, e := models.GetYanxuanSpecialAuthorList(condition, pars, 0, 999)
+	if e != nil {
+		err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
+		return
+	}
+	for _, v := range list {
+		time.Sleep(500 * time.Millisecond) // 延迟0.5秒
+		GetYanxuanSpecialAuthoMomentsImg(v.UserId)
+	}
+	return
+}

+ 29 - 22
services/html2Img.go

@@ -67,16 +67,18 @@ func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
 }
 
 //func init() {
-//	//MakeYanxuanSpecialMomentsImg(185)
-//	var condition string
-//	var pars []interface{}
-//	condition += ` AND a.status = 3 `
+//MakeYanxuanSpecialMomentsImg(185)
+//var condition string
+//var pars []interface{}
+//condition += ` AND a.status = 3 `
 //
-//	list, err := models.GetYanxuanSpecialList(0, condition, pars, 0, 999)
-//	fmt.Println(err)
-//	for _, v := range list {
-//		MakeYanxuanSpecialMomentsImg(v.Id)
-//	}
+//list, err := models.GetYanxuanSpecialList(0, condition, pars, 0, 999)
+//fmt.Println(err)
+//for _, v := range list {
+//	MakeYanxuanSpecialMomentsImg(v.Id)
+//}
+
+//GetYanxuanSpecialAuthoListMomentsImg()
 //}
 
 // 生成研选专栏分享到朋友圈的图片
@@ -206,17 +208,17 @@ func GetYanxuanSpecialAuthoListMomentsImg() (imgUrl string) {
 }
 
 // 生成研选专栏分享到朋友圈的图片
-func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
-	var err error
+func GetYanxuanSpecialAuthoMomentsImg(userId int) (err error) {
+	//var err error
 	//time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
 	defer func() {
 		if err != nil {
 			fmt.Println("err:", err)
-			go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(userId), 3)
+			go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,GetYanxuanSpecialAuthoMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(userId), 3)
 		}
 	}()
 
-	articleInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
+	authorInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
 	if e != nil {
 		err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
 		return
@@ -229,13 +231,13 @@ func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
 		return
 	}
 	configValue := detailConfig.ConfigValue
-	configValue = strings.Replace(configValue, "{{HeadImg}}", articleInfo.HeadImg, -1)
-	configValue = strings.Replace(configValue, "{{SpecialName}}", articleInfo.SpecialName, -1)
-	configValue = strings.Replace(configValue, "{{NickName}}", articleInfo.NickName, -1)
-	configValue = strings.Replace(configValue, "{{SpecialArticleNum}}", strconv.Itoa(articleInfo.SpecialArticleNum), -1)
-	configValue = strings.Replace(configValue, "{{CollectNum}}", strconv.Itoa(articleInfo.CollectNum), -1)
-	configValue = strings.Replace(configValue, "{{FollowNum}}", strconv.Itoa(articleInfo.FollowNum), -1)
-	configValue = strings.Replace(configValue, "{{Introduction}}", articleInfo.Introduction, -1)
+	configValue = strings.Replace(configValue, "{{HeadImg}}", authorInfo.HeadImg, -1)
+	configValue = strings.Replace(configValue, "{{SpecialName}}", authorInfo.SpecialName, -1)
+	configValue = strings.Replace(configValue, "{{NickName}}", authorInfo.NickName, -1)
+	configValue = strings.Replace(configValue, "{{SpecialArticleNum}}", strconv.Itoa(authorInfo.SpecialArticleNum), -1)
+	configValue = strings.Replace(configValue, "{{CollectNum}}", strconv.Itoa(authorInfo.CollectNum), -1)
+	configValue = strings.Replace(configValue, "{{FollowNum}}", strconv.Itoa(authorInfo.FollowNum), -1)
+	configValue = strings.Replace(configValue, "{{Introduction}}", authorInfo.Introduction, -1)
 
 	htm2ImgReq := make(map[string]interface{})
 	htm2ImgReq["html_content"] = configValue
@@ -250,8 +252,13 @@ func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
 		err = errors.New("html转图片失败: " + res.Msg)
 		return
 	}
-	imgUrl = res.Data
-	err = models.UpdateYanxuanSpecialauthorMomentsImg(imgUrl, userId)
+	imgUrl := res.Data
+	e = models.UpdateYanxuanSpecialauthorMomentsImg(imgUrl, userId)
+	if e != nil {
+		err = errors.New("UpdateYanxuanSpecialauthorMomentsImg Err: " + e.Error())
+		return
+	}
+	DeleteUploadFileToAliyun(authorInfo.MomentsImg) // 删除老的图片
 	fmt.Println(imgUrl)
 	return
 

+ 51 - 6
services/oss.go

@@ -1,8 +1,10 @@
 package services
 
 import (
+	"fmt"
 	"github.com/aliyun/aliyun-oss-go-sdk/oss"
 	"os"
+	"strings"
 	"time"
 
 	"hongze/hongze_cygx/utils"
@@ -26,7 +28,7 @@ func init() {
 }
 */
 
-//图片上传到阿里云
+// 图片上传到阿里云
 func UploadAliyun(filename, filepath string) (string, error) {
 	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
@@ -46,7 +48,7 @@ func UploadAliyun(filename, filepath string) (string, error) {
 	return path, err
 }
 
-//音频上传到阿里云
+// 音频上传到阿里云
 func UploadAudioAliyun(filename, filepath string) (string, error) {
 	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
@@ -66,7 +68,7 @@ func UploadAudioAliyun(filename, filepath string) (string, error) {
 	return path, err
 }
 
-//视频上传到阿里云
+// 视频上传到阿里云
 func UploadVideoAliyun(filename, filepath, savePath string) error {
 	defer func() {
 		os.Remove(filepath)
@@ -90,7 +92,7 @@ func UploadVideoAliyun(filename, filepath, savePath string) error {
 	return err
 }
 
-//PDF上传到阿里云
+// PDF上传到阿里云
 func UploadPdfAliyun(filename, filepath string) (string, error) {
 	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
@@ -115,7 +117,7 @@ const (
 	HzBucketName string = "hzchart"
 )
 
-//上传文件到阿里云
+// 上传文件到阿里云
 func UploadFileToAliyun(filename, filepath, savePath string) error {
 	defer func() {
 		os.Remove(filepath)
@@ -172,4 +174,47 @@ func GetCloudDiskResourceFileTypeExtMap() map[string]string {
 		".mp3": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
 		".wma": "https://hzstatic.hzinsights.com/static/icon/file_type_audio.png",
 	}
-}
+}
+
+//func init() {
+//	DeleteUploadFileToAliyun("https://hzstatic.hzinsights.com/static/images/htm2img/9iElUd8oxpWgggf5pcKOCB8LZTiB.jpg")
+//}
+
+// 根据图片路径删除阿里云上的文件
+func DeleteUploadFileToAliyun(imgUrl string) error {
+	var err error
+	defer func() {
+		if err != nil {
+			fmt.Println("err:", err)
+			go utils.SendAlarmMsg("删除Oss图片失败, Err:"+err.Error()+"图片路径:"+imgUrl, 3)
+		}
+	}()
+	client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
+	if err != nil {
+		return err
+	}
+	bucket, err := client.Bucket(HzBucketName)
+	if err != nil {
+		return err
+	}
+	var objectName string
+	sliceObj := strings.Split(imgUrl, ".com/")
+	if len(sliceObj) == 2 {
+		objectName = sliceObj[1]
+	}
+	if objectName == "" {
+		return err
+	}
+	fmt.Println(objectName)
+
+	//return err
+	// 如需删除文件夹,请将objectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
+	//objectName = "static/images/htm2img/G32q2Lgp6soeh7xOW12yfZ4zC2uK.jpg"
+
+	// 删除单个文件。
+	err = bucket.DeleteObject(objectName)
+	if err != nil {
+		return err
+	}
+	return err
+}

+ 4 - 0
services/task.go

@@ -132,6 +132,10 @@ func Task() {
 		task.AddTask("updateCygxUserYanxuanPermissionToClose", updateCygxUserYanxuanPermissionToClose) //关闭到期个人用户研选权限
 
 	}
+
+	//updateYanxuanSpecialAuthoMomentsImg := task.NewTask("updateYanxuanSpecialAuthoMomentsImg", "0 0 1 * * *", UpdateYanxuanSpecialAuthoMomentsImg)
+	//task.AddTask(" 更新研选专栏作者朋友圈封面图片,并删除老的图片", updateYanxuanSpecialAuthoMomentsImg) // 更新研选专栏作者朋友圈封面图片,并删除老的图片
+
 	//if utils.RunMode != "release" {
 	//	getArticleListByApi := task.NewTask("getArticleListByApi", "0 */60 * * * *", GetArticleListByApi) //通过三方接口获取策略平台上的文章
 	//	task.AddTask("getArticleListByApi", getArticleListByApi)