wechat_send_msg.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. listSellerMobile, e := models.GetCygxSellerMobileMap()
  101. if e != nil {
  102. err = errors.New("GetCygxSellerMobileMap, Err: " + e.Error())
  103. return
  104. }
  105. //mapMobile := make(map[string]string)
  106. for _, vm := range listSellerMobile {
  107. if adminShare.Mobile == vm.ServerMobile {
  108. adminShare.Mobile = vm.Mobile
  109. }
  110. }
  111. mobileOpenid = sealldetail.Mobile + "," + adminShare.Mobile
  112. } else {
  113. mobileOpenid = sealldetail.Mobile
  114. }
  115. }
  116. openIdList, e := models.GetWxOpenIdByMobileList(mobileOpenid)
  117. if e != nil {
  118. err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error())
  119. return
  120. }
  121. if len(openIdList) == 0 {
  122. return
  123. }
  124. openIdArr := make([]string, 0)
  125. for _, v := range openIdList {
  126. openIdArr = append(openIdArr, v.OpenId)
  127. }
  128. var first string
  129. var keyword1 string
  130. var keyword2 string
  131. var keyword3 string
  132. var keyword4 string
  133. var remark string
  134. //var redirectUrl string
  135. detail, e := models.GetCygxApplyRecordByMobile(mobile)
  136. if e != nil {
  137. err = errors.New("GetCygxApplyRecordByMobile" + e.Error())
  138. return
  139. }
  140. //如果是潜在客户就推送可查看详情的模版消息
  141. if detail.ApplyMethod == 2 {
  142. redirectUrl = utils.WX_MSG_PATH_APPLY_DETAIL + strconv.Itoa(detail.ApplyRecordId)
  143. }
  144. keyword1 = "权限申请:" + realName + "【" + companyName + "】"
  145. keyword2 = mobile
  146. keyword3 = time.Now().Format(utils.FormatDateTime)
  147. keyword4 = applyMethod
  148. sendInfo := new(SendWxTemplate)
  149. sendInfo.First = first
  150. sendInfo.Keyword1 = keyword1
  151. sendInfo.Keyword2 = keyword2
  152. sendInfo.Keyword3 = keyword3
  153. sendInfo.Keyword4 = keyword4
  154. sendInfo.Remark = remark
  155. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  156. sendInfo.RedirectUrl = redirectUrl
  157. sendInfo.RedirectTarget = 3
  158. sendInfo.Resource = mobile
  159. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
  160. sendInfo.OpenIdArr = openIdArr
  161. err = PublicSendTemplateMsg(sendInfo)
  162. return
  163. }
  164. func sendTemplateMsg(sendUrl string, sendMap map[string]interface{}, itemOpenid *models.OpenIdList, resource string, sendType int) (err error) {
  165. sendMap["touser"] = itemOpenid.OpenId
  166. data, err := json.Marshal(sendMap)
  167. if err != nil {
  168. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  169. return err
  170. }
  171. err = toSendTemplateMsg(sendUrl, data, resource, sendType, itemOpenid)
  172. if err != nil {
  173. fmt.Println("send err:", err.Error())
  174. }
  175. return
  176. }
  177. func toSendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) {
  178. client := http.Client{}
  179. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  180. if err != nil {
  181. return
  182. }
  183. defer resp.Body.Close()
  184. body, _ := ioutil.ReadAll(resp.Body)
  185. var templateResponse SendTemplateResponse
  186. err = json.Unmarshal(body, &templateResponse)
  187. fmt.Println(templateResponse)
  188. utils.FileLog.Info("SendResult %s:", string(body))
  189. if err != nil {
  190. utils.FileLog.Info("SendResult Unmarshal Err:%s", err.Error())
  191. return err
  192. }
  193. //新增模板消息推送记录
  194. {
  195. tr := new(models.UserTemplateRecord)
  196. tr.UserId = dataItem.UserId
  197. tr.OpenId = dataItem.OpenId
  198. tr.Resource = resource
  199. tr.SendData = string(data)
  200. tr.Result = string(body)
  201. tr.CreateDate = time.Now().Format(utils.FormatDate)
  202. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  203. if templateResponse.Errcode == 0 {
  204. tr.SendStatus = 1
  205. } else {
  206. tr.SendStatus = 0
  207. }
  208. tr.SendType = sendType
  209. go func() {
  210. err = models.AddUserTemplateRecord(tr)
  211. if err != nil {
  212. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  213. }
  214. }()
  215. }
  216. return
  217. }
  218. type SendTemplateResponse struct {
  219. Errcode int `json:"errcode"`
  220. Errmsg string `json:"errmsg"`
  221. MsgID int `json:"msgid"`
  222. }
  223. type SendWxTemplate struct {
  224. WxAppId string `description:"公众号appId"`
  225. First string `description:"模板消息first字段"`
  226. Keyword1 string `description:"模板消息keyword1字段"`
  227. Keyword2 string `description:"模板消息keyword2字段"`
  228. Keyword3 string `description:"模板消息keyword3字段"`
  229. Keyword4 string `description:"模板消息keyword4字段"`
  230. Remark string `description:"模板消息remark字段"`
  231. TemplateId string `description:"模板id"`
  232. RedirectUrl string `description:"跳转地址"`
  233. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  234. Resource string `description:"资源唯一标识"`
  235. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  236. OpenIdArr []string `description:"消息接收者openid"`
  237. }
  238. // 推送模板消息
  239. func PublicSendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  240. postData, err := json.Marshal(sendInfo)
  241. if err != nil {
  242. utils.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  243. return err
  244. }
  245. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  246. client := &http.Client{}
  247. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  248. if err != nil {
  249. utils.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  250. return err
  251. }
  252. contentType := "application/json;charset=utf-8"
  253. req.Header.Set("Content-Type", contentType)
  254. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  255. resp, err := client.Do(req)
  256. if err != nil {
  257. fmt.Println("http client.Do Err:" + err.Error())
  258. return err
  259. }
  260. defer resp.Body.Close()
  261. b, err := ioutil.ReadAll(resp.Body)
  262. if err != nil {
  263. return err
  264. }
  265. result := new(models.BaseResponse)
  266. err = json.Unmarshal(b, &result)
  267. if err != nil {
  268. return err
  269. }
  270. if result.Ret != 200 {
  271. err = errors.New(string(b))
  272. return err
  273. }
  274. return
  275. }