wechat.go 5.0 KB

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