123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_clpt/models"
- "hongze/hongze_clpt/utils"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
- )
- var (
- ServerUrl = "http://127.0.0.1:5008/"
- Cygx_activity_sigin_html = "cygx_activity_sigin_html"
- Cygx_mp3_html = "cygx_mp3_html"
- Cygx_mp4_html = "cygx_mp4_html"
- )
- type Html2ImgResp struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data string `json:"data"`
- }
- // postHtml2Img 请求htm2img接口
- func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error) {
- // 目前仅此处调用该接口,暂不加授权、校验等
- postUrl := ServerUrl + "htm2img"
- postData, err := json.Marshal(param)
- if err != nil {
- return
- }
- result, err := Html2ImgHttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- if err = json.Unmarshal(result, &resp); err != nil {
- return
- }
- return resp, nil
- }
- // Html2ImgHttpPost post请求
- func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- fmt.Println("HttpPost:" + string(b))
- return b, err
- }
- //func init() {
- // //MakeYanxuanSpecialMomentsImg(185)
- // list, err := models.GetYanxuanSpecialListByconditioninit()
- // if err != nil {
- // fmt.Println(err)
- // }
- // for _, v := range list {
- // fmt.Println(v.Id)
- // MakeYanxuanSpecialMomentsImg(v.Id)
- // }
- //}
- // 生成研选专栏分享到朋友圈的图片
- func MakeYanxuanSpecialMomentsImg(specialId int) (imgUrl string) {
- var err error
- //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
- defer func() {
- if err != nil {
- fmt.Println("err:", err)
- go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeYanxuanSpecialMomentsImg Err:"+err.Error()+"专栏ID"+strconv.Itoa(specialId), 3)
- }
- }()
- detail, e := models.GetYanxuanSpecialById(specialId, 0)
- if e != nil {
- 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 {
- err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
- return
- }
- var typeName, content string
- if detail.Type == 1 {
- typeName = "笔记"
- } else {
- typeName = "观点"
- }
- content, _ = GetReportContentTextSubNew(detail.Content)
- configValue := detailConfig.ConfigValue
- configValue = strings.Replace(configValue, "{{HeadImg}}", detail.HeadImg, -1)
- configValue = strings.Replace(configValue, "{{NickName}}", detail.NickName, -1)
- configValue = strings.Replace(configValue, "{{TypeName}}", typeName, -1)
- configValue = strings.Replace(configValue, "{{PublishTime}}", detail.PublishTime, -1)
- configValue = strings.Replace(configValue, "{{Title}}", detail.Title, -1)
- configValue = strings.Replace(configValue, "{{Content}}", content, -1)
- htm2ImgReq := make(map[string]interface{})
- htm2ImgReq["html_content"] = configValue
- htm2ImgReq["width"] = 2250
- htm2ImgReq["height"] = 3813
- res, err := postHtml2Img(htm2ImgReq)
- if err != nil {
- err = errors.New("html转图片失败: " + err.Error())
- return
- }
- if res == nil {
- err = errors.New("html转图片失败: ")
- return
- }
- if res.Code != 200 {
- err = errors.New("html转图片失败: " + res.Msg)
- 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 {
- err = errors.New("html转图片失败: " + err.Error())
- return
- }
- if res == nil {
- err = errors.New("html转图片失败: ")
- 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
- }
|