wechat_send_msg.go 76 KB

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