wechat_send_msg.go 11 KB

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