wx_template_msg.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_task/models"
  8. "hongze/hongze_task/services/alarm_msg"
  9. "hongze/hongze_task/utils"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "time"
  14. )
  15. func SendWxMsgWithFrequency(first, keyword1, keyword2, remark string, openIdList []*models.OpenIdList) (err error) {
  16. var msg string
  17. defer func() {
  18. if err != nil {
  19. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  20. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  21. }
  22. fmt.Println("line 21", err, msg)
  23. }()
  24. utils.FileLog.Info("services SendMsg")
  25. /*accessToken, err := models.GetWxAccessToken()
  26. if err != nil {
  27. msg = "GetWxAccessToken Err:" + err.Error()
  28. return
  29. }
  30. if accessToken == "" {
  31. msg = "accessToken is empty"
  32. return
  33. }
  34. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  35. templateId := utils.TemplateId
  36. //if true{
  37. sendMap := make(map[string]interface{})
  38. sendData := make(map[string]interface{})
  39. sendMap["template_id"] = templateId
  40. //redirectUrl:="report.hzinsights.com/#/allindex/"+strconv.Itoa(v.ResearchReportId)+"/"+v.ReportType;
  41. //sendMap["url"]=redirectUrl
  42. sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  43. sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  44. sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  45. sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  46. sendMap["data"] = sendData
  47. WxSendTemplateMsg(sendUrl, sendMap, openIdList)*/
  48. wxAppPath := ""
  49. openIdArr := make([]string, len(openIdList))
  50. for i, v := range openIdList {
  51. openIdArr[i] = v.OpenId
  52. }
  53. sendInfo := new(SendWxTemplate)
  54. sendInfo.WxAppId = utils.AdminWxAppId
  55. sendInfo.First = first
  56. sendInfo.Keyword1 = keyword1
  57. sendInfo.Keyword2 = keyword2
  58. sendInfo.RedirectUrl = wxAppPath
  59. sendInfo.RedirectTarget = 0
  60. sendInfo.TemplateId = utils.TemplateId
  61. sendInfo.Resource = wxAppPath
  62. sendInfo.OpenIdArr = openIdArr
  63. sendInfo.Remark = remark
  64. err = SendTemplateMsgV2(sendInfo)
  65. return
  66. }
  67. func WxSendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList) (err error) {
  68. for _, v := range items {
  69. sendMap["touser"] = v.OpenId
  70. data, err := json.Marshal(sendMap)
  71. if err != nil {
  72. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  73. return err
  74. }
  75. utils.FileLog.Info(fmt.Sprintf("One SendData:%s", string(data)))
  76. err = SendTemplateMsg(sendUrl, data)
  77. if err != nil {
  78. fmt.Println("send err:", err.Error())
  79. return err
  80. }
  81. }
  82. return
  83. }
  84. func SendTemplateMsg(sendUrl string, data []byte) (err error) {
  85. client := http.Client{}
  86. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  87. if err != nil {
  88. return
  89. }
  90. defer resp.Body.Close()
  91. body, _ := ioutil.ReadAll(resp.Body)
  92. utils.FileLog.Info(fmt.Sprintf("SendResult:%s", string(body)))
  93. var templateResponse models.SendTemplateResponse
  94. err = json.Unmarshal(body, &templateResponse)
  95. if err != nil {
  96. return err
  97. }
  98. return
  99. }
  100. // 到期提醒模板消息
  101. func SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, code string, openIdList []*models.OpenIdList) (err error) {
  102. var msg string
  103. defer func() {
  104. if err != nil {
  105. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  106. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  107. }
  108. }()
  109. utils.FileLog.Info("%s", "services SendMsg")
  110. /*accessToken, err := models.GetWxAccessToken()
  111. if err != nil {
  112. msg = "GetWxAccessToken Err:" + err.Error()
  113. return
  114. }
  115. if accessToken == "" {
  116. msg = "accessToken is empty"
  117. return
  118. }
  119. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  120. sendMap := make(map[string]interface{})
  121. sendData := make(map[string]interface{})
  122. sendMap["template_id"] = utils.RemindTemplateId
  123. sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  124. sendData["productname"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  125. sendData["date"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  126. sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  127. sendMap["data"] = sendData
  128. WxSendTemplateMsg(sendUrl, sendMap, openIdList)*/
  129. var wxAppPath string
  130. if utils.RunMode == `debug` {
  131. wxAppPath = `http://msgsendtest.hzinsights.com/productExpirationHint.html?code=` + code
  132. } else {
  133. wxAppPath = `https://msgsend.hzinsights.com//productExpirationHint.html?code=` + code
  134. }
  135. openIdArr := make([]string, len(openIdList))
  136. for i, v := range openIdList {
  137. openIdArr[i] = v.OpenId
  138. }
  139. sendInfo := new(SendWxTemplate)
  140. sendInfo.WxAppId = utils.AdminWxAppId
  141. sendInfo.First = first
  142. sendInfo.Productname = keyword1
  143. sendInfo.Date = keyword2
  144. sendInfo.RedirectUrl = wxAppPath
  145. sendInfo.RedirectTarget = 0
  146. sendInfo.TemplateId = utils.RemindTemplateId
  147. sendInfo.Resource = wxAppPath
  148. sendInfo.OpenIdArr = openIdArr
  149. sendInfo.Remark = remark
  150. err = SendTemplateMsgV2(sendInfo)
  151. return
  152. }
  153. // SendWxMsgWithActivityAppointmentRemind 活动预约/报名时间通知
  154. func SendWxMsgWithActivityAppointmentRemind(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath string, openIdList []*models.OpenIdList) (err error) {
  155. var msg string
  156. defer func() {
  157. if err != nil {
  158. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  159. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s", err.Error()))
  160. }
  161. }()
  162. utils.FileLog.Info("%s", "services SendMsg")
  163. openIdArr := make([]string, len(openIdList))
  164. for i, v := range openIdList {
  165. openIdArr[i] = v.OpenId
  166. }
  167. sendInfo := new(SendWxTemplate)
  168. sendInfo.WxAppId = utils.WxAppId
  169. sendInfo.First = first
  170. sendInfo.Keyword1 = keyword1
  171. sendInfo.Keyword2 = keyword2
  172. sendInfo.RedirectUrl = wxAppPath
  173. sendInfo.RedirectTarget = 1
  174. sendInfo.TemplateId = utils.AppointmentRemindTemplateId
  175. sendInfo.Resource = wxAppPath
  176. sendInfo.OpenIdArr = openIdArr
  177. sendInfo.Remark = remark
  178. err = SendTemplateMsgV2(sendInfo)
  179. return
  180. }
  181. type SendWxTemplate struct {
  182. WxAppId string `description:"公众号appId"`
  183. First string `description:"模板消息first字段"`
  184. Keyword1 string `description:"模板消息keyword1字段"`
  185. Keyword2 string `description:"模板消息keyword2字段"`
  186. Keyword3 string `description:"模板消息keyword3字段"`
  187. Keyword4 string `description:"模板消息keyword4字段"`
  188. Productname string `description:"模板消息productname字段"`
  189. Date string `description:"模板消息date字段"`
  190. Remark string `description:"模板消息remark字段"`
  191. TemplateId string `description:"模板id"`
  192. RedirectUrl string `description:"跳转地址"`
  193. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  194. Resource string `description:"资源唯一标识"`
  195. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  196. OpenIdArr []string `description:"消息接收者openid"`
  197. }
  198. // 推送模板消息
  199. func SendTemplateMsgV2(sendInfo *SendWxTemplate) (err error) {
  200. postData, err := json.Marshal(sendInfo)
  201. if err != nil {
  202. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  203. return err
  204. }
  205. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  206. client := &http.Client{}
  207. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  208. if err != nil {
  209. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  210. return err
  211. }
  212. contentType := "application/json;charset=utf-8"
  213. req.Header.Set("Content-Type", contentType)
  214. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  215. resp, err := client.Do(req)
  216. if err != nil {
  217. fmt.Println("http client.Do Err:" + err.Error())
  218. return err
  219. }
  220. defer resp.Body.Close()
  221. b, err := ioutil.ReadAll(resp.Body)
  222. if err != nil {
  223. return err
  224. }
  225. result := new(models.BaseResponse)
  226. err = json.Unmarshal(b, &result)
  227. if err != nil {
  228. return err
  229. }
  230. if result.Ret != 200 {
  231. err = errors.New(string(b))
  232. return err
  233. }
  234. return
  235. }
  236. // SendYbVoiceBroadcastWxMsg 推送研报语音播报模板消息
  237. func SendYbVoiceBroadcastWxMsg(broadcastId int, sectionName, broadcastName string) (err error) {
  238. var msg string
  239. defer func() {
  240. if err != nil {
  241. errMsg := "SendYbVoiceBroadcastWxMsg-定时任务发送语音播报模版消息失败; Err: " + err.Error() + "; Msg: " + msg
  242. utils.FileLog.Info("%s", errMsg)
  243. go alarm_msg.SendAlarmMsg(errMsg, 3)
  244. }
  245. }()
  246. openIdList, err := models.GetOpenIdListV2()
  247. if err != nil {
  248. msg = "get openIdList err:" + err.Error()
  249. return
  250. }
  251. openIdArr := make([]string, 0)
  252. for _, v := range openIdList {
  253. openIdArr = append(openIdArr, v.OpenId)
  254. }
  255. first := "您好,有新的语音播报待查看"
  256. //keyword1 := fmt.Sprint(sectionName, ":", broadcastName)
  257. //keyword2 := "待查看"
  258. keyword1 := broadcastName
  259. keyword2 := fmt.Sprintf("【%s】 待查看", sectionName)
  260. remark := "请点击详情查看"
  261. wxAppPath := fmt.Sprintf("pages-voice/voiceDetail?voiceId=%d", broadcastId)
  262. sendInfo := new(SendWxTemplate)
  263. sendInfo.First = first
  264. sendInfo.Keyword1 = keyword1
  265. sendInfo.Keyword2 = keyword2
  266. sendInfo.RedirectUrl = wxAppPath
  267. sendInfo.TemplateId = utils.YbCommonTemplateId
  268. sendInfo.RedirectTarget = 1
  269. sendInfo.Resource = wxAppPath
  270. sendInfo.SendType = utils.TEMPLATE_MSG_YB_VOICE_BROADCAST
  271. sendInfo.OpenIdArr = openIdArr
  272. sendInfo.Remark = remark
  273. err = SendTemplateMsgV2(sendInfo)
  274. return
  275. }
  276. // SendWxMsgWithEtaBusinessRemind ETA商家-到期提醒
  277. func SendWxMsgWithEtaBusinessRemind(first, keyword1, keyword2, remark, code string, openIdList []*models.OpenIdList) (err error) {
  278. defer func() {
  279. if err != nil {
  280. tips := "ETA商家-推送到期提醒模板消息失败, Err: " + err.Error()
  281. utils.FileLog.Info("%s", tips)
  282. go alarm_msg.SendAlarmMsg(tips, 3)
  283. //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  284. }
  285. }()
  286. utils.FileLog.Info("%s", "services SendMsg")
  287. var wxAppPath string
  288. if utils.RunMode == `debug` {
  289. wxAppPath = `http://msgsendtest.hzinsights.com/ETAExpirationHint.html?code=` + code
  290. } else {
  291. wxAppPath = `https://msgsend.hzinsights.com/ETAExpirationHint.html?code=` + code
  292. }
  293. openIdArr := make([]string, len(openIdList))
  294. for i, v := range openIdList {
  295. openIdArr[i] = v.OpenId
  296. }
  297. if len(openIdArr) == 0 {
  298. return
  299. }
  300. sendInfo := new(SendWxTemplate)
  301. sendInfo.WxAppId = utils.AdminWxAppId
  302. sendInfo.First = first
  303. sendInfo.Productname = keyword1
  304. sendInfo.Date = keyword2
  305. sendInfo.RedirectUrl = wxAppPath
  306. sendInfo.RedirectTarget = 0
  307. sendInfo.TemplateId = utils.RemindTemplateId
  308. sendInfo.Resource = wxAppPath
  309. sendInfo.OpenIdArr = openIdArr
  310. sendInfo.Remark = remark
  311. err = SendTemplateMsgV2(sendInfo)
  312. return
  313. }