主逻辑代码
**说明:**这一段代码可以粘贴到任何一个地方,当你的流程走到某一步之后需要给用户发送信息,这一段就是发送的信息。
redirect_uri后面跟的是你要进入的指定路径,我的路径是,设置菜单栏的时候设置的路径,只不过这里加上了参数,后台直接可以用req.getParameter(“id”)获取就行了,要是不理解可以看我的博客中的“微信公众号开发–前期配置”里面我写的很详细
Article a = new Article();
// 连接标题
a.setTitle("一键报装");
// 内容
a.setDescription("报装已提交"+"\n"+"点击查看详情");
// 路径
a.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己的" + "&redirect_uri=https://自己的域名/redirect_uri/doGet?id="+id+"&response_type=code&scope=snsapi_userinfo" +
"&state" +
"=STATE" +
"#wechat_redirect");
// username就是用户的openid,在同一个公众号下,openid唯一
// accesstoken就是从数据获取的,要是不清楚可以进我的博客看“微信公众号开发--获取access_token和Ticket”里面写的很详细
HfMessage.sendNewsToUser(accesstoken, username,a);
工具类
import system.weChat.domain.Article;
import net.sf.json.JSONArray;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* 给指定用户发送信息
*/
public class HfMessage {
/**
* 连接请求微信后台接口
*/
public static void connectWeiXinInterface(String action,String json){
URL url;
try {
url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));// 传入参数
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes, "UTF-8");
System.out.println("请求返回结果:"+result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送图文给所有的用户
*/
public static void sendNewsToUser(String accessToken,String openId,Article a){
ArrayList<Object> articles = new ArrayList<Object>();
articles.add(a);
JSONArray result = JSONArray.fromObject(articles);
String str = result.toString();
String json = "{\"touser\":\""+openId+"\",\"msgtype\":\"news\",\"news\":" +
"{\"articles\":" +str +"}"+"}";
json = json.replace("picUrl", "picurl");
//获取请求路径
String action = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
try {
connectWeiXinInterface(action,json);
} catch (Exception e) {
e.printStackTrace();
}
}
}
实体类
/**
* Article图文消息封装实体类
*/
public class Article {
// 图文消息名称
private String Title;
// 图文消息描述
private String Description;
// 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80,限制图片链接的域名需要与开发者填写的基本资料中的Url一致
private String PicUrl;
// 点击图文消息跳转链接
private String Url;
private String thumb_media_id;
private String author;
private String content_source_url;
private String content;
private String digest;
private Integer show_cover_pic;
public String getThumb_media_id() {
return thumb_media_id;
}
public void setThumb_media_id(String thumb_media_id) {
this.thumb_media_id = thumb_media_id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent_source_url() {
return content_source_url;
}
public void setContent_source_url(String content_source_url) {
this.content_source_url = content_source_url;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDigest() {
return digest;
}
public void setDigest(String digest) {
this.digest = digest;
}
public Integer getShow_cover_pic() {
return show_cover_pic;
}
public void setShow_cover_pic(Integer show_cover_pic) {
this.show_cover_pic = show_cover_pic;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return null == Description ? "" : Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPicUrl() {
return null == PicUrl ? "" : PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getUrl() {
return null == Url ? "" : Url;
}
public void setUrl(String url) {
Url = url;
}
}
结果
喜欢的可以收藏,关注,评论,不喜欢的就当没看见