Commit 37a355cd1b932dd6a49acff1f1b2bbd5bf855664
1 parent
a7f712063d
Exists in
master
and in
1 other branch
文章推送逻辑+微信公众号推送
Showing 9 changed files with 227 additions and 33 deletions
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushedart.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PatientInfo.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PushArticle.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/HXTask.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/TokenThread.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java
- talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushedart.java
View file @
37a355c
| ... | ... | @@ -51,6 +51,18 @@ |
| 51 | 51 | @TableField(value = "readtime") |
| 52 | 52 | private Date readtime; |
| 53 | 53 | |
| 54 | + /** | |
| 55 | + * 是否推送短消息1(0否,1是) | |
| 56 | + */ | |
| 57 | + @TableField(value = "isweixinone") | |
| 58 | + private Integer isweixinone; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * 是否推送短消息2(0否,1是) | |
| 62 | + */ | |
| 63 | + @TableField(value = "isweixintwo") | |
| 64 | + private Integer isweixintwo; | |
| 65 | + | |
| 54 | 66 | @TableField(exist = false) |
| 55 | 67 | private static final long serialVersionUID = 1L; |
| 56 | 68 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PatientInfo.java
View file @
37a355c
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PushArticle.java
View file @
37a355c
| ... | ... | @@ -153,6 +153,18 @@ |
| 153 | 153 | @TableField(value = "aid") |
| 154 | 154 | private Integer aid; |
| 155 | 155 | |
| 156 | + /** | |
| 157 | + * 短文字1(公众号推送内容) | |
| 158 | + */ | |
| 159 | + @TableField(value = "weixTextOne") | |
| 160 | + private String weixTextOne; | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * 短文字2(公众号推送内容) | |
| 164 | + */ | |
| 165 | + @TableField(value = "weixTextTwo") | |
| 166 | + private String weixTextTwo; | |
| 167 | + | |
| 156 | 168 | @TableField(exist = false) |
| 157 | 169 | private static final long serialVersionUID = 1L; |
| 158 | 170 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java
View file @
37a355c
| 1 | +package com.lyms.talkonlineweb.task; | |
| 2 | + | |
| 3 | +import org.springframework.stereotype.Component; | |
| 4 | + | |
| 5 | +import javax.annotation.PostConstruct; | |
| 6 | +import javax.servlet.ServletException; | |
| 7 | +import javax.servlet.http.HttpServlet; | |
| 8 | + | |
| 9 | +@Component | |
| 10 | +public class AccessTokenServlet extends HttpServlet { | |
| 11 | + final String appId ="wxd3c36244d006cb90";//公众号appid | |
| 12 | + final String appSecret ="fc80b5dd03a581a088adcd2c65a7e10a";//公众号AppSecret | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * 启动后开启线程每1小时59获取一次token,保证长期有效。(微信公众号规定2小时token失效,每天只能获取2000次。) | |
| 16 | + * @throws ServletException | |
| 17 | + */ | |
| 18 | + @PostConstruct | |
| 19 | + public void init() throws ServletException { | |
| 20 | + TokenThread.appId = appId; | |
| 21 | + TokenThread.appSecret = appSecret; | |
| 22 | + new Thread(new TokenThread()).start(); //启动进程 | |
| 23 | + } | |
| 24 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/HXTask.java
View file @
37a355c
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java
View file @
37a355c
| 1 | 1 | package com.lyms.talkonlineweb.task; |
| 2 | 2 | |
| 3 | -import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 4 | 4 | import com.lyms.talkonlineweb.domain.*; |
| 5 | 5 | import com.lyms.talkonlineweb.service.*; |
| 6 | +import com.lyms.talkonlineweb.util.StringUtil; | |
| 7 | +import com.lyms.talkonlineweb.util.WeiXinUtil; | |
| 6 | 8 | import lombok.extern.log4j.Log4j2; |
| 7 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | -import org.springframework.scheduling.annotation.Scheduled; | |
| 9 | 10 | import org.springframework.stereotype.Component; |
| 11 | +import java.util.*; | |
| 10 | 12 | |
| 11 | -import java.util.Date; | |
| 12 | -import java.util.HashMap; | |
| 13 | -import java.util.List; | |
| 14 | -import java.util.Map; | |
| 15 | - | |
| 16 | 13 | /** |
| 17 | 14 | * 推送文章任务 |
| 18 | 15 | */ |
| 19 | 16 | |
| 20 | 17 | |
| ... | ... | @@ -32,12 +29,12 @@ |
| 32 | 29 | |
| 33 | 30 | @Autowired |
| 34 | 31 | private LymsDictService lymsDictService; |
| 32 | + final String gzopenid ="oQEmP6uq2pUQ80NctiX3GnFxDMn8";//测试:公众号关注的微信openId | |
| 33 | + final String template_id ="";//测试:公众号模板id | |
| 35 | 34 | |
| 36 | 35 | // @Scheduled(initialDelay=10000, fixedRate=30000) |
| 37 | 36 | public void pushArtcle(){ |
| 38 | - | |
| 39 | 37 | Map<String,Object> param=new HashMap<>(); |
| 40 | - | |
| 41 | 38 | param.put("vtype",999); |
| 42 | 39 | List<LymsDict> dcLst=lymsDictService.listByMap(param); |
| 43 | 40 | if (dcLst.size()>0 && dcLst.get(0).getCode()==1){ |
| 44 | 41 | |
| 45 | 42 | |
| ... | ... | @@ -45,31 +42,44 @@ |
| 45 | 42 | List<PushArticle> pushArticle=pushArticleService.list(); |
| 46 | 43 | for (PushArticle article : pushArticle) { |
| 47 | 44 | LymsPushedart pushedart=new LymsPushedart(); |
| 48 | - pushedart.setPid(article.getId()); | |
| 49 | - pushedart.setAid(article.getAid()); | |
| 50 | - pushedart.setCreatedtime(new Date()); | |
| 51 | - pushedart.setIsread((byte) 0); | |
| 52 | - lymsPushedartService.save(pushedart);//插入到提送记录 | |
| 45 | + pushedart.setPid(article.getId()); | |
| 46 | + pushedart.setAid(article.getAid()); | |
| 47 | + pushedart.setCreatedtime(new Date()); | |
| 48 | + pushedart.setIsread((byte) 0); | |
| 49 | + //查询推送文章后短文字推送状态 | |
| 50 | + QueryWrapper<LymsPushedart> queryWrapper=new QueryWrapper<>(); | |
| 51 | + queryWrapper.eq("pid", article.getId()); | |
| 52 | + queryWrapper.eq("aid", article.getAid()); | |
| 53 | + LymsPushedart lymsPushedart = lymsPushedartService.getOne(queryWrapper); | |
| 54 | + if(null!=lymsPushedart){ | |
| 55 | + pushedart.setId(lymsPushedart.getId()); | |
| 56 | + if(lymsPushedart.getIsweixinone()==0){ | |
| 57 | + if(StringUtil.isEmpty(TokenThread.accessToken)){ | |
| 58 | + log.info("获取token失败。。。"+article.getDtname()+":短消息1推送失败"); | |
| 59 | + continue; | |
| 60 | + } | |
| 61 | + //推送微信消息1 | |
| 62 | + Map<String,Object> map=new HashMap<>(); | |
| 63 | + map.put("data", article.getWeixTextOne()); | |
| 64 | + WeiXinUtil.SendWeChatMsg(TokenThread.accessToken, gzopenid, template_id,map); | |
| 65 | + //成功标记记录1 | |
| 66 | + pushedart.setIsweixinone(1); | |
| 67 | + }else if (lymsPushedart.getIsweixintwo()==0){ | |
| 68 | + if(StringUtil.isEmpty(TokenThread.accessToken)){ | |
| 69 | + log.info("获取token失败。。。"+article.getDtname()+":短消息2推送失败"); | |
| 70 | + continue; | |
| 71 | + } | |
| 72 | + //推送微信消息2 | |
| 73 | + Map<String,Object> map=new HashMap<>(); | |
| 74 | + map.put("data", article.getWeixTextTwo()); | |
| 75 | + WeiXinUtil.SendWeChatMsg(TokenThread.accessToken, gzopenid, template_id,map); | |
| 76 | + //成功标记记录2 | |
| 77 | + pushedart.setIsweixintwo(1); | |
| 78 | + } | |
| 79 | + } | |
| 80 | + lymsPushedartService.saveOrUpdate(pushedart);//插入到提送记录 | |
| 53 | 81 | } |
| 54 | -// List<LymsArticle> aLst=lymsArticleService.sltNeedPush();//获取待推送文章 | |
| 55 | -// | |
| 56 | -// aLst.forEach(e->{ | |
| 57 | -// PatientInfo patientInfo=new PatientInfo(); | |
| 58 | -// patientInfo.setIid(e.getIid());//针对特定疾病人群推送文章 | |
| 59 | -// List<PatientInfo> pLst=patientInfoService.list(Wrappers.query(patientInfo)); | |
| 60 | -// | |
| 61 | -// pLst.forEach(p->{ | |
| 62 | -// LymsPushedart pushedart=new LymsPushedart(); | |
| 63 | -// pushedart.setPid(p.getId()); | |
| 64 | -// pushedart.setAid(e.getAid()); | |
| 65 | -// pushedart.setCreatedtime(new Date()); | |
| 66 | -// pushedart.setIsread((byte) 0); | |
| 67 | -// lymsPushedartService.save(pushedart);//插入到提送记录 | |
| 68 | -// }); | |
| 69 | -// | |
| 70 | -// }); | |
| 71 | 82 | } |
| 72 | - | |
| 73 | 83 | } |
| 74 | 84 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/TokenThread.java
View file @
37a355c
| 1 | +package com.lyms.talkonlineweb.task; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSONObject; | |
| 4 | +import com.lyms.talkonlineweb.util.HttpUtil; | |
| 5 | + | |
| 6 | +public class TokenThread implements Runnable{ | |
| 7 | + public static String appId = "wxd3c36244d006cb90"; | |
| 8 | + | |
| 9 | + public static String appSecret= "fc80b5dd03a581a088adcd2c65a7e10a"; | |
| 10 | + | |
| 11 | + public static String accessToken = null; | |
| 12 | + | |
| 13 | + public void run(){ | |
| 14 | + while (true){ | |
| 15 | + try{ | |
| 16 | + accessToken = this.getToken(); | |
| 17 | + if(null!=accessToken){ | |
| 18 | + System.out.println(accessToken); | |
| 19 | + Thread.sleep(7000 * 1); //获取到access_token 休眠7000秒 | |
| 20 | + | |
| 21 | + }else{ | |
| 22 | + Thread.sleep(1000*3); //获取的access_token为空 休眠3秒 | |
| 23 | + } | |
| 24 | + }catch(Exception e){ | |
| 25 | + System.out.println("发生异常:"+e.getMessage()); | |
| 26 | + e.printStackTrace(); | |
| 27 | + try{ | |
| 28 | + Thread.sleep(1000*10); //发生异常休眠1秒 | |
| 29 | + }catch (Exception e1){ | |
| 30 | + | |
| 31 | + } | |
| 32 | + } | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * 获取token | |
| 39 | + * 微信公众号 | |
| 40 | + * @return token | |
| 41 | + */ | |
| 42 | + public String getToken() { | |
| 43 | + // 授予形式 | |
| 44 | + String grant_type = "client_credential"; | |
| 45 | + // 接口地址拼接参数 | |
| 46 | + String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + this.appId | |
| 47 | + + "&secret=" + this.appSecret; | |
| 48 | + String tokenJsonStr = HttpUtil.doGetPost(getTokenApi, "GET", null); | |
| 49 | + JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr); | |
| 50 | + String token = tokenJson.get("access_token").toString(); | |
| 51 | + System.out.println("获取到的TOKEN : " + token); | |
| 52 | + return token; | |
| 53 | + } | |
| 54 | +} |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java
View file @
37a355c
| 1 | 1 | package com.lyms.talkonlineweb.util; |
| 2 | 2 | |
| 3 | 3 | import java.io.IOException; |
| 4 | +import java.io.InputStream; | |
| 5 | +import java.io.OutputStreamWriter; | |
| 6 | +import java.net.HttpURLConnection; | |
| 7 | +import java.net.URL; | |
| 8 | +import java.util.Map; | |
| 4 | 9 | |
| 5 | 10 | |
| 6 | 11 | import lombok.extern.log4j.Log4j2; |
| ... | ... | @@ -13,6 +18,7 @@ |
| 13 | 18 | import org.apache.http.impl.client.CloseableHttpClient; |
| 14 | 19 | import org.apache.http.impl.client.HttpClients; |
| 15 | 20 | import org.apache.http.util.EntityUtils; |
| 21 | +import com.alibaba.fastjson.JSON; | |
| 16 | 22 | /** |
| 17 | 23 | * @ProjectName: talkonline |
| 18 | 24 | * @Package: com.lyms.talkonlineweb.util |
| ... | ... | @@ -89,5 +95,56 @@ |
| 89 | 95 | return result; |
| 90 | 96 | } |
| 91 | 97 | |
| 98 | + /** | |
| 99 | + * 调用接口 post | |
| 100 | + * 微信公众号 | |
| 101 | + * @param apiPath | |
| 102 | + */ | |
| 103 | + public static String doGetPost(String apiPath, String type, Map<String, Object> paramMap){ | |
| 104 | + OutputStreamWriter out = null; | |
| 105 | + InputStream is = null; | |
| 106 | + String result = null; | |
| 107 | + try{ | |
| 108 | + URL url = new URL(apiPath);// 创建连接 | |
| 109 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
| 110 | + connection.setDoOutput(true); | |
| 111 | + connection.setDoInput(true); | |
| 112 | + connection.setUseCaches(false); | |
| 113 | + connection.setInstanceFollowRedirects(true); | |
| 114 | + connection.setRequestMethod(type) ; // 设置请求方式 | |
| 115 | + connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式 | |
| 116 | + connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式 | |
| 117 | + connection.connect(); | |
| 118 | + if(type.equals("POST")){ | |
| 119 | + out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码 | |
| 120 | + out.append(JSON.toJSONString(paramMap)); | |
| 121 | + out.flush(); | |
| 122 | + out.close(); | |
| 123 | + } | |
| 124 | + // 读取响应 | |
| 125 | + is = connection.getInputStream(); | |
| 126 | + int length = (int) connection.getContentLength();// 获取长度 | |
| 127 | + if (length != -1) { | |
| 128 | + byte[] data = new byte[length]; | |
| 129 | + byte[] temp = new byte[512]; | |
| 130 | + int readLen = 0; | |
| 131 | + int destPos = 0; | |
| 132 | + while ((readLen = is.read(temp)) > 0) { | |
| 133 | + System.arraycopy(temp, 0, data, destPos, readLen); | |
| 134 | + destPos += readLen; | |
| 135 | + } | |
| 136 | + result = new String(data, "UTF-8"); // utf-8编码 | |
| 137 | + } | |
| 138 | + } catch (IOException e) { | |
| 139 | + e.printStackTrace(); | |
| 140 | + } finally { | |
| 141 | + try { | |
| 142 | + is.close(); | |
| 143 | + } catch (IOException e) { | |
| 144 | + e.printStackTrace(); | |
| 145 | + } | |
| 146 | + } | |
| 147 | + return result; | |
| 148 | + } | |
| 92 | 149 | } |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java
View file @
37a355c
| 1 | 1 | package com.lyms.talkonlineweb.util; |
| 2 | 2 | |
| 3 | +import com.alibaba.fastjson.JSONObject; | |
| 3 | 4 | import lombok.extern.log4j.Log4j2; |
| 4 | 5 | |
| 5 | 6 | import java.util.HashMap; |
| ... | ... | @@ -49,6 +50,25 @@ |
| 49 | 50 | log.error("getWxOpenId by code: {}", code, e); |
| 50 | 51 | } |
| 51 | 52 | return null; |
| 53 | + } | |
| 54 | + | |
| 55 | + /*** | |
| 56 | + * 发送消息 | |
| 57 | + * | |
| 58 | + * @param token | |
| 59 | + * @param openid 关注公众号的微信id | |
| 60 | + * @param template_id 公众号消息模板id | |
| 61 | + * @param dataMap 推送内容消息主题显示相关map | |
| 62 | + */ | |
| 63 | + public static void SendWeChatMsg(String token,String openid,String template_id,Map<String,Object> dataMap) { | |
| 64 | + // 接口地址 | |
| 65 | + String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token; | |
| 66 | + //整体参数map | |
| 67 | + Map<String, Object> paramMap = new HashMap<String, Object>(); | |
| 68 | + paramMap.put("touser", openid); | |
| 69 | + paramMap.put("template_id", template_id); | |
| 70 | + paramMap.put("data", dataMap); | |
| 71 | + System.out.println(HttpUtil.doGetPost(sendMsgApi,"POST",paramMap)); | |
| 52 | 72 | } |
| 53 | 73 | |
| 54 | 74 | } |