Commit eef7ebb4200b5e0bc749db59b9fe9ff8ea90ef71

Authored by shiyang
1 parent 4ffbbe003d
Exists in master

文章推送优化2

Showing 4 changed files with 137 additions and 85 deletions

talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java View file @ eef7ebb
... ... @@ -393,7 +393,7 @@
393 393  
394 394 /**
395 395 * 登录小程序后获取小程序关联公众号的OpenId
396   - *
  396 + * 同时检查是否关注了公众号
397 397 * @return
398 398 */
399 399 @GetMapping("getGZOpenId")
400 400  
... ... @@ -403,9 +403,13 @@
403 403 if(StringUtil.isNotEmpty(patient.getCode())&&null!=patient.getId()){
404 404 String openid = WeiXinUtil.getWxGzOpenId(patient.getCode());
405 405 if(StringUtil.isNotEmpty(openid)){
  406 + patient.setGzopenid(openid);
406 407 //更新到登录患者的gzopenid
407   - boolean b = lymsPatientService.updateById(patient);
408   - if(b){
  408 + lymsPatientService.updateById(patient);
  409 + //检查是否关注了公众号
  410 + String subscribe = WeiXinUtil.getIsWxGZH(openid);
  411 + if(StringUtil.isNotEmpty(subscribe)){
  412 + baseResponse.setObject(subscribe);
409 413 baseResponse.setErrormsg("成功");
410 414 return baseResponse;
411 415 }
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java View file @ eef7ebb
... ... @@ -52,107 +52,111 @@
52 52 log.debug("开始给患者推送文章>>>>>>");
53 53 //从LymsPushMessages记录表查询今天0点筛选出要推送的文章
54 54 QueryWrapper<LymsPushMessages> queryWrapper = new QueryWrapper<>();
55   - queryWrapper.apply("TO_DAYS(created_time) = TO_DAYS(NOW())");
  55 + queryWrapper.apply("TO_DAYS(created_time) = TO_DAYS(NOW()) and state=0");
56 56 List<LymsPushMessages> lymsPushMessages=lymsPushMessagesService.list(queryWrapper);
57 57 for (LymsPushMessages lymsPushMessage : lymsPushMessages) {
58   - //推送的文章在LymsPushedart做记录。用于患者读取推送的文章
59   - LymsPushedart pushedart=new LymsPushedart();
60   - pushedart.setPid(lymsPushMessage.getPid());
61   - pushedart.setAid(lymsPushMessage.getAid());
62   - pushedart.setCreatedtime(new Date());
63   - pushedart.setIsread((byte) 0);
64   - //PushedartId(如果null表示需要推送文章,否则需要推送短消息)
65   - if(null!=lymsPushMessage.getPushedartId()){
66   - //获取token失败
67   - if(StringUtil.isEmpty(token)){
68   - //更新到LymsPushMessages记录
69   - lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
70   - lymsPushMessage.setRemark("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败。。。");
71   - lymsPushMessage.setPushTime(new Date());
72   - lymsPushMessagesService.updateById(lymsPushMessage);
73   - //更新到LymsPushedart记录
74   - if(lymsPushMessage.getIsweixinOne()==1){
75   - pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
76   - }else {
77   - pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
78   - }
79   - lymsPushedartService.updateById(pushedart);
80   - log.info("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败。。。");
81   - continue;
82   - }
83   - //公众号openId为空
84   - if(StringUtil.isEmpty(lymsPushMessage.getGzopenid())){
85   - //更新到LymsPushMessages记录
86   - lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
87   - lymsPushMessage.setRemark(lymsPushMessage.getPname()+":公众号openId为空。。。");
88   - lymsPushMessage.setPushTime(new Date());
89   - lymsPushMessagesService.updateById(lymsPushMessage);
90   - //更新到LymsPushedart记录
91   - if(lymsPushMessage.getIsweixinOne()==1){
92   - pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
93   - }else {
94   - pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
95   - }
96   - lymsPushedartService.updateById(pushedart);
97   - log.info(lymsPushMessage.getPname()+":公众号openId为空。。。");
98   - continue;
99   - }
100   - //LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态
101   - pushedart.setId(lymsPushMessage.getPushedartId());
102   - if(lymsPushMessage.getIsweixinOne()==1){
103   - //推送微信消息1
104   - Map<String,Object> map=new HashMap<>();
105   - map.put("first",new DataEntity(lymsPushMessage.getWeixTextOne(),"#173177"));
106   - map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177"));
107   - map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177"));
108   - map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177"));
109   - map.put("remark",new DataEntity("预祝您早日康复","#173177"));
110   - Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map);
111   - if(null==code||code!=0){
  58 + try {
  59 + //推送的文章在LymsPushedart做记录。用于患者读取推送的文章
  60 + LymsPushedart pushedart=new LymsPushedart();
  61 + pushedart.setPid(lymsPushMessage.getPid());
  62 + pushedart.setAid(lymsPushMessage.getAid());
  63 + pushedart.setCreatedtime(new Date());
  64 + pushedart.setIsread((byte) 0);
  65 + //PushedartId(如果null表示需要推送文章,否则需要推送短消息)
  66 + if(null!=lymsPushMessage.getPushedartId()){
  67 + //LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态
  68 + pushedart.setId(lymsPushMessage.getPushedartId());
  69 + //获取token失败
  70 + if(StringUtil.isEmpty(token)){
112 71 //更新到LymsPushMessages记录
113 72 lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
114   - lymsPushMessage.setRemark("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code);
  73 + lymsPushMessage.setRemark("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败!");
115 74 lymsPushMessage.setPushTime(new Date());
116 75 lymsPushMessagesService.updateById(lymsPushMessage);
117 76 //更新到LymsPushedart记录
118   - pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
  77 + if(lymsPushMessage.getIsweixinOne()==1){
  78 + pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
  79 + }else {
  80 + pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
  81 + }
119 82 lymsPushedartService.updateById(pushedart);
120   - log.info("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code);
  83 + log.info("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败!");
121 84 continue;
122 85 }
123   - //成功标记记录1
124   - pushedart.setIsweixinone(1);//是否推送短消息1(0否,1是 2推送失败)
125   - }else if(lymsPushMessage.getIsweixinTwo()==1){
126   - //推送微信消息2
127   - Map<String,Object> map=new HashMap<>();
128   - map.put("first",new DataEntity(lymsPushMessage.getWeixTextTwo(),"#173177"));
129   - map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177"));
130   - map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177"));
131   - map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177"));
132   - map.put("remark",new DataEntity("预祝您早日康复","#173177"));
133   - Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map);
134   - if(null==code||code!=0){
  86 + //公众号openId为空
  87 + if(StringUtil.isEmpty(lymsPushMessage.getGzopenid())){
135 88 //更新到LymsPushMessages记录
136 89 lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
137   - lymsPushMessage.setRemark("推送短消息2失败。。。"+lymsPushMessage.getPname()+"; code:"+code);
  90 + lymsPushMessage.setRemark(lymsPushMessage.getPname()+":公众号openId为空!");
138 91 lymsPushMessage.setPushTime(new Date());
139 92 lymsPushMessagesService.updateById(lymsPushMessage);
140 93 //更新到LymsPushedart记录
141   - pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
  94 + if(lymsPushMessage.getIsweixinOne()==1){
  95 + pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
  96 + }else {
  97 + pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
  98 + }
142 99 lymsPushedartService.updateById(pushedart);
143   - log.info("推送短消息2失败。。。"+lymsPushMessage.getPname()+"; code:"+code);
  100 + log.info(lymsPushMessage.getPname()+":公众号openId为空!");
144 101 continue;
145 102 }
146   - //成功标记记录2
147   - pushedart.setIsweixintwo(1);//是否推送短消息1(0否,1是 2推送失败)
  103 + if(lymsPushMessage.getIsweixinOne()==1){
  104 + //推送微信消息1
  105 + Map<String,Object> map=new HashMap<>();
  106 + map.put("first",new DataEntity(lymsPushMessage.getWeixTextOne(),"#173177"));
  107 + map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177"));
  108 + map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177"));
  109 + map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177"));
  110 + map.put("remark",new DataEntity("预祝您早日康复","#173177"));
  111 + Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map);
  112 + if(null==code||code!=0){
  113 + //更新到LymsPushMessages记录
  114 + lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
  115 + lymsPushMessage.setRemark("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code);
  116 + lymsPushMessage.setPushTime(new Date());
  117 + lymsPushMessagesService.updateById(lymsPushMessage);
  118 + //更新到LymsPushedart记录
  119 + pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
  120 + lymsPushedartService.updateById(pushedart);
  121 + log.info("推送短消息1失败!"+lymsPushMessage.getPname()+"; code:"+code);
  122 + continue;
  123 + }
  124 + //成功标记记录1
  125 + pushedart.setIsweixinone(1);//是否推送短消息1(0否,1是 2推送失败)
  126 + }else if(lymsPushMessage.getIsweixinTwo()==1){
  127 + //推送微信消息2
  128 + Map<String,Object> map=new HashMap<>();
  129 + map.put("first",new DataEntity(lymsPushMessage.getWeixTextTwo(),"#173177"));
  130 + map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177"));
  131 + map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177"));
  132 + map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177"));
  133 + map.put("remark",new DataEntity("预祝您早日康复","#173177"));
  134 + Integer code= WeiXinUtil.SendWeChatMsg(token,lymsPushMessage.getGzopenid(),template_id,map);
  135 + if(null==code||code!=0){
  136 + //更新到LymsPushMessages记录
  137 + lymsPushMessage.setState(2);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
  138 + lymsPushMessage.setRemark("推送短消息2失败!"+lymsPushMessage.getPname()+"; code:"+code);
  139 + lymsPushMessage.setPushTime(new Date());
  140 + lymsPushMessagesService.updateById(lymsPushMessage);
  141 + //更新到LymsPushedart记录
  142 + pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
  143 + lymsPushedartService.updateById(pushedart);
  144 + log.info("推送短消息2失败!"+lymsPushMessage.getPname()+"; code:"+code);
  145 + continue;
  146 + }
  147 + //成功标记记录2
  148 + pushedart.setIsweixintwo(1);//是否推送短消息1(0否,1是 2推送失败)
  149 + }
148 150 }
  151 + //插入or更新到LymsPushedart记录
  152 + lymsPushedartService.saveOrUpdate(pushedart);
  153 + //更新到LymsPushMessages记录
  154 + lymsPushMessage.setState(1);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
  155 + lymsPushMessage.setPushTime(new Date());
  156 + lymsPushMessagesService.updateById(lymsPushMessage);
  157 + } catch (Exception e) {
  158 + e.printStackTrace();
149 159 }
150   - //插入or更新到LymsPushedart记录
151   - lymsPushedartService.saveOrUpdate(pushedart);
152   - //更新到LymsPushMessages记录
153   - lymsPushMessage.setState(1);//推送状态:0待推送(9点开始推送) 1成功 2失败。(在备注写失败原因)
154   - lymsPushMessage.setPushTime(new Date());
155   - lymsPushMessagesService.updateById(lymsPushMessage);
156 160 }
157 161 }
158 162 }
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java View file @ eef7ebb
... ... @@ -16,6 +16,7 @@
16 16 import org.springframework.beans.factory.annotation.Autowired;
17 17 import org.springframework.scheduling.annotation.Scheduled;
18 18 import org.springframework.stereotype.Component;
  19 +import org.springframework.util.CollectionUtils;
19 20 import org.springframework.util.unit.DataUnit;
20 21  
21 22 import java.util.*;
... ... @@ -78,6 +79,11 @@
78 79 }
79 80 }
80 81 //保存到要推送的表lyms_push_messages
  82 + List<LymsPushMessages> list = lymsPushMessagesService.list(new QueryWrapper<>(lymsPushMessages));
  83 + if(!CollectionUtils.isEmpty(list)){
  84 + log.info("要推送的文章已存在不需要再次推送>>>>>>");
  85 + continue;
  86 + }
81 87 lymsPushMessagesService.save(lymsPushMessages);
82 88 } catch (BeansException e) {
83 89 e.printStackTrace();
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java View file @ eef7ebb
... ... @@ -3,6 +3,7 @@
3 3 import com.alibaba.fastjson.JSON;
4 4 import com.alibaba.fastjson.JSONObject;
5 5 import com.lyms.talkonlineweb.result.BaseResponse;
  6 +import com.lyms.talkonlineweb.task.AccessTokenServlet;
6 7 import lombok.extern.log4j.Log4j2;
7 8 import org.springframework.util.CollectionUtils;
8 9 import org.springframework.util.StringUtils;
... ... @@ -97,7 +98,8 @@
97 98 // 接口地址
98 99 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;
99 100 log.info(reqUrl);
100   - String result = HttpUtil.getData(reqUrl);
  101 +// String result = HttpUtil.getData(reqUrl);
  102 + String result = repeatDoGetPost(reqUrl,"GET",null);
101 103 log.info("result :" + result);
102 104 if (StringUtils.isEmpty(result)) {
103 105 log.info("取微信用户的公众号openid : 请求返回null..." );
... ... @@ -106,6 +108,42 @@
106 108 Map<String, Object> resultMap = JSON.parseObject(result, HashMap.class);
107 109 if (resultMap.containsKey(OPEN_ID_KEY)) {
108 110 return resultMap.get(OPEN_ID_KEY).toString();
  111 + }
  112 + } catch (Exception e) {
  113 + e.printStackTrace();
  114 + }
  115 + return null;
  116 + }
  117 +
  118 + /**
  119 + * 检查用户是否关注了公众号
  120 + *
  121 + * @param openId 登录时获取的code
  122 + * @return 微信方获取openid
  123 + */
  124 + public static String getIsWxGZH(String openId)
  125 + {
  126 + if (StringUtils.isEmpty(openId)) {
  127 + log.info("openId:null");
  128 + return null;
  129 + }
  130 + if (StringUtils.isEmpty(AccessTokenServlet.accessToken)) {
  131 + log.info("accessToken:null");
  132 + return null;
  133 + }
  134 + try {
  135 + // 接口地址
  136 + String reqUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + AccessTokenServlet.accessToken + "&openid=" + openId + "&lang=zh_CN";
  137 + log.info(reqUrl);
  138 + String result = repeatDoGetPost(reqUrl,"GET",null);
  139 + log.info("result :" + result);
  140 + if (StringUtils.isEmpty(result)) {
  141 + log.info("检查用户是否关注了公众号请求 : 返回null..." );
  142 + return null;
  143 + }
  144 + Map<String, Object> resultMap = JSON.parseObject(result, HashMap.class);
  145 + if (resultMap.containsKey("subscribe")) {
  146 + return resultMap.get("subscribe").toString();
109 147 }
110 148 } catch (Exception e) {
111 149 e.printStackTrace();