From eef7ebb4200b5e0bc749db59b9fe9ff8ea90ef71 Mon Sep 17 00:00:00 2001 From: shiyang <316555390@qq.com> Date: Tue, 21 Dec 2021 16:47:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E6=8E=A8=E9=80=81=E4=BC=98?= =?UTF-8?q?=E5=8C=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ArticleController.java | 10 +- .../lyms/talkonlineweb/task/PushArticleTask.java | 166 +++++++++++---------- .../talkonlineweb/task/PushArticleTaskData.java | 6 + .../com/lyms/talkonlineweb/util/WeiXinUtil.java | 40 ++++- 4 files changed, 137 insertions(+), 85 deletions(-) diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java index 7e787cd..f6a43d9 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java @@ -393,7 +393,7 @@ public class ArticleController { /** * 登录小程序后获取小程序关联公众号的OpenId - * + * 同时检查是否关注了公众号 * @return */ @GetMapping("getGZOpenId") @@ -403,9 +403,13 @@ public class ArticleController { if(StringUtil.isNotEmpty(patient.getCode())&&null!=patient.getId()){ String openid = WeiXinUtil.getWxGzOpenId(patient.getCode()); if(StringUtil.isNotEmpty(openid)){ + patient.setGzopenid(openid); //更新到登录患者的gzopenid - boolean b = lymsPatientService.updateById(patient); - if(b){ + lymsPatientService.updateById(patient); + //检查是否关注了公众号 + String subscribe = WeiXinUtil.getIsWxGZH(openid); + if(StringUtil.isNotEmpty(subscribe)){ + baseResponse.setObject(subscribe); baseResponse.setErrormsg("成功"); return baseResponse; } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java index 8dc9840..d67303c 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java @@ -52,107 +52,111 @@ public class PushArticleTask { log.debug("开始给患者推送文章>>>>>>"); //从LymsPushMessages记录表查询今天0点筛选出要推送的文章 QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.apply("TO_DAYS(created_time) = TO_DAYS(NOW())"); + queryWrapper.apply("TO_DAYS(created_time) = TO_DAYS(NOW()) and state=0"); List lymsPushMessages=lymsPushMessagesService.list(queryWrapper); for (LymsPushMessages lymsPushMessage : lymsPushMessages) { - //推送的文章在LymsPushedart做记录。用于患者读取推送的文章 - LymsPushedart pushedart=new LymsPushedart(); - pushedart.setPid(lymsPushMessage.getPid()); - pushedart.setAid(lymsPushMessage.getAid()); - pushedart.setCreatedtime(new Date()); - pushedart.setIsread((byte) 0); - //PushedartId(如果null表示需要推送文章,否则需要推送短消息) - if(null!=lymsPushMessage.getPushedartId()){ - //获取token失败 - if(StringUtil.isEmpty(token)){ - //更新到LymsPushMessages记录 - lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) - lymsPushMessage.setRemark("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败。。。"); - lymsPushMessage.setPushTime(new Date()); - lymsPushMessagesService.updateById(lymsPushMessage); - //更新到LymsPushedart记录 - if(lymsPushMessage.getIsweixinOne()==1){ - pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败) - }else { - pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败) - } - lymsPushedartService.updateById(pushedart); - log.info("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败。。。"); - continue; - } - //公众号openId为空 - if(StringUtil.isEmpty(lymsPushMessage.getGzopenid())){ - //更新到LymsPushMessages记录 - lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) - lymsPushMessage.setRemark(lymsPushMessage.getPname()+":公众号openId为空。。。"); - lymsPushMessage.setPushTime(new Date()); - lymsPushMessagesService.updateById(lymsPushMessage); - //更新到LymsPushedart记录 - if(lymsPushMessage.getIsweixinOne()==1){ - pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败) - }else { - pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败) - } - lymsPushedartService.updateById(pushedart); - log.info(lymsPushMessage.getPname()+":公众号openId为空。。。"); - continue; - } - //LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态 - pushedart.setId(lymsPushMessage.getPushedartId()); - if(lymsPushMessage.getIsweixinOne()==1){ - //推送微信消息1 - Map map=new HashMap<>(); - map.put("first",new DataEntity(lymsPushMessage.getWeixTextOne(),"#173177")); - map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177")); - map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177")); - map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177")); - map.put("remark",new DataEntity("预祝您早日康复","#173177")); - Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map); - if(null==code||code!=0){ + try { + //推送的文章在LymsPushedart做记录。用于患者读取推送的文章 + LymsPushedart pushedart=new LymsPushedart(); + pushedart.setPid(lymsPushMessage.getPid()); + pushedart.setAid(lymsPushMessage.getAid()); + pushedart.setCreatedtime(new Date()); + pushedart.setIsread((byte) 0); + //PushedartId(如果null表示需要推送文章,否则需要推送短消息) + if(null!=lymsPushMessage.getPushedartId()){ + //LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态 + pushedart.setId(lymsPushMessage.getPushedartId()); + //获取token失败 + if(StringUtil.isEmpty(token)){ //更新到LymsPushMessages记录 lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) - lymsPushMessage.setRemark("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + lymsPushMessage.setRemark("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败!"); lymsPushMessage.setPushTime(new Date()); lymsPushMessagesService.updateById(lymsPushMessage); //更新到LymsPushedart记录 - pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败) + if(lymsPushMessage.getIsweixinOne()==1){ + pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败) + }else { + pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败) + } lymsPushedartService.updateById(pushedart); - log.info("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + log.info("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败!"); continue; } - //成功标记记录1 - pushedart.setIsweixinone(1);//是否推送短消息1(0否,1是 2推送失败) - }else if(lymsPushMessage.getIsweixinTwo()==1){ - //推送微信消息2 - Map map=new HashMap<>(); - map.put("first",new DataEntity(lymsPushMessage.getWeixTextTwo(),"#173177")); - map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177")); - map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177")); - map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177")); - map.put("remark",new DataEntity("预祝您早日康复","#173177")); - Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map); - if(null==code||code!=0){ + //公众号openId为空 + if(StringUtil.isEmpty(lymsPushMessage.getGzopenid())){ //更新到LymsPushMessages记录 lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) - lymsPushMessage.setRemark("推送短消息2失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + lymsPushMessage.setRemark(lymsPushMessage.getPname()+":公众号openId为空!"); lymsPushMessage.setPushTime(new Date()); lymsPushMessagesService.updateById(lymsPushMessage); //更新到LymsPushedart记录 - pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败) + if(lymsPushMessage.getIsweixinOne()==1){ + pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败) + }else { + pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败) + } lymsPushedartService.updateById(pushedart); - log.info("推送短消息2失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + log.info(lymsPushMessage.getPname()+":公众号openId为空!"); continue; } - //成功标记记录2 - pushedart.setIsweixintwo(1);//是否推送短消息1(0否,1是 2推送失败) + if(lymsPushMessage.getIsweixinOne()==1){ + //推送微信消息1 + Map map=new HashMap<>(); + map.put("first",new DataEntity(lymsPushMessage.getWeixTextOne(),"#173177")); + map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177")); + map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177")); + map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177")); + map.put("remark",new DataEntity("预祝您早日康复","#173177")); + Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map); + if(null==code||code!=0){ + //更新到LymsPushMessages记录 + lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) + lymsPushMessage.setRemark("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + lymsPushMessage.setPushTime(new Date()); + lymsPushMessagesService.updateById(lymsPushMessage); + //更新到LymsPushedart记录 + pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败) + lymsPushedartService.updateById(pushedart); + log.info("推送短消息1失败!"+lymsPushMessage.getPname()+"; code:"+code); + continue; + } + //成功标记记录1 + pushedart.setIsweixinone(1);//是否推送短消息1(0否,1是 2推送失败) + }else if(lymsPushMessage.getIsweixinTwo()==1){ + //推送微信消息2 + Map map=new HashMap<>(); + map.put("first",new DataEntity(lymsPushMessage.getWeixTextTwo(),"#173177")); + map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177")); + map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177")); + map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177")); + map.put("remark",new DataEntity("预祝您早日康复","#173177")); + Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map); + if(null==code||code!=0){ + //更新到LymsPushMessages记录 + lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) + lymsPushMessage.setRemark("推送短消息2失败!"+lymsPushMessage.getPname()+"; code:"+code); + lymsPushMessage.setPushTime(new Date()); + lymsPushMessagesService.updateById(lymsPushMessage); + //更新到LymsPushedart记录 + pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败) + lymsPushedartService.updateById(pushedart); + log.info("推送短消息2失败!"+lymsPushMessage.getPname()+"; code:"+code); + continue; + } + //成功标记记录2 + pushedart.setIsweixintwo(1);//是否推送短消息1(0否,1是 2推送失败) + } } + //插入or更新到LymsPushedart记录 + lymsPushedartService.saveOrUpdate(pushedart); + //更新到LymsPushMessages记录 + lymsPushMessage.setState(1);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) + lymsPushMessage.setPushTime(new Date()); + lymsPushMessagesService.updateById(lymsPushMessage); + } catch (Exception e) { + e.printStackTrace(); } - //插入or更新到LymsPushedart记录 - lymsPushedartService.saveOrUpdate(pushedart); - //更新到LymsPushMessages记录 - lymsPushMessage.setState(1);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因) - lymsPushMessage.setPushTime(new Date()); - lymsPushMessagesService.updateById(lymsPushMessage); } } } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java index c68d70e..76b28d5 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java @@ -16,6 +16,7 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import org.springframework.util.unit.DataUnit; import java.util.*; @@ -78,6 +79,11 @@ public class PushArticleTaskData { } } //保存到要推送的表lyms_push_messages + List list = lymsPushMessagesService.list(new QueryWrapper<>(lymsPushMessages)); + if(!CollectionUtils.isEmpty(list)){ + log.info("要推送的文章已存在不需要再次推送>>>>>>"); + continue; + } lymsPushMessagesService.save(lymsPushMessages); } catch (BeansException e) { e.printStackTrace(); diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java index fbdd9de..e1c9111 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java @@ -3,6 +3,7 @@ package com.lyms.talkonlineweb.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.lyms.talkonlineweb.result.BaseResponse; +import com.lyms.talkonlineweb.task.AccessTokenServlet; import lombok.extern.log4j.Log4j2; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -97,7 +98,8 @@ public class WeiXinUtil { // 接口地址 String reqUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constant.GZ_APP_ID + "&secret=" + Constant.GZ_SECRET + "&code=" + code + "&grant_type="+Constant.GZ_AUTH; log.info(reqUrl); - String result = HttpUtil.getData(reqUrl); +// String result = HttpUtil.getData(reqUrl); + String result = repeatDoGetPost(reqUrl,"GET",null); log.info("result :" + result); if (StringUtils.isEmpty(result)) { log.info("取微信用户的公众号openid : 请求返回null..." ); @@ -114,6 +116,42 @@ public class WeiXinUtil { } /** + * 检查用户是否关注了公众号 + * + * @param openId 登录时获取的code + * @return 微信方获取openid + */ + public static String getIsWxGZH(String openId) + { + if (StringUtils.isEmpty(openId)) { + log.info("openId:null"); + return null; + } + if (StringUtils.isEmpty(AccessTokenServlet.accessToken)) { + log.info("accessToken:null"); + return null; + } + try { + // 接口地址 + String reqUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + AccessTokenServlet.accessToken + "&openid=" + openId + "&lang=zh_CN"; + log.info(reqUrl); + String result = repeatDoGetPost(reqUrl,"GET",null); + log.info("result :" + result); + if (StringUtils.isEmpty(result)) { + log.info("检查用户是否关注了公众号请求 : 返回null..." ); + return null; + } + Map resultMap = JSON.parseObject(result, HashMap.class); + if (resultMap.containsKey("subscribe")) { + return resultMap.get("subscribe").toString(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** * 网络超时-重新发送3次 */ public static String repeatDoGetPost(String sendMsgApi,String type,Map paramMap) { -- 1.8.3.1