935090232@qq.com
2021-01-16 7ac435d93858f6658248c2d4ea763ce1cfd0f012
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.matrix.system.wechart.templateMsg.Task;
 
 
import com.alibaba.fastjson.JSONObject;
import com.matrix.biz.bean.BizUser;
import com.matrix.biz.service.BizUserService;
import com.matrix.component.tools.HttpClientUtil;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.rr.GlueFactory;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.hive.bean.SysProjServices;
import com.matrix.system.hive.bean.SysVipInfo;
import com.matrix.system.hive.dao.SysProjServicesDao;
import com.matrix.system.hive.dao.SysShopInfoDao;
import com.matrix.system.hive.dao.SysVipInfoDao;
import com.matrix.system.shopXcx.api.WeChatGzhApiTools;
import com.matrix.system.shopXcx.bean.ShopWxtemplateMsg;
import com.matrix.system.shopXcx.dao.ShopWxtemplateMsgDao;
import com.matrix.system.wechart.templateMsg.GzhTemplateMessagePojo;
import com.matrix.system.wechart.templateMsg.UniformMsgPojo;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import io.swagger.models.auth.In;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.List;
import java.util.Map;
 
/**
 * 小程序统一消息模板消息发送提醒
 * @author jyy
 */
@Component
public class UniformMsgSentTask implements DeliverCallback {
 
    @Autowired
    private ShopWxtemplateMsgDao shopWxtemplateMsgDao;
 
    @Autowired
    private BusParameterSettingsDao busParameterSettingsDao;
 
    /**
     * 发送模板消息需要传JSONO字符串作为格式
     * 例如:{"companyId":17}
     * companyId 是必须属性
     * @param consumerTag
     * @param message
     * @throws IOException
     */
    @Override
    public void handle(String consumerTag, Delivery message) throws IOException {
        try {
 
 
            String messages = new String(message.getBody(), "UTF-8");
            JSONObject messageJsonParam=JSONObject.parseObject(messages);
 
            if(!messageJsonParam.containsKey("companyId")||(!messageJsonParam.containsKey("templateCode"))){
                LogUtil.error("小程序消息推送参数格式异常发送模板消息需要传JSONO字符串作为格式 例如:{\"companyId\":17,\"templateCode\":10000} companyId 、templateCode 是必须属性");
                return;
            }
            Long companyId = Long.parseLong( messageJsonParam.get("companyId").toString());
            Integer templateCode = Integer.parseInt( messageJsonParam.get("templateCode").toString());
 
            //获取公司微信配置参数
            BusParameterSettings xcxAppId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId);
            BusParameterSettings xcxSecret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_SECRET, companyId);
            BusParameterSettings gzhAppid = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.GZH_APPID, companyId);
            //获取消息模板
            ShopWxtemplateMsg template = shopWxtemplateMsgDao.selectByCode(templateCode,companyId);
            //校验参数
            if(xcxAppId==null||xcxSecret==null||gzhAppid==null||template==null){
                LogUtil.error("小程序消息推送配置缺失xcxAppId="+xcxAppId+";xcxSecret="+xcxSecret+";gzhAppid="+gzhAppid+";template="+template);
                return;
            }
            //获取模板动态构建类
            TemplateMessageBulder templateMessageBulder = (TemplateMessageBulder) GlueFactory.getInstance().loadInstance(template.getTemplateClass());
            //为模板动态类装备必要的参数
            Map<String, Object> bulderParam = new HashedMap();
            bulderParam.put("xcxAppId", xcxAppId.getParamValue());
            bulderParam.put("gzhAppid", gzhAppid.getParamValue());
            bulderParam.put("template_id", template.getUuid());
            bulderParam.put("messageJsonParam", messageJsonParam);
 
 
            //调用模板计算出消息体
            Map msgResult = templateMessageBulder.buildMsg(bulderParam);
 
            if(msgResult.containsKey("error")){
                //错误消息处理
                LogUtil.error("模板消息发送失败:"+msgResult.get("error"));
                return;
            }else{
                //正常返回消息
                List msgList = (List) msgResult.get("msgList");
                //获取acceToken
                String ACCESS_TOKEN = WeChatGzhApiTools.getAccessToken(xcxAppId.getParamValue(), xcxSecret.getParamValue());
                String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN".replaceAll("ACCESS_TOKEN", ACCESS_TOKEN);
                for (Object msg : msgList) {
                    UniformMsgPojo uniformMsgPojo = (UniformMsgPojo) msg;
                    //推送消息到微信
                    JSONObject result = HttpClientUtil.sendPostWithJson(url, JSONObject.toJSON(uniformMsgPojo).toString());
                    //微信返回值
                    LogUtil.debug("微信小程序模板消息推送结果:" + result.toString());
                }
            }
 
        } catch (Exception e) {
            LogUtil.error("消费者执行异常", e);
        }
 
    }
}