|  |  |  | 
|---|
|  |  |  | 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.TemplateNameConstance; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | @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")){ | 
|---|
|  |  |  | LogUtil.error("小程序消息推送参数格式异常发送模板消息需要传JSONO字符串作为格式 例如:{\"companyId\":17} companyId 是必须属性"); | 
|---|
|  |  |  | 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(TemplateNameConstance.GZH_YYTX,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")); | 
|---|
|  |  |  | LogUtil.error("模板消息发送内容:"+msgResult.get("error")); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //正常返回消息 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | LogUtil.debug("微信小程序模板消息推送:" + JSONObject.toJSON(uniformMsgPojo).toString()); | 
|---|
|  |  |  | //推送消息到微信 | 
|---|
|  |  |  | JSONObject result = HttpClientUtil.sendPostWithJson(url, JSONObject.toJSON(uniformMsgPojo).toString()); | 
|---|
|  |  |  | //微信返回值 | 
|---|
|  |  |  | LogUtil.debug("微信小程序模板消息推送结果:" + result.toString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | LogUtil.error("消费者执行异常", e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|