html2Img.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_clpt/models"
  7. "hongze/hongze_clpt/utils"
  8. "io/ioutil"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. )
  13. var (
  14. ServerUrl = "http://127.0.0.1:5008/"
  15. Cygx_activity_sigin_html = "cygx_activity_sigin_html"
  16. Cygx_mp3_html = "cygx_mp3_html"
  17. Cygx_mp4_html = "cygx_mp4_html"
  18. )
  19. type Html2ImgResp struct {
  20. Code int `json:"code"`
  21. Msg string `json:"msg"`
  22. Data string `json:"data"`
  23. }
  24. // postHtml2Img 请求htm2img接口
  25. func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error) {
  26. // 目前仅此处调用该接口,暂不加授权、校验等
  27. postUrl := ServerUrl + "htm2img"
  28. postData, err := json.Marshal(param)
  29. if err != nil {
  30. return
  31. }
  32. result, err := Html2ImgHttpPost(postUrl, string(postData), "application/json")
  33. if err != nil {
  34. return
  35. }
  36. if err = json.Unmarshal(result, &resp); err != nil {
  37. return
  38. }
  39. return resp, nil
  40. }
  41. // Html2ImgHttpPost post请求
  42. func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
  43. body := ioutil.NopCloser(strings.NewReader(postData))
  44. client := &http.Client{}
  45. req, err := http.NewRequest("POST", url, body)
  46. if err != nil {
  47. return nil, err
  48. }
  49. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  50. if len(params) > 0 && params[0] != "" {
  51. contentType = params[0]
  52. }
  53. req.Header.Set("Content-Type", contentType)
  54. resp, err := client.Do(req)
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer resp.Body.Close()
  59. b, err := ioutil.ReadAll(resp.Body)
  60. fmt.Println("HttpPost:" + string(b))
  61. return b, err
  62. }
  63. //func init() {
  64. // MakeYanxuanSpecialMomentsImg(185)
  65. //}
  66. // 生成研选专栏分享到朋友圈的图片
  67. func MakeYanxuanSpecialMomentsImg(specialId int) (imgUrl string) {
  68. var err error
  69. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  70. defer func() {
  71. if err != nil {
  72. fmt.Println("err:", err)
  73. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeYanxuanSpecialMomentsImg Err:"+err.Error()+"专栏ID"+strconv.Itoa(specialId), 3)
  74. }
  75. }()
  76. detail, e := models.GetYanxuanSpecialById(specialId, 0)
  77. if e != nil {
  78. err = errors.New("GetYanxuanSpecialById, Err: " + e.Error())
  79. return
  80. }
  81. go GetYanxuanSpecialAuthoListMomentsImg() //有专栏生成的时候,最新专栏列表封面图
  82. //go GetYanxuanSpecialAuthoMomentsImg(detail.UserId) // 有专栏生成的时候,最新wen
  83. configCode := "yanxuan_special_moments_img_html"
  84. detailConfig, e := models.GetConfigByCode(configCode)
  85. if e != nil {
  86. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  87. return
  88. }
  89. var typeName, content string
  90. if detail.Type == 1 {
  91. typeName = "笔记"
  92. } else {
  93. typeName = "观点"
  94. }
  95. content, _ = GetReportContentTextSubNew(detail.Content)
  96. configValue := detailConfig.ConfigValue
  97. configValue = strings.Replace(configValue, "{{HeadImg}}", detail.HeadImg, -1)
  98. configValue = strings.Replace(configValue, "{{NickName}}", detail.NickName, -1)
  99. configValue = strings.Replace(configValue, "{{TypeName}}", typeName, -1)
  100. configValue = strings.Replace(configValue, "{{PublishTime}}", detail.PublishTime, -1)
  101. configValue = strings.Replace(configValue, "{{Title}}", detail.Title, -1)
  102. configValue = strings.Replace(configValue, "{{Content}}", content, -1)
  103. htm2ImgReq := make(map[string]interface{})
  104. htm2ImgReq["html_content"] = configValue
  105. htm2ImgReq["width"] = 2250
  106. htm2ImgReq["height"] = 3813
  107. res, err := postHtml2Img(htm2ImgReq)
  108. if err != nil {
  109. err = errors.New("html转图片失败: " + err.Error())
  110. return
  111. }
  112. if res == nil {
  113. err = errors.New("html转图片失败: ")
  114. return
  115. }
  116. if res.Code != 200 {
  117. err = errors.New("html转图片失败: " + res.Msg)
  118. return
  119. }
  120. imgUrl = res.Data
  121. err = models.UpdateYanxuanSpecialMomentsImg(imgUrl, specialId)
  122. return
  123. }
  124. //func init() {
  125. // var pars []interface{}
  126. // //GetYanxuanSpecialAuthoMomentsImg(53592)
  127. // list, tmpErr := models.GetYanxuanSpecialAuthorList("", "", pars, 0, 9999)
  128. // if tmpErr != nil {
  129. // fmt.Println(tmpErr)
  130. // return
  131. // }
  132. //
  133. // for _, v := range list {
  134. // GetYanxuanSpecialAuthoMomentsImg(v.UserId)
  135. // }
  136. //}
  137. // 生成研选专栏分享到朋友圈的图片
  138. func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
  139. var err error
  140. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  141. defer func() {
  142. if err != nil {
  143. fmt.Println("err:", err)
  144. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(userId), 3)
  145. }
  146. }()
  147. articleInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
  148. if e != nil {
  149. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  150. return
  151. }
  152. configCode := "special_author_moments_img_html"
  153. detailConfig, e := models.GetConfigByCode(configCode)
  154. if e != nil {
  155. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  156. return
  157. }
  158. configValue := detailConfig.ConfigValue
  159. configValue = strings.Replace(configValue, "{{HeadImg}}", articleInfo.HeadImg, -1)
  160. configValue = strings.Replace(configValue, "{{SpecialName}}", articleInfo.SpecialName, -1)
  161. configValue = strings.Replace(configValue, "{{NickName}}", articleInfo.NickName, -1)
  162. configValue = strings.Replace(configValue, "{{SpecialArticleNum}}", strconv.Itoa(articleInfo.SpecialArticleNum), -1)
  163. configValue = strings.Replace(configValue, "{{CollectNum}}", strconv.Itoa(articleInfo.CollectNum), -1)
  164. configValue = strings.Replace(configValue, "{{FollowNum}}", strconv.Itoa(articleInfo.FollowNum), -1)
  165. configValue = strings.Replace(configValue, "{{Introduction}}", articleInfo.Introduction, -1)
  166. htm2ImgReq := make(map[string]interface{})
  167. htm2ImgReq["html_content"] = configValue
  168. htm2ImgReq["width"] = 2250
  169. htm2ImgReq["height"] = 3813
  170. res, err := postHtml2Img(htm2ImgReq)
  171. if err != nil || res == nil {
  172. err = errors.New("html转图片失败: " + res.Msg)
  173. return
  174. }
  175. if res.Code != 200 {
  176. err = errors.New("html转图片失败: " + res.Msg)
  177. return
  178. }
  179. imgUrl = res.Data
  180. err = models.UpdateYanxuanSpecialauthorMomentsImg(imgUrl, userId)
  181. fmt.Println(imgUrl)
  182. return
  183. }
  184. // 生成研选专栏分享到朋友圈的图片
  185. func GetYanxuanSpecialAuthoListMomentsImg() (imgUrl string) {
  186. var err error
  187. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  188. defer func() {
  189. if err != nil {
  190. fmt.Println("err:", err)
  191. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error(), 3)
  192. }
  193. }()
  194. var condition string
  195. var pars []interface{}
  196. condition += ` AND a.nick_name <> '' `
  197. condition += ` ORDER BY latest_publish_time DESC`
  198. list, e := models.GetYanxuanSpecialAuthorList("", condition, pars, 0, 1)
  199. if e != nil {
  200. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  201. return
  202. }
  203. var SpecialName, HeadImg, NickName, Introduction, PublishTime, Title string
  204. var userIds []int
  205. for _, v := range list {
  206. SpecialName = v.SpecialName
  207. HeadImg = v.HeadImg
  208. NickName = v.NickName
  209. Introduction = v.Introduction
  210. userIds = append(userIds, v.UserId)
  211. }
  212. bestNew := GetBestNewYanxuanSpecialByUserId(userIds)
  213. for _, v := range list {
  214. if bestNew[v.UserId] != nil {
  215. PublishTime = bestNew[v.UserId].PublishTime
  216. Title = bestNew[v.UserId].Title
  217. }
  218. }
  219. configCode := "special_author_list_moments_img_html"
  220. detailConfig, e := models.GetConfigByCode(configCode)
  221. if e != nil {
  222. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  223. return
  224. }
  225. configValue := detailConfig.ConfigValue
  226. configValue = strings.Replace(configValue, "{{SpecialName}}", SpecialName, -1)
  227. configValue = strings.Replace(configValue, "{{HeadImg}}", HeadImg, -1)
  228. configValue = strings.Replace(configValue, "{{NickName}}", NickName, -1)
  229. configValue = strings.Replace(configValue, "{{Introduction}}", Introduction, -1)
  230. configValue = strings.Replace(configValue, "{{PublishTime}}", PublishTime, -1)
  231. configValue = strings.Replace(configValue, "{{Title}}", Title, -1)
  232. htm2ImgReq := make(map[string]interface{})
  233. htm2ImgReq["html_content"] = configValue
  234. htm2ImgReq["width"] = 2250
  235. htm2ImgReq["height"] = 3813
  236. res, err := postHtml2Img(htm2ImgReq)
  237. if err != nil {
  238. err = errors.New("html转图片失败: " + err.Error())
  239. return
  240. }
  241. if res == nil {
  242. err = errors.New("html转图片失败: ")
  243. return
  244. }
  245. if res.Code != 200 {
  246. err = errors.New("html转图片失败: " + res.Msg)
  247. return
  248. }
  249. imgUrl = res.Data
  250. configCodeUpdate := "special_author_list_moments_img"
  251. err = models.UpdateConfigByCode(imgUrl, configCodeUpdate)
  252. return
  253. }