package controllers import ( "encoding/json" "eta/eta_mini_bridge/models" "eta/eta_mini_bridge/services" "eta/eta_mini_bridge/services/alarm_msg" "eta/eta_mini_bridge/utils" "fmt" "html" "strconv" ) type WeChatController struct { BaseAuthController } // @Title 发送微信模板接口 // @Description 发送微信模板接口 // @Param ReportId query int true "报告id" // @Success 200 {object} models.WechatSign // @router /send_template_msg [get] func (this *WeChatController) SendTemplateMsg() { br := new(models.BaseResponse).Init() defer func() { if br.Ret != 200 { b, _ := json.Marshal(br) alarm_msg.SendAlarmMsg(string(b), 1) } this.Data["json"] = br this.ServeJSON() }() reportId, _ := this.GetInt("ReportId") if reportId <= 0 { br.Msg = "参数错误" br.ErrMsg = "参数错误,报告id小于等于0" return } report, err := models.GetReportById(reportId) if err != nil { br.Msg = "该报告已删除" br.ErrMsg = "获取报告详情失败,Err:" + err.Error() return } report.ContentSub = html.UnescapeString(report.ContentSub) report.Content = html.UnescapeString(report.Content) if report == nil { br.Msg = "报告不存在" return } reportChartPermissionIdStrs, err := models.GetChartPermissionIdsListByClassifyId(report.ClassifyIdSecond) if err != nil { br.Msg = "获取研报权限失败" br.ErrMsg = "获取研报权限失败,Err:" + err.Error() return } if len(reportChartPermissionIdStrs) == 0 { br.Msg = "该研报所在分类未关联品种权限,不能推送" return } chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIdStrs) if err != nil { br.Msg = "获取研报权限失败" br.ErrMsg = "获取研报权限失败,Err:" + err.Error() return } permissionMapping, err := models.GetUserChartPermissionMapping() if err != nil { br.Msg = "获取用户权限失败" br.ErrMsg = "获取用户权限失败,Err:" + err.Error() return } // 获取不同二级品种分类下的的小程序用户列表映射 permissionMap := make(map[int][]int) for _, v := range permissionMapping { if _, ok := permissionMap[v.ChartPermissionId]; !ok { permissionMap[v.ChartPermissionId] = make([]int, 0) } else { permissionMap[v.ChartPermissionId] = append(permissionMap[v.ChartPermissionId], v.UserId) } } var IsPublic bool for _, v := range chartPermissionList { if v.IsPublic == 1 { IsPublic = true break } } var openIds []*services.OpenIdList if IsPublic { userList, err := models.GetUserBySubscribe() if err != nil { br.Msg = "获取用户列表失败" br.ErrMsg = "获取用户列表失败,Err:" + err.Error() return } userUnionMap := make(map[int]string) for _, v := range userList { userUnionMap[v.UserId] = v.UnionId } var unionIds []string for _, v := range userList { unionIds = append(unionIds, v.UnionId) } // 给所有人,发送模板消息 records, err := models.GetUserRecordByUnionids(unionIds) if err != nil { br.Msg = "获取用户记录失败" br.ErrMsg = "获取用户记录失败,Err:" + err.Error() return } for _, v := range records { openIds = append(openIds, &services.OpenIdList{ OpenId: v.OpenId, UserId: v.UserId, }) } } else { // 给指定用户,发送模板消息 // 报告所属的二级品种分类 userList, err := models.GetUserBySubscribeAndFormatUser() if err != nil { br.Msg = "获取用户列表失败" br.ErrMsg = "获取用户列表失败,Err:" + err.Error() return } userUnionMap := make(map[int]string) for _, v := range userList { userUnionMap[v.UserId] = v.UnionId } var permissionIds []int for _, v := range reportChartPermissionIdStrs { vv, _ := strconv.Atoi(v) permissionIds = append(permissionIds, vv) } var sendUserIds []int // 获取报告所属的二级品种分类对应的用户列表 for _, v := range permissionIds { if _, ok := permissionMap[v]; ok { sendUserIds = append(sendUserIds, permissionMap[v]...) } } sendUserIds = utils.Unique(sendUserIds) var unionIds []string for _, v := range sendUserIds { if id, ok := userUnionMap[v]; ok { unionIds = append(unionIds, id) } } records, err := models.GetUserRecordByUnionids(unionIds) if err != nil { br.Msg = "获取用户记录失败" br.ErrMsg = "获取用户记录失败,Err:" + err.Error() return } for _, v := range records { openIds = append(openIds, &services.OpenIdList{ OpenId: v.OpenId, UserId: v.UserId, }) } } sendData := make(map[string]interface{}) sendData["keyword1"] = map[string]interface{}{"value": report.ClassifyNameSecond, "color": "#173177"} sendData["keyword2"] = map[string]interface{}{"value": report.Title, "color": "#173177"} sendData["keyword3"] = map[string]interface{}{"value": report.PublishTime, "color": "#173177"} sendData["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"} // 推送模板消息 go func() { fmt.Println("推送模板消息:", reportId) err = services.SendMultiTemplateMsg(sendData, openIds, 1, reportId) if err != nil { utils.FileLog.Info("推送模板消息失败, Err:" + err.Error()) } }() br.Ret = 200 br.Msg = "发送成功" br.Success = true }