wechat_send_msg.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/utils"
  8. "io/ioutil"
  9. "net/http"
  10. "time"
  11. )
  12. //访谈申请
  13. func SendInterviewApplyTemplateMsg(realName, companyName, mobile, articleTitle, openId string) (err error) {
  14. var msg string
  15. defer func() {
  16. if err != nil {
  17. go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  18. }
  19. if msg != "" {
  20. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  21. }
  22. }()
  23. accessToken, err := models.GetWxAccessToken()
  24. if err != nil {
  25. msg = "GetWxAccessToken Err:" + err.Error()
  26. return
  27. }
  28. if accessToken == "" {
  29. msg = "accessToken is empty"
  30. return
  31. }
  32. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  33. sendMap := make(map[string]interface{})
  34. sendData := make(map[string]interface{})
  35. keyword1 := realName + "【" + companyName + "】"
  36. first := "您有一条新的访谈申请,请及时处理。"
  37. keyword2 := mobile
  38. keyword3 := time.Now().Format(utils.FormatDateTime)
  39. keyword4 := articleTitle
  40. fontColor := "#D9001B"
  41. sendData["first"] = map[string]interface{}{"value": first, "color": fontColor}
  42. sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": fontColor}
  43. sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": fontColor}
  44. sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": fontColor}
  45. sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": fontColor}
  46. sendMap["template_id"] = utils.WxMsgTemplateIdApply
  47. sendMap["data"] = sendData
  48. sendTemplateMsg(sendUrl, openId, sendMap)
  49. fmt.Println("send end")
  50. return
  51. }
  52. //访谈申请取消
  53. func SendInterviewApplyCancelTemplateMsg(realName, companyName, mobile, articleTitle, openId string) (err error) {
  54. var msg string
  55. defer func() {
  56. if err != nil {
  57. go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  58. }
  59. if msg != "" {
  60. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  61. }
  62. }()
  63. accessToken, err := models.GetWxAccessToken()
  64. if err != nil {
  65. msg = "GetWxAccessToken Err:" + err.Error()
  66. return
  67. }
  68. if accessToken == "" {
  69. msg = "accessToken is empty"
  70. return
  71. }
  72. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  73. sendMap := make(map[string]interface{})
  74. sendData := make(map[string]interface{})
  75. first := realName + "-" + mobile + "【" + companyName + "】的访谈申请已取消,请留意。"
  76. keyword1 := articleTitle
  77. keyword2 := time.Now().Format(utils.FormatDateTime)
  78. fontColor := "#D9001B"
  79. sendData["first"] = map[string]interface{}{"value": first, "color": fontColor}
  80. sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": fontColor}
  81. sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": fontColor}
  82. sendMap["template_id"] = utils.WxMsgTemplateIdApplyCancel
  83. sendMap["data"] = sendData
  84. sendTemplateMsg(sendUrl, openId, sendMap)
  85. fmt.Println("send end")
  86. return
  87. }
  88. func SendPermissionApplyTemplateMsg(realName, companyName, mobile, openId,applyMethod string) (err error) {
  89. var msg string
  90. defer func() {
  91. if err != nil {
  92. go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  93. }
  94. if msg != "" {
  95. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  96. }
  97. }()
  98. accessToken, err := models.GetWxAccessToken()
  99. if err != nil {
  100. msg = "GetWxAccessToken Err:" + err.Error()
  101. return
  102. }
  103. if accessToken == "" {
  104. msg = "accessToken is empty"
  105. return
  106. }
  107. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  108. sendMap := make(map[string]interface{})
  109. sendData := make(map[string]interface{})
  110. keyword1 := realName + "【" + companyName + "】"
  111. first := "查研观向有一条新的权限申请,请及时处理。"
  112. keyword2 := mobile
  113. keyword3 := time.Now().Format(utils.FormatDateTime)
  114. keyword4 := applyMethod
  115. fontColor := "#D9001B"
  116. sendData["first"] = map[string]interface{}{"value": first, "color": fontColor}
  117. sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": fontColor}
  118. sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": fontColor}
  119. sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": fontColor}
  120. sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": fontColor}
  121. sendMap["template_id"] = utils.WxMsgTemplateIdApply
  122. sendMap["data"] = sendData
  123. sendTemplateMsg(sendUrl, openId, sendMap)
  124. fmt.Println("send end")
  125. return
  126. }
  127. func sendTemplateMsg(sendUrl, openId string, sendMap map[string]interface{}) (err error) {
  128. sendMap["touser"] = openId
  129. data, err := json.Marshal(sendMap)
  130. if err != nil {
  131. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  132. return err
  133. }
  134. err = toSendTemplateMsg(sendUrl, data)
  135. if err != nil {
  136. fmt.Println("send err:", err.Error())
  137. }
  138. return
  139. }
  140. func toSendTemplateMsg(sendUrl string, data []byte) (err error) {
  141. client := http.Client{}
  142. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  143. if err != nil {
  144. return
  145. }
  146. defer resp.Body.Close()
  147. body, _ := ioutil.ReadAll(resp.Body)
  148. var templateResponse SendTemplateResponse
  149. err = json.Unmarshal(body, &templateResponse)
  150. if err != nil {
  151. utils.FileLog.Info("SendResult Unmarshal Err:%s", err.Error())
  152. return err
  153. }
  154. return
  155. }
  156. type SendTemplateResponse struct {
  157. Errcode int `json:"errcode"`
  158. Errmsg string `json:"errmsg"`
  159. MsgID int `json:"msgid"`
  160. }