您的位置:首页 > 编程语言 > Java开发

微信公众号推送消息(基于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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐