template_msg.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. package wechat
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "hongze/hongze_yb/controller/response"
  9. "hongze/hongze_yb/global"
  10. "hongze/hongze_yb/models/tables/user_record"
  11. "hongze/hongze_yb/models/tables/user_template_record"
  12. "hongze/hongze_yb/models/tables/wx_user"
  13. "hongze/hongze_yb/services/alarm_msg"
  14. "hongze/hongze_yb/services/wx_app"
  15. "hongze/hongze_yb/utils"
  16. "io/ioutil"
  17. "net/http"
  18. "strings"
  19. "time"
  20. )
  21. var (
  22. TemplateMsgSendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s"
  23. TemplateMsgClearQuotaUrl = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=%s"
  24. )
  25. type TemplateMsgSendClient struct {
  26. AccessToken string
  27. Data []byte
  28. }
  29. type SendTemplateResponse struct {
  30. Errcode int `json:"errcode"`
  31. Errmsg string `json:"errmsg"`
  32. MsgID int `json:"msgid"`
  33. }
  34. type ClearQuotaResponse struct {
  35. Errcode int `json:"errcode"`
  36. Errmsg string `json:"errmsg"`
  37. }
  38. type OpenIdList struct {
  39. OpenId string
  40. UserId int
  41. }
  42. type SendWxTemplate struct {
  43. WxAppId string `description:"公众号appId"`
  44. First string `description:"模板消息first字段"`
  45. Keyword1 string `description:"模板消息keyword1字段"`
  46. Keyword2 string `description:"模板消息keyword2字段"`
  47. Keyword3 string `description:"模板消息keyword3字段"`
  48. Keyword4 string `description:"模板消息keyword4字段"`
  49. Remark string `description:"模板消息remark字段"`
  50. TemplateId string `description:"模板id"`
  51. RedirectUrl string `description:"跳转地址"`
  52. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  53. Resource string `description:"资源唯一标识"`
  54. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  55. OpenIdArr []string `description:"消息接收者openid"`
  56. }
  57. // 推送模板消息
  58. func SendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  59. postData, err := json.Marshal(sendInfo)
  60. if err != nil {
  61. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  62. return err
  63. }
  64. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  65. client := &http.Client{}
  66. req, err := http.NewRequest("POST", wx_app.SendWxTemplateMsgUrl, body)
  67. if err != nil {
  68. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  69. return err
  70. }
  71. contentType := "application/json;charset=utf-8"
  72. req.Header.Set("Content-Type", contentType)
  73. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  74. resp, err := client.Do(req)
  75. if err != nil {
  76. fmt.Println("http client.Do Err:" + err.Error())
  77. return err
  78. }
  79. defer resp.Body.Close()
  80. b, err := ioutil.ReadAll(resp.Body)
  81. if err != nil {
  82. return err
  83. }
  84. result := new(response.BaseResponse)
  85. err = json.Unmarshal(b, &result)
  86. if err != nil {
  87. return err
  88. }
  89. if result.Ret != 200 {
  90. err = errors.New(string(b))
  91. return err
  92. }
  93. return
  94. }
  95. // TemplateMsgSendClient.SendMsg 推送消息
  96. func (c *TemplateMsgSendClient) SendMsg() (sendRes *SendTemplateResponse, err error) {
  97. // 请求接口
  98. sendUrl := fmt.Sprintf(TemplateMsgSendUrl, c.AccessToken)
  99. client := http.Client{}
  100. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(c.Data))
  101. if err != nil {
  102. return
  103. }
  104. defer func() {
  105. _ = resp.Body.Close()
  106. }()
  107. body, _ := ioutil.ReadAll(resp.Body)
  108. if err = json.Unmarshal(body, &sendRes); err != nil {
  109. return
  110. }
  111. // 模板消息发送超过当日10万次限制错误处理
  112. if sendRes.Errcode == 45009 {
  113. // 发送提示邮件
  114. go alarm_msg.SendAlarmMsg("模板消息发送超过当日10万次限制, SendTemplateResponse: "+string(body), 3)
  115. // 清理限制
  116. clearRes, e := c.ClearQuota()
  117. if e != nil {
  118. err = e
  119. return
  120. }
  121. if clearRes.Errcode != 0 {
  122. clearJson, _ := json.Marshal(clearRes)
  123. go alarm_msg.SendAlarmMsg("自动清理模板消息限制接口, 调用失败, ClearQuotaResponse: "+string(clearJson), 3)
  124. return
  125. }
  126. // 发送成功邮件
  127. go alarm_msg.SendAlarmMsg("自动清理模板消息限制接口, 调用成功", 1)
  128. // 重新推送
  129. go func() {
  130. reSend, e := c.SendMsg()
  131. if e != nil {
  132. reSendJson, _ := json.Marshal(reSend)
  133. alarm_msg.SendAlarmMsg("重新推送模板消息失败, SendTemplateResponse: "+string(reSendJson), 3)
  134. }
  135. }()
  136. }
  137. if sendRes.Errcode != 0 {
  138. err = errors.New("推送模板消息失败, SendTemplateResponse: " + string(body))
  139. }
  140. return
  141. }
  142. // TemplateMsgSendClient.ClearQuota 清除发送超过当日10万次限制
  143. func (c *TemplateMsgSendClient) ClearQuota() (result *ClearQuotaResponse, err error) {
  144. key := "CACHE_SendTemplateMsg_ERR"
  145. exists, _ := global.Redis.Exists(context.TODO(), key).Result()
  146. if exists == 1 {
  147. return
  148. }
  149. _ = global.Redis.SetEX(context.TODO(), key, 1, 6*time.Minute)
  150. sendUrl := fmt.Sprintf(TemplateMsgClearQuotaUrl, c.AccessToken)
  151. client := http.Client{}
  152. clearData := make(map[string]interface{})
  153. clearData["appid"] = WxAppId
  154. clearJson, _ := json.Marshal(clearData)
  155. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(clearJson))
  156. if err != nil {
  157. return
  158. }
  159. defer func() {
  160. _ = resp.Body.Close()
  161. }()
  162. clearBody, err := ioutil.ReadAll(resp.Body)
  163. err = json.Unmarshal(clearBody, &result)
  164. return
  165. }
  166. // AddUserTemplateRecord 新增模板消息推送记录
  167. func AddUserTemplateRecord(userId, sendStatus, sendType int, openid, resource, sendData, result string) (err error) {
  168. item := &user_template_record.UserTemplateRecord{
  169. UserID: userId,
  170. OpenID: openid,
  171. Resource: resource,
  172. SendData: sendData,
  173. Result: result,
  174. CreateDate: time.Now().Format(utils.FormatDate),
  175. CreateTime: time.Now().Format(utils.FormatDateTime),
  176. SendStatus: sendStatus,
  177. SendType: sendType,
  178. }
  179. err = item.Create()
  180. return
  181. }
  182. // SendMultiTemplateMsg 推送模板消息至多个用户
  183. func SendMultiTemplateMsg(sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
  184. ws := GetWxChat()
  185. accessToken, err := ws.GetAccessToken()
  186. if err != nil {
  187. return
  188. }
  189. for _, item := range items {
  190. sendMap["touser"] = item.OpenId
  191. data, e := json.Marshal(sendMap)
  192. if e != nil {
  193. err = e
  194. return
  195. }
  196. ts := &TemplateMsgSendClient{
  197. AccessToken: accessToken,
  198. Data: data,
  199. }
  200. result, e := ts.SendMsg()
  201. if result == nil {
  202. return
  203. }
  204. // 推送消息记录
  205. {
  206. go func(v *OpenIdList) {
  207. sendStatus := 1
  208. if e != nil {
  209. sendStatus = 0
  210. }
  211. resultJson, _ := json.Marshal(result)
  212. _ = AddUserTemplateRecord(v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
  213. }(item)
  214. }
  215. if e != nil {
  216. err = e
  217. return
  218. }
  219. }
  220. return
  221. }
  222. // SendQuestionReplyWxMsg 推送研报小程序模板消息-问答社区回复
  223. func SendQuestionReplyWxMsg(questionId, userId int, questionTitle string) (err error) {
  224. if userId == 0 {
  225. return
  226. }
  227. var errMsg string
  228. defer func() {
  229. if err != nil {
  230. alarmMsg := fmt.Sprintf("SendQuestionReplyWxMsg-推送问答社区回复模版消息失败; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  231. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  232. }
  233. }()
  234. // 校验用户是否取消关注公众号
  235. userRecord, e := user_record.GetByUserId(userId, utils.USER_RECORD_PLATFORM_RDDP)
  236. if e != nil {
  237. // 用户无公众号信息
  238. if e == utils.ErrNoRow {
  239. return
  240. }
  241. err = errors.New("获取用户Record信息失败, Err:" + e.Error())
  242. return
  243. }
  244. // 取消关注则不推送
  245. if userRecord.Subscribe == 0 {
  246. return
  247. }
  248. openIdList := make([]*OpenIdList, 0)
  249. openIdList = append(openIdList, &OpenIdList{
  250. OpenId: userRecord.OpenID,
  251. UserId: userId,
  252. })
  253. //sendMap := make(map[string]interface{})
  254. //sendData := make(map[string]interface{})
  255. first := "您好,您的提问已被回复"
  256. keyword1 := questionTitle
  257. keyword2 := "待查看"
  258. remark := "请点击详情查看回复"
  259. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  260. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  261. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  262. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  263. //
  264. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  265. //sendMap["data"] = sendData
  266. //
  267. wxAppPath := fmt.Sprintf("pages-question/answerDetail?id=%d", questionId)
  268. if global.CONFIG.Serve.RunMode == "debug" {
  269. // 仅测试环境测试用
  270. wxAppPath = "pages-report/reportDetail?reportId=3800"
  271. }
  272. //if wxAppPath != "" {
  273. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxYbAppId, "pagepath": wxAppPath}
  274. //}
  275. //err = SendMultiTemplateMsg(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  276. openIdArr := make([]string, len(openIdList))
  277. for i, v := range openIdList {
  278. openIdArr[i] = v.OpenId
  279. }
  280. sendInfo := new(SendWxTemplate)
  281. sendInfo.WxAppId = WxAppId
  282. sendInfo.First = first
  283. sendInfo.Keyword1 = keyword1
  284. sendInfo.Keyword2 = keyword2
  285. sendInfo.Remark = remark
  286. sendInfo.RedirectUrl = wxAppPath
  287. sendInfo.TemplateId = TemplateIdWithCommunityQuestion
  288. sendInfo.RedirectTarget = 1
  289. sendInfo.Resource = wxAppPath
  290. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  291. sendInfo.OpenIdArr = openIdArr
  292. err = SendTemplateMsg(sendInfo)
  293. return
  294. }
  295. // SendVoiceBroadcastWxMsg 推送研报小程序模板消息-语音播报
  296. func SendVoiceBroadcastWxMsg(broadcastId, varietyId int, sectionName, broadcastName string) (err error) {
  297. var errMsg string
  298. defer func() {
  299. if err != nil {
  300. alarmMsg := fmt.Sprintf("SendVoiceBroadcastWxMsg-推送语音播报模版消息失败; broadcastId: %d; Err: %s; Msg: %s", broadcastId, err.Error(), errMsg)
  301. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  302. }
  303. }()
  304. openIdArr, err := wx_user.GetOpenIdArrByVarietyTag(varietyId)
  305. //List, err := wx_user.GetOpenIdList()
  306. //if err != nil {
  307. // return
  308. //}
  309. //openIdList := make([]*OpenIdList, 0)
  310. //for _, item := range List {
  311. // openIdList = append(openIdList, &OpenIdList{
  312. // OpenId: item.OpenID,
  313. // UserId: item.UserID,
  314. // })
  315. //}
  316. //openIdList = append(openIdList, &OpenIdList{
  317. // OpenId: "oN0jD1cuiBLxV1IRpu74_oHnoOjk",
  318. // UserId: 52709,
  319. // })
  320. //sendMap := make(map[string]interface{})
  321. //sendData := make(map[string]interface{})
  322. first := "您好,有新的语音播报待查看"
  323. //keyword1 := sectionName + ":" + broadcastName
  324. //keyword2 := "待查看"
  325. keyword1 := broadcastName
  326. keyword2 := fmt.Sprintf("【%s】 待查看", sectionName)
  327. remark := "请点击详情查看"
  328. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  329. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  330. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  331. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  332. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  333. //sendMap["data"] = sendData
  334. wxAppPath := fmt.Sprintf("pages-voice/voiceDetail?voiceId=%d", broadcastId)
  335. if global.CONFIG.Serve.RunMode == "debug" {
  336. // 仅测试环境测试用
  337. wxAppPath = "pages-report/reportDetail?reportId=3800"
  338. }
  339. //if wxAppPath != "" {
  340. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxYbAppId, "pagepath": wxAppPath}
  341. //}
  342. //err = SendMultiTemplateMsgNoReturn(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_VOICE_BROADCAST)
  343. //openIdArr := make([]string, len(openIdList))
  344. //for i, v := range openIdList {
  345. // openIdArr[i] = v.OpenId
  346. //}
  347. sendInfo := new(SendWxTemplate)
  348. sendInfo.WxAppId = WxAppId
  349. sendInfo.First = first
  350. sendInfo.Keyword1 = keyword1
  351. sendInfo.Keyword2 = keyword2
  352. sendInfo.Remark = remark
  353. sendInfo.RedirectUrl = wxAppPath
  354. sendInfo.TemplateId = TemplateIdWithCommunityQuestion
  355. sendInfo.RedirectTarget = 1
  356. sendInfo.Resource = wxAppPath
  357. sendInfo.SendType = utils.TEMPLATE_MSG_YB_VOICE_BROADCAST
  358. sendInfo.OpenIdArr = openIdArr
  359. err = SendTemplateMsg(sendInfo)
  360. return
  361. }
  362. // SendMultiTemplateMsg 推送模板消息至多个用户中间出错不返回
  363. func SendMultiTemplateMsgNoReturn(sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
  364. ws := GetWxChat()
  365. accessToken, err := ws.GetAccessToken()
  366. if err != nil {
  367. return
  368. }
  369. for _, item := range items {
  370. sendMap["touser"] = item.OpenId
  371. data, e := json.Marshal(sendMap)
  372. if e != nil {
  373. err = e
  374. return
  375. }
  376. ts := &TemplateMsgSendClient{
  377. AccessToken: accessToken,
  378. Data: data,
  379. }
  380. result, e := ts.SendMsg()
  381. if result == nil {
  382. return
  383. }
  384. // 推送消息记录
  385. {
  386. go func(v *OpenIdList) {
  387. sendStatus := 1
  388. if e != nil {
  389. sendStatus = 0
  390. }
  391. resultJson, _ := json.Marshal(result)
  392. _ = AddUserTemplateRecord(v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
  393. }(item)
  394. }
  395. if e != nil {
  396. err = e
  397. }
  398. }
  399. return
  400. }
  401. // SendQuestionToAdmin 推送研报小程序模板消息-用户提问时,通知到管理员
  402. func SendQuestionToAdmin(questionId, adminId int, adminOpenId string, questionTitle string) (err error) {
  403. if adminId == 0 || adminOpenId == "" {
  404. return
  405. }
  406. var errMsg string
  407. defer func() {
  408. if err != nil {
  409. alarmMsg := fmt.Sprintf("SendQuestionToAdmin-推送研报小程序模板消息-用户提问时,通知到管理员; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  410. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  411. }
  412. }()
  413. openIdList := make([]*OpenIdList, 0)
  414. openIdList = append(openIdList, &OpenIdList{
  415. OpenId: adminOpenId,
  416. UserId: 0,
  417. })
  418. //sendMap := make(map[string]interface{})
  419. //sendData := make(map[string]interface{})
  420. first := "您好,有新的提问待分配"
  421. keyword1 := questionTitle
  422. keyword2 := "待查看"
  423. remark := "请点击详情查看回复"
  424. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  425. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  426. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  427. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  428. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  429. //sendMap["data"] = sendData
  430. wxAppPath := fmt.Sprintf("/pages-question/detail/index?type=question&id=%d", questionId)
  431. if global.CONFIG.Serve.RunMode == "debug" {
  432. // 仅测试环境测试用
  433. wxAppPath = "pages-approve/seal/list"
  434. }
  435. //if wxAppPath != "" {
  436. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxMobileCrmAppId, "pagepath": wxAppPath}
  437. //}
  438. //err = SendMultiTemplateMsg(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  439. openIdArr := make([]string, len(openIdList))
  440. for i, v := range openIdList {
  441. openIdArr[i] = v.OpenId
  442. }
  443. sendInfo := new(SendWxTemplate)
  444. sendInfo.WxAppId = AdminWxAppId
  445. sendInfo.First = first
  446. sendInfo.Keyword1 = keyword1
  447. sendInfo.Keyword2 = keyword2
  448. sendInfo.Remark = remark
  449. sendInfo.RedirectUrl = wxAppPath
  450. sendInfo.TemplateId = TemplateIdWithCommunityQuestionNotifyAdmin
  451. sendInfo.RedirectTarget = 2
  452. sendInfo.Resource = wxAppPath
  453. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  454. sendInfo.OpenIdArr = openIdArr
  455. err = SendTemplateMsg(sendInfo)
  456. return
  457. }
  458. // SendQuestionCommentToAdmin 推送研报小程序模板消息-用户评论问答时,通知到管理员
  459. func SendQuestionCommentToAdmin(commentId, adminId int, adminOpenId string, commentContent string) (err error) {
  460. if adminId == 0 || adminOpenId == "" {
  461. return
  462. }
  463. var errMsg string
  464. defer func() {
  465. if err != nil {
  466. alarmMsg := fmt.Sprintf("SendQuestionCommentToAdmin-推送研报小程序模板消息-用户评论问答时,通知到管理员; CommentId: %d; Err: %s; Msg: %s", commentId, err.Error(), errMsg)
  467. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  468. }
  469. }()
  470. openIdList := make([]*OpenIdList, 0)
  471. openIdList = append(openIdList, &OpenIdList{
  472. OpenId: adminOpenId,
  473. UserId: 0,
  474. })
  475. //sendMap := make(map[string]interface{})
  476. //sendData := make(map[string]interface{})
  477. first := "您好,有新的评论待查看"
  478. keyword1 := commentContent
  479. keyword2 := "待查看"
  480. remark := "请点击详情查看回复"
  481. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  482. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  483. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  484. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  485. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  486. //sendMap["data"] = sendData
  487. wxAppPath := fmt.Sprintf("/pages-question/detail/index?type=comment&id=%d", commentId)
  488. if global.CONFIG.Serve.RunMode == "debug" {
  489. // 仅测试环境测试用
  490. wxAppPath = "pages-approve/seal/list"
  491. }
  492. //if wxAppPath != "" {
  493. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxMobileCrmAppId, "pagepath": wxAppPath}
  494. //}
  495. //err = SendMultiTemplateMsg(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  496. openIdArr := make([]string, len(openIdList))
  497. for i, v := range openIdList {
  498. openIdArr[i] = v.OpenId
  499. }
  500. sendInfo := new(SendWxTemplate)
  501. sendInfo.WxAppId = AdminWxAppId
  502. sendInfo.First = first
  503. sendInfo.Keyword1 = keyword1
  504. sendInfo.Keyword2 = keyword2
  505. sendInfo.Remark = remark
  506. sendInfo.RedirectUrl = wxAppPath
  507. sendInfo.TemplateId = TemplateIdWithCommunityQuestionNotifyAdmin
  508. sendInfo.RedirectTarget = 2
  509. sendInfo.Resource = wxAppPath
  510. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  511. sendInfo.OpenIdArr = openIdArr
  512. err = SendTemplateMsg(sendInfo)
  513. return
  514. }
  515. // SendQuestionToResearcher 推送研报小程序模板消息-用户提问时,通知到研究员
  516. func SendQuestionToResearcher(questionId int, adminOpenId, questionTitle, remark string) (err error) {
  517. if adminOpenId == "" {
  518. return
  519. }
  520. var errMsg string
  521. defer func() {
  522. if err != nil {
  523. alarmMsg := fmt.Sprintf("SendQuestionToAdmin-推送研报小程序模板消息-用户提问时,通知到研究员; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  524. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  525. }
  526. }()
  527. openIdList := make([]*OpenIdList, 0)
  528. openIdList = append(openIdList, &OpenIdList{
  529. OpenId: adminOpenId,
  530. UserId: 0,
  531. })
  532. first := "您好,有新的提问待回复"
  533. keyword1 := questionTitle
  534. keyword2 := "待回复"
  535. if remark == "" {
  536. remark = "请点击详情尽快处理"
  537. keyword2 = remark
  538. }
  539. wxAppPath := fmt.Sprintf("pages-question/answerDetail?id=%d", questionId)
  540. if global.CONFIG.Serve.RunMode == "debug" {
  541. // 仅测试环境测试用
  542. wxAppPath = "pages-report/reportDetail?reportId=3800"
  543. }
  544. openIdArr := make([]string, len(openIdList))
  545. for i, v := range openIdList {
  546. openIdArr[i] = v.OpenId
  547. }
  548. sendInfo := new(SendWxTemplate)
  549. sendInfo.WxAppId = AdminWxAppId
  550. sendInfo.First = first
  551. sendInfo.Keyword1 = keyword1
  552. sendInfo.Keyword2 = keyword2
  553. sendInfo.Remark = remark
  554. sendInfo.RedirectUrl = wxAppPath
  555. sendInfo.TemplateId = TemplateIdWithCommunityQuestionNotifyAdmin
  556. sendInfo.RedirectTarget = 1
  557. sendInfo.Resource = wxAppPath
  558. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  559. sendInfo.OpenIdArr = openIdArr
  560. err = SendTemplateMsg(sendInfo)
  561. return
  562. }