html2Img.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // list, err := models.GetYanxuanSpecialListByconditioninit()
  66. // if err != nil {
  67. // fmt.Println(err)
  68. // }
  69. // for _, v := range list {
  70. // fmt.Println(v.Id)
  71. // MakeYanxuanSpecialMomentsImg(v.Id)
  72. // }
  73. //}
  74. // 生成研选专栏分享到朋友圈的图片
  75. func MakeYanxuanSpecialMomentsImg(specialId int) (imgUrl string) {
  76. var err error
  77. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  78. defer func() {
  79. if err != nil {
  80. fmt.Println("err:", err)
  81. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeYanxuanSpecialMomentsImg Err:"+err.Error()+"专栏ID"+strconv.Itoa(specialId), 3)
  82. }
  83. }()
  84. detail, e := models.GetYanxuanSpecialById(specialId, 0)
  85. if e != nil {
  86. err = errors.New("GetYanxuanSpecialById, Err: " + e.Error())
  87. return
  88. }
  89. go GetYanxuanSpecialAuthoListMomentsImg() //有专栏生成的时候,最新专栏列表封面图
  90. //go GetYanxuanSpecialAuthoMomentsImg(detail.UserId) // 有专栏生成的时候,最新wen
  91. configCode := "yanxuan_special_moments_img_html"
  92. detailConfig, e := models.GetConfigByCode(configCode)
  93. if e != nil {
  94. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  95. return
  96. }
  97. var typeName, content string
  98. if detail.Type == 1 {
  99. typeName = "笔记"
  100. } else {
  101. typeName = "观点"
  102. }
  103. content, _ = GetReportContentTextSubNew(detail.Content)
  104. configValue := detailConfig.ConfigValue
  105. configValue = strings.Replace(configValue, "{{HeadImg}}", detail.HeadImg, -1)
  106. configValue = strings.Replace(configValue, "{{NickName}}", detail.NickName, -1)
  107. configValue = strings.Replace(configValue, "{{TypeName}}", typeName, -1)
  108. configValue = strings.Replace(configValue, "{{PublishTime}}", detail.PublishTime, -1)
  109. configValue = strings.Replace(configValue, "{{Title}}", detail.Title, -1)
  110. configValue = strings.Replace(configValue, "{{Content}}", content, -1)
  111. htm2ImgReq := make(map[string]interface{})
  112. htm2ImgReq["html_content"] = configValue
  113. htm2ImgReq["width"] = 2250
  114. htm2ImgReq["height"] = 3813
  115. res, err := postHtml2Img(htm2ImgReq)
  116. if err != nil {
  117. err = errors.New("html转图片失败: " + err.Error())
  118. return
  119. }
  120. if res == nil {
  121. err = errors.New("html转图片失败: ")
  122. return
  123. }
  124. if res.Code != 200 {
  125. err = errors.New("html转图片失败: " + res.Msg)
  126. return
  127. }
  128. imgUrl = res.Data
  129. err = models.UpdateYanxuanSpecialMomentsImg(imgUrl, specialId)
  130. return
  131. }
  132. //func init() {
  133. // var pars []interface{}
  134. // //GetYanxuanSpecialAuthoMomentsImg(53592)
  135. // list, tmpErr := models.GetYanxuanSpecialAuthorList("", "", pars, 0, 9999)
  136. // if tmpErr != nil {
  137. // fmt.Println(tmpErr)
  138. // return
  139. // }
  140. //
  141. // for _, v := range list {
  142. // GetYanxuanSpecialAuthoMomentsImg(v.UserId)
  143. // }
  144. //}
  145. // 生成研选专栏分享到朋友圈的图片
  146. func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
  147. var err error
  148. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  149. defer func() {
  150. if err != nil {
  151. fmt.Println("err:", err)
  152. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(userId), 3)
  153. }
  154. }()
  155. articleInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
  156. if e != nil {
  157. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  158. return
  159. }
  160. configCode := "special_author_moments_img_html"
  161. detailConfig, e := models.GetConfigByCode(configCode)
  162. if e != nil {
  163. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  164. return
  165. }
  166. configValue := detailConfig.ConfigValue
  167. configValue = strings.Replace(configValue, "{{HeadImg}}", articleInfo.HeadImg, -1)
  168. configValue = strings.Replace(configValue, "{{SpecialName}}", articleInfo.SpecialName, -1)
  169. configValue = strings.Replace(configValue, "{{NickName}}", articleInfo.NickName, -1)
  170. configValue = strings.Replace(configValue, "{{SpecialArticleNum}}", strconv.Itoa(articleInfo.SpecialArticleNum), -1)
  171. configValue = strings.Replace(configValue, "{{CollectNum}}", strconv.Itoa(articleInfo.CollectNum), -1)
  172. configValue = strings.Replace(configValue, "{{FollowNum}}", strconv.Itoa(articleInfo.FollowNum), -1)
  173. configValue = strings.Replace(configValue, "{{Introduction}}", articleInfo.Introduction, -1)
  174. htm2ImgReq := make(map[string]interface{})
  175. htm2ImgReq["html_content"] = configValue
  176. htm2ImgReq["width"] = 2250
  177. htm2ImgReq["height"] = 3813
  178. res, err := postHtml2Img(htm2ImgReq)
  179. if err != nil || res == nil {
  180. err = errors.New("html转图片失败: " + res.Msg)
  181. return
  182. }
  183. if res.Code != 200 {
  184. err = errors.New("html转图片失败: " + res.Msg)
  185. return
  186. }
  187. imgUrl = res.Data
  188. err = models.UpdateYanxuanSpecialauthorMomentsImg(imgUrl, userId)
  189. fmt.Println(imgUrl)
  190. return
  191. }
  192. // 生成研选专栏分享到朋友圈的图片
  193. func GetYanxuanSpecialAuthoListMomentsImg() (imgUrl string) {
  194. var err error
  195. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  196. defer func() {
  197. if err != nil {
  198. fmt.Println("err:", err)
  199. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error(), 3)
  200. }
  201. }()
  202. var condition string
  203. var pars []interface{}
  204. condition += ` AND a.nick_name <> '' `
  205. condition += ` ORDER BY latest_publish_time DESC`
  206. list, e := models.GetYanxuanSpecialAuthorList("", condition, pars, 0, 1)
  207. if e != nil {
  208. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  209. return
  210. }
  211. var SpecialName, HeadImg, NickName, Introduction, PublishTime, Title string
  212. var userIds []int
  213. for _, v := range list {
  214. SpecialName = v.SpecialName
  215. HeadImg = v.HeadImg
  216. NickName = v.NickName
  217. Introduction = v.Introduction
  218. userIds = append(userIds, v.UserId)
  219. }
  220. bestNew := GetBestNewYanxuanSpecialByUserId(userIds)
  221. for _, v := range list {
  222. if bestNew[v.UserId] != nil {
  223. PublishTime = bestNew[v.UserId].PublishTime
  224. Title = bestNew[v.UserId].Title
  225. }
  226. }
  227. configCode := "special_author_list_moments_img_html"
  228. detailConfig, e := models.GetConfigByCode(configCode)
  229. if e != nil {
  230. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  231. return
  232. }
  233. configValue := detailConfig.ConfigValue
  234. configValue = strings.Replace(configValue, "{{SpecialName}}", SpecialName, -1)
  235. configValue = strings.Replace(configValue, "{{HeadImg}}", HeadImg, -1)
  236. configValue = strings.Replace(configValue, "{{NickName}}", NickName, -1)
  237. configValue = strings.Replace(configValue, "{{Introduction}}", Introduction, -1)
  238. configValue = strings.Replace(configValue, "{{PublishTime}}", PublishTime, -1)
  239. configValue = strings.Replace(configValue, "{{Title}}", Title, -1)
  240. htm2ImgReq := make(map[string]interface{})
  241. htm2ImgReq["html_content"] = configValue
  242. htm2ImgReq["width"] = 2250
  243. htm2ImgReq["height"] = 3813
  244. res, err := postHtml2Img(htm2ImgReq)
  245. if err != nil {
  246. err = errors.New("html转图片失败: " + err.Error())
  247. return
  248. }
  249. if res == nil {
  250. err = errors.New("html转图片失败: ")
  251. return
  252. }
  253. if res.Code != 200 {
  254. err = errors.New("html转图片失败: " + res.Msg)
  255. return
  256. }
  257. imgUrl = res.Data
  258. configCodeUpdate := "special_author_list_moments_img"
  259. err = models.UpdateConfigByCode(imgUrl, configCodeUpdate)
  260. return
  261. }