wechat.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if len(reportChartPermissionIdStrs) == 0 {
  55. br.Msg = "该研报所在分类未关联品种权限,不能推送"
  56. return
  57. }
  58. chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIdStrs)
  59. if err != nil {
  60. br.Msg = "获取研报权限失败"
  61. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  62. return
  63. }
  64. permissionMapping, err := models.GetUserChartPermissionMapping()
  65. if err != nil {
  66. br.Msg = "获取用户权限失败"
  67. br.ErrMsg = "获取用户权限失败,Err:" + err.Error()
  68. return
  69. }
  70. // 获取不同二级品种分类下的的小程序用户列表映射
  71. permissionMap := make(map[int][]int)
  72. for _, v := range permissionMapping {
  73. if _, ok := permissionMap[v.ChartPermissionId]; !ok {
  74. permissionMap[v.ChartPermissionId] = make([]int, 0)
  75. } else {
  76. permissionMap[v.ChartPermissionId] = append(permissionMap[v.ChartPermissionId], v.UserId)
  77. }
  78. }
  79. var IsPublic bool
  80. for _, v := range chartPermissionList {
  81. if v.IsPublic == 1 {
  82. IsPublic = true
  83. break
  84. }
  85. }
  86. var openIds []*services.OpenIdList
  87. if IsPublic {
  88. userList, err := models.GetUserBySubscribe()
  89. if err != nil {
  90. br.Msg = "获取用户列表失败"
  91. br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
  92. return
  93. }
  94. userUnionMap := make(map[int]string)
  95. for _, v := range userList {
  96. userUnionMap[v.UserId] = v.UnionId
  97. }
  98. var unionIds []string
  99. for _, v := range userList {
  100. unionIds = append(unionIds, v.UnionId)
  101. }
  102. // 给所有人,发送模板消息
  103. records, err := models.GetUserRecordByUnionids(unionIds)
  104. if err != nil {
  105. br.Msg = "获取用户记录失败"
  106. br.ErrMsg = "获取用户记录失败,Err:" + err.Error()
  107. return
  108. }
  109. for _, v := range records {
  110. openIds = append(openIds, &services.OpenIdList{
  111. OpenId: v.OpenId,
  112. UserId: v.UserId,
  113. })
  114. }
  115. } else {
  116. // 给指定用户,发送模板消息
  117. // 报告所属的二级品种分类
  118. userList, err := models.GetUserBySubscribeAndFormatUser()
  119. if err != nil {
  120. br.Msg = "获取用户列表失败"
  121. br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
  122. return
  123. }
  124. userUnionMap := make(map[int]string)
  125. for _, v := range userList {
  126. userUnionMap[v.UserId] = v.UnionId
  127. }
  128. var permissionIds []int
  129. for _, v := range reportChartPermissionIdStrs {
  130. vv, _ := strconv.Atoi(v)
  131. permissionIds = append(permissionIds, vv)
  132. }
  133. var sendUserIds []int
  134. // 获取报告所属的二级品种分类对应的用户列表
  135. for _, v := range permissionIds {
  136. if _, ok := permissionMap[v]; ok {
  137. sendUserIds = append(sendUserIds, permissionMap[v]...)
  138. }
  139. }
  140. sendUserIds = utils.Unique(sendUserIds)
  141. var unionIds []string
  142. for _, v := range sendUserIds {
  143. if id, ok := userUnionMap[v]; ok {
  144. unionIds = append(unionIds, id)
  145. }
  146. }
  147. records, err := models.GetUserRecordByUnionids(unionIds)
  148. if err != nil {
  149. br.Msg = "获取用户记录失败"
  150. br.ErrMsg = "获取用户记录失败,Err:" + err.Error()
  151. return
  152. }
  153. for _, v := range records {
  154. openIds = append(openIds, &services.OpenIdList{
  155. OpenId: v.OpenId,
  156. UserId: v.UserId,
  157. })
  158. }
  159. }
  160. sendData := make(map[string]interface{})
  161. sendData["keyword1"] = map[string]interface{}{"value": report.ClassifyNameSecond, "color": "#173177"}
  162. sendData["keyword2"] = map[string]interface{}{"value": report.Title, "color": "#173177"}
  163. sendData["keyword3"] = map[string]interface{}{"value": report.PublishTime, "color": "#173177"}
  164. sendData["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"}
  165. // 推送模板消息
  166. go func() {
  167. fmt.Println("推送模板消息:", reportId)
  168. err = services.SendMultiTemplateMsg(sendData, openIds, 1, reportId)
  169. if err != nil {
  170. utils.FileLog.Info("推送模板消息失败, Err:" + err.Error())
  171. }
  172. }()
  173. br.Ret = 200
  174. br.Msg = "发送成功"
  175. br.Success = true
  176. }