wx_template_msg.go 13 KB

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