xingzai 1 éve
szülő
commit
c91b446e67
2 módosított fájl, 146 hozzáadás és 3 törlés
  1. 8 0
      models/cygx_yanxuan_special_user.go
  2. 138 3
      services/html2Img.go

+ 8 - 0
models/cygx_yanxuan_special_user.go

@@ -281,3 +281,11 @@ func UdpateYanxuanSpecialauthorArticleNum(articleNum, userId int) (err error) {
 	_, err = o.Raw(sql, articleNum, userId).Exec()
 	return
 }
+
+// 更新作者分享到朋友圈的封面图片
+func UpdateYanxuanSpecialauthorMomentsImg(momentsImg string, userId int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE cygx_yanxuan_special_author SET moments_img = ?  WHERE user_id = ? `
+	_, err = o.Raw(sql, momentsImg, userId).Exec()
+	return
+}

+ 138 - 3
services/html2Img.go

@@ -77,14 +77,16 @@ func MakeYanxuanSpecialMomentsImg(specialId int) (imgUrl string) {
 	defer func() {
 		if err != nil {
 			fmt.Println("err:", err)
-			go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(specialId), 3)
+			go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeYanxuanSpecialMomentsImg Err:"+err.Error()+"专栏ID"+strconv.Itoa(specialId), 3)
 		}
 	}()
 	detail, e := models.GetYanxuanSpecialById(specialId, 0)
 	if e != nil {
-		err = errors.New("GetYanxuanSpecialBySpecialId, Err: " + e.Error())
+		err = errors.New("GetYanxuanSpecialById, Err: " + e.Error())
 		return
 	}
+	go GetYanxuanSpecialAuthoListMomentsImg() //有专栏生成的时候,最新专栏列表封面图
+	//go GetYanxuanSpecialAuthoMomentsImg(detail.UserId) // 有专栏生成的时候,最新wen
 	configCode := "yanxuan_special_moments_img_html"
 	detailConfig, e := models.GetConfigByCode(configCode)
 	if e != nil {
@@ -120,8 +122,141 @@ func MakeYanxuanSpecialMomentsImg(specialId int) (imgUrl string) {
 		return
 	}
 	imgUrl = res.Data
-
 	err = models.UpdateYanxuanSpecialMomentsImg(imgUrl, specialId)
+	return
+}
+
+//func init() {
+//	var pars []interface{}
+//	//GetYanxuanSpecialAuthoMomentsImg(53592)
+//	list, tmpErr := models.GetYanxuanSpecialAuthorList("", "", pars, 0, 9999)
+//	if tmpErr != nil {
+//		fmt.Println(tmpErr)
+//		return
+//	}
+//
+//	for _, v := range list {
+//		GetYanxuanSpecialAuthoMomentsImg(v.UserId)
+//	}
+//}
+
+// 生成研选专栏分享到朋友圈的图片
+func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
+	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)
+		}
+	}()
+
+	articleInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
+	if e != nil {
+		err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
+		return
+	}
+
+	configCode := "special_author_moments_img_html"
+	detailConfig, e := models.GetConfigByCode(configCode)
+	if e != nil {
+		err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
+		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)
+
+	htm2ImgReq := make(map[string]interface{})
+	htm2ImgReq["html_content"] = configValue
+	htm2ImgReq["width"] = 2250
+	htm2ImgReq["height"] = 3813
+	res, err := postHtml2Img(htm2ImgReq)
+	if err != nil || res == nil {
+		err = errors.New("html转图片失败: " + res.Msg)
+		return
+	}
+	if res.Code != 200 {
+		err = errors.New("html转图片失败: " + res.Msg)
+		return
+	}
+	imgUrl = res.Data
+	err = models.UpdateYanxuanSpecialauthorMomentsImg(imgUrl, userId)
+	fmt.Println(imgUrl)
+	return
+
+}
+
+// 生成研选专栏分享到朋友圈的图片
+func GetYanxuanSpecialAuthoListMomentsImg() (imgUrl string) {
+	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, 1)
+	if e != nil {
+		err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
+		return
+	}
+
+	var SpecialName, HeadImg, NickName, Introduction, PublishTime, Title string
+	var userIds []int
+	for _, v := range list {
+		SpecialName = v.SpecialName
+		HeadImg = v.HeadImg
+		NickName = v.NickName
+		Introduction = v.Introduction
+		userIds = append(userIds, v.UserId)
+	}
+	bestNew := GetBestNewYanxuanSpecialByUserId(userIds)
+	for _, v := range list {
+		if bestNew[v.UserId] != nil {
+			PublishTime = bestNew[v.UserId].PublishTime
+			Title = bestNew[v.UserId].Title
+		}
+	}
+	configCode := "special_author_list_moments_img_html"
+	detailConfig, e := models.GetConfigByCode(configCode)
+	if e != nil {
+		err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
+		return
+	}
+	configValue := detailConfig.ConfigValue
+	configValue = strings.Replace(configValue, "{{SpecialName}}", SpecialName, -1)
+	configValue = strings.Replace(configValue, "{{HeadImg}}", HeadImg, -1)
+	configValue = strings.Replace(configValue, "{{NickName}}", NickName, -1)
+	configValue = strings.Replace(configValue, "{{Introduction}}", Introduction, -1)
+	configValue = strings.Replace(configValue, "{{PublishTime}}", PublishTime, -1)
+	configValue = strings.Replace(configValue, "{{Title}}", Title, -1)
 
+	htm2ImgReq := make(map[string]interface{})
+	htm2ImgReq["html_content"] = configValue
+	htm2ImgReq["width"] = 2250
+	htm2ImgReq["height"] = 3813
+	res, err := postHtml2Img(htm2ImgReq)
+	if err != nil || res == nil {
+		err = errors.New("html转图片失败: " + res.Msg)
+		return
+	}
+	if res.Code != 200 {
+		err = errors.New("html转图片失败: " + res.Msg)
+		return
+	}
+	imgUrl = res.Data
+	configCodeUpdate := "special_author_list_moments_img"
+	err = models.UpdateConfigByCode(imgUrl, configCodeUpdate)
 	return
 }