Bläddra i källkod

优化 模板消息推送成功

java110 6 år sedan
förälder
incheckning
b85a7a62ad

+ 4 - 0
java110-bean/src/main/java/com/java110/dto/wechatMenu/WechatMenuDto.java

@@ -17,6 +17,10 @@ public class WechatMenuDto extends PageDto implements Serializable {
 
     public static final String MENU_LEVEL_ONE = "101"; //一级菜单
     public static final String MENU_LEVEL_TWO = "202"; //二级菜单
+
+    public static final String MENU_TYPE_VIEW = "view"; // 连接
+
+    public static final String MENU_TYPE_MINIPROGRAM = "miniprogram";// 小程序
     private String pagepath;
     private String appId;
     private String menuLevel;

+ 5 - 0
java110-utils/src/main/java/com/java110/utils/constant/ServiceCodeWechatMenuConstant.java

@@ -27,5 +27,10 @@ public class ServiceCodeWechatMenuConstant {
      */
     public static final String LIST_WECHATMENUS = "smallWeChat.listWechatMenus";
 
+    /**
+     * 发布公众号菜单
+     */
+    public static final String PUBLIC_WECHATMENU = "smallWeChat.publicWechatMenu";
+
 
 }

+ 2 - 0
java110-utils/src/main/java/com/java110/utils/constant/WechatConstant.java

@@ -42,4 +42,6 @@ public class WechatConstant {
 
     public static final String OPEN_AUTH = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URL&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
 
+    //创建菜单
+    public static final String CREATE_MENU = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
 }

+ 170 - 0
service-api/src/main/java/com/java110/api/listener/smallWeChat/PublishWechatMenuListener.java

@@ -0,0 +1,170 @@
+package com.java110.api.listener.smallWeChat;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.java110.api.bmo.smallWeChat.IWechatMenuBMO;
+import com.java110.api.listener.AbstractServiceApiPlusListener;
+import com.java110.core.annotation.Java110Listener;
+import com.java110.core.context.DataFlowContext;
+import com.java110.core.event.service.api.ServiceDataFlowEvent;
+import com.java110.core.factory.WechatFactory;
+import com.java110.core.smo.store.ISmallWeChatInnerServiceSMO;
+import com.java110.core.smo.wechatMenu.IWechatMenuInnerServiceSMO;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
+import com.java110.dto.wechatMenu.WechatMenuDto;
+import com.java110.utils.constant.ServiceCodeWechatMenuConstant;
+import com.java110.utils.constant.WechatConstant;
+import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
+import com.java110.vo.ResultVo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.List;
+
+/**
+ * 保存商户侦听
+ * add by wuxw 2019-06-30
+ */
+@Java110Listener("publishWechatMenuListener")
+public class PublishWechatMenuListener extends AbstractServiceApiPlusListener {
+
+    @Autowired
+    private IWechatMenuBMO wechatMenuBMOImpl;
+
+    @Autowired
+    private RestTemplate restTemplateNoLoadBalanced;
+
+    @Autowired
+    private IWechatMenuInnerServiceSMO wechatMenuInnerServiceSMOImpl;
+
+    @Autowired
+    private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
+
+
+    @Override
+    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {
+        //Assert.hasKeyAndValue(reqJson, "xxx", "xxx");
+        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");
+    }
+
+    /**
+     * {
+     * "button":[
+     * {
+     * "type":"click",
+     * "name":"今日歌曲",
+     * "key":"V1001_TODAY_MUSIC"
+     * },
+     * {
+     * "name":"菜单",
+     * "sub_button":[
+     * {
+     * "type":"view",
+     * "name":"搜索",
+     * "url":"http://www.soso.com/"
+     * },
+     * {
+     * "type":"miniprogram",
+     * "name":"wxa",
+     * "url":"http://mp.weixin.qq.com",
+     * "appid":"wx286b93c14bbf93aa",
+     * "pagepath":"pages/lunar/index"
+     * },
+     * {
+     * "type":"click",
+     * "name":"赞一下我们",
+     * "key":"V1001_GOOD"
+     * }]
+     * }]
+     * }
+     *
+     * @param event   事件对象
+     * @param context 数据上文对象
+     * @param reqJson 请求报文
+     */
+    @Override
+    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
+        //查询一级菜单
+        WechatMenuDto wechatMenuDto = BeanConvertUtil.covertBean(reqJson, WechatMenuDto.class);
+        wechatMenuDto.setMenuLevel(WechatMenuDto.MENU_LEVEL_ONE);
+        List<WechatMenuDto> wechatMenuDtos = wechatMenuInnerServiceSMOImpl.queryWechatMenus(wechatMenuDto);
+
+        if (wechatMenuDtos == null || wechatMenuDtos.size() < 1 || wechatMenuDtos.size() > 3) {
+            throw new IllegalArgumentException("一级菜单数量为" + wechatMenuDtos.size() + "不符合公众号菜单数量要求");
+        }
+        JSONObject wechatMenuObj = new JSONObject();
+        JSONArray button = new JSONArray();
+        for (WechatMenuDto tmpWechatMenuDto : wechatMenuDtos) {
+            wechatMenuDto = new WechatMenuDto();
+            wechatMenuDto.setMenuLevel(WechatMenuDto.MENU_LEVEL_TWO);
+            wechatMenuDto.setCommunityId(tmpWechatMenuDto.getCommunityId());
+            wechatMenuDto.setParentMenuId(tmpWechatMenuDto.getWechatMenuId());
+            List<WechatMenuDto> tmpWechatMenuDtos = wechatMenuInnerServiceSMOImpl.queryWechatMenus(wechatMenuDto);
+            JSONObject menuButton = new JSONObject();
+            menuButton.put("name", tmpWechatMenuDto.getMenuName());
+            if (tmpWechatMenuDtos != null && tmpWechatMenuDtos.size() > 1) {
+                addSubMenu(menuButton, tmpWechatMenuDtos);
+                button.add(menuButton);
+                continue;
+            }
+
+            menuButton.put("type", tmpWechatMenuDto.getMenuType());
+            if (WechatMenuDto.MENU_TYPE_VIEW.equals(tmpWechatMenuDto.getMenuType())) {
+                menuButton.put("url", tmpWechatMenuDto.getMenuValue());
+            } else if (WechatMenuDto.MENU_TYPE_MINIPROGRAM.equals(tmpWechatMenuDto.getMenuType())) {
+                menuButton.put("url", tmpWechatMenuDto.getMenuValue());
+                menuButton.put("appid", tmpWechatMenuDto.getAppId());
+                menuButton.put("pagepath", tmpWechatMenuDto.getPagepath());
+            }
+            button.add(menuButton);
+        }
+        wechatMenuObj.put("button", button);
+        SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
+        smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
+        smallWeChatDto.setObjId(reqJson.getString("communityId"));
+        List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
+
+        if (smallWeChatDtos == null || smallWeChatDtos.size() < 1) {
+            throw new IllegalArgumentException("当前小区未配置公众号 请先配置");
+        }
+        String token = WechatFactory.getAccessToken(smallWeChatDtos.get(0).getAppId(), smallWeChatDtos.get(0).getAppSecret());
+        String url = WechatConstant.CREATE_MENU.replace("ACCESS_TOKEN", token);
+        ResponseEntity<String> responseEntity = restTemplateNoLoadBalanced.postForEntity(url, wechatMenuObj.toJSONString(), String.class);
+
+        JSONObject wechatOutObj = JSONObject.parseObject(responseEntity.getBody());
+        context.setResponseEntity(ResultVo.createResponseEntity(wechatOutObj.getInteger("errcode"), wechatOutObj.getString("errmsg")));
+    }
+
+    private void addSubMenu(JSONObject wechatMenuObj, List<WechatMenuDto> wechatMenuDtos) {
+        JSONArray subButtons = new JSONArray();
+        JSONObject subButton = null;
+        for (WechatMenuDto wechatMenuDto : wechatMenuDtos) {
+            subButton = new JSONObject();
+            subButton.put("name", wechatMenuDto.getMenuName());
+            subButton.put("type", wechatMenuDto.getMenuType());
+            if (WechatMenuDto.MENU_TYPE_VIEW.equals(wechatMenuDto.getMenuType())) {
+                subButton.put("url", wechatMenuDto.getMenuValue());
+            } else if (WechatMenuDto.MENU_TYPE_MINIPROGRAM.equals(wechatMenuDto.getMenuType())) {
+                subButton.put("url", wechatMenuDto.getMenuValue());
+                subButton.put("appid", wechatMenuDto.getAppId());
+                subButton.put("pagepath", wechatMenuDto.getPagepath());
+            }
+            subButtons.add(subButton);
+        }
+        wechatMenuObj.put("sub_button", subButtons);
+    }
+
+    @Override
+    public String getServiceCode() {
+        return ServiceCodeWechatMenuConstant.PUBLIC_WECHATMENU;
+    }
+
+    @Override
+    public HttpMethod getHttpMethod() {
+        return HttpMethod.POST;
+    }
+
+}

+ 41 - 8
service-job/src/main/java/com/java110/job/task/wechat/PublicWeChatPushMessageTemplate.java

@@ -3,20 +3,19 @@ package com.java110.job.task.wechat;
 import com.alibaba.fastjson.JSON;
 import com.java110.core.factory.WechatFactory;
 import com.java110.core.smo.fee.IFeeInnerServiceSMO;
+import com.java110.core.smo.smallWechatAttr.ISmallWechatAttrInnerServiceSMO;
 import com.java110.core.smo.store.ISmallWeChatInnerServiceSMO;
 import com.java110.core.smo.user.IOwnerAppUserInnerServiceSMO;
 import com.java110.dto.community.CommunityDto;
 import com.java110.dto.fee.BillOweFeeDto;
 import com.java110.dto.owner.OwnerAppUserDto;
 import com.java110.dto.smallWeChat.SmallWeChatDto;
+import com.java110.dto.smallWechatAttr.SmallWechatAttrDto;
 import com.java110.dto.task.TaskDto;
 import com.java110.entity.wechat.Content;
 import com.java110.entity.wechat.Data;
 import com.java110.entity.wechat.PropertyFeeTemplateMessage;
 import com.java110.job.quartz.TaskSystemQuartz;
-import com.java110.utils.cache.MappingCache;
-import com.java110.utils.constant.WechatConstant;
-import com.java110.utils.util.StringUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -47,6 +46,9 @@ public class PublicWeChatPushMessageTemplate extends TaskSystemQuartz {
     @Autowired
     private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
 
+    @Autowired
+    private ISmallWechatAttrInnerServiceSMO smallWechatAttrInnerServiceSMOImpl;
+
     @Autowired
     private IOwnerAppUserInnerServiceSMO ownerAppUserInnerServiceSMOImpl;
 
@@ -61,24 +63,55 @@ public class PublicWeChatPushMessageTemplate extends TaskSystemQuartz {
 
 
     @Override
-    protected void process(TaskDto taskDto) throws Exception {
+    protected void process(TaskDto taskDto) {
         logger.debug("开始执行微信模板信息推送" + taskDto.toString());
 
-        String templateId = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.KEY_PROPERTY_FEE_TEMPLATE_ID);
+        // 获取小区
+        List<CommunityDto> communityDtos = getAllCommunity();
+
+        for (CommunityDto communityDto : communityDtos) {
+            try {
+                publishMsg(taskDto, communityDto);
+            } catch (Exception e) {
+                logger.error("推送消息失败", e);
+            }
+        }
+    }
+
+    private void publishMsg(TaskDto taskDto, CommunityDto communityDto) throws Exception {
 
-        templateId = StringUtil.isEmpty(templateId) ? DEFAULT_TEMPLATE_ID : templateId;
+//
+//        String templateId = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.KEY_PROPERTY_FEE_TEMPLATE_ID);
+//
+//        templateId = StringUtil.isEmpty(templateId) ? DEFAULT_TEMPLATE_ID : templateId;
 
         //查询公众号配置
         SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
         smallWeChatDto.setWeChatType("1100");
+        smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
+        smallWeChatDto.setObjId(communityDto.getCommunityId());
         List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
 
-        if (smallWeChatDtos.size() <= 0 || smallWeChatDto == null) {
+        if (smallWeChatDto == null || smallWeChatDtos.size() <= 0) {
             logger.info("未配置微信公众号信息,定时任务执行结束");
             return;
         }
-
         SmallWeChatDto weChatDto = smallWeChatDtos.get(0);
+
+
+        SmallWechatAttrDto smallWechatAttrDto = new SmallWechatAttrDto();
+        smallWechatAttrDto.setCommunityId(communityDto.getCommunityId());
+        smallWechatAttrDto.setWechatId(weChatDto.getWeChatId());
+        smallWechatAttrDto.setSpecCd(SmallWechatAttrDto.SPEC_CD_OWE_FEE_TEMPLATE);
+        List<SmallWechatAttrDto> smallWechatAttrDtos = smallWechatAttrInnerServiceSMOImpl.querySmallWechatAttrs(smallWechatAttrDto);
+
+        if (smallWechatAttrDtos == null || smallWechatAttrDtos.size() <= 0) {
+            logger.info("未配置微信公众号消息模板");
+            return;
+        }
+
+        String templateId = smallWechatAttrDtos.get(0).getValue();
+
         String accessToken = WechatFactory.getAccessToken(weChatDto.getAppId(), weChatDto.getAppSecret());
 
         if (accessToken == null || accessToken == "") {