wx_template_msg.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_mfyx/models"
  8. "hongze/hongze_mfyx/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. )
  14. // 发送报告提问消息提醒
  15. func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.OpenIdList, articleId int) (err error) {
  16. if len(openIdList) == 0 {
  17. return
  18. }
  19. var msg string
  20. defer func() {
  21. if err != nil {
  22. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  23. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,用户名", name, "文章ID:", articleId), 2)
  24. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  25. }
  26. fmt.Println("line 21", err, msg)
  27. }()
  28. var first string
  29. var keyword1 string
  30. var keyword2 string
  31. var keyword3 string
  32. var keyword4 string
  33. var remark string
  34. first = "研选报告有新的提问,请及时处理"
  35. keyword1 = name
  36. keyword3 = askTime
  37. keyword2 = ""
  38. keyword4 = askMsg
  39. remark = title
  40. openIdArr := make([]string, 0)
  41. for _, v := range openIdList {
  42. openIdArr = append(openIdArr, v.OpenId)
  43. }
  44. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  45. sendInfo := new(SendWxTemplate)
  46. sendInfo.First = first
  47. sendInfo.Keyword1 = keyword1
  48. sendInfo.Keyword2 = keyword2
  49. sendInfo.Keyword3 = keyword3
  50. sendInfo.Keyword4 = keyword4
  51. sendInfo.Remark = remark
  52. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  53. sendInfo.RedirectUrl = redirectUrl
  54. sendInfo.RedirectTarget = 3
  55. sendInfo.Resource = strconv.Itoa(articleId)
  56. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  57. sendInfo.OpenIdArr = openIdArr
  58. err = PublicSendTemplateMsg(sendInfo)
  59. if err != nil {
  60. return
  61. }
  62. return
  63. }
  64. // 发送用户阅读报告消息提醒
  65. func SendWxMsgWithArticleUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, articleId int) (err error) {
  66. if len(openIdList) == 0 {
  67. return
  68. }
  69. defer func() {
  70. if err != nil {
  71. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,文章ID:", articleId), 2)
  72. }
  73. }()
  74. //accessToken, err := models.GetWxAccessTokenByXzs()
  75. //if err != nil {
  76. // msg = "GetWxAccessToken Err:" + err.Error()
  77. // return
  78. //}
  79. //if accessToken == "" {
  80. // msg = "accessToken is empty"
  81. // return
  82. //}
  83. //var first string
  84. //first = keyWord1
  85. ////keyword1 := keyWord2
  86. ////keyword1 := keyWord2
  87. //remark := "点击查看报告详情"
  88. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  89. //templateId := utils.WxMsgTemplateIdArticleUserRemind
  90. //sendMap := make(map[string]interface{})
  91. //sendData := make(map[string]interface{})
  92. //sendMap["template_id"] = templateId
  93. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)}
  94. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  95. //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
  96. //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
  97. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  98. //sendMap["data"] = sendData
  99. //WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(articleId), utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
  100. openIdArr := make([]string, 0)
  101. for _, v := range openIdList {
  102. openIdArr = append(openIdArr, v.OpenId)
  103. }
  104. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  105. sendInfo := new(SendWxTemplate)
  106. sendInfo.Keyword1 = keyword1
  107. sendInfo.Keyword2 = keyword2
  108. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  109. sendInfo.RedirectUrl = redirectUrl
  110. sendInfo.RedirectTarget = 3
  111. sendInfo.Resource = strconv.Itoa(articleId)
  112. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  113. sendInfo.OpenIdArr = openIdArr
  114. err = PublicSendTemplateMsg(sendInfo)
  115. return
  116. }
  117. // 发送用户操作活动消息提醒
  118. func SendWxMsgWithActivityUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, activityId int) (err error) {
  119. if len(openIdList) == 0 {
  120. return
  121. }
  122. defer func() {
  123. if err != nil {
  124. go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,文章ID:", activityId), 2)
  125. }
  126. }()
  127. //accessToken, err := models.GetWxAccessTokenByXzs()
  128. //if err != nil {
  129. // msg = "GetWxAccessToken Err:" + err.Error()
  130. // return
  131. //}
  132. //if accessToken == "" {
  133. // msg = "accessToken is empty"
  134. // return
  135. //}
  136. //var first string
  137. ////first = keyWord1
  138. ////keyword1 := keyWord2
  139. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  140. //templateId := utils.WxMsgTemplateIdArticleUserRemind
  141. //sendMap := make(map[string]interface{})
  142. //sendData := make(map[string]interface{})
  143. //sendMap["template_id"] = templateId
  144. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "activityPages/activityDetail/activityDetail?id=" + strconv.Itoa(activityId)}
  145. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  146. //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
  147. //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
  148. //sendMap["data"] = sendData
  149. //WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(activityId), utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
  150. openIdArr := make([]string, 0)
  151. for _, v := range openIdList {
  152. openIdArr = append(openIdArr, v.OpenId)
  153. }
  154. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  155. sendInfo := new(SendWxTemplate)
  156. sendInfo.Keyword1 = keyword1
  157. sendInfo.Keyword2 = keyword2
  158. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  159. sendInfo.RedirectUrl = redirectUrl
  160. sendInfo.RedirectTarget = 3
  161. sendInfo.Resource = strconv.Itoa(activityId)
  162. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  163. sendInfo.OpenIdArr = openIdArr
  164. err = PublicSendTemplateMsg(sendInfo)
  165. return
  166. }
  167. func WxSendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList, resource string, sendType int) (err error) {
  168. for _, v := range items {
  169. sendMap["touser"] = v.OpenId
  170. data, err := json.Marshal(sendMap)
  171. if err != nil {
  172. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  173. return err
  174. }
  175. utils.FileLog.Info("One SendData:%s", string(data))
  176. fmt.Println(string(data))
  177. err = SendTemplateMsg(sendUrl, data, resource, sendType, v)
  178. if err != nil {
  179. fmt.Println("send err:", err.Error())
  180. return err
  181. }
  182. }
  183. return
  184. }
  185. func SendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) {
  186. client := http.Client{}
  187. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  188. if err != nil {
  189. return
  190. }
  191. defer resp.Body.Close()
  192. body, _ := ioutil.ReadAll(resp.Body)
  193. utils.FileLog.Info("SendResult:%s", string(body))
  194. var templateResponse models.SendTemplateResponse
  195. err = json.Unmarshal(body, &templateResponse)
  196. fmt.Println(templateResponse.Errcode)
  197. fmt.Println(templateResponse.Errmsg)
  198. if err != nil {
  199. return err
  200. }
  201. //新增模板消息推送记录
  202. {
  203. tr := new(models.UserTemplateRecord)
  204. tr.UserId = dataItem.UserId
  205. tr.OpenId = dataItem.OpenId
  206. tr.Resource = resource
  207. tr.SendData = string(data)
  208. tr.Result = string(body)
  209. tr.CreateDate = time.Now().Format(utils.FormatDate)
  210. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  211. if templateResponse.Errcode == 0 {
  212. tr.SendStatus = 1
  213. } else {
  214. tr.SendStatus = 0
  215. }
  216. tr.SendType = sendType
  217. go func() {
  218. err = models.AddUserTemplateRecord(tr)
  219. if err != nil {
  220. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  221. }
  222. }()
  223. }
  224. return
  225. }
  226. // 发送用户留言提醒
  227. // func SendCommentWxTemplateMsg(title, companyName, userName, sellerName, createTime, content string, openIdList []*models.OpenIdList, articleId int) (err error) {
  228. func SendCommentWxTemplateMsg(req models.AddCygxArticleCommentReq, user *models.WxUserItem, articleInfo *models.ArticleDetail, msgId int) (err error) {
  229. var msg string
  230. defer func() {
  231. if err != nil {
  232. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  233. go utils.SendAlarmMsg(fmt.Sprint("发送用户用户留言提醒失败,文章ID:", req.ArticleId), 2)
  234. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  235. }
  236. }()
  237. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  238. if e != nil {
  239. return
  240. }
  241. if companyItem == nil {
  242. msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId)
  243. }
  244. var first string
  245. var keyword1 string
  246. var keyword2 string
  247. var keyword3 string
  248. var keyword4 string
  249. var remark string
  250. var redirectUrl string
  251. var mobile string
  252. first = "【" + articleInfo.Title + "】有新的留言,请及时处理"
  253. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  254. keyword2 = "留言:" + articleInfo.Title
  255. keyword3 = time.Now().Format(utils.FormatDateTime)
  256. keyword4 = req.Content
  257. remark = "点击查看报告详情"
  258. if articleInfo.ArticleTypeId > 0 {
  259. //研选报告添加汪洋
  260. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  261. } else {
  262. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  263. }
  264. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  265. if e != nil {
  266. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  267. return
  268. }
  269. openIdArr := make([]string, 0)
  270. for _, v := range openIdList {
  271. openIdArr = append(openIdArr, v.OpenId)
  272. }
  273. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  274. if utils.RunMode != "release" {
  275. redirectUrl = utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleInfo.ArticleId)
  276. } else {
  277. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=1"
  278. }
  279. sendInfo := new(SendWxTemplate)
  280. sendInfo.First = first
  281. sendInfo.Keyword1 = keyword1
  282. sendInfo.Keyword2 = keyword2
  283. sendInfo.Keyword3 = keyword3
  284. sendInfo.Keyword4 = keyword4
  285. sendInfo.Remark = remark
  286. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  287. sendInfo.RedirectUrl = redirectUrl
  288. sendInfo.RedirectTarget = 3
  289. sendInfo.Resource = strconv.Itoa(req.ArticleId)
  290. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  291. sendInfo.OpenIdArr = openIdArr
  292. err = PublicSendTemplateMsg(sendInfo)
  293. if err != nil {
  294. return
  295. }
  296. return
  297. }