wechat_send_msg.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_clpt/models"
  8. "hongze/hongze_clpt/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 访谈申请
  16. func SendInterviewApplyTemplateMsg(realName, companyName, mobile, articleTitle string, itemOpenid *models.OpenIdList) (err error) {
  17. defer func() {
  18. if err != nil {
  19. go utils.SendAlarmMsg(fmt.Sprint("访谈申请模板消息推送消息失败,手机号", mobile, "公司:", companyName, "手机号:", mobile), 2)
  20. }
  21. }()
  22. keyword1 := realName + "【" + companyName + "】"
  23. first := "您有一条新的访谈申请,请及时处理。"
  24. keyword2 := mobile
  25. keyword3 := time.Now().Format(utils.FormatDateTime)
  26. keyword4 := articleTitle
  27. openIdArr := make([]string, 0)
  28. openIdArr = append(openIdArr, itemOpenid.OpenId)
  29. redirectUrl := ""
  30. sendInfo := new(SendWxTemplate)
  31. sendInfo.First = first
  32. sendInfo.Keyword1 = keyword1
  33. sendInfo.Keyword2 = keyword2
  34. sendInfo.Keyword3 = keyword3
  35. sendInfo.Keyword4 = keyword4
  36. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  37. sendInfo.RedirectUrl = redirectUrl
  38. sendInfo.RedirectTarget = 3
  39. //sendInfo.Resource =
  40. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
  41. sendInfo.OpenIdArr = openIdArr
  42. err = PublicSendTemplateMsg(sendInfo)
  43. return
  44. }
  45. // 访谈申请取消
  46. func SendInterviewApplyCancelTemplateMsg(realName, companyName, mobile, articleTitle string, itemOpenid *models.OpenIdList) (err error) {
  47. defer func() {
  48. if err != nil {
  49. go utils.SendAlarmMsg(fmt.Sprint("访谈申请取消模板消息推送消息失败,手机号", mobile, "公司:", companyName, "手机号:", mobile), 2)
  50. }
  51. }()
  52. keyword1 := articleTitle
  53. keyword2 := time.Now().Format(utils.FormatDateTime)
  54. openIdArr := make([]string, 0)
  55. openIdArr = append(openIdArr, itemOpenid.OpenId)
  56. redirectUrl := ""
  57. sendInfo := new(SendWxTemplate)
  58. sendInfo.Keyword1 = keyword1
  59. sendInfo.Keyword2 = keyword2
  60. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyCancelXzs
  61. sendInfo.RedirectUrl = redirectUrl
  62. sendInfo.RedirectTarget = 3
  63. //sendInfo.Resource =
  64. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
  65. sendInfo.OpenIdArr = openIdArr
  66. err = PublicSendTemplateMsg(sendInfo)
  67. fmt.Println("send end")
  68. return
  69. }
  70. // 权限申请
  71. func SendPermissionApplyTemplateMsg(realName, companyName, mobile, applyMethod, redirectUrl string, user *models.WxUserItem) (err error) {
  72. defer func() {
  73. if err != nil {
  74. go utils.SendAlarmMsg(fmt.Sprint("权限申请模板消息推送消息失败,手机号", mobile, "公司:", companyName, "手机号:", mobile), 2)
  75. }
  76. }()
  77. companyId := user.CompanyId
  78. //权益客户
  79. raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
  80. if e != nil {
  81. err = errors.New("GetCompanyProductCount, Err: " + e.Error())
  82. return
  83. }
  84. var mobileOpenid string
  85. if raiCount == 0 {
  86. mobileOpenid = utils.WxMsgTemplateIdAskMsgMobilePublic
  87. } else {
  88. sealldetail, e := models.GetRaiSellerByCompanyId(companyId)
  89. if e != nil {
  90. err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error())
  91. return
  92. }
  93. //如果有共享销售给共享销售也推模版消息
  94. if sealldetail.ShareSellerId > 0 {
  95. adminShare, e := models.GetSellerByAdminId(sealldetail.ShareSellerId)
  96. if e != nil {
  97. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  98. return
  99. }
  100. mobileOpenid = sealldetail.Mobile + "," + adminShare.Mobile
  101. } else {
  102. mobileOpenid = sealldetail.Mobile
  103. }
  104. }
  105. openIdList, e := models.GetWxOpenIdByMobileList(mobileOpenid)
  106. if e != nil {
  107. err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error())
  108. return
  109. }
  110. if len(openIdList) == 0 {
  111. return
  112. }
  113. openIdArr := make([]string, 0)
  114. for _, v := range openIdList {
  115. openIdArr = append(openIdArr, v.OpenId)
  116. }
  117. var first string
  118. var keyword1 string
  119. var keyword2 string
  120. var keyword3 string
  121. var keyword4 string
  122. var remark string
  123. //var redirectUrl string
  124. detail, e := models.GetCygxApplyRecordByMobile(mobile)
  125. if e != nil {
  126. err = errors.New("GetCygxApplyRecordByMobile" + e.Error())
  127. return
  128. }
  129. //如果是潜在客户就推送可查看详情的模版消息
  130. if detail.ApplyMethod == 2 {
  131. redirectUrl = utils.WX_MSG_PATH_APPLY_DETAIL + strconv.Itoa(detail.ApplyRecordId)
  132. }
  133. keyword1 = "权限申请:" + realName + "【" + companyName + "】"
  134. keyword2 = mobile
  135. keyword3 = time.Now().Format(utils.FormatDateTime)
  136. keyword4 = applyMethod
  137. sendInfo := new(SendWxTemplate)
  138. sendInfo.First = first
  139. sendInfo.Keyword1 = keyword1
  140. sendInfo.Keyword2 = keyword2
  141. sendInfo.Keyword3 = keyword3
  142. sendInfo.Keyword4 = keyword4
  143. sendInfo.Remark = remark
  144. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  145. sendInfo.RedirectUrl = redirectUrl
  146. sendInfo.RedirectTarget = 3
  147. sendInfo.Resource = mobile
  148. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
  149. sendInfo.OpenIdArr = openIdArr
  150. err = PublicSendTemplateMsg(sendInfo)
  151. return
  152. }
  153. func sendTemplateMsg(sendUrl string, sendMap map[string]interface{}, itemOpenid *models.OpenIdList, resource string, sendType int) (err error) {
  154. sendMap["touser"] = itemOpenid.OpenId
  155. data, err := json.Marshal(sendMap)
  156. if err != nil {
  157. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  158. return err
  159. }
  160. err = toSendTemplateMsg(sendUrl, data, resource, sendType, itemOpenid)
  161. if err != nil {
  162. fmt.Println("send err:", err.Error())
  163. }
  164. return
  165. }
  166. func toSendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) {
  167. client := http.Client{}
  168. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  169. if err != nil {
  170. return
  171. }
  172. defer resp.Body.Close()
  173. body, _ := ioutil.ReadAll(resp.Body)
  174. var templateResponse SendTemplateResponse
  175. err = json.Unmarshal(body, &templateResponse)
  176. fmt.Println(templateResponse)
  177. utils.FileLog.Info("SendResult %s:", string(body))
  178. if err != nil {
  179. utils.FileLog.Info("SendResult Unmarshal Err:%s", err.Error())
  180. return err
  181. }
  182. //新增模板消息推送记录
  183. {
  184. tr := new(models.UserTemplateRecord)
  185. tr.UserId = dataItem.UserId
  186. tr.OpenId = dataItem.OpenId
  187. tr.Resource = resource
  188. tr.SendData = string(data)
  189. tr.Result = string(body)
  190. tr.CreateDate = time.Now().Format(utils.FormatDate)
  191. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  192. if templateResponse.Errcode == 0 {
  193. tr.SendStatus = 1
  194. } else {
  195. tr.SendStatus = 0
  196. }
  197. tr.SendType = sendType
  198. go func() {
  199. err = models.AddUserTemplateRecord(tr)
  200. if err != nil {
  201. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  202. }
  203. }()
  204. }
  205. return
  206. }
  207. type SendTemplateResponse struct {
  208. Errcode int `json:"errcode"`
  209. Errmsg string `json:"errmsg"`
  210. MsgID int `json:"msgid"`
  211. }
  212. type SendWxTemplate struct {
  213. WxAppId string `description:"公众号appId"`
  214. First string `description:"模板消息first字段"`
  215. Keyword1 string `description:"模板消息keyword1字段"`
  216. Keyword2 string `description:"模板消息keyword2字段"`
  217. Keyword3 string `description:"模板消息keyword3字段"`
  218. Keyword4 string `description:"模板消息keyword4字段"`
  219. Remark string `description:"模板消息remark字段"`
  220. TemplateId string `description:"模板id"`
  221. RedirectUrl string `description:"跳转地址"`
  222. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  223. Resource string `description:"资源唯一标识"`
  224. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  225. OpenIdArr []string `description:"消息接收者openid"`
  226. }
  227. // 推送模板消息
  228. func PublicSendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  229. postData, err := json.Marshal(sendInfo)
  230. if err != nil {
  231. utils.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  232. return err
  233. }
  234. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  235. client := &http.Client{}
  236. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  237. if err != nil {
  238. utils.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  239. return err
  240. }
  241. contentType := "application/json;charset=utf-8"
  242. req.Header.Set("Content-Type", contentType)
  243. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  244. resp, err := client.Do(req)
  245. if err != nil {
  246. fmt.Println("http client.Do Err:" + err.Error())
  247. return err
  248. }
  249. defer resp.Body.Close()
  250. b, err := ioutil.ReadAll(resp.Body)
  251. if err != nil {
  252. return err
  253. }
  254. result := new(models.BaseResponse)
  255. err = json.Unmarshal(b, &result)
  256. if err != nil {
  257. return err
  258. }
  259. if result.Ret != 200 {
  260. err = errors.New(string(b))
  261. return err
  262. }
  263. return
  264. }