wechat_send_msg.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_mobile_admin/models"
  7. "hongze/hongze_mobile_admin/models/tables/admin"
  8. "hongze/hongze_mobile_admin/services/alarm_msg"
  9. "hongze/hongze_mobile_admin/utils"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "time"
  14. )
  15. type SendWxTemplate struct {
  16. WxAppId string `description:"公众号appId"`
  17. First string `description:"模板消息first字段"`
  18. Keyword1 string `description:"模板消息keyword1字段"`
  19. Keyword2 string `description:"模板消息keyword2字段"`
  20. Keyword3 string `description:"模板消息keyword3字段"`
  21. Keyword4 string `description:"模板消息keyword4字段"`
  22. Keyword5 string `description:"模板消息keyword5字段"`
  23. Remark string `description:"模板消息remark字段"`
  24. TemplateId string `description:"模板id"`
  25. RedirectUrl string `description:"跳转地址"`
  26. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  27. Resource string `description:"资源唯一标识"`
  28. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  29. OpenIdArr []string `description:"消息接收者openid"`
  30. }
  31. type BaseResponse struct {
  32. Ret int
  33. Msg string
  34. ErrMsg string
  35. ErrCode string
  36. Data interface{}
  37. Success bool `description:"true 执行成功,false 执行失败"`
  38. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  39. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  40. }
  41. type SendTemplateResponse struct {
  42. Errcode int `json:"errcode"`
  43. Errmsg string `json:"errmsg"`
  44. MsgID int `json:"msgid"`
  45. }
  46. // SendCompanyApplyWxTemplateMsg 发送待办消息
  47. func SendCompanyApplyWxTemplateMsg(mobile, redirectUrl, wxAppPath string, wxMsgMap map[int]string) (err error) {
  48. var msg string
  49. defer func() {
  50. if err != nil {
  51. go alarm_msg.SendAlarmMsg(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg), 3)
  52. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  53. utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg)
  54. }
  55. if msg != "" {
  56. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  57. }
  58. }()
  59. utils.FileLog.Info("%s", "services SendMsg")
  60. utils.FileLog.Info("mobile:%s", mobile)
  61. //获取openid列表
  62. openIdStr := WxAdminOpenIdGet()
  63. openIdList, err := admin.GetOpenIdListByMobile(mobile, openIdStr)
  64. if err != nil {
  65. msg = "get openIdList err:" + err.Error()
  66. return
  67. }
  68. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  69. //fmt.Println("openIdListCount:", len(openIdList))
  70. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  71. utils.FileLog.Info("start send")
  72. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  73. fmt.Println("send start")
  74. utils.FileLog.Info("send start")
  75. //sendMap := make(map[string]interface{})
  76. //sendData := make(map[string]interface{})
  77. var first, keyword1, keyword2, keyword3, remark string
  78. if tmpStr, ok := wxMsgMap[0]; ok {
  79. first = tmpStr
  80. } else {
  81. first = "Hi,有新的订单需要审批"
  82. }
  83. if tmpStr, ok := wxMsgMap[1]; ok {
  84. keyword1 = tmpStr
  85. }
  86. if tmpStr, ok := wxMsgMap[2]; ok {
  87. keyword2 = tmpStr
  88. }
  89. if tmpStr, ok := wxMsgMap[3]; ok {
  90. keyword3 = tmpStr
  91. }
  92. if tmpStr, ok := wxMsgMap[4]; ok {
  93. remark = tmpStr
  94. }
  95. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  96. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  97. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  98. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  99. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  100. //
  101. //sendMap["template_id"] = utils.TemplateIdByCompanyApply
  102. //sendMap["url"] = redirectUrl
  103. //sendMap["data"] = sendData
  104. ////小程序信息
  105. //if wxAppPath != "" {
  106. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
  107. //}
  108. //sendTemplateMsg(sendUrl, sendMap, openIdList, mobile, utils.TEMPLATE_MSG_APPLY)
  109. openIdArr := make([]string, len(openIdList))
  110. for i, v := range openIdList {
  111. openIdArr[i] = v.OpenId
  112. }
  113. sendInfo := new(SendWxTemplate)
  114. sendInfo.WxAppId = utils.AdminWxAppId
  115. sendInfo.First = first
  116. sendInfo.Keyword1 = keyword1
  117. sendInfo.Keyword2 = keyword2
  118. sendInfo.Keyword3 = keyword3
  119. sendInfo.Remark = remark
  120. sendInfo.TemplateId = utils.TemplateIdByCompanyApply
  121. sendInfo.RedirectUrl = redirectUrl
  122. sendInfo.RedirectTarget = 2
  123. sendInfo.Resource = mobile
  124. sendInfo.SendType = utils.TEMPLATE_MSG_APPLY
  125. sendInfo.OpenIdArr = openIdArr
  126. if wxAppPath != "" {
  127. sendInfo.RedirectUrl = wxAppPath
  128. }
  129. err = SendTemplateMsg(sendInfo)
  130. }
  131. utils.FileLog.Info("send end")
  132. return
  133. }
  134. // SendWxMsgWithRoadshowDetailResult 路演->销售收到处理结果
  135. func SendWxMsgWithRoadshowDetailResult(first, keyword1, keyword2, keyword3, keyword4, keyword5, remark, mobile, redirectUrl, wxAppPath string) (err error) {
  136. var msg string
  137. defer func() {
  138. if err != nil {
  139. go alarm_msg.SendAlarmMsg(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg), 3)
  140. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  141. utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg)
  142. }
  143. if msg != "" {
  144. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  145. }
  146. }()
  147. utils.FileLog.Info("%s", "services SendMsg")
  148. utils.FileLog.Info("mobile:%s", mobile)
  149. //获取openid列表
  150. openIdStr := WxAdminOpenIdGet()
  151. openIdList, err := admin.GetOpenIdListByMobile(mobile, openIdStr)
  152. if err != nil {
  153. msg = "get openIdList err:" + err.Error()
  154. return
  155. }
  156. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  157. //fmt.Println("openIdListCount:", len(openIdList))
  158. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  159. utils.FileLog.Info("start send")
  160. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  161. //fmt.Println("send start")
  162. //utils.FileLog.Info("send start")
  163. //sendMap := make(map[string]interface{})
  164. //sendData := make(map[string]interface{})
  165. //
  166. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  167. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  168. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  169. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  170. //
  171. //sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowDetailResult
  172. //sendMap["url"] = redirectUrl
  173. //sendMap["data"] = sendData
  174. ////小程序信息
  175. //if wxAppPath != "" {
  176. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
  177. //}
  178. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  179. openIdArr := make([]string, len(openIdList))
  180. for i, v := range openIdList {
  181. openIdArr[i] = v.OpenId
  182. }
  183. sendInfo := new(SendWxTemplate)
  184. sendInfo.WxAppId = utils.AdminWxAppId
  185. sendInfo.First = first
  186. sendInfo.Keyword1 = keyword1
  187. sendInfo.Keyword2 = keyword2
  188. sendInfo.Keyword3 = keyword3
  189. sendInfo.Keyword4 = keyword4
  190. sendInfo.Keyword5 = keyword5
  191. sendInfo.Remark = remark
  192. sendInfo.TemplateId = utils.WxMsgTemplateIdWithRoadshowDetailResult
  193. if wxAppPath != "" && utils.RunMode == "release" {
  194. sendInfo.RedirectUrl = wxAppPath
  195. }
  196. sendInfo.RedirectTarget = 2
  197. sendInfo.Resource = wxAppPath
  198. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  199. sendInfo.OpenIdArr = openIdArr
  200. err = SendTemplateMsg(sendInfo)
  201. }
  202. utils.FileLog.Info("send end")
  203. return
  204. }
  205. // SendWxMsgWithRoadshowPending 路演->研究员收到待处理的申请
  206. func SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath, mobile string) (err error) {
  207. //utils.WxMsgTemplateIdWithRoadshowPending
  208. var msg string
  209. defer func() {
  210. if err != nil {
  211. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  212. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+msg, 3)
  213. utils.FileLog.Info("发送模版消息失败,Err:" + err.Error() + msg)
  214. }
  215. if msg != "" {
  216. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  217. }
  218. }()
  219. utils.FileLog.Info("%s", "services SendMsg")
  220. //获取openid列表
  221. openIdStr := WxAdminOpenIdGet()
  222. openIdList, err := admin.GetOpenIdListByMobile(mobile, openIdStr)
  223. if err != nil {
  224. msg = "get openIdList err:" + err.Error()
  225. return
  226. }
  227. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  228. //fmt.Println("openIdListCount:", len(openIdList))
  229. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  230. utils.FileLog.Info("start send")
  231. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  232. //fmt.Println("send start")
  233. //utils.FileLog.Info("send start")
  234. //sendMap := make(map[string]interface{})
  235. //sendData := make(map[string]interface{})
  236. //
  237. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  238. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  239. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  240. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  241. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  242. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  243. //
  244. //sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowPending
  245. //sendMap["data"] = sendData
  246. ////小程序信息
  247. //if wxAppPath != "" {
  248. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
  249. //}
  250. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  251. openIdArr := make([]string, len(openIdList))
  252. for i, v := range openIdList {
  253. openIdArr[i] = v.OpenId
  254. }
  255. sendInfo := new(SendWxTemplate)
  256. sendInfo.WxAppId = utils.AdminWxAppId
  257. sendInfo.First = first
  258. sendInfo.Keyword1 = keyword1
  259. sendInfo.Keyword2 = keyword2
  260. sendInfo.Keyword3 = keyword3
  261. sendInfo.Keyword4 = keyword4
  262. sendInfo.Remark = remark
  263. sendInfo.TemplateId = utils.WxMsgTemplateIdWithRoadshowPending
  264. if wxAppPath != "" && utils.RunMode == "release" {
  265. sendInfo.RedirectUrl = wxAppPath
  266. }
  267. sendInfo.RedirectTarget = 2
  268. sendInfo.Resource = wxAppPath
  269. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  270. sendInfo.OpenIdArr = openIdArr
  271. err = SendTemplateMsg(sendInfo)
  272. }
  273. utils.FileLog.Info("send end")
  274. return
  275. }
  276. // SendWxMsgWithRoadshowDeleteNotice 路演->研究员收到活动删除通知
  277. func SendWxMsgWithRoadshowDeleteNotice(first, keyword1, keyword2, remark, wxAppPath, mobile string) (err error) {
  278. var msg string
  279. defer func() {
  280. if err != nil {
  281. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  282. go alarm_msg.SendAlarmMsg("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05")+";Msg:"+msg+";Err:"+err.Error(), 3)
  283. utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg)
  284. }
  285. if msg != "" {
  286. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  287. }
  288. }()
  289. utils.FileLog.Info("%s", "services SendMsg")
  290. //获取openid列表
  291. openIdStr := WxAdminOpenIdGet()
  292. openIdList, err := admin.GetOpenIdListByMobile(mobile, openIdStr)
  293. if err != nil {
  294. msg = "get openIdList err:" + err.Error()
  295. return
  296. }
  297. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  298. //fmt.Println("openIdListCount:", len(openIdList))
  299. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  300. utils.FileLog.Info("start send")
  301. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  302. //sendMap := make(map[string]interface{})
  303. //sendData := make(map[string]interface{})
  304. //
  305. //sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowDeleteNotice
  306. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  307. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  308. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  309. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  310. ////小程序信息
  311. //if wxAppPath != "" {
  312. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
  313. //}
  314. //sendMap["data"] = sendData
  315. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  316. openIdArr := make([]string, len(openIdList))
  317. for i, v := range openIdList {
  318. openIdArr[i] = v.OpenId
  319. }
  320. sendInfo := new(SendWxTemplate)
  321. sendInfo.WxAppId = utils.AdminWxAppId
  322. sendInfo.First = first
  323. sendInfo.Keyword1 = keyword1
  324. sendInfo.Keyword2 = keyword2
  325. sendInfo.Remark = remark
  326. sendInfo.TemplateId = utils.WxMsgTemplateIdWithRoadshowDeleteNotice
  327. if wxAppPath != "" && utils.RunMode == "release" {
  328. sendInfo.RedirectUrl = wxAppPath
  329. }
  330. sendInfo.RedirectTarget = 2
  331. sendInfo.Resource = wxAppPath
  332. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  333. sendInfo.OpenIdArr = openIdArr
  334. err = SendTemplateMsg(sendInfo)
  335. }
  336. utils.FileLog.Info("send end")
  337. return
  338. }
  339. // SendSealFinishedWxTemplateMsg 给销售发送 用印申请已签回的模版消息
  340. func SendSealFinishedWxTemplateMsg(mobile string, companyName string, sealId int) (err error) {
  341. var msg string
  342. defer func() {
  343. if err != nil {
  344. fmt.Println("err:", err)
  345. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  346. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  347. }
  348. if msg != "" {
  349. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  350. }
  351. }()
  352. utils.FileLog.Info("%s", "services SendMsg")
  353. utils.FileLog.Info("mobile:%s", mobile)
  354. openIdStr := WxAdminOpenIdGet()
  355. openIdList, err := admin.GetOpenIdListByMobile(mobile, openIdStr)
  356. if err != nil {
  357. msg = "get openIdList err:" + err.Error()
  358. return
  359. }
  360. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  361. if len(openIdList) > 0 && utils.WxMsgTemplateIdWithSealApplyFinished != "" {
  362. utils.FileLog.Info("start send")
  363. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  364. //fmt.Println("send start")
  365. //utils.FileLog.Info("send start")
  366. //sendMap := make(map[string]interface{})
  367. //sendData := make(map[string]interface{})
  368. first := "您的用印申请已签回,可前往提交转正/续约申请"
  369. keyword1 := companyName
  370. //keyword2 := "已签回"
  371. keyword2 := "用印申请已签回,可前往提交转正/续约申请"
  372. remark := "点击查看用印详情"
  373. //
  374. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  375. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  376. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  377. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  378. //
  379. //sendMap["template_id"] = utils.WxMsgTemplateIdWithSealApplyFinished
  380. //sendMap["url"] = "" //跳转地址
  381. //sendMap["data"] = sendData
  382. // TODO 随手办公小程序的跳转地址
  383. wxAppPath := fmt.Sprintf("pages-approve/seal/detail?SealId=%d", sealId)
  384. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
  385. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_SEAL_FINISHED)
  386. openIdArr := make([]string, len(openIdList))
  387. for i, v := range openIdList {
  388. openIdArr[i] = v.OpenId
  389. }
  390. sendInfo := new(SendWxTemplate)
  391. sendInfo.WxAppId = utils.AdminWxAppId
  392. sendInfo.First = first
  393. sendInfo.Keyword1 = keyword1
  394. sendInfo.Keyword2 = keyword2
  395. sendInfo.Remark = remark
  396. sendInfo.RedirectUrl = wxAppPath
  397. sendInfo.TemplateId = utils.WxMsgTemplateIdWithSealApplyFinished
  398. sendInfo.RedirectTarget = 2
  399. sendInfo.Resource = wxAppPath
  400. sendInfo.SendType = utils.TEMPLATE_MSG_SEAL_FINISHED
  401. sendInfo.OpenIdArr = openIdArr
  402. err = SendTemplateMsg(sendInfo)
  403. }
  404. utils.FileLog.Info("send end")
  405. return
  406. }
  407. // SendYbQuestionDistributeWxMsg 推送研报小程序模板消息-问答社区分配
  408. func SendYbQuestionDistributeWxMsg(questionId, adminId int, openid, questionTitle string) (err error) {
  409. var errMsg string
  410. defer func() {
  411. if err != nil {
  412. alarmMsg := fmt.Sprintf("SendMiniProgramReportWxMsg-推送问答社区分配模版消息失败; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  413. utils.FileLog.Error(alarmMsg)
  414. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  415. }
  416. }()
  417. openIdList := make([]*admin.OpenIdList, 0)
  418. openIdList = append(openIdList, &admin.OpenIdList{
  419. OpenId: openid,
  420. AdminId: adminId,
  421. })
  422. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  423. //sendMap := make(map[string]interface{})
  424. //sendData := make(map[string]interface{})
  425. first := "您好,有新的提问待回复"
  426. keyword1 := questionTitle
  427. keyword2 := "待回复"
  428. remark := "请点击详情尽快处理"
  429. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  430. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  431. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  432. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  433. //
  434. //sendMap["template_id"] = utils.WxMsgTemplateIdWithYbCommunityQuestion
  435. //sendMap["data"] = sendData
  436. //
  437. wxAppPath := fmt.Sprintf("pages-question/answerDetail?id=%d", questionId)
  438. //if wxAppPath != "" {
  439. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  440. //}
  441. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  442. openIdArr := make([]string, len(openIdList))
  443. for i, v := range openIdList {
  444. openIdArr[i] = v.OpenId
  445. }
  446. sendInfo := new(SendWxTemplate)
  447. sendInfo.WxAppId = utils.AdminWxAppId
  448. sendInfo.First = first
  449. sendInfo.Keyword1 = keyword1
  450. sendInfo.Keyword2 = keyword2
  451. sendInfo.Remark = remark
  452. sendInfo.RedirectUrl = wxAppPath
  453. sendInfo.TemplateId = utils.WxMsgTemplateIdWithYbCommunityQuestion
  454. sendInfo.RedirectTarget = 1
  455. sendInfo.Resource = wxAppPath
  456. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  457. sendInfo.OpenIdArr = openIdArr
  458. err = SendTemplateMsg(sendInfo)
  459. utils.FileLog.Info("send end")
  460. return
  461. }
  462. // 推送模板消息
  463. func SendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  464. postData, err := json.Marshal(sendInfo)
  465. if err != nil {
  466. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  467. return err
  468. }
  469. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  470. client := &http.Client{}
  471. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  472. if err != nil {
  473. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  474. return err
  475. }
  476. contentType := "application/json;charset=utf-8"
  477. req.Header.Set("Content-Type", contentType)
  478. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  479. resp, err := client.Do(req)
  480. if err != nil {
  481. fmt.Println("http client.Do Err:" + err.Error())
  482. return err
  483. }
  484. defer resp.Body.Close()
  485. b, err := ioutil.ReadAll(resp.Body)
  486. if err != nil {
  487. return err
  488. }
  489. result := new(BaseResponse)
  490. err = json.Unmarshal(b, &result)
  491. if err != nil {
  492. return err
  493. }
  494. if result.Ret != 200 {
  495. err = errors.New(string(b))
  496. return err
  497. }
  498. return
  499. }
  500. // SendWxMsgWithRaiSell 权益销售客户申请转正后,消息群发给所有销售
  501. func SendWxMsgWithRaiSell(first, keyword1, keyword2, keyword3, keyword4 string, openIdList []*models.OpenIdList) (err error) {
  502. var msg string
  503. defer func() {
  504. if err != nil {
  505. fmt.Println("err:", err)
  506. go alarm_msg.SendAlarmMsg("权益销售客户申请转正后,消息群发给所有销售,Err:"+err.Error()+";msg:"+msg, 3)
  507. }
  508. if msg != "" {
  509. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  510. }
  511. }()
  512. openIdArr := make([]string, len(openIdList))
  513. for i, v := range openIdList {
  514. openIdArr[i] = v.OpenId
  515. }
  516. sendInfo := new(SendWxTemplate)
  517. sendInfo.First = first
  518. sendInfo.Keyword1 = keyword1
  519. sendInfo.Keyword2 = keyword2
  520. sendInfo.Keyword3 = keyword3
  521. sendInfo.Keyword4 = keyword4
  522. sendInfo.Remark = ""
  523. sendInfo.TemplateId = utils.WxMsgTemplateIdCompanyApprovalMessageRai
  524. sendInfo.RedirectUrl = ""
  525. sendInfo.RedirectTarget = 3
  526. sendInfo.SendType = utils.TEMPLATE_MSG_APPLY
  527. sendInfo.OpenIdArr = openIdArr
  528. fmt.Println(sendInfo)
  529. err = SendTemplateMsg(sendInfo)
  530. return
  531. }
  532. // SendWxMsgWithRaiWangYang 客户研选行业转正时(王芳审批通过),模板消息提醒汪洋
  533. func SendWxMsgWithRaiWangYang(keyword1, keyword2, keyword3, keyword4 string, openIdList []*models.OpenIdList) (err error) {
  534. var msg string
  535. defer func() {
  536. if err != nil {
  537. fmt.Println("err:", err)
  538. go alarm_msg.SendAlarmMsg("客户研选行业转正时(王芳审批通过),模板消息提醒汪洋,Err:"+err.Error()+";msg:"+msg, 3)
  539. }
  540. if msg != "" {
  541. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  542. }
  543. }()
  544. openIdArr := make([]string, len(openIdList))
  545. for i, v := range openIdList {
  546. openIdArr[i] = v.OpenId
  547. }
  548. sendInfo := new(SendWxTemplate)
  549. sendInfo.Keyword1 = keyword1
  550. sendInfo.Keyword2 = keyword2
  551. sendInfo.Keyword3 = keyword3
  552. sendInfo.Keyword4 = keyword4
  553. sendInfo.TemplateId = utils.WxMsgTemplateIdCompanyApprovalMessageRai
  554. sendInfo.RedirectUrl = ""
  555. sendInfo.RedirectTarget = 3
  556. sendInfo.SendType = utils.TEMPLATE_MSG_APPLY
  557. sendInfo.OpenIdArr = openIdArr
  558. err = SendTemplateMsg(sendInfo)
  559. return
  560. }