wechat_send_msg.go 9.3 KB

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