|
@@ -4,6 +4,11 @@ import (
|
|
|
"encoding/json"
|
|
|
"eta/eta_mini_bridge/models"
|
|
|
"eta/eta_mini_bridge/models/request"
|
|
|
+ "eta/eta_mini_bridge/services"
|
|
|
+ "eta/eta_mini_bridge/utils"
|
|
|
+ "fmt"
|
|
|
+ "html"
|
|
|
+ "strconv"
|
|
|
)
|
|
|
|
|
|
type WeChatController struct {
|
|
@@ -12,7 +17,7 @@ type WeChatController struct {
|
|
|
|
|
|
// @Title 发送微信模板接口
|
|
|
// @Description 发送微信模板接口
|
|
|
-// @Param Url query string true "url地址"
|
|
|
+// @Param request body request.SendWxTemplateReq true "type json string"
|
|
|
// @Success 200 {object} models.WechatSign
|
|
|
// @router /send_template_msg [post]
|
|
|
func (this *WeChatController) SendTemplateMsg() {
|
|
@@ -27,4 +32,132 @@ func (this *WeChatController) SendTemplateMsg() {
|
|
|
br.ErrMsg = "参数解析错误,Err:" + err.Error()
|
|
|
return
|
|
|
}
|
|
|
+ report, err := models.GetReportById(req.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
|
|
|
+ }
|
|
|
+ chartPermissionList, err := models.GetChartPermissionIdsByIds(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
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userList, err := models.GetUserBySubscribe()
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "获取用户列表失败"
|
|
|
+ br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var openIds []*services.OpenIdList
|
|
|
+ if IsPublic {
|
|
|
+ var unionIds []string
|
|
|
+ userMap := make(map[int]*models.User)
|
|
|
+ for _, v := range userList {
|
|
|
+ unionIds = append(unionIds, v.UnionId)
|
|
|
+ userMap[v.UserId] = v
|
|
|
+ }
|
|
|
+ // 给所有人,发送模板消息
|
|
|
+ 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 {
|
|
|
+ // 给指定用户,发送模板消息
|
|
|
+ // 报告所属的二级品种分类
|
|
|
+ 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]...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ unionIds, err := models.GetUserUnionIdListByIds(sendUserIds)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "获取用户信息失败"
|
|
|
+ br.ErrMsg = "获取用户unionId失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ 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,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sendMap := make(map[string]interface{})
|
|
|
+ sendMap["template_id"] = utils.TEMPLATE_ID_BY_PRODUCT
|
|
|
+ sendMap["first"] = fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
|
|
|
+ sendMap["keyword1"] = map[string]interface{}{"value": fmt.Sprintf("弘则%s", report.ClassifyNameSecond), "color": "#173177"}
|
|
|
+ sendMap["keyword2"] = map[string]interface{}{"value": report.ContentSub, "color": "#173177"}
|
|
|
+ sendMap["keyword3"] = map[string]interface{}{"value": report.PublishTime, "color": "#173177"}
|
|
|
+ sendMap["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"}
|
|
|
+ sendMap["miniprogram"] = map[string]string{
|
|
|
+ "appid": utils.WX_MINI_APPID,
|
|
|
+ "pagepath": fmt.Sprintf("pages-report/reportDetail/index?id=%d", report.Id),
|
|
|
+ }
|
|
|
+ err = services.SendMultiTemplateMsg(sendMap, openIds, 1)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "发送失败"
|
|
|
+ br.ErrMsg = "发送失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ br.Ret = 200
|
|
|
+ br.Msg = "发送成功"
|
|
|
+ br.Success = true
|
|
|
}
|