wechat.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_bridge/models"
  5. "eta/eta_mini_bridge/services"
  6. "eta/eta_mini_bridge/services/alarm_msg"
  7. "eta/eta_mini_bridge/utils"
  8. "fmt"
  9. "html"
  10. "strconv"
  11. )
  12. type WeChatController struct {
  13. BaseAuthController
  14. }
  15. // @Title 发送微信模板接口
  16. // @Description 发送微信模板接口
  17. // @Param ReportId query int true "报告id"
  18. // @Success 200 {object} models.WechatSign
  19. // @router /send_template_msg [get]
  20. func (this *WeChatController) SendTemplateMsg() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. if br.Ret != 200 {
  24. b, _ := json.Marshal(br)
  25. alarm_msg.SendAlarmMsg(string(b), 1)
  26. }
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. reportId, _ := this.GetInt("ReportId")
  31. if reportId <= 0 {
  32. br.Msg = "参数错误"
  33. br.ErrMsg = "参数错误,报告id小于等于0"
  34. return
  35. }
  36. report, err := models.GetReportById(reportId)
  37. if err != nil {
  38. br.Msg = "该报告已删除"
  39. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  40. return
  41. }
  42. report.ContentSub = html.UnescapeString(report.ContentSub)
  43. report.Content = html.UnescapeString(report.Content)
  44. if report == nil {
  45. br.Msg = "报告不存在"
  46. return
  47. }
  48. reportChartPermissionIdStrs, err := models.GetChartPermissionIdsListByClassifyId(report.ClassifyIdSecond)
  49. if err != nil {
  50. br.Msg = "获取研报权限失败"
  51. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  52. return
  53. }
  54. chartPermissionList, err := models.GetChartPermissionIdsByIds(reportChartPermissionIdStrs)
  55. if err != nil {
  56. br.Msg = "获取研报权限失败"
  57. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  58. return
  59. }
  60. permissionMapping, err := models.GetUserChartPermissionMapping()
  61. if err != nil {
  62. br.Msg = "获取用户权限失败"
  63. br.ErrMsg = "获取用户权限失败,Err:" + err.Error()
  64. return
  65. }
  66. // 获取不同二级品种分类下的的小程序用户列表映射
  67. permissionMap := make(map[int][]int)
  68. for _, v := range permissionMapping {
  69. if _, ok := permissionMap[v.ChartPermissionId]; !ok {
  70. permissionMap[v.ChartPermissionId] = make([]int, 0)
  71. } else {
  72. permissionMap[v.ChartPermissionId] = append(permissionMap[v.ChartPermissionId], v.UserId)
  73. }
  74. }
  75. var IsPublic bool
  76. for _, v := range chartPermissionList {
  77. if v.IsPublic == 1 {
  78. IsPublic = true
  79. break
  80. }
  81. }
  82. userList, err := models.GetUserBySubscribe()
  83. if err != nil {
  84. br.Msg = "获取用户列表失败"
  85. br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
  86. return
  87. }
  88. userUnionMap := make(map[int]string)
  89. for _, v := range userList {
  90. userUnionMap[v.UserId] = v.UnionId
  91. }
  92. var openIds []*services.OpenIdList
  93. if IsPublic {
  94. var unionIds []string
  95. for _, v := range userList {
  96. unionIds = append(unionIds, v.UnionId)
  97. }
  98. // 给所有人,发送模板消息
  99. records, err := models.GetUserRecordByUnionids(unionIds)
  100. if err != nil {
  101. br.Msg = "获取用户记录失败"
  102. br.ErrMsg = "获取用户记录失败,Err:" + err.Error()
  103. return
  104. }
  105. for _, v := range records {
  106. openIds = append(openIds, &services.OpenIdList{
  107. OpenId: v.OpenId,
  108. UserId: v.UserId,
  109. })
  110. }
  111. } else {
  112. // 给指定用户,发送模板消息
  113. // 报告所属的二级品种分类
  114. var permissionIds []int
  115. for _, v := range reportChartPermissionIdStrs {
  116. vv, _ := strconv.Atoi(v)
  117. permissionIds = append(permissionIds, vv)
  118. }
  119. var sendUserIds []int
  120. // 获取报告所属的二级品种分类对应的用户列表
  121. for _, v := range permissionIds {
  122. if _, ok := permissionMap[v]; ok {
  123. sendUserIds = append(sendUserIds, permissionMap[v]...)
  124. }
  125. }
  126. sendUserIds = utils.Unique(sendUserIds)
  127. var unionIds []string
  128. for _, v := range sendUserIds {
  129. if id, ok := userUnionMap[v]; ok {
  130. unionIds = append(unionIds, id)
  131. }
  132. }
  133. records, err := models.GetUserRecordByUnionids(unionIds)
  134. if err != nil {
  135. br.Msg = "获取用户记录失败"
  136. br.ErrMsg = "获取用户记录失败,Err:" + err.Error()
  137. return
  138. }
  139. for _, v := range records {
  140. openIds = append(openIds, &services.OpenIdList{
  141. OpenId: v.OpenId,
  142. UserId: v.UserId,
  143. })
  144. }
  145. }
  146. sendData := make(map[string]interface{})
  147. sendData["first"] = fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  148. sendData["keyword1"] = map[string]interface{}{"value": fmt.Sprintf("弘则%s", report.ClassifyNameSecond), "color": "#173177"}
  149. sendData["keyword2"] = map[string]interface{}{"value": report.ContentSub, "color": "#173177"}
  150. sendData["keyword3"] = map[string]interface{}{"value": report.PublishTime, "color": "#173177"}
  151. sendData["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"}
  152. err = services.SendMultiTemplateMsg(sendData, openIds, 1, reportId)
  153. if err != nil {
  154. br.Msg = "发送失败"
  155. br.ErrMsg = "发送失败,Err:" + err.Error()
  156. return
  157. }
  158. br.Ret = 200
  159. br.Msg = "发送成功"
  160. br.Success = true
  161. }