微信公众号推送消息(基于idea+spring boot)
2018-02-08 13:10
316 查看
微服务功能:提供推送微信模板消息接口,并打成jar包运行;
pom文件:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.paibo.police</groupId>
<artifactId>wxmsg</artifactId>
<version>0.5.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wxmsg</name>
<description>微信消息</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!-- 可执行的jar 必须有mainClass入口-->
<mainClass>com.paibo.police.check.CheckApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 打包时将jsp文件拷贝到META-INF目录下-->
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/resources</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
主要代码:
public class SendThread implements Runnable {
public String custom = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
public String mode = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
public String app_id;
public String app_secret;
public String content;
private String redirectUrl;
public final String tempId = "模板消息ID";
/**
* 多个openid 格式 1111,22222-33333,555555-666666,8888888
*/
private String[] openIds;
/**
* 超时时间 单位分钟
*/
private String timeout;
public SendThread(String[] openIds,String timeout,String appId,String appSecret,String content,String redirectUrl) {
this.openIds = openIds;
this.timeout = timeout;
this.app_id = appId;
this.app_secret = appSecret;
this.content = content;
this.redirectUrl = redirectUrl;
}
@Override
public void run() {
for(String openId:openIds){
String[] os = openId.split(",");
for(String op:os){
//每级用户
sendText(this.content,op,this.redirectUrl);
}
//超时时间
try{
Long time = Integer.parseInt(timeout)*60*1000L;
Thread.sleep(time);
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 客服消息
* @param text
* @param openId
*/
public void sendText(String text,String openId){
if(StringUtils.isEmpty(openId)){
return;
}
if("null".equals(openId)){
return ;
}
String url = custom + GetAccessToken.getAs(app_id,app_secret);
JSONObject js = new JSONObject();
js.put("touser", openId);
js.put("msgtype", "text");
JSONObject ct = new JSONObject();
ct.put("content", text);
js.put("text", ct);
String s = HttpUtil.sendPost(url, js.toString());
LogUtil.print(s,SendThread.class);
}
/**
* 模板消息
* @param text
* @param openId
* @param redirectUrl
*/
public void sendText(String text,String openId,String redirectUrl){
if(StringUtils.isEmpty(openId)){
return;
}
if("null".equals(openId)){
return ;
}
String url = mode + GetAccessToken.getAs(app_id,app_secret);
JSONObject js = new JSONObject();
js.put("touser", openId);
js.put("template_id", tempId);
js.put("url",redirectUrl);
JSONObject ct = new JSONObject();
JSONObject ct1 = new JSONObject();
ct1.put("value",text);
ct1.put("color","#173177");
ct.put("first", ct1);
JSONObject ct2 = new JSONObject();
ct2.put("value","消息通知");
ct2.put("color","#173177");
ct.put("keyword1",ct2);
JSONObject ct3 = new JSONObject();
ct3.put("value","请至系统内处理");
ct3.put("color","#173177");
ct.put("keyword2",ct3);
JSONObject ct4 = new JSONObject();
ct4.put("value","点击进行签收");
ct4.put("color","#173177");
ct.put("remark",ct4);
js.put("data", ct);
String s = HttpUtil.sendPost(url, js.toString());
LogUtil.print(s,SendThread.class);
}
}获取微信as:
/**
* 获取as
* @author hjl
*/
public class GetAccessToken {
public static final String url = "https://api.weixin.qq.com/cgi-bin/token";
public static Long timeout = 4000000L;
public static String getAs(String app_id,String app_secret){
if(StringUtils.isEmpty(CheckApplication.as)){
getAccessToken(app_id,app_secret);
}
Date now = new Date();
LogUtil.print(now.getTime()+" "+ CheckApplication.old.getTime()+" "+timeout,GetAccessToken.class);
if(now.getTime()-CheckApplication.old.getTime()>timeout){
getAccessToken(app_id,app_secret);
CheckApplication.old = new Date();
}
return CheckApplication.as;
}
public static void getAccessToken(String app_id,String app_secret){
LogUtil.print("获取access_token",GetAccessToken.class);
String result = HttpUtil.sendGet(url, "grant_type=client_credential&appid="+app_id+"&secret="+app_secret);
if(StringUtils.isEmpty(result)){
return;
}
try{
JSONObject js = JSONObject.fromObject(result);
LogUtil.print(js.toString(),GetAccessToken.class);
String at = js.get("access_token").toString();
if(StringUtils.isEmpty(at)){
return;
}
CheckApplication.as = at;
}catch (Exception e) {
e.printStackTrace();
}
}
}
idea将spring boot打成jar包:
xsheel后台运行jar包方法:
nohup java -jar ***.jar >***.log &
这样就不会关闭当前连接 程序就自动停止
权当记录
hjl
2018年2月8日13:10:05
pom文件:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.paibo.police</groupId>
<artifactId>wxmsg</artifactId>
<version>0.5.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wxmsg</name>
<description>微信消息</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!-- 可执行的jar 必须有mainClass入口-->
<mainClass>com.paibo.police.check.CheckApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 打包时将jsp文件拷贝到META-INF目录下-->
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/resources</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
主要代码:
public class SendThread implements Runnable {
public String custom = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
public String mode = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
public String app_id;
public String app_secret;
public String content;
private String redirectUrl;
public final String tempId = "模板消息ID";
/**
* 多个openid 格式 1111,22222-33333,555555-666666,8888888
*/
private String[] openIds;
/**
* 超时时间 单位分钟
*/
private String timeout;
public SendThread(String[] openIds,String timeout,String appId,String appSecret,String content,String redirectUrl) {
this.openIds = openIds;
this.timeout = timeout;
this.app_id = appId;
this.app_secret = appSecret;
this.content = content;
this.redirectUrl = redirectUrl;
}
@Override
public void run() {
for(String openId:openIds){
String[] os = openId.split(",");
for(String op:os){
//每级用户
sendText(this.content,op,this.redirectUrl);
}
//超时时间
try{
Long time = Integer.parseInt(timeout)*60*1000L;
Thread.sleep(time);
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 客服消息
* @param text
* @param openId
*/
public void sendText(String text,String openId){
if(StringUtils.isEmpty(openId)){
return;
}
if("null".equals(openId)){
return ;
}
String url = custom + GetAccessToken.getAs(app_id,app_secret);
JSONObject js = new JSONObject();
js.put("touser", openId);
js.put("msgtype", "text");
JSONObject ct = new JSONObject();
ct.put("content", text);
js.put("text", ct);
String s = HttpUtil.sendPost(url, js.toString());
LogUtil.print(s,SendThread.class);
}
/**
* 模板消息
* @param text
* @param openId
* @param redirectUrl
*/
public void sendText(String text,String openId,String redirectUrl){
if(StringUtils.isEmpty(openId)){
return;
}
if("null".equals(openId)){
return ;
}
String url = mode + GetAccessToken.getAs(app_id,app_secret);
JSONObject js = new JSONObject();
js.put("touser", openId);
js.put("template_id", tempId);
js.put("url",redirectUrl);
JSONObject ct = new JSONObject();
JSONObject ct1 = new JSONObject();
ct1.put("value",text);
ct1.put("color","#173177");
ct.put("first", ct1);
JSONObject ct2 = new JSONObject();
ct2.put("value","消息通知");
ct2.put("color","#173177");
ct.put("keyword1",ct2);
JSONObject ct3 = new JSONObject();
ct3.put("value","请至系统内处理");
ct3.put("color","#173177");
ct.put("keyword2",ct3);
JSONObject ct4 = new JSONObject();
ct4.put("value","点击进行签收");
ct4.put("color","#173177");
ct.put("remark",ct4);
js.put("data", ct);
String s = HttpUtil.sendPost(url, js.toString());
LogUtil.print(s,SendThread.class);
}
}获取微信as:
/**
* 获取as
* @author hjl
*/
public class GetAccessToken {
public static final String url = "https://api.weixin.qq.com/cgi-bin/token";
public static Long timeout = 4000000L;
public static String getAs(String app_id,String app_secret){
if(StringUtils.isEmpty(CheckApplication.as)){
getAccessToken(app_id,app_secret);
}
Date now = new Date();
LogUtil.print(now.getTime()+" "+ CheckApplication.old.getTime()+" "+timeout,GetAccessToken.class);
if(now.getTime()-CheckApplication.old.getTime()>timeout){
getAccessToken(app_id,app_secret);
CheckApplication.old = new Date();
}
return CheckApplication.as;
}
public static void getAccessToken(String app_id,String app_secret){
LogUtil.print("获取access_token",GetAccessToken.class);
String result = HttpUtil.sendGet(url, "grant_type=client_credential&appid="+app_id+"&secret="+app_secret);
if(StringUtils.isEmpty(result)){
return;
}
try{
JSONObject js = JSONObject.fromObject(result);
LogUtil.print(js.toString(),GetAccessToken.class);
String at = js.get("access_token").toString();
if(StringUtils.isEmpty(at)){
return;
}
CheckApplication.as = at;
}catch (Exception e) {
e.printStackTrace();
}
}
}
idea将spring boot打成jar包:
xsheel后台运行jar包方法:
nohup java -jar ***.jar >***.log &
这样就不会关闭当前连接 程序就自动停止
权当记录
hjl
2018年2月8日13:10:05
相关文章推荐
- 基于springboot+mybatis的微信公众号开发第二篇-验证与消息推送的基本设置
- [置顶] 基于springboot+mybatis的微信公众号开发第二篇-验证与消息推送的基本设置
- 基于springboot+mybatis的微信公众号开发第三篇-消息的接收与回复
- Spring Boot 构建应用——基于 WebSocket 的消息推送
- [置顶] 基于springboot+mybatis的微信公众号开发第三篇-消息的接收与回复
- springboot整合websocket实现一对一消息推送和广播消息推送
- spring boot 整合activemq 进行服务端消息推送(web页面)
- 基于Spring boot,使用idea方便地切换启动环境
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- 使用SpringBoot快速搭建WebSocket实现消息推送
- [ Spring Boot ] 整合 Websocket 实现消息推送框架的设计笔记
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- SpringBoot集成WebSocket【基于STOMP协议】进行点对点[一对一]和广播[一对多]实时推送
- SpringBoot的WebSocket基于STOMP协议自动广播推送配置
- [置顶] 基于springboot+mybatis的微信公众号开发第一篇-基本架构
- [置顶] 使用IDEA基于springboot开发ssm框架(undertow代替tomcat以及其他功能)
- 微信开发——基于springboot接入微信公众号(ngrok内网穿透技术)
- 基于springboot+mybatis的微信公众号开发第一篇-基本架构
- springboot,mybatics框架搭建基于idea
- IDEA创建基于springboot的多个module聚合项目,并生成war包