wechat_send_msg.go 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/cygx"
  9. "hongze/hz_crm_api/models/system"
  10. "hongze/hz_crm_api/services/alarm_msg"
  11. "hongze/hz_crm_api/utils"
  12. "io/ioutil"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. func SendWxTemplateMsg(reportId int) (err error) {
  19. var msg string
  20. defer func() {
  21. if err != nil {
  22. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  23. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  24. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  25. }
  26. if msg != "" {
  27. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,msg:%s", msg))
  28. }
  29. }()
  30. utils.FileLog.Info("%s", "services SendMsg")
  31. report, err := models.GetReportById(reportId)
  32. if err != nil {
  33. msg = "GetReportInfo Err:" + err.Error()
  34. return
  35. }
  36. if report == nil {
  37. utils.FileLog.Info("报告信息不存在")
  38. return
  39. }
  40. //accessToken, err := models.GetWxAccessToken()
  41. //if err != nil {
  42. // msg = "GetWxAccessToken Err:" + err.Error()
  43. // return
  44. //}
  45. //if accessToken == "" {
  46. // msg = "accessToken is empty"
  47. // return
  48. //}
  49. openIdArr, err := models.GetOpenIdArr()
  50. if err != nil {
  51. msg = "get GetOpenIdArr err:" + err.Error()
  52. return
  53. }
  54. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  55. //fmt.Println("send start")
  56. //utils.FileLog.Info("send start")
  57. ////if report.MsgIsSend == 0 {
  58. //sendMap := make(map[string]interface{})
  59. //sendData := make(map[string]interface{})
  60. redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId)
  61. keyword1 := "弘则日度点评"
  62. first := "Hi,最新一期日度点评已上线,欢迎查看"
  63. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  64. keyword1 = fmt.Sprintf("弘则%s", report.ClassifyNameSecond)
  65. first = fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  66. }
  67. keyword2 := report.Title
  68. //keyword3 := report.PublishTime.Format(utils.FormatDateTime)
  69. keyword3 := report.PublishTime
  70. keyword4 := report.Abstract
  71. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  72. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  73. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  74. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  75. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  76. //
  77. //sendMap["template_id"] = utils.TemplateIdByProduct
  78. //sendMap["url"] = redirectUrl
  79. //sendMap["data"] = sendData
  80. //sendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(reportId), utils.TEMPLATE_MSG_REPORT)
  81. //}
  82. sendInfo := new(SendWxTemplate)
  83. sendInfo.First = first
  84. sendInfo.Keyword1 = keyword1
  85. sendInfo.Keyword2 = keyword2
  86. sendInfo.Keyword3 = keyword3
  87. sendInfo.Keyword4 = keyword4
  88. sendInfo.TemplateId = utils.TemplateIdByProduct
  89. sendInfo.RedirectUrl = redirectUrl
  90. sendInfo.RedirectTarget = 1
  91. sendInfo.Resource = strconv.Itoa(reportId)
  92. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  93. sendInfo.OpenIdArr = openIdArr
  94. err = SendTemplateMsg(sendInfo)
  95. return
  96. }
  97. func sendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList, resource string, sendType int) (err error) {
  98. for _, v := range items {
  99. sendMap["touser"] = v.OpenId
  100. data, err := json.Marshal(sendMap)
  101. if err != nil {
  102. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  103. utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error()))
  104. return err
  105. }
  106. err = toSendTemplateMsg(sendUrl, data, resource, sendType, v)
  107. if err != nil {
  108. fmt.Println("send err:", err.Error())
  109. utils.FileLog.Info(fmt.Sprintf("ToSendTemplateMsg Err:%s", err.Error()))
  110. }
  111. }
  112. return
  113. }
  114. func toSendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) {
  115. var msg string
  116. utils.FileLog.Info("Send:" + string(data))
  117. client := http.Client{}
  118. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  119. if err != nil {
  120. return
  121. }
  122. defer resp.Body.Close()
  123. body, _ := ioutil.ReadAll(resp.Body)
  124. utils.FileLog.Info("SendResult:" + string(body))
  125. var templateResponse SendTemplateResponse
  126. err = json.Unmarshal(body, &templateResponse)
  127. if err != nil {
  128. utils.FileLog.Info(fmt.Sprintf("SendResult Unmarshal Err:%s", err.Error()))
  129. return err
  130. }
  131. //新增模板消息推送记录
  132. {
  133. tr := new(models.UserTemplateRecord)
  134. tr.UserId = dataItem.UserId
  135. tr.OpenId = dataItem.OpenId
  136. tr.Resource = resource
  137. tr.SendData = string(data)
  138. tr.Result = string(body)
  139. tr.CreateDate = time.Now().Format(utils.FormatDate)
  140. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  141. if templateResponse.Errcode == 0 {
  142. tr.SendStatus = 1
  143. } else {
  144. tr.SendStatus = 0
  145. }
  146. tr.SendType = sendType
  147. go func() {
  148. err = models.AddUserTemplateRecord(tr)
  149. if err != nil {
  150. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  151. }
  152. }()
  153. }
  154. accessToken, err := models.GetWxAccessToken()
  155. if err != nil {
  156. msg = "GetWxAccessToken Err:" + err.Error()
  157. utils.FileLog.Info("获取Token失败,msg:" + msg)
  158. return
  159. }
  160. if accessToken == "" {
  161. msg = "accessToken is empty"
  162. utils.FileLog.Info("accessToken为空,msg:" + msg)
  163. return
  164. }
  165. //模板消息发送超过当日10万次限制错误处理
  166. if templateResponse.Errcode == 45009 {
  167. key := "CACHE_SendTemplateMsg_ERR"
  168. isExist := utils.Rc.IsExist(key)
  169. if isExist == true {
  170. return
  171. } else {
  172. result, _ := json.Marshal(templateResponse)
  173. if err != nil {
  174. utils.FileLog.Info(fmt.Sprintf("templateResponse Marshal Err:%s", err.Error()))
  175. return err
  176. }
  177. //发送邮件提醒异常
  178. go alarm_msg.SendAlarmMsg("模板消息发送超过当日10万次限制,templateResponse = "+string(result), 3)
  179. //go utils.SendEmail("异常提醒:", "模板消息发送超过当日10万次限制,templateResponse = "+string(result), utils.EmailSendToUsers)
  180. //设置3分钟缓存,不允许重复添加
  181. utils.Rc.SetNX(key, 1, 6*time.Minute)
  182. }
  183. //清空发送次数
  184. sendUrl := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=%s", accessToken)
  185. clearData := make(map[string]interface{})
  186. clearData["appid"] = utils.WxAppId
  187. clearJson, _ := json.Marshal(clearData)
  188. utils.FileLog.Info("clear_quota data:" + string(clearJson))
  189. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(clearJson))
  190. if err != nil {
  191. return err
  192. }
  193. defer resp.Body.Close()
  194. clearBody, err := ioutil.ReadAll(resp.Body)
  195. utils.FileLog.Info("clear_quota result:" + string(clearBody))
  196. var clearQuotaResponse ClearQuotaResponse
  197. err = json.Unmarshal(clearBody, &clearQuotaResponse)
  198. if err != nil {
  199. utils.FileLog.Info(fmt.Sprintf("clearQuotaResponse Unmarshal Err:%s", err.Error()))
  200. return err
  201. }
  202. if clearQuotaResponse.Errcode == 0 {
  203. //发送邮件解决异常
  204. go alarm_msg.SendAlarmMsg("异常已解决,自动清理限制接口,调用成功", 3)
  205. //go utils.SendEmail("异常已解决:", "自动清理限制接口,调用成功", utils.EmailSendToUsers)
  206. //重新推送一次
  207. toSendTemplateMsg(sendUrl, data, resource, sendType, dataItem)
  208. }
  209. }
  210. return
  211. }
  212. type SendTemplateResponse struct {
  213. Errcode int `json:"errcode"`
  214. Errmsg string `json:"errmsg"`
  215. MsgID int `json:"msgid"`
  216. }
  217. type ClearQuotaResponse struct {
  218. Errcode int `json:"errcode"`
  219. Errmsg string `json:"errmsg"`
  220. }
  221. // SendCompanyApplyWxTemplateMsg 微信审批通知模板消息
  222. func SendCompanyApplyWxTemplateMsg(mobile, redirectUrl, wxAppPath string, wxMsgMap map[int]string) (err error) {
  223. var msg string
  224. defer func() {
  225. if err != nil {
  226. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  227. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  228. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  229. }
  230. if msg != "" {
  231. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  232. }
  233. }()
  234. utils.FileLog.Info("%s", "services SendMsg")
  235. //accessToken, err := models.GetWxAccessToken()
  236. //if err != nil {
  237. // msg = "GetWxAccessToken Err:" + err.Error()
  238. // return
  239. //}
  240. //if accessToken == "" {
  241. // msg = "accessToken is empty"
  242. // return
  243. //}
  244. utils.FileLog.Info("mobile:%s", mobile)
  245. openIdList, err := system.GetAdminOpenIdListByMobile(mobile)
  246. if err != nil {
  247. msg = "get openIdList err:" + err.Error()
  248. return
  249. }
  250. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  251. //fmt.Println("openIdListCount:", len(openIdList))
  252. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  253. utils.FileLog.Info("start send")
  254. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  255. fmt.Println("send start")
  256. utils.FileLog.Info("send start")
  257. //sendMap := make(map[string]interface{})
  258. //sendData := make(map[string]interface{})
  259. var first, keyword1, keyword2, keyword3, remark string
  260. if tmpStr, ok := wxMsgMap[0]; ok {
  261. first = tmpStr
  262. } else {
  263. first = "Hi,有新的订单需要审批"
  264. }
  265. if tmpStr, ok := wxMsgMap[1]; ok {
  266. keyword1 = tmpStr
  267. }
  268. if tmpStr, ok := wxMsgMap[2]; ok {
  269. keyword2 = tmpStr
  270. }
  271. if tmpStr, ok := wxMsgMap[3]; ok {
  272. keyword3 = tmpStr
  273. }
  274. if tmpStr, ok := wxMsgMap[4]; ok {
  275. remark = tmpStr
  276. }
  277. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  278. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  279. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  280. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  281. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  282. //
  283. //sendMap["template_id"] = utils.TemplateIdByCompanyApply
  284. //sendMap["url"] = redirectUrl //跳转地址
  285. //sendMap["data"] = sendData
  286. //if wxAppPath != "" {
  287. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCrmAppId, "pagepath": wxAppPath}
  288. //}
  289. //sendTemplateMsg(sendUrl, sendMap, openIdList, mobile, utils.TEMPLATE_MSG_APPLY)
  290. openIdArr := make([]string, len(openIdList))
  291. for i, v := range openIdList {
  292. openIdArr[i] = v.OpenId
  293. }
  294. sendInfo := new(SendWxTemplate)
  295. sendInfo.WxAppId = utils.AdminWxAppId
  296. sendInfo.First = first
  297. sendInfo.Keyword1 = keyword1
  298. sendInfo.Keyword2 = keyword2
  299. sendInfo.Keyword3 = keyword3
  300. sendInfo.Remark = remark
  301. sendInfo.TemplateId = utils.TemplateIdByCompanyApply
  302. sendInfo.RedirectUrl = redirectUrl
  303. sendInfo.RedirectTarget = 2
  304. sendInfo.Resource = mobile
  305. sendInfo.SendType = utils.TEMPLATE_MSG_APPLY
  306. sendInfo.OpenIdArr = openIdArr
  307. if wxAppPath != "" {
  308. sendInfo.RedirectUrl = wxAppPath
  309. }
  310. err = SendTemplateMsg(sendInfo)
  311. }
  312. utils.FileLog.Info("send end")
  313. return
  314. }
  315. // 销售跨部门领取客户通知
  316. func SendCompanyReceiveWxTemplateMsg(companyName, productName, sellerName, otherSellerMobile string) (err error) {
  317. var msg string
  318. defer func() {
  319. if err != nil {
  320. fmt.Println("err:", err)
  321. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  322. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  323. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  324. }
  325. if msg != "" {
  326. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  327. }
  328. }()
  329. utils.FileLog.Info("%s", "services SendMsg")
  330. //TemplateIdByCompanyReceive
  331. //accessToken, err := models.GetWxAccessToken()
  332. //
  333. //if err != nil {
  334. // msg = "GetWxAccessToken Err:" + err.Error()
  335. // return
  336. //}
  337. //if accessToken == "" {
  338. // msg = "accessToken is empty"
  339. // return
  340. //}
  341. utils.FileLog.Info("mobile:%s", otherSellerMobile)
  342. openIdList, err := system.GetAdminOpenIdListByMobile(otherSellerMobile)
  343. if err != nil {
  344. msg = "get openIdList err:" + err.Error()
  345. return
  346. }
  347. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  348. redirectUrl := ""
  349. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  350. utils.FileLog.Info("start send")
  351. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  352. fmt.Println("send start")
  353. utils.FileLog.Info("send start")
  354. //sendMap := make(map[string]interface{})
  355. //sendData := make(map[string]interface{})
  356. first := fmt.Sprint("您有客户被", productName, "销售-", sellerName, "领取,请留意。")
  357. keyword1 := companyName
  358. //keyword2 := "客户跨部门领取"
  359. keyword2 := fmt.Sprintf("%s销售-%s跨部门领取", productName, sellerName)
  360. keyword3 := ""
  361. remark := ""
  362. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  363. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  364. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  365. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  366. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  367. //
  368. //sendMap["template_id"] = utils.TemplateIdByCompanyReceive
  369. //sendMap["url"] = redirectUrl //跳转地址
  370. //sendMap["data"] = sendData
  371. //sendTemplateMsg(sendUrl, sendMap, openIdList, otherSellerMobile, utils.TEMPLATE_MSG_RECEIVE)
  372. openIdArr := make([]string, len(openIdList))
  373. for i, v := range openIdList {
  374. openIdArr[i] = v.OpenId
  375. }
  376. sendInfo := new(SendWxTemplate)
  377. sendInfo.WxAppId = utils.AdminWxAppId
  378. sendInfo.First = first
  379. sendInfo.Keyword1 = keyword1
  380. sendInfo.Keyword2 = keyword2
  381. sendInfo.Keyword3 = keyword3
  382. sendInfo.Remark = remark
  383. sendInfo.TemplateId = utils.TemplateIdByCompanyReceive
  384. sendInfo.RedirectUrl = redirectUrl
  385. sendInfo.RedirectTarget = 2
  386. sendInfo.Resource = otherSellerMobile
  387. sendInfo.SendType = utils.TEMPLATE_MSG_RECEIVE
  388. sendInfo.OpenIdArr = openIdArr
  389. err = SendTemplateMsg(sendInfo)
  390. }
  391. utils.FileLog.Info("send end")
  392. return
  393. }
  394. func SendArticleWxTemplateMsg(nickName, aticleTiele, publishDate, abstract, industryName, msgType string, articleId, departmentId, industrialManagementId int, isResearch bool) (err error) {
  395. var msg string
  396. defer func() {
  397. if err != nil {
  398. fmt.Println("err:", err)
  399. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  400. //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  401. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  402. }
  403. if msg != "" {
  404. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  405. }
  406. }()
  407. var openIdList []*cygx.OpenIdList
  408. var resource string
  409. if msgType == "Department" {
  410. openIdList, err = cygx.GetFollowDepartmentOpenIdList(departmentId)
  411. resource = strconv.Itoa(departmentId)
  412. } else {
  413. openIdList, err = cygx.GetFollowindustrialOpenIdList(industrialManagementId)
  414. resource = strconv.Itoa(industrialManagementId)
  415. }
  416. if err != nil {
  417. msg = "get openIdList err:" + err.Error()
  418. return
  419. }
  420. if len(openIdList) == 0 {
  421. return err
  422. }
  423. if len(openIdList) > 0 && utils.TemplateIdByProduct != "" {
  424. var first string
  425. var keyword1 string
  426. var keyword2 string
  427. if msgType == "Department" {
  428. first = fmt.Sprint("您关注的作者发布了新的报告")
  429. keyword1 = "作者昵称--" + nickName
  430. } else {
  431. first = fmt.Sprint("您关注的产业有更新报告")
  432. keyword1 = industryName
  433. }
  434. if isResearch {
  435. first = "您关注的主题有更新报告"
  436. abstract = "--"
  437. }
  438. keyword2 = aticleTiele
  439. keyword3 := publishDate
  440. keyword4 := abstract
  441. remark := ""
  442. openIdArr := make([]string, len(openIdList))
  443. for i, v := range openIdList {
  444. openIdArr[i] = v.OpenId
  445. }
  446. sendInfo := new(SendWxTemplate)
  447. sendInfo.First = first
  448. sendInfo.Keyword1 = keyword1
  449. sendInfo.Keyword2 = keyword2
  450. sendInfo.Keyword3 = keyword3
  451. sendInfo.Keyword4 = keyword4
  452. sendInfo.Remark = remark
  453. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  454. sendInfo.RedirectUrl = "pageMy/reportDetail/reportDetail?id=" + strconv.Itoa(articleId)
  455. sendInfo.RedirectTarget = 3
  456. sendInfo.Resource = resource
  457. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE
  458. sendInfo.OpenIdArr = openIdArr
  459. err = SendTemplateMsg(sendInfo)
  460. }
  461. return
  462. }
  463. // SendDayReportWxTemplateMsg 发送日报(周报、双周报、月报)模板消息
  464. //func SendDayReportWxTemplateMsg(researchReport *models.ResearchReport) (err error) {
  465. // var msg string
  466. // defer func() {
  467. // if err != nil {
  468. // go alarm_msg.SendAlarmMsg("发送日报(周报)模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  469. // //go utils.SendEmail("发送日报(周报)模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  470. // utils.FileLog.Info(fmt.Sprintf("发送日报(周报)模版消息失败,Err:%s,%s", err.Error(), msg))
  471. // }
  472. // if msg != "" {
  473. // utils.FileLog.Info("发送日报(周报)模版消息失败,msg:%s", msg)
  474. // }
  475. // }()
  476. //
  477. // utils.FileLog.Info("%s", "services SendMsg")
  478. // resource := strconv.Itoa(researchReport.ResearchReportId)
  479. // //跳转地址
  480. // redirectUrl := fmt.Sprint(utils.DayReportTemplateRedirectUrl, researchReport.ResearchReportId, "/", researchReport.Type)
  481. // first := "Hi,最新一期晨报已上线,欢迎查看"
  482. // keyword1 := "弘则晨报"
  483. // keyword2 := researchReport.ResearchReportName
  484. // keyword3 := researchReport.ResearchReportDate.Format(utils.FormatDate)
  485. // keyword4 := researchReport.ResearchReportName
  486. // switch researchReport.Type {
  487. // case "day":
  488. // first = "Hi,最新一期晨报已上线,欢迎查看"
  489. // keyword1 = "弘则晨报"
  490. // case "week":
  491. // first = "Hi,最新一期周报已上线,欢迎查看"
  492. // keyword1 = "弘则周报"
  493. // case "two_week":
  494. // first = "Hi,最新一期双周报已上线,欢迎查看"
  495. // keyword1 = "弘则双周报"
  496. // case "month":
  497. // first = "Hi,最新一期月报已上线,欢迎查看"
  498. // keyword1 = "弘则月报"
  499. //
  500. // // 模板消息的名称替换
  501. // tmpReport, _ := models.GetReportByOldReportId(researchReport.ResearchReportId)
  502. // if tmpReport != nil {
  503. // keyword1 = fmt.Sprintf("弘则%s", tmpReport.ClassifyNameSecond)
  504. // }
  505. // case "other":
  506. // first = "Hi,最新一期点评已上线,欢迎查看"
  507. // keyword1 = "数据点评"
  508. // }
  509. //
  510. // //accessToken, err := models.GetWxAccessToken()
  511. // //if err != nil {
  512. // // msg = "GetWxAccessToken Err:" + err.Error()
  513. // // return
  514. // //}
  515. // //fmt.Println(accessToken)
  516. // //if accessToken == "" {
  517. // // msg = "accessToken is empty"
  518. // // return
  519. // //}
  520. // openIdList, err := models.GetOpenIdList()
  521. // if err != nil {
  522. // msg = "get openIdList err:" + err.Error()
  523. // return
  524. // }
  525. // fmt.Println("openIdListCount:", len(openIdList))
  526. // //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  527. // fmt.Println("send start")
  528. // utils.FileLog.Info("send start")
  529. // //if report.MsgIsSend == 0 {
  530. // //sendMap := make(map[string]interface{})
  531. // //sendData := make(map[string]interface{})
  532. //
  533. // //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  534. // //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  535. // //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  536. // //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  537. // //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  538. //
  539. // //sendMap["template_id"] = utils.TemplateIdByProduct
  540. // //sendMap["url"] = redirectUrl
  541. // //sendMap["data"] = sendData
  542. // //sendTemplateMsg(sendUrl, sendMap, openIdList, resource, utils.TEMPLATE_MSG_CYGX_DAY_REPORT)
  543. // //}
  544. //
  545. // openIdArr := make([]string, len(openIdList))
  546. // for i, v := range openIdList {
  547. // openIdArr[i] = v.OpenId
  548. // }
  549. //
  550. // sendInfo := new(SendWxTemplate)
  551. // sendInfo.First = first
  552. // sendInfo.Keyword1 = keyword1
  553. // sendInfo.Keyword2 = keyword2
  554. // sendInfo.Keyword3 = keyword3
  555. // sendInfo.Keyword4 = keyword4
  556. // sendInfo.TemplateId = utils.TemplateIdByProduct
  557. // sendInfo.RedirectUrl = redirectUrl
  558. // sendInfo.RedirectTarget = 1
  559. // sendInfo.Resource = resource
  560. // sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_DAY_REPORT
  561. // sendInfo.OpenIdArr = openIdArr
  562. //
  563. // err = SendTemplateMsg(sendInfo)
  564. //
  565. // fmt.Println("send end")
  566. // utils.FileLog.Info("send end")
  567. // return
  568. //}
  569. // SendWxMsgWithActivityAppointmentRemind 活动预约/报名时间通知
  570. func SendWxMsgWithActivityAppointmentRemind(first, keyword1, keyword2, remark, wxAppPath string, openIdList []*models.OpenIdList) (err error) {
  571. var msg string
  572. defer func() {
  573. if err != nil {
  574. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  575. //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  576. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s", err.Error()))
  577. }
  578. }()
  579. //utils.FileLog.Info("%s", "services SendMsg")
  580. //accessToken, err := models.GetWxAccessToken()
  581. //if err != nil {
  582. // msg = "GetWxAccessToken Err:" + err.Error()
  583. // return
  584. //}
  585. //if accessToken == "" {
  586. // msg = "accessToken is empty"
  587. // return
  588. //}
  589. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  590. //sendMap := make(map[string]interface{})
  591. //sendData := make(map[string]interface{})
  592. //sendMap["template_id"] = utils.WxMsgTemplateIdActivityChangeApply
  593. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  594. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  595. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  596. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  597. //if wxAppPath != "" {
  598. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  599. //}
  600. //sendMap["data"] = sendData
  601. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  602. openIdArr := make([]string, len(openIdList))
  603. for i, v := range openIdList {
  604. openIdArr[i] = v.OpenId
  605. }
  606. sendInfo := new(SendWxTemplate)
  607. sendInfo.First = first
  608. sendInfo.Keyword1 = keyword1
  609. sendInfo.Keyword2 = keyword2
  610. sendInfo.Remark = remark
  611. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApply
  612. sendInfo.RedirectUrl = wxAppPath
  613. sendInfo.RedirectTarget = 1
  614. sendInfo.Resource = wxAppPath
  615. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  616. sendInfo.OpenIdArr = openIdArr
  617. err = SendTemplateMsg(sendInfo)
  618. return
  619. }
  620. // 路演->研究员收到待处理的申请
  621. func SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath string, openIdList []*system.OpenIdList) (err error) {
  622. var msg string
  623. defer func() {
  624. if err != nil {
  625. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  626. //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  627. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s", err.Error()))
  628. }
  629. }()
  630. utils.FileLog.Info("%s", "services SendMsg")
  631. //accessToken, err := models.GetWxAccessToken()
  632. //if err != nil {
  633. // msg = "GetWxAccessToken Err:" + err.Error()
  634. // return
  635. //}
  636. //if accessToken == "" {
  637. // msg = "accessToken is empty"
  638. // return
  639. //}
  640. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  641. //sendMap := make(map[string]interface{})
  642. //sendData := make(map[string]interface{})
  643. //sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowPending
  644. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  645. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  646. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  647. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  648. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  649. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  650. //if wxAppPath != "" && utils.RunMode == "release" {
  651. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCrmAppId, "pagepath": wxAppPath}
  652. //}
  653. //sendMap["data"] = sendData
  654. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  655. openIdArr := make([]string, len(openIdList))
  656. for i, v := range openIdList {
  657. openIdArr[i] = v.OpenId
  658. }
  659. sendInfo := new(SendWxTemplate)
  660. sendInfo.WxAppId = utils.AdminWxAppId
  661. sendInfo.First = first
  662. sendInfo.Keyword1 = keyword1
  663. sendInfo.Keyword2 = keyword2
  664. sendInfo.Keyword3 = keyword3
  665. sendInfo.Keyword4 = keyword4
  666. sendInfo.Remark = remark
  667. sendInfo.TemplateId = utils.WxMsgTemplateIdWithRoadshowPending
  668. if wxAppPath != "" && utils.RunMode == "release" {
  669. sendInfo.RedirectUrl = wxAppPath
  670. }
  671. sendInfo.RedirectTarget = 2
  672. sendInfo.Resource = wxAppPath
  673. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  674. sendInfo.OpenIdArr = openIdArr
  675. err = SendTemplateMsg(sendInfo)
  676. return
  677. }
  678. // 路演->销售收到处理结果
  679. func SendWxMsgWithRoadshowDetailResult(first, keyword1, keyword2, keyword3, keyword4, keyword5, remark, wxAppPath string, openIdList []*system.OpenIdList) (err error) {
  680. var msg string
  681. defer func() {
  682. if err != nil {
  683. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  684. //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  685. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s", err.Error()))
  686. }
  687. }()
  688. utils.FileLog.Info("%s", "services SendMsg")
  689. //accessToken, err := models.GetWxAccessToken()
  690. //if err != nil {
  691. // msg = "GetWxAccessToken Err:" + err.Error()
  692. // return
  693. //}
  694. //if accessToken == "" {
  695. // msg = "accessToken is empty"
  696. // return
  697. //}
  698. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  699. //sendMap := make(map[string]interface{})
  700. //sendData := make(map[string]interface{})
  701. //sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowDetailResult
  702. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  703. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  704. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  705. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  706. //
  707. //if wxAppPath != "" && utils.RunMode == "release" {
  708. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCrmAppId, "pagepath": wxAppPath}
  709. //}
  710. //sendMap["data"] = sendData
  711. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  712. openIdArr := make([]string, len(openIdList))
  713. for i, v := range openIdList {
  714. openIdArr[i] = v.OpenId
  715. }
  716. sendInfo := new(SendWxTemplate)
  717. sendInfo.WxAppId = utils.AdminWxAppId
  718. sendInfo.First = first
  719. sendInfo.Keyword1 = keyword1
  720. sendInfo.Keyword2 = keyword2
  721. sendInfo.Keyword3 = keyword3
  722. sendInfo.Keyword4 = keyword4
  723. sendInfo.Keyword5 = keyword5
  724. sendInfo.Remark = remark
  725. sendInfo.TemplateId = utils.WxMsgTemplateIdWithRoadshowDetailResult
  726. if wxAppPath != "" && utils.RunMode == "release" {
  727. sendInfo.RedirectUrl = wxAppPath
  728. }
  729. sendInfo.RedirectTarget = 2
  730. sendInfo.Resource = wxAppPath
  731. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  732. sendInfo.OpenIdArr = openIdArr
  733. err = SendTemplateMsg(sendInfo)
  734. return
  735. }
  736. // 路演->研究员收到活动删除通知
  737. func SendWxMsgWithRoadshowDeleteNotice(first, keyword1, keyword2, remark, wxAppPath string, openIdList []*system.OpenIdList) (err error) {
  738. var msg string
  739. defer func() {
  740. if err != nil {
  741. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  742. //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  743. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s", err.Error()))
  744. }
  745. }()
  746. utils.FileLog.Info("%s", "services SendMsg")
  747. //accessToken, err := models.GetWxAccessToken()
  748. //if err != nil {
  749. // msg = "GetWxAccessToken Err:" + err.Error()
  750. // return
  751. //}
  752. //if accessToken == "" {
  753. // msg = "accessToken is empty"
  754. // return
  755. //}
  756. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  757. //sendMap := make(map[string]interface{})
  758. //sendData := make(map[string]interface{})
  759. //sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowDeleteNotice
  760. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  761. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  762. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  763. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  764. //if wxAppPath != "" && utils.RunMode == "release" {
  765. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCrmAppId, "pagepath": wxAppPath}
  766. //}
  767. //sendMap["data"] = sendData
  768. //sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT)
  769. openIdArr := make([]string, len(openIdList))
  770. for i, v := range openIdList {
  771. openIdArr[i] = v.OpenId
  772. }
  773. sendInfo := new(SendWxTemplate)
  774. sendInfo.WxAppId = utils.AdminWxAppId
  775. sendInfo.First = first
  776. sendInfo.Keyword1 = keyword1
  777. sendInfo.Keyword2 = keyword2
  778. sendInfo.Remark = remark
  779. sendInfo.TemplateId = utils.WxMsgTemplateIdWithRoadshowDeleteNotice
  780. if wxAppPath != "" && utils.RunMode == "release" {
  781. sendInfo.RedirectUrl = wxAppPath
  782. }
  783. sendInfo.RedirectTarget = 2
  784. sendInfo.Resource = wxAppPath
  785. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  786. sendInfo.OpenIdArr = openIdArr
  787. err = SendTemplateMsg(sendInfo)
  788. return
  789. }
  790. // SendMiniProgramReportWxMsg 推送报告微信模板消息-小程序链接
  791. func SendMiniProgramReportWxMsg(reportId int) (err error) {
  792. var msg string
  793. reportIdStr := strconv.Itoa(reportId)
  794. defer func() {
  795. if err != nil {
  796. fmt.Println("msg:", msg)
  797. utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error()))
  798. go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3)
  799. //go utils.SendEmail("SendMiniProgramReportWxMsg发送报告模版消息失败"+"【"+utils.APPNAME+"】"+"【"+utils.RunMode+"】"+time.Now().Format("2006-01-02 15:04:05"), "ReportId:"+reportIdStr+";"+msg+";Err:"+err.Error(), toUser)
  800. }
  801. }()
  802. utils.FileLog.Info("%s", "services SendMsg")
  803. report, err := models.GetReportById(reportId)
  804. if err != nil {
  805. msg = "GetReportInfo Err:" + err.Error()
  806. return
  807. }
  808. if report == nil {
  809. utils.FileLog.Info("报告信息不存在")
  810. return
  811. }
  812. //if report.MsgIsSend == 1 {
  813. // return
  814. //}
  815. //accessToken, err := models.GetWxAccessToken()
  816. //if err != nil {
  817. // msg = "GetWxAccessToken Err:" + err.Error()
  818. // return
  819. //}
  820. //if accessToken == "" {
  821. // msg = "accessToken is empty"
  822. // return
  823. //}
  824. var openIdArr []string
  825. if report.ClassifyIdSecond <= 0 {
  826. openIdArr, err = models.GetOpenIdArr()
  827. if err != nil {
  828. msg = "get GetOpenIdArr err:" + err.Error()
  829. return
  830. }
  831. } else {
  832. classify, err := models.GetClassifyById(report.ClassifyIdSecond)
  833. if err != nil {
  834. msg = "获取报告分类失败 err:" + err.Error()
  835. return err
  836. }
  837. if classify.IsMassSend == 1 {
  838. openIdArr, err = models.GetOpenIdArr()
  839. if err != nil {
  840. msg = "get GetOpenIdArr err:" + err.Error()
  841. return err
  842. }
  843. } else {
  844. openIdArr, err = models.GetOpenIdArrByClassifyNameSecond(report.ClassifyNameSecond)
  845. if err != nil {
  846. msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error()
  847. return err
  848. }
  849. }
  850. }
  851. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  852. //fmt.Println("send start")
  853. //utils.FileLog.Info("send start")
  854. //sendMap := make(map[string]interface{})
  855. //sendData := make(map[string]interface{})
  856. title := fmt.Sprintf("弘则%s", report.ClassifyNameFirst)
  857. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  858. title = fmt.Sprintf("弘则%s", report.ClassifyNameSecond)
  859. }
  860. //redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId)
  861. first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  862. keyword1 := title
  863. keyword2 := report.Title
  864. keyword3 := report.PublishTime
  865. keyword4 := report.Abstract
  866. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  867. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  868. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  869. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  870. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  871. //
  872. //sendMap["template_id"] = utils.TemplateIdByProduct
  873. ////sendMap["url"] = redirectUrl
  874. //sendMap["data"] = sendData
  875. var wxAppPath string
  876. if report.ChapterType == utils.REPORT_TYPE_WEEK {
  877. wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr)
  878. } else {
  879. wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr)
  880. }
  881. //if wxAppPath != "" {
  882. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  883. //}
  884. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_REPORT)
  885. sendInfo := new(SendWxTemplate)
  886. sendInfo.First = first
  887. sendInfo.Keyword1 = keyword1
  888. sendInfo.Keyword2 = keyword2
  889. sendInfo.Keyword3 = keyword3
  890. sendInfo.Keyword4 = keyword4
  891. sendInfo.TemplateId = utils.TemplateIdByProduct
  892. sendInfo.RedirectUrl = wxAppPath
  893. sendInfo.Resource = wxAppPath
  894. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  895. sendInfo.OpenIdArr = openIdArr
  896. sendInfo.RedirectTarget = 1
  897. err = SendTemplateMsg(sendInfo)
  898. return
  899. }
  900. // CheckTwoWeekOrMonthReport 校验推送报告是否为双周报或者月报
  901. func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) {
  902. if utils.RunMode == "debug" {
  903. miniStrArr := []string{
  904. "双周报", "月报",
  905. }
  906. if utils.InArrayByStr(miniStrArr, classifyName) {
  907. ok = true
  908. }
  909. } else {
  910. // 此处生产环境用ID主要是担心分类改了名字...
  911. IdArr := []int{
  912. 96, 112,
  913. }
  914. if utils.InArrayByInt(IdArr, classifyId) {
  915. ok = true
  916. }
  917. }
  918. return
  919. }
  920. // SendYbQuestionDistributeWxMsg 推送研报小程序模板消息-问答社区分配
  921. func SendYbQuestionDistributeWxMsg(questionId, adminId int, openid, questionTitle, remark string) (err error) {
  922. var errMsg string
  923. defer func() {
  924. if err != nil {
  925. alarmMsg := fmt.Sprintf("SendMiniProgramReportWxMsg-推送问答社区分配模版消息失败; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  926. utils.FileLog.Error(alarmMsg)
  927. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  928. }
  929. }()
  930. //accessToken, err := models.GetWxAccessToken()
  931. //if err != nil {
  932. // errMsg = "GetWxAccessToken Err:" + err.Error()
  933. // return
  934. //}
  935. //if accessToken == "" {
  936. // errMsg = "accessToken is empty"
  937. // return
  938. //}
  939. openIdList := make([]*system.OpenIdList, 0)
  940. openIdList = append(openIdList, &system.OpenIdList{
  941. OpenId: openid,
  942. AdminId: adminId,
  943. })
  944. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  945. //sendMap := make(map[string]interface{})
  946. //sendData := make(map[string]interface{})
  947. first := "您好,有新的提问待回复"
  948. keyword1 := questionTitle
  949. keyword2 := "待回复"
  950. if remark == "" {
  951. //remark = "请点击详情尽快处理"
  952. keyword2 = remark
  953. }
  954. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  955. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  956. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  957. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  958. //
  959. //sendMap["template_id"] = utils.WxMsgTemplateIdWithYbCommunityQuestion
  960. //sendMap["data"] = sendData
  961. wxAppPath := fmt.Sprintf("pages-question/answerDetail?id=%d", questionId)
  962. if utils.RunMode == "debug" {
  963. // 仅测试环境测试用
  964. wxAppPath = "pages-report/chapterList?reportId=3800"
  965. }
  966. //if wxAppPath != "" {
  967. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  968. //}
  969. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  970. openIdArr := make([]string, len(openIdList))
  971. for i, v := range openIdList {
  972. openIdArr[i] = v.OpenId
  973. }
  974. sendInfo := new(SendWxTemplate)
  975. sendInfo.WxAppId = utils.AdminWxAppId
  976. sendInfo.First = first
  977. sendInfo.Keyword1 = keyword1
  978. sendInfo.Keyword2 = keyword2
  979. sendInfo.Remark = remark
  980. sendInfo.RedirectUrl = wxAppPath
  981. sendInfo.TemplateId = utils.WxMsgTemplateIdWithYbCommunityQuestion
  982. sendInfo.RedirectTarget = 1
  983. sendInfo.Resource = wxAppPath
  984. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  985. sendInfo.OpenIdArr = openIdArr
  986. err = SendTemplateMsg(sendInfo)
  987. return
  988. }
  989. // 给销售发送 开启或禁用"添加客户/领取客户"权限的通知
  990. func SendCompanyReceivePermissionWxTemplateMsg(sellerMobile string, enabled int) (err error) {
  991. var msg string
  992. defer func() {
  993. if err != nil {
  994. fmt.Println("err:", err)
  995. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  996. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  997. }
  998. if msg != "" {
  999. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1000. }
  1001. }()
  1002. utils.FileLog.Info("%s", "services SendMsg")
  1003. //accessToken, err := models.GetWxAccessToken()
  1004. //if err != nil {
  1005. // msg = "GetWxAccessToken Err:" + err.Error()
  1006. // return
  1007. //}
  1008. //if accessToken == "" {
  1009. // msg = "accessToken is empty"
  1010. // return
  1011. //}
  1012. utils.FileLog.Info("mobile:%s", sellerMobile)
  1013. openIdList, err := system.GetAdminOpenIdListByMobile(sellerMobile)
  1014. if err != nil {
  1015. msg = "get openIdList err:" + err.Error()
  1016. return
  1017. }
  1018. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  1019. redirectUrl := ""
  1020. if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" {
  1021. utils.FileLog.Info("start send")
  1022. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  1023. fmt.Println("send start")
  1024. utils.FileLog.Info("send start")
  1025. //sendMap := make(map[string]interface{})
  1026. //sendData := make(map[string]interface{})
  1027. enabledStr := ""
  1028. if enabled >= 1 {
  1029. enabledStr = "已开启"
  1030. } else {
  1031. enabledStr = "已关闭"
  1032. }
  1033. first := "权限变更通知"
  1034. keyword1 := fmt.Sprint("添加/领取客户权限", enabledStr)
  1035. keyword2 := enabledStr + `(有疑问请联系管理员)`
  1036. remark := "如有疑问请联系ficc/权益管理员"
  1037. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  1038. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  1039. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  1040. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  1041. //sendMap["template_id"] = utils.TemplateIdByCompanyReceivePermission
  1042. //sendMap["url"] = redirectUrl //跳转地址
  1043. //sendMap["data"] = sendData
  1044. //sendTemplateMsg(sendUrl, sendMap, openIdList, sellerMobile, utils.TEMPLATE_MSG_RECEIVE_PERMISSION)
  1045. openIdArr := make([]string, len(openIdList))
  1046. for i, v := range openIdList {
  1047. openIdArr[i] = v.OpenId
  1048. }
  1049. sendInfo := new(SendWxTemplate)
  1050. sendInfo.WxAppId = utils.AdminWxAppId
  1051. sendInfo.First = first
  1052. sendInfo.Keyword1 = keyword1
  1053. sendInfo.Keyword2 = keyword2
  1054. sendInfo.Remark = remark
  1055. sendInfo.RedirectUrl = redirectUrl
  1056. sendInfo.TemplateId = utils.TemplateIdByCompanyReceivePermission
  1057. sendInfo.RedirectTarget = 0
  1058. sendInfo.Resource = sellerMobile
  1059. sendInfo.SendType = utils.TEMPLATE_MSG_RECEIVE_PERMISSION
  1060. sendInfo.OpenIdArr = openIdArr
  1061. err = SendTemplateMsg(sendInfo)
  1062. }
  1063. utils.FileLog.Info("send end")
  1064. return
  1065. }
  1066. // SendYbPriceDrivenWxMsg 推送研报小程序价格驱动模板消息
  1067. func SendYbPriceDrivenWxMsg(varietyTagId int, title, publishTime string) (err error) {
  1068. var msg string
  1069. defer func() {
  1070. if err != nil {
  1071. errMsg := "SendYbPriceDrivenWxMsg 发送报告模版消息失败; Err: " + err.Error() + "; Msg: " + msg
  1072. utils.FileLog.Info("%s", errMsg)
  1073. go alarm_msg.SendAlarmMsg(errMsg, 3)
  1074. }
  1075. }()
  1076. // 标签
  1077. if varietyTagId <= 0 {
  1078. return
  1079. }
  1080. varietyTag, err := models.GetVarietyTagById(varietyTagId)
  1081. if err != nil {
  1082. msg = "GetVarietyTagById Err:" + err.Error()
  1083. return
  1084. }
  1085. //accessToken, err := models.GetWxAccessToken()
  1086. //if err != nil {
  1087. // msg = "GetWxAccessToken Err:" + err.Error()
  1088. // return
  1089. //}
  1090. //if accessToken == "" {
  1091. // msg = "accessToken is empty"
  1092. // return
  1093. //}
  1094. openIdList, err := models.GetOpenIdList()
  1095. if err != nil {
  1096. msg = "get openIdList err:" + err.Error()
  1097. return
  1098. }
  1099. utils.FileLog.Info("send start")
  1100. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  1101. //sendMap := make(map[string]interface{})
  1102. //sendData := make(map[string]interface{})
  1103. first := "Hi,最新一期价格驱动已上线,欢迎查看"
  1104. keyword1 := fmt.Sprintf("%s价格驱动", varietyTag.ChartPermissionName)
  1105. keyword2 := title
  1106. keyword3 := publishTime
  1107. keyword4 := ""
  1108. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  1109. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  1110. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  1111. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  1112. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  1113. //sendMap["template_id"] = utils.TemplateIdByProduct
  1114. //sendMap["data"] = sendData
  1115. wxAppPath := fmt.Sprintf("pages/pricedriven/pricedriven?default_classify_first=%d&default_classify_sub=%d", varietyTag.VarietyClassifyId, varietyTag.VarietyTagId)
  1116. if utils.RunMode == "debug" {
  1117. wxAppPath = "pages-report/chapterList?reportId=3800"
  1118. }
  1119. //if wxAppPath != "" {
  1120. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  1121. //}
  1122. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_PRICE_DRIVEN)
  1123. openIdArr := make([]string, len(openIdList))
  1124. for i, v := range openIdList {
  1125. openIdArr[i] = v.OpenId
  1126. }
  1127. sendInfo := new(SendWxTemplate)
  1128. sendInfo.First = first
  1129. sendInfo.Keyword1 = keyword1
  1130. sendInfo.Keyword2 = keyword2
  1131. sendInfo.Keyword3 = keyword3
  1132. sendInfo.Keyword4 = keyword4
  1133. sendInfo.RedirectUrl = wxAppPath
  1134. sendInfo.TemplateId = utils.TemplateIdByProduct
  1135. sendInfo.RedirectTarget = 1
  1136. sendInfo.Resource = wxAppPath
  1137. sendInfo.SendType = utils.TEMPLATE_MSG_YB_PRICE_DRIVEN
  1138. sendInfo.OpenIdArr = openIdArr
  1139. err = SendTemplateMsg(sendInfo)
  1140. return
  1141. }
  1142. // 给销售发送 用印申请已签回的模版消息
  1143. func SendSealFinishedWxTemplateMsg(sellerMobile string, companyName string, sealId int) (err error) {
  1144. var msg string
  1145. defer func() {
  1146. if err != nil {
  1147. fmt.Println("err:", err)
  1148. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  1149. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  1150. }
  1151. if msg != "" {
  1152. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1153. }
  1154. }()
  1155. utils.FileLog.Info("%s", "services SendMsg")
  1156. //accessToken, err := models.GetWxAccessToken()
  1157. //if err != nil {
  1158. // msg = "GetWxAccessToken Err:" + err.Error()
  1159. // return
  1160. //}
  1161. //if accessToken == "" {
  1162. // msg = "accessToken is empty"
  1163. // return
  1164. //}
  1165. utils.FileLog.Info("mobile:%s", sellerMobile)
  1166. openIdList, err := system.GetAdminOpenIdListByMobile(sellerMobile)
  1167. if err != nil {
  1168. msg = "get openIdList err:" + err.Error()
  1169. return
  1170. }
  1171. utils.FileLog.Info("openIdListCount:%s", len(openIdList))
  1172. if len(openIdList) > 0 && utils.WxMsgTemplateIdWithSealApplyFinished != "" {
  1173. utils.FileLog.Info("start send")
  1174. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  1175. fmt.Println("send start")
  1176. utils.FileLog.Info("send start")
  1177. //sendMap := make(map[string]interface{})
  1178. //sendData := make(map[string]interface{})
  1179. first := "您的用印申请已签回,可前往提交转正/续约申请"
  1180. keyword1 := companyName
  1181. //keyword2 := "已签回"
  1182. keyword2 := "用印申请已签回,可前往提交转正/续约申请"
  1183. remark := "点击查看用印详情"
  1184. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  1185. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  1186. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  1187. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  1188. //sendMap["template_id"] = utils.WxMsgTemplateIdWithSealApplyFinished
  1189. //sendMap["url"] = "" //跳转地址
  1190. //sendMap["data"] = sendData
  1191. // 随手办公小程序的跳转地址
  1192. wxAppPath := fmt.Sprintf("pages-approve/seal/detail?SealId=%d", sealId)
  1193. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCrmAppId, "pagepath": wxAppPath}
  1194. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_SEAL_FINISHED)
  1195. openIdArr := make([]string, len(openIdList))
  1196. for i, v := range openIdList {
  1197. openIdArr[i] = v.OpenId
  1198. }
  1199. sendInfo := new(SendWxTemplate)
  1200. sendInfo.WxAppId = utils.AdminWxAppId
  1201. sendInfo.First = first
  1202. sendInfo.Keyword1 = keyword1
  1203. sendInfo.Keyword2 = keyword2
  1204. sendInfo.Remark = remark
  1205. sendInfo.RedirectUrl = wxAppPath
  1206. sendInfo.TemplateId = utils.WxMsgTemplateIdWithSealApplyFinished
  1207. sendInfo.RedirectTarget = 2
  1208. sendInfo.Resource = wxAppPath
  1209. sendInfo.SendType = utils.TEMPLATE_MSG_SEAL_FINISHED
  1210. sendInfo.OpenIdArr = openIdArr
  1211. err = SendTemplateMsg(sendInfo)
  1212. }
  1213. utils.FileLog.Info("send end")
  1214. return
  1215. }
  1216. // SendYbCommunityVideoWxMsg 推送研报小程序视频社区模板消息
  1217. func SendYbCommunityVideoWxMsg(videoId int, varietyTagName, title, publishTime string) (err error) {
  1218. var msg string
  1219. defer func() {
  1220. if err != nil {
  1221. errMsg := "SendYbCommunityVideoWxMsg 发送视频社区模版消息失败; Err: " + err.Error() + "; Msg: " + msg
  1222. utils.FileLog.Info("%s", errMsg)
  1223. go alarm_msg.SendAlarmMsg(errMsg, 3)
  1224. }
  1225. }()
  1226. //accessToken, err := models.GetWxAccessToken()
  1227. //if err != nil {
  1228. // msg = "GetWxAccessToken Err:" + err.Error()
  1229. // return
  1230. //}
  1231. //if accessToken == "" {
  1232. // msg = "accessToken is empty"
  1233. // return
  1234. //}
  1235. openIdList, err := models.GetOpenIdList()
  1236. if err != nil {
  1237. msg = "get openIdList err:" + err.Error()
  1238. return
  1239. }
  1240. utils.FileLog.Info("send start")
  1241. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  1242. //sendMap := make(map[string]interface{})
  1243. //sendData := make(map[string]interface{})
  1244. first := "Hi,最新一期视频已发布,欢迎查看"
  1245. keyword1 := fmt.Sprintf("%s小视频", varietyTagName)
  1246. keyword2 := title
  1247. keyword3 := publishTime
  1248. keyword4 := ""
  1249. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  1250. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  1251. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  1252. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  1253. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  1254. //sendMap["template_id"] = utils.TemplateIdByProduct
  1255. //sendMap["data"] = sendData
  1256. if utils.RunMode == "debug" {
  1257. videoId = 1
  1258. }
  1259. wxAppPath := fmt.Sprint(`/pages/video/videoList?videoId=`, videoId)
  1260. //if wxAppPath != "" {
  1261. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  1262. //}
  1263. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_VIDEO)
  1264. openIdArr := make([]string, len(openIdList))
  1265. for i, v := range openIdList {
  1266. openIdArr[i] = v.OpenId
  1267. }
  1268. sendInfo := new(SendWxTemplate)
  1269. sendInfo.First = first
  1270. sendInfo.Keyword1 = keyword1
  1271. sendInfo.Keyword2 = keyword2
  1272. sendInfo.Keyword3 = keyword3
  1273. sendInfo.Keyword4 = keyword4
  1274. sendInfo.RedirectUrl = wxAppPath
  1275. sendInfo.TemplateId = utils.UserTemplateIdByProduct
  1276. sendInfo.RedirectTarget = 1
  1277. sendInfo.Resource = wxAppPath
  1278. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_VIDEO
  1279. sendInfo.OpenIdArr = openIdArr
  1280. err = SendTemplateMsg(sendInfo)
  1281. return
  1282. }
  1283. // SendYbRoadVideoWxMsg 推送研报小程序线上路演模板消息
  1284. func SendYbRoadVideoWxMsg(videoId int, title, publishTime, chartPermissionIds string) (err error) {
  1285. var msg string
  1286. defer func() {
  1287. if err != nil {
  1288. errMsg := "SendYbRoadVideoWxMsg 推送研报小程序线上路演模板消息; Err: " + err.Error() + "; Msg: " + msg
  1289. utils.FileLog.Info("%s", errMsg)
  1290. go alarm_msg.SendAlarmMsg(errMsg, 3)
  1291. }
  1292. }()
  1293. if chartPermissionIds == "" {
  1294. msg = "权限id为空,不可发送模板消息,videoId:" + strconv.Itoa(videoId)
  1295. return
  1296. }
  1297. chartPermissionNameList := make([]string, 0)
  1298. chartPermissionIdList := strings.Split(chartPermissionIds, ",")
  1299. tmpList, _ := models.GetChartPermissionByIds(chartPermissionIdList)
  1300. if len(tmpList) > 0 {
  1301. for _, v := range tmpList {
  1302. chartPermissionNameList = append(chartPermissionNameList, v.PermissionName)
  1303. }
  1304. }
  1305. openIdArr, err := models.GetOpenIdArrByChartPermissionIds(chartPermissionIds)
  1306. if err != nil {
  1307. msg = "get openIdList err:" + err.Error()
  1308. return
  1309. }
  1310. utils.FileLog.Info("send start")
  1311. first := "Hi,最新一期视频已发布,欢迎查看"
  1312. keyword1 := "线上路演"
  1313. keyword2 := title
  1314. keyword3 := publishTime
  1315. keyword4 := strings.Join(chartPermissionNameList, ",")
  1316. if utils.RunMode == "debug" {
  1317. videoId = 1
  1318. }
  1319. wxAppPath := fmt.Sprint(`/pages/roadShow/video/list?videoId=`, videoId)
  1320. //openIdArr := make([]string, len(openIdList))
  1321. //for i, v := range openIdList {
  1322. // openIdArr[i] = v.OpenId
  1323. //}bee r
  1324. sendInfo := new(SendWxTemplate)
  1325. sendInfo.First = first
  1326. sendInfo.Keyword1 = keyword1
  1327. sendInfo.Keyword2 = keyword2
  1328. sendInfo.Keyword3 = keyword3
  1329. sendInfo.Keyword4 = keyword4
  1330. sendInfo.RedirectUrl = wxAppPath
  1331. sendInfo.TemplateId = utils.UserTemplateIdByProduct
  1332. sendInfo.RedirectTarget = 1
  1333. sendInfo.Resource = wxAppPath
  1334. sendInfo.SendType = utils.TEMPLATE_MSG_YB_ROAD_VIDEO
  1335. sendInfo.OpenIdArr = openIdArr
  1336. err = SendTemplateMsg(sendInfo)
  1337. return
  1338. }
  1339. // 本周研究汇总,上周纪要汇总,报告精选
  1340. func SendWxMsgThisWeekLastWeek(first, keyword1, keyword2, keyword3, keyword4 string, openIdList []*models.OpenIdList, articleId, source int) (err error) {
  1341. var msg string
  1342. defer func() {
  1343. if err != nil {
  1344. fmt.Println("err:", err)
  1345. go alarm_msg.SendAlarmMsg("查研观向,本周研究汇总,上周纪要汇总,报告精选发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  1346. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  1347. }
  1348. if msg != "" {
  1349. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1350. }
  1351. }()
  1352. var redirectUrl string
  1353. if source == 1 {
  1354. redirectUrl = utils.WX_MSG_PATH_KEY_COMPANY_DETAIL + strconv.Itoa(articleId) + "&IsSendWx=1"
  1355. } else if source == 2 {
  1356. redirectUrl = utils.WX_MSG_PATH_THIS_WEEK_DETAIL + strconv.Itoa(articleId) + "&IsSendWx=1"
  1357. } else if source == 3 {
  1358. redirectUrl = utils.WX_MSG_PATH_LAST_WEEK_DETAIL + strconv.Itoa(articleId) + "&IsSendWx=1"
  1359. }
  1360. openIdArr := make([]string, len(openIdList))
  1361. for i, v := range openIdList {
  1362. openIdArr[i] = v.OpenId
  1363. }
  1364. sendInfo := new(SendWxTemplate)
  1365. //sendInfo.First = first
  1366. sendInfo.Keyword1 = keyword1
  1367. sendInfo.Keyword2 = keyword2
  1368. sendInfo.Keyword3 = keyword3
  1369. sendInfo.Keyword4 = keyword4
  1370. //sendInfo.Remark = remark
  1371. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  1372. sendInfo.RedirectUrl = redirectUrl
  1373. sendInfo.RedirectTarget = 3
  1374. sendInfo.Resource = strconv.Itoa(articleId)
  1375. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE
  1376. sendInfo.OpenIdArr = openIdArr
  1377. err = SendTemplateMsg(sendInfo)
  1378. return
  1379. }
  1380. // 推送预约研选的纪要活动、策略报告、研选报告
  1381. func SendWxMsgWithCygxActivityAppointmentNew(first, keyword1, keyword2, keyword3, keyword4 string, item *cygx.OpenIdList, articleId int) (err error) {
  1382. var msg string
  1383. defer func() {
  1384. if err != nil {
  1385. fmt.Println("err:", err)
  1386. go alarm_msg.SendAlarmMsg("查研观向,推送预约研选的纪要活动、策略报告、研选报告,Err:"+err.Error()+";msg:"+msg, 3)
  1387. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  1388. }
  1389. if msg != "" {
  1390. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1391. }
  1392. }()
  1393. //var accessToken string
  1394. //accessToken, err = models.GetWxAccessTokenByXzs()
  1395. //if err != nil {
  1396. // msg = "GetWxAccessToken Err:" + err.Error()
  1397. // return
  1398. //}
  1399. //if accessToken == "" {
  1400. // msg = "accessToken is empty"
  1401. // return
  1402. //}
  1403. var openIdList []*cygx.OpenIdList
  1404. openIdList = append(openIdList, item)
  1405. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  1406. //sendMap := make(map[string]interface{})
  1407. //sendData := make(map[string]interface{})
  1408. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  1409. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  1410. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  1411. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  1412. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  1413. //sendData["remark"] = map[string]interface{}{"value": "点击查看报告详情", "color": "#173177"}
  1414. //sendMap["template_id"] = utils.TemplateIdByProductXzs
  1415. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCygxAppId, "pagepath": "pageMy/reportDetail/reportDetail?id=" + strconv.Itoa(articleId) + "&IsSendWx=1"}
  1416. //sendMap["data"] = sendData
  1417. //sendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(articleId), utils.TEMPLATE_MSG_CYGX_ARTICLE)
  1418. openIdArr := make([]string, len(openIdList))
  1419. for i, v := range openIdList {
  1420. openIdArr[i] = v.OpenId
  1421. }
  1422. sendInfo := new(SendWxTemplate)
  1423. sendInfo.First = first
  1424. sendInfo.Keyword1 = keyword1
  1425. sendInfo.Keyword2 = keyword2
  1426. sendInfo.Keyword3 = keyword3
  1427. sendInfo.Keyword4 = keyword4
  1428. sendInfo.Remark = "点击查看报告详情"
  1429. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  1430. sendInfo.RedirectUrl = utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  1431. sendInfo.RedirectTarget = 3
  1432. sendInfo.Resource = strconv.Itoa(articleId)
  1433. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE
  1434. sendInfo.OpenIdArr = openIdArr
  1435. err = SendTemplateMsg(sendInfo)
  1436. return
  1437. }
  1438. type SendWxTemplate struct {
  1439. WxAppId string `description:"公众号appId"`
  1440. First string `description:"模板消息first字段"`
  1441. Keyword1 string `description:"模板消息keyword1字段"`
  1442. Keyword2 string `description:"模板消息keyword2字段"`
  1443. Keyword3 string `description:"模板消息keyword3字段"`
  1444. Keyword4 string `description:"模板消息keyword4字段"`
  1445. Keyword5 string `description:"模板消息keyword5字段"`
  1446. Remark string `description:"模板消息remark字段"`
  1447. TemplateId string `description:"模板id"`
  1448. RedirectUrl string `description:"跳转地址"`
  1449. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  1450. Resource string `description:"资源唯一标识"`
  1451. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  1452. OpenIdArr []string `description:"消息接收者openid"`
  1453. }
  1454. // 推送模板消息
  1455. func SendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  1456. postData, err := json.Marshal(sendInfo)
  1457. if err != nil {
  1458. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  1459. return err
  1460. }
  1461. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  1462. client := &http.Client{}
  1463. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  1464. if err != nil {
  1465. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  1466. return err
  1467. }
  1468. contentType := "application/json;charset=utf-8"
  1469. req.Header.Set("Content-Type", contentType)
  1470. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  1471. resp, err := client.Do(req)
  1472. if err != nil {
  1473. fmt.Println("http client.Do Err:" + err.Error())
  1474. return err
  1475. }
  1476. defer resp.Body.Close()
  1477. b, err := ioutil.ReadAll(resp.Body)
  1478. if err != nil {
  1479. return err
  1480. }
  1481. result := new(models.BaseResponse)
  1482. err = json.Unmarshal(b, &result)
  1483. if err != nil {
  1484. return err
  1485. }
  1486. if result.Ret != 200 {
  1487. err = errors.New(string(b))
  1488. return err
  1489. }
  1490. return
  1491. }
  1492. func SendWxMsgWithCygxMorningMeeting(meetingId, industryId int, industryName string) (err error) {
  1493. var msg string
  1494. defer func() {
  1495. if err != nil {
  1496. fmt.Println("err:", err)
  1497. go alarm_msg.SendAlarmMsg("查研观向推送晨会点评模板消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  1498. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  1499. }
  1500. if msg != "" {
  1501. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1502. }
  1503. }()
  1504. reviewInfo, err := cygx.GetMorningMeetingReviewById(meetingId)
  1505. if err != nil {
  1506. err = errors.New("获取晨会点评失败, Err: " + err.Error())
  1507. return
  1508. }
  1509. chapter, err := cygx.GetCygxMorningMeetingReviewsListByIdAndIndustryId(meetingId, industryId)
  1510. if err != nil {
  1511. err = errors.New("获取晨会点评章节失败, Err: " + err.Error())
  1512. return
  1513. }
  1514. meetingTime := reviewInfo.MeetingTime.Format(utils.FormatDate)
  1515. openIdList, err := models.GetCygxUserIndustryFllowOpneid(industryId)
  1516. if err != nil {
  1517. err = errors.New("GetCygxUserIndustryFllowOpneid, Err: " + err.Error())
  1518. return
  1519. }
  1520. listUser, e := models.GetWxUserListCompanyId(utils.JMCJ_COMPANY_ID)
  1521. if e != nil {
  1522. err = errors.New("GetWxUserListCompanyId" + e.Error())
  1523. return
  1524. }
  1525. var mobileArr []string
  1526. for _, v := range listUser {
  1527. mobileArr = append(mobileArr, v.Mobile)
  1528. }
  1529. openIdListJmcj, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  1530. if e != nil {
  1531. err = errors.New("GetUserRecordListByMobileArr" + e.Error())
  1532. return
  1533. }
  1534. mapOpneiIdJmcj := make(map[string]bool)
  1535. for _, v := range openIdListJmcj {
  1536. mapOpneiIdJmcj[v.OpenId] = true
  1537. }
  1538. var openIdArr []string
  1539. for _, v := range openIdList {
  1540. if !mapOpneiIdJmcj[v.OpenId] {
  1541. openIdArr = append(openIdArr, v.OpenId)
  1542. }
  1543. }
  1544. //保留回撤,去掉空行
  1545. content := strings.Replace(chapter.Content, "<br></p>", "", -1)
  1546. content = strings.Replace(content, "</p>", "\n", -1)
  1547. content, err = utils.GetReportContentTextSub(content)
  1548. if err != nil {
  1549. err = errors.New("获取晨会点评章节文本失败, Err: " + err.Error())
  1550. return
  1551. }
  1552. sendInfo := new(SendWxTemplate)
  1553. //sendInfo.First = "【" + industryName + "】产业,有新的跟踪点评"
  1554. sendInfo.Keyword1 = "【" + industryName + "】产业,有新的跟踪点评"
  1555. sendInfo.Keyword2 = content
  1556. sendInfo.Keyword3 = meetingTime
  1557. sendInfo.Remark = "点击进入小程序对应的产业资源包页面"
  1558. if utils.RunMode == "release" {
  1559. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  1560. } else {
  1561. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  1562. }
  1563. sendInfo.RedirectUrl = utils.WX_MSG_PATH_INDUSTRY_DETAIL + strconv.Itoa(industryId) + "&IsSendWx=1&ChapterId=" + strconv.Itoa(chapter.Id)
  1564. sendInfo.RedirectTarget = 3
  1565. sendInfo.Resource = strconv.Itoa(industryId)
  1566. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_MORNING_MEETING
  1567. sendInfo.OpenIdArr = openIdArr
  1568. err = SendTemplateMsg(sendInfo)
  1569. return
  1570. }
  1571. //func init() {
  1572. // fmt.Println("start send template msg")
  1573. // sendInfo := new(SendWxTemplate)
  1574. //
  1575. // openIdArr := make([]string, len(openIdList))
  1576. // keyword1 := "弘则日度点评"
  1577. // first := "Hi,最新一期日度点评已上线,欢迎查看"
  1578. // sendInfo.First = first
  1579. // sendInfo.Keyword1 = keyword1
  1580. // sendInfo.Keyword2 = "测试报告-模板消息推送"
  1581. // sendInfo.Keyword3 = time.Now().Format(utils.FormatDateTime)
  1582. // sendInfo.Keyword4 = "测试报告简介-模板消息推送"
  1583. // sendInfo.TemplateId = utils.TemplateIdByProduct
  1584. // sendInfo.RedirectUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id=2441"
  1585. // sendInfo.Resource = "100"
  1586. // sendInfo.SendType = 1
  1587. //
  1588. // openIdArr = append(openIdArr, "oN0jD1eTfIAf68Y2n24RrvIGXFw4")
  1589. // sendInfo.OpenIdArr = openIdArr
  1590. // SendTemplateMsg(sendInfo)
  1591. // fmt.Println("end send template msg")
  1592. //}
  1593. func PublicsendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList, resource string, sendType int) (err error) {
  1594. for _, v := range items {
  1595. sendMap["touser"] = v.OpenId
  1596. data, err := json.Marshal(sendMap)
  1597. if err != nil {
  1598. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  1599. utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error()))
  1600. return err
  1601. }
  1602. err = toSendTemplateMsg(sendUrl, data, resource, sendType, v)
  1603. if err != nil {
  1604. fmt.Println("send err:", err.Error())
  1605. utils.FileLog.Info(fmt.Sprintf("ToSendTemplateMsg Err:%s", err.Error()))
  1606. }
  1607. }
  1608. return
  1609. }
  1610. // SendYbCommunityVideoWxMsg 推送研报小程序视频社区模板消息
  1611. func SendYbCommunityVideoWxMsgV2(videoId, varietyTagId int, varietyTagName, title, publishTime string) (err error) {
  1612. var msg string
  1613. defer func() {
  1614. if err != nil {
  1615. errMsg := "SendYbCommunityVideoWxMsg 发送视频社区模版消息失败; Err: " + err.Error() + "; Msg: " + msg
  1616. utils.FileLog.Info("%s", errMsg)
  1617. go alarm_msg.SendAlarmMsg(errMsg, 3)
  1618. }
  1619. }()
  1620. //accessToken, err := models.GetWxAccessToken()
  1621. //if err != nil {
  1622. // msg = "GetWxAccessToken Err:" + err.Error()
  1623. // return
  1624. //}
  1625. //if accessToken == "" {
  1626. // msg = "accessToken is empty"
  1627. // return
  1628. //}
  1629. openIdList, err := models.GetOpenIdArrByVarietyTag(varietyTagId)
  1630. if err != nil {
  1631. msg = "get openIdList err:" + err.Error()
  1632. return
  1633. }
  1634. utils.FileLog.Info("send start")
  1635. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  1636. //sendMap := make(map[string]interface{})
  1637. //sendData := make(map[string]interface{})
  1638. first := "Hi,最新一期视频已发布,欢迎查看"
  1639. keyword1 := fmt.Sprintf("%s小视频", varietyTagName)
  1640. keyword2 := title
  1641. keyword3 := publishTime
  1642. keyword4 := ""
  1643. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  1644. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  1645. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  1646. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  1647. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  1648. //sendMap["template_id"] = utils.TemplateIdByProduct
  1649. //sendMap["data"] = sendData
  1650. if utils.RunMode == "debug" {
  1651. videoId = 1
  1652. }
  1653. wxAppPath := fmt.Sprint(`/pages/video/videoList?videoId=`, videoId)
  1654. //if wxAppPath != "" {
  1655. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  1656. //}
  1657. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_VIDEO)
  1658. //openIdArr := make([]string, len(openIdList))
  1659. //for i, v := range openIdList {
  1660. // openIdArr[i] = v.OpenId
  1661. //}
  1662. sendInfo := new(SendWxTemplate)
  1663. sendInfo.First = first
  1664. sendInfo.Keyword1 = keyword1
  1665. sendInfo.Keyword2 = keyword2
  1666. sendInfo.Keyword3 = keyword3
  1667. sendInfo.Keyword4 = keyword4
  1668. sendInfo.RedirectUrl = wxAppPath
  1669. sendInfo.TemplateId = utils.UserTemplateIdByProduct
  1670. sendInfo.RedirectTarget = 1
  1671. sendInfo.Resource = wxAppPath
  1672. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_VIDEO
  1673. sendInfo.OpenIdArr = openIdList
  1674. err = SendTemplateMsg(sendInfo)
  1675. return
  1676. }
  1677. // SendWxMsgWithRaiSell 权益销售客户申请转正后,消息群发给所有销售
  1678. func SendWxMsgWithRaiSell(first, keyword1, keyword2, keyword3, keyword4 string, openIdList []*cygx.OpenIdList) (err error) {
  1679. var msg string
  1680. defer func() {
  1681. if err != nil {
  1682. fmt.Println("err:", err)
  1683. go alarm_msg.SendAlarmMsg("权益销售客户申请转正后,消息群发给所有销售,Err:"+err.Error()+";msg:"+msg, 3)
  1684. }
  1685. if msg != "" {
  1686. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1687. }
  1688. }()
  1689. openIdArr := make([]string, len(openIdList))
  1690. for i, v := range openIdList {
  1691. openIdArr[i] = v.OpenId
  1692. }
  1693. sendInfo := new(SendWxTemplate)
  1694. sendInfo.First = first
  1695. sendInfo.Keyword1 = keyword1
  1696. sendInfo.Keyword2 = keyword2
  1697. sendInfo.Keyword3 = keyword3
  1698. sendInfo.Keyword4 = keyword4
  1699. sendInfo.Remark = ""
  1700. sendInfo.TemplateId = utils.WxMsgTemplateIdCompanyApprovalMessageRai
  1701. sendInfo.RedirectUrl = ""
  1702. sendInfo.RedirectTarget = 3
  1703. sendInfo.SendType = utils.TEMPLATE_MSG_APPLY
  1704. sendInfo.OpenIdArr = openIdArr
  1705. fmt.Println(sendInfo)
  1706. err = SendTemplateMsg(sendInfo)
  1707. return
  1708. }
  1709. // SendWxMsgWithRaiWangYang 客户研选行业转正时(王芳审批通过),模板消息提醒汪洋
  1710. func SendWxMsgWithRaiWangYang(keyword1, keyword2, keyword3, keyword4 string, openIdList []*cygx.OpenIdList) (err error) {
  1711. var msg string
  1712. defer func() {
  1713. if err != nil {
  1714. fmt.Println("err:", err)
  1715. go alarm_msg.SendAlarmMsg("客户研选行业转正时(王芳审批通过),模板消息提醒汪洋,Err:"+err.Error()+";msg:"+msg, 3)
  1716. }
  1717. if msg != "" {
  1718. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  1719. }
  1720. }()
  1721. openIdArr := make([]string, len(openIdList))
  1722. for i, v := range openIdList {
  1723. openIdArr[i] = v.OpenId
  1724. }
  1725. sendInfo := new(SendWxTemplate)
  1726. sendInfo.Keyword1 = keyword1
  1727. sendInfo.Keyword2 = keyword2
  1728. sendInfo.Keyword3 = keyword3
  1729. sendInfo.Keyword4 = keyword4
  1730. sendInfo.TemplateId = utils.WxMsgTemplateIdCompanyApprovalMessageRai
  1731. sendInfo.RedirectUrl = ""
  1732. sendInfo.RedirectTarget = 3
  1733. sendInfo.SendType = utils.TEMPLATE_MSG_APPLY
  1734. sendInfo.OpenIdArr = openIdArr
  1735. err = SendTemplateMsg(sendInfo)
  1736. return
  1737. }