wechat_send_msg.go 10 KB

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