activity_poster.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "io"
  12. "io/ioutil"
  13. "net/http"
  14. "os"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. var (
  20. ServerUrl = "http://127.0.0.1:5008/"
  21. Cygx_activity_sigin_html = "cygx_activity_sigin_html"
  22. Cygx_mp3_html = "cygx_mp3_html"
  23. Cygx_mp4_html = "cygx_mp4_html"
  24. )
  25. type Html2ImgResp struct {
  26. Code int `json:"code"`
  27. Msg string `json:"msg"`
  28. Data string `json:"data"`
  29. }
  30. // postHtml2Img 请求htm2img接口
  31. func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error) {
  32. // 目前仅此处调用该接口,暂不加授权、校验等
  33. postUrl := ServerUrl + "htm2img"
  34. postData, err := json.Marshal(param)
  35. if err != nil {
  36. return
  37. }
  38. result, err := Html2ImgHttpPost(postUrl, string(postData), "application/json")
  39. if err != nil {
  40. return
  41. }
  42. if err = json.Unmarshal(result, &resp); err != nil {
  43. return
  44. }
  45. return resp, nil
  46. }
  47. // Html2ImgHttpPost post请求
  48. func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
  49. body := ioutil.NopCloser(strings.NewReader(postData))
  50. client := &http.Client{}
  51. req, err := http.NewRequest("POST", url, body)
  52. if err != nil {
  53. return nil, err
  54. }
  55. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  56. if len(params) > 0 && params[0] != "" {
  57. contentType = params[0]
  58. }
  59. req.Header.Set("Content-Type", contentType)
  60. resp, err := client.Do(req)
  61. if err != nil {
  62. return nil, err
  63. }
  64. defer resp.Body.Close()
  65. b, err := ioutil.ReadAll(resp.Body)
  66. fmt.Println("HttpPost:" + string(b))
  67. return b, err
  68. }
  69. //func init() {
  70. // MakeActivitySigninImg(2161)
  71. //}
  72. // MakeActivitySigninImg 生成太阳码并上传OSS
  73. func MakeActivitySigninImg(activityId int) (imgUrl string, err error) {
  74. var msg string
  75. defer func() {
  76. if err != nil || msg != "" {
  77. fmt.Println(err)
  78. utils.FileLog.Info("MakeActivitySigninImg Err:", err.Error())
  79. go alarm_msg.SendAlarmMsg("扫码签到日志记录,失败,活动ID:"+strconv.Itoa(activityId)+err.Error()+";msg:"+msg, 3)
  80. }
  81. }()
  82. activityInfo, e := cygx.GetAddActivityInfoById(activityId)
  83. if e != nil {
  84. err = errors.New("活动不存在, Err: " + e.Error())
  85. return
  86. }
  87. if activityInfo == nil {
  88. return
  89. }
  90. if activityInfo.ActivityTypeId != 5 && activityInfo.ActivityTypeId != 6 && activityInfo.ActivityTypeId != 8 {
  91. return
  92. }
  93. itemToken, err := models.GetWxToken(utils.WxCygxAppId, utils.WxCygxAppSecret)
  94. if err != nil {
  95. return
  96. }
  97. if itemToken.AccessToken == "" {
  98. msg = "accessToken is empty"
  99. return
  100. }
  101. var envVersion string
  102. var resourceUrl string
  103. if utils.RunMode == "release" {
  104. envVersion = "release"
  105. } else {
  106. envVersion = "trial"
  107. }
  108. url := "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + itemToken.AccessToken
  109. method := "POST"
  110. payload := strings.NewReader(`{
  111. "page":"` + utils.WX_MSG_PATH_ACTIVITY_SIGNIN + `",
  112. "scene":"` + strconv.Itoa(activityId) + `",
  113. "env_version":"` + envVersion + `",
  114. "check_path":false,
  115. "auto_color":true
  116. }`)
  117. client := &http.Client{}
  118. req, err := http.NewRequest(method, url, payload)
  119. if err != nil {
  120. msg = "获取微信二维码失败,Err:" + err.Error()
  121. return
  122. }
  123. req.Header.Add("Content-Type", "application/json")
  124. postBody, err := client.Do(req)
  125. if err != nil {
  126. msg = "获取微信二维码失败,Err:" + err.Error()
  127. return
  128. }
  129. defer postBody.Body.Close()
  130. uploadDir := "static/img/share/"
  131. uuid := utils.GetRandStringNoSpecialChar(28)
  132. if !utils.FileIsExist(uploadDir) {
  133. err = os.MkdirAll(uploadDir, 0755)
  134. if err != nil {
  135. msg = "生成文件夹失败,Err:" + err.Error()
  136. return
  137. }
  138. }
  139. imagePath := uploadDir + uuid + ".jpg"
  140. switch header := postBody.Header.Get("Content-Type"); {
  141. case strings.HasPrefix(header, "application/json"):
  142. tokenResp := ReturnBodyRule{}
  143. decoder := json.NewDecoder(postBody.Body)
  144. if decodeErr := decoder.Decode(&tokenResp); decodeErr != nil {
  145. msg = "获取微信二维码失败,Err:" + decodeErr.Error()
  146. return
  147. }
  148. case strings.HasPrefix(header, "image"):
  149. reply, e := ioutil.ReadAll(postBody.Body)
  150. if e != nil {
  151. err = e
  152. msg = "获取微信二维码失败,Err:" + err.Error()
  153. return
  154. }
  155. imageContent, e := os.Create(imagePath)
  156. if e != nil {
  157. err = e
  158. msg = "获取微信二维码失败,Err:" + err.Error()
  159. return
  160. }
  161. writeStringRes, e := io.WriteString(imageContent, string(reply))
  162. if e != nil {
  163. err = e
  164. fmt.Println(writeStringRes)
  165. return
  166. }
  167. closeErr := imageContent.Close()
  168. if closeErr != nil {
  169. err = closeErr
  170. return
  171. }
  172. randStr := utils.GetRandStringNoSpecialChar(28)
  173. fileName := randStr + ".jpg"
  174. savePath := uploadDir + time.Now().Format("200601/20060102/")
  175. savePath += fileName
  176. //上传到阿里云
  177. err = services.UploadFileToAliyun(fileName, imagePath, savePath)
  178. if err != nil {
  179. fmt.Println("文件上传失败,Err:" + err.Error())
  180. return
  181. }
  182. fileHost := "https://hzstatic.hzinsights.com/"
  183. resourceUrl = fileHost + savePath
  184. defer func() {
  185. os.Remove(imagePath)
  186. }()
  187. default:
  188. msg = "生成二维码失败"
  189. return
  190. }
  191. detailConfig, e := cygx.GetCygxConfigDetailByCode(Cygx_activity_sigin_html)
  192. if e != nil {
  193. err = errors.New("GetCygxConfigDetailByCode 获取配置签到码格式信息失败, Err: " + e.Error())
  194. return
  195. }
  196. configValue := detailConfig.ConfigValue
  197. configValue = strings.Replace(configValue, "{{TITLE}}", activityInfo.ActivityName, -1)
  198. configValue = strings.Replace(configValue, "{{IMG}}", resourceUrl, -1)
  199. configValue = strings.Replace(configValue, "{{PLEASE}}", "请扫码确认签到", -1)
  200. configValue = strings.Replace(configValue, "{{SHOWTEXT}}", "将签到成功页面出示给接待人员", -1)
  201. htm2ImgReq := make(map[string]interface{})
  202. htm2ImgReq["html_content"] = configValue
  203. htm2ImgReq["width"] = 1364
  204. htm2ImgReq["height"] = 2060
  205. res, err := postHtml2Img(htm2ImgReq)
  206. if err != nil || res == nil {
  207. msg = "html转图片请求失败"
  208. return
  209. }
  210. if res.Code != 200 {
  211. msg = "html转图片请求失败"
  212. err = errors.New("html转图片失败: " + res.Msg)
  213. return
  214. }
  215. imgUrl = res.Data
  216. // 记录海报信息
  217. err = cygx.UpdateCygxActivitySigninImg(imgUrl, activityId)
  218. if err != nil {
  219. return
  220. }
  221. item := new(cygx.CygxActivityPoster)
  222. item.ActivityId = activityId
  223. item.ImgXcx = resourceUrl
  224. item.ImgPoster = imgUrl
  225. item.CreateTime = time.Now()
  226. err = cygx.AddCygxActivityPoster(item)
  227. return
  228. }
  229. // 生成音视频分享封面图
  230. func MakeCygxMp3HtmlImg(videoDuration string) (imgUrl string, err error) {
  231. var msg string
  232. defer func() {
  233. if err != nil || msg != "" {
  234. fmt.Println(err)
  235. go alarm_msg.SendAlarmMsg("生成音视频分享封面图,失败 MakeCygxMp3HtmlImg:"+err.Error()+";msg:"+msg, 3)
  236. }
  237. }()
  238. detailConfig, e := cygx.GetCygxConfigDetailByCode(Cygx_mp3_html)
  239. if e != nil {
  240. err = errors.New("GetCygxConfigDetailByCode 获取配置生成音视频分享封面图格式信息失败, Err: " + e.Error())
  241. return
  242. }
  243. // 处理时长带有小数点的字符串
  244. slice := strings.Split(videoDuration, ".")
  245. for k, v := range slice {
  246. if k != 0 {
  247. continue
  248. }
  249. videoDuration = v
  250. }
  251. //先转换时长展示样式再替换
  252. secondNum, _ := strconv.Atoi(videoDuration)
  253. videoDuration = utils.HideSecondsToMs(secondNum)
  254. configValue := detailConfig.ConfigValue
  255. configValue = strings.Replace(configValue, "{{TIME}}", videoDuration, -1)
  256. htm2ImgReq := make(map[string]interface{})
  257. htm2ImgReq["html_content"] = configValue
  258. htm2ImgReq["width"] = 1364
  259. htm2ImgReq["height"] = 2060
  260. res, err := postHtml2Img(htm2ImgReq)
  261. if err != nil || res == nil {
  262. msg = "html转图片请求失败"
  263. return
  264. }
  265. if res.Code != 200 {
  266. msg = "html转图片请求失败"
  267. err = errors.New("html转图片失败: " + res.Msg)
  268. return
  269. }
  270. imgUrl = res.Data
  271. return
  272. }
  273. // 生成音视频分享封面图
  274. func MakeCygxMp4HtmlImg(videoDuration string) (imgUrl string, err error) {
  275. var msg string
  276. defer func() {
  277. if err != nil || msg != "" {
  278. fmt.Println(err)
  279. go alarm_msg.SendAlarmMsg("生成音视频分享封面图,失败 MakeCygxMp4HtmlImg:"+err.Error()+";msg:"+msg, 3)
  280. }
  281. }()
  282. detailConfig, e := cygx.GetCygxConfigDetailByCode(Cygx_mp4_html)
  283. if e != nil {
  284. err = errors.New("GetCygxConfigDetailByCode 获取配置生成音视频分享封面图格式信息失败, Err: " + e.Error())
  285. return
  286. }
  287. // 处理时长带有小数点的字符串
  288. slice := strings.Split(videoDuration, ".")
  289. for k, v := range slice {
  290. if k != 0 {
  291. continue
  292. }
  293. videoDuration = v
  294. }
  295. //先转换时长展示样式再替换
  296. secondNum, _ := strconv.Atoi(videoDuration)
  297. videoDuration = utils.HideSecondsToMs(secondNum)
  298. configValue := detailConfig.ConfigValue
  299. configValue = strings.Replace(configValue, "{{TIME}}", videoDuration, -1)
  300. htm2ImgReq := make(map[string]interface{})
  301. htm2ImgReq["html_content"] = configValue
  302. htm2ImgReq["width"] = 1364
  303. htm2ImgReq["height"] = 2060
  304. res, err := postHtml2Img(htm2ImgReq)
  305. if err != nil || res == nil {
  306. msg = "html转图片请求失败"
  307. return
  308. }
  309. if res.Code != 200 {
  310. msg = "html转图片请求失败"
  311. err = errors.New("html转图片失败: " + res.Msg)
  312. return
  313. }
  314. imgUrl = res.Data
  315. return
  316. }
  317. func init() {
  318. configValue := `<!DOCTYPE html>
  319. <html lang="en">
  320. <head>
  321. <meta charset="UTF-8" />
  322. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  323. <title>Document</title>
  324. <style>
  325. body,
  326. html {
  327. margin: 0;
  328. padding: 0;
  329. font-family: PingFang SC;
  330. color: #333;
  331. }
  332. * {
  333. padding: 0;
  334. margin: 0;
  335. }
  336. div {
  337. box-sizing: border-box;
  338. }
  339. .box {
  340. position: relative;
  341. width: 2250px;
  342. height: 3813px;
  343. }
  344. .img-responsive {
  345. width: 100%;
  346. height: 100%;
  347. object-fit: cover;
  348. }
  349. .content-box {
  350. position: absolute;
  351. left: 162px;
  352. top: 1290px;
  353. width: 1926px;
  354. height: 1170px;
  355. padding: 30px;
  356. background-color: #fff;
  357. overflow: hidden;
  358. }
  359. .content-top .report-title {
  360. font-weight: 500;
  361. font-size: 108px;
  362. line-height: 162px;
  363. margin-bottom: 90px;
  364. }
  365. .content-top .report-research {
  366. height: 250px;
  367. overflow: hidden;
  368. }
  369. .content-top .report-research .research-author {
  370. font-weight: 600;
  371. font-size: 96px;
  372. margin-left: 30px;
  373. }
  374. .content-top .report-research .research-author .time {
  375. color: #999;
  376. margin-top: 10px;
  377. font-weight: 400;
  378. font-size: 84px;
  379. }
  380. .content-top .report-research img {
  381. width: 243px;
  382. height: 243px;
  383. float: left;
  384. object-fit: cover;
  385. margin-right: 30px;
  386. }
  387. .abstract-content {
  388. position: relative;
  389. margin-top: 90px;
  390. font-size: 96px;
  391. line-height: 150px;
  392. padding-bottom: 126px;
  393. border-bottom: 6px dashed #dcdfe6;
  394. }
  395. .abstract-content:after {
  396. content: "";
  397. position: absolute;
  398. width: 12px;
  399. height: 84px;
  400. left: 0;
  401. top: 25px;
  402. background-color: #3385ff;
  403. }
  404. .detail-report {
  405. margin-top: 90px;
  406. font-size: 96px;
  407. line-height: 150px;
  408. }
  409. .text-two {
  410. -webkit-line-clamp: 2;
  411. -webkit-box-orient: vertical;
  412. display: -webkit-box;
  413. overflow: hidden;
  414. text-overflow: ellipsis;
  415. }
  416. </style>
  417. </head>
  418. <body>
  419. <div class="box">
  420. <img class="img-responsive" src="https://hzstatic.hzinsights.com/yx_xcx/share_bg.jpg" alt="" />
  421. <div class="content-box">
  422. <div class="content-top">
  423. <div class="report-title">{{Title}} 这里是标题</div>
  424. <div class="report-research">
  425. <img class="user-imh" src="https://hzstatic.hzinsights.com/cygx/config/ico_mp3.png" alt="这里是作者头像" />
  426. <div class="research-author">
  427. <p class="text-two">{{ SellerAndMobile }} 这里名称</p>
  428. <p class="time">{{ PublishDate }}这里是发布时间</p>
  429. </div>
  430. </div>
  431. <div class="abstract-content">&nbsp;&nbsp;摘要:&nbsp;{{ Abstract }}这里是摘要这里是摘要,</div>
  432. </div>
  433. <div class="detail-report">这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容</div>
  434. </div>
  435. </div>
  436. </body>
  437. </html>
  438. `
  439. //configValue = strings.Replace(configValue, "{{TIME}}", videoDuration, -1)
  440. htm2ImgReq := make(map[string]interface{})
  441. htm2ImgReq["html_content"] = configValue
  442. htm2ImgReq["width"] = 2250
  443. htm2ImgReq["height"] = 3813
  444. res, err := postHtml2Img(htm2ImgReq)
  445. if err != nil || res == nil {
  446. //msg = "html转图片请求失败"
  447. fmt.Println(err)
  448. return
  449. }
  450. if res.Code != 200 {
  451. //msg = "html转图片请求失败"
  452. err = errors.New("html转图片失败: " + res.Msg)
  453. fmt.Println(err)
  454. return
  455. }
  456. imgUrl := res.Data
  457. fmt.Println(imgUrl)
  458. }