template_msg.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. // 获取小程序配置
  281. appConf, err := wx_app.GetWxAppConf()
  282. if err != nil {
  283. return
  284. }
  285. sendInfo := new(SendWxTemplate)
  286. sendInfo.WxAppId = WxAppId
  287. sendInfo.First = first
  288. sendInfo.Keyword1 = keyword1
  289. sendInfo.Keyword2 = keyword2
  290. sendInfo.Remark = remark
  291. sendInfo.RedirectUrl = wxAppPath
  292. sendInfo.TemplateId = TemplateIdWithCommunityQuestion
  293. sendInfo.RedirectTarget = appConf.MsgRedirectTarget
  294. sendInfo.Resource = wxAppPath
  295. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  296. sendInfo.OpenIdArr = openIdArr
  297. err = SendTemplateMsg(sendInfo)
  298. return
  299. }
  300. // SendVoiceBroadcastWxMsg 推送研报小程序模板消息-语音播报
  301. func SendVoiceBroadcastWxMsg(broadcastId, varietyId int, sectionName, broadcastName string) (err error) {
  302. var errMsg string
  303. defer func() {
  304. if err != nil {
  305. alarmMsg := fmt.Sprintf("SendVoiceBroadcastWxMsg-推送语音播报模版消息失败; broadcastId: %d; Err: %s; Msg: %s", broadcastId, err.Error(), errMsg)
  306. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  307. }
  308. }()
  309. openIdArr, err := wx_user.GetOpenIdArrByVarietyTag(varietyId)
  310. //List, err := wx_user.GetOpenIdList()
  311. //if err != nil {
  312. // return
  313. //}
  314. //openIdList := make([]*OpenIdList, 0)
  315. //for _, item := range List {
  316. // openIdList = append(openIdList, &OpenIdList{
  317. // OpenId: item.OpenID,
  318. // UserId: item.UserID,
  319. // })
  320. //}
  321. //openIdList = append(openIdList, &OpenIdList{
  322. // OpenId: "oN0jD1cuiBLxV1IRpu74_oHnoOjk",
  323. // UserId: 52709,
  324. // })
  325. //sendMap := make(map[string]interface{})
  326. //sendData := make(map[string]interface{})
  327. first := "您好,有新的语音播报待查看"
  328. //keyword1 := sectionName + ":" + broadcastName
  329. //keyword2 := "待查看"
  330. keyword1 := broadcastName
  331. keyword2 := fmt.Sprintf("【%s】 待查看", sectionName)
  332. remark := "请点击详情查看"
  333. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  334. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  335. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  336. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  337. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  338. //sendMap["data"] = sendData
  339. wxAppPath := fmt.Sprintf("pages-voice/voiceDetail?voiceId=%d", broadcastId)
  340. if global.CONFIG.Serve.RunMode == "debug" {
  341. // 仅测试环境测试用
  342. wxAppPath = "pages-report/reportDetail?reportId=3800"
  343. }
  344. //if wxAppPath != "" {
  345. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxYbAppId, "pagepath": wxAppPath}
  346. //}
  347. //err = SendMultiTemplateMsgNoReturn(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_VOICE_BROADCAST)
  348. //openIdArr := make([]string, len(openIdList))
  349. //for i, v := range openIdList {
  350. // openIdArr[i] = v.OpenId
  351. //}
  352. // 获取小程序配置
  353. appConf, err := wx_app.GetWxAppConf()
  354. if err != nil {
  355. return
  356. }
  357. //openIdArr := make([]string, len(openIdList))
  358. //for i, v := range openIdList {
  359. // openIdArr[i] = v.OpenId
  360. //}
  361. sendInfo := new(SendWxTemplate)
  362. sendInfo.WxAppId = WxAppId
  363. sendInfo.First = first
  364. sendInfo.Keyword1 = keyword1
  365. sendInfo.Keyword2 = keyword2
  366. sendInfo.Remark = remark
  367. sendInfo.RedirectUrl = wxAppPath
  368. sendInfo.TemplateId = TemplateIdWithCommunityQuestion
  369. sendInfo.RedirectTarget = appConf.MsgRedirectTarget
  370. sendInfo.Resource = wxAppPath
  371. sendInfo.SendType = utils.TEMPLATE_MSG_YB_VOICE_BROADCAST
  372. sendInfo.OpenIdArr = openIdArr
  373. err = SendTemplateMsg(sendInfo)
  374. return
  375. }
  376. // SendMultiTemplateMsg 推送模板消息至多个用户中间出错不返回
  377. func SendMultiTemplateMsgNoReturn(sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
  378. ws := GetWxChat()
  379. accessToken, err := ws.GetAccessToken()
  380. if err != nil {
  381. return
  382. }
  383. for _, item := range items {
  384. sendMap["touser"] = item.OpenId
  385. data, e := json.Marshal(sendMap)
  386. if e != nil {
  387. err = e
  388. return
  389. }
  390. ts := &TemplateMsgSendClient{
  391. AccessToken: accessToken,
  392. Data: data,
  393. }
  394. result, e := ts.SendMsg()
  395. if result == nil {
  396. return
  397. }
  398. // 推送消息记录
  399. {
  400. go func(v *OpenIdList) {
  401. sendStatus := 1
  402. if e != nil {
  403. sendStatus = 0
  404. }
  405. resultJson, _ := json.Marshal(result)
  406. _ = AddUserTemplateRecord(v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
  407. }(item)
  408. }
  409. if e != nil {
  410. err = e
  411. }
  412. }
  413. return
  414. }
  415. // SendQuestionToAdmin 推送研报小程序模板消息-用户提问时,通知到管理员
  416. func SendQuestionToAdmin(questionId, adminId int, adminOpenId string, questionTitle string) (err error) {
  417. if adminId == 0 || adminOpenId == "" {
  418. return
  419. }
  420. var errMsg string
  421. defer func() {
  422. if err != nil {
  423. alarmMsg := fmt.Sprintf("SendQuestionToAdmin-推送研报小程序模板消息-用户提问时,通知到管理员; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  424. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  425. }
  426. }()
  427. openIdList := make([]*OpenIdList, 0)
  428. openIdList = append(openIdList, &OpenIdList{
  429. OpenId: adminOpenId,
  430. UserId: 0,
  431. })
  432. //sendMap := make(map[string]interface{})
  433. //sendData := make(map[string]interface{})
  434. first := "您好,有新的提问待分配"
  435. keyword1 := questionTitle
  436. keyword2 := "待查看"
  437. remark := "请点击详情查看回复"
  438. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  439. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  440. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  441. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  442. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  443. //sendMap["data"] = sendData
  444. wxAppPath := fmt.Sprintf("/pages-question/detail/index?type=question&id=%d", questionId)
  445. if global.CONFIG.Serve.RunMode == "debug" {
  446. // 仅测试环境测试用
  447. wxAppPath = "pages-approve/seal/list"
  448. }
  449. //if wxAppPath != "" {
  450. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxMobileCrmAppId, "pagepath": wxAppPath}
  451. //}
  452. //err = SendMultiTemplateMsg(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  453. openIdArr := make([]string, len(openIdList))
  454. for i, v := range openIdList {
  455. openIdArr[i] = v.OpenId
  456. }
  457. sendInfo := new(SendWxTemplate)
  458. sendInfo.WxAppId = AdminWxAppId
  459. sendInfo.First = first
  460. sendInfo.Keyword1 = keyword1
  461. sendInfo.Keyword2 = keyword2
  462. sendInfo.Remark = remark
  463. sendInfo.RedirectUrl = wxAppPath
  464. sendInfo.TemplateId = TemplateIdWithCommunityQuestionNotifyAdmin
  465. sendInfo.RedirectTarget = 2
  466. sendInfo.Resource = wxAppPath
  467. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  468. sendInfo.OpenIdArr = openIdArr
  469. err = SendTemplateMsg(sendInfo)
  470. return
  471. }
  472. // SendQuestionCommentToAdmin 推送研报小程序模板消息-用户评论问答时,通知到管理员
  473. func SendQuestionCommentToAdmin(commentId, adminId int, adminOpenId string, commentContent string) (err error) {
  474. if adminId == 0 || adminOpenId == "" {
  475. return
  476. }
  477. var errMsg string
  478. defer func() {
  479. if err != nil {
  480. alarmMsg := fmt.Sprintf("SendQuestionCommentToAdmin-推送研报小程序模板消息-用户评论问答时,通知到管理员; CommentId: %d; Err: %s; Msg: %s", commentId, err.Error(), errMsg)
  481. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  482. }
  483. }()
  484. openIdList := make([]*OpenIdList, 0)
  485. openIdList = append(openIdList, &OpenIdList{
  486. OpenId: adminOpenId,
  487. UserId: 0,
  488. })
  489. //sendMap := make(map[string]interface{})
  490. //sendData := make(map[string]interface{})
  491. first := "您好,有新的评论待查看"
  492. keyword1 := commentContent
  493. keyword2 := "待查看"
  494. remark := "请点击详情查看回复"
  495. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  496. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  497. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  498. //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  499. //sendMap["template_id"] = TemplateIdWithCommunityQuestion
  500. //sendMap["data"] = sendData
  501. wxAppPath := fmt.Sprintf("/pages-question/detail/index?type=comment&id=%d", commentId)
  502. if global.CONFIG.Serve.RunMode == "debug" {
  503. // 仅测试环境测试用
  504. wxAppPath = "pages-approve/seal/list"
  505. }
  506. //if wxAppPath != "" {
  507. // sendMap["miniprogram"] = map[string]interface{}{"appid": WxMobileCrmAppId, "pagepath": wxAppPath}
  508. //}
  509. //err = SendMultiTemplateMsg(sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION)
  510. openIdArr := make([]string, len(openIdList))
  511. for i, v := range openIdList {
  512. openIdArr[i] = v.OpenId
  513. }
  514. sendInfo := new(SendWxTemplate)
  515. sendInfo.WxAppId = AdminWxAppId
  516. sendInfo.First = first
  517. sendInfo.Keyword1 = keyword1
  518. sendInfo.Keyword2 = keyword2
  519. sendInfo.Remark = remark
  520. sendInfo.RedirectUrl = wxAppPath
  521. sendInfo.TemplateId = TemplateIdWithCommunityQuestionNotifyAdmin
  522. sendInfo.RedirectTarget = 2
  523. sendInfo.Resource = wxAppPath
  524. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  525. sendInfo.OpenIdArr = openIdArr
  526. err = SendTemplateMsg(sendInfo)
  527. return
  528. }
  529. // SendQuestionToResearcher 推送研报小程序模板消息-用户提问时,通知到研究员
  530. func SendQuestionToResearcher(questionId int, adminOpenId, questionTitle, remark string) (err error) {
  531. var errMsg string
  532. defer func() {
  533. if err != nil {
  534. alarmMsg := fmt.Sprintf("SendQuestionToAdmin-推送研报小程序模板消息-用户提问时,通知到研究员; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
  535. go alarm_msg.SendAlarmMsg(alarmMsg, 3)
  536. }
  537. }()
  538. if adminOpenId == "" {
  539. err = errors.New("研究员openid为空")
  540. errMsg = "研究员openid为空"
  541. return
  542. }
  543. openIdList := make([]*OpenIdList, 0)
  544. openIdList = append(openIdList, &OpenIdList{
  545. OpenId: adminOpenId,
  546. UserId: 0,
  547. })
  548. first := "您好,有新的提问待回复"
  549. keyword1 := questionTitle
  550. keyword2 := "待回复"
  551. if remark == "" {
  552. remark = "请点击详情尽快处理"
  553. keyword2 = remark
  554. }
  555. wxAppPath := fmt.Sprintf("pages-question/answerDetail?id=%d", questionId)
  556. if global.CONFIG.Serve.RunMode == "debug" {
  557. // 仅测试环境测试用
  558. wxAppPath = "pages-report/reportDetail?reportId=3800"
  559. }
  560. openIdArr := make([]string, len(openIdList))
  561. for i, v := range openIdList {
  562. openIdArr[i] = v.OpenId
  563. }
  564. sendInfo := new(SendWxTemplate)
  565. sendInfo.WxAppId = AdminWxAppId
  566. sendInfo.First = first
  567. sendInfo.Keyword1 = keyword1
  568. sendInfo.Keyword2 = keyword2
  569. sendInfo.Remark = remark
  570. sendInfo.RedirectUrl = wxAppPath
  571. sendInfo.TemplateId = TemplateIdWithCommunityQuestionNotifyAdmin
  572. sendInfo.RedirectTarget = 1
  573. sendInfo.Resource = wxAppPath
  574. sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION
  575. sendInfo.OpenIdArr = openIdArr
  576. err = SendTemplateMsg(sendInfo)
  577. return
  578. }