微信公众号发送模板消息
1.打开微信公众平台登陆进去 模板消息 添加模板
2.获取 access_token
需要 AppId 和 AppSecret 就可以了
API 的接口是 "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
3.后台的代码为
public String getAccessToken() {
String url = getCodeRequest();
String result = WXHttpHelper.sendGet(url);
if (isError(result)) {
return null;
}
MpAccessToken wxAccessToken = parseObject(result, MpAccessToken.class);
return wxAccessToken.getAccess_token();
}
// 替换
private String getCodeRequest() {
String appid = "你的AppId";
String appsecret = "你的appsecret";
String getcoderequest = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
String result = null;
getcoderequest = getcoderequest.replace("APPID", appid);
getcoderequest = getcoderequest.replace("SECRET", appsecret);
result = getcoderequest;
return result;
}
private static boolean isError(String result) {
if (StringUtils.isEmpty(result)) {
return true;
}
JSONObject json = parseObject(result);
if (json.getString("errcode") == null) {
return false;
}
return true;
}
@PostMapping("/pushTemplateMessage")
@ResponseBody
public void pushTemplateMessage() {
String accessToken = getAccessToken();
String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser", ""); // openid
jsonObject.put("template_id", "FjI7DbTO4_IZHsYpwR5yW8LIGBehlpD5tD0NdKWQC9Q");
jsonObject.put("url", "http://www.baidu.com");
JSONObject data = new JSONObject();
JSONObject first = new JSONObject();
JSONObject keyword1 = new JSONObject();
JSONObject keyword2 = new JSONObject();
JSONObject keyword3 = new JSONObject();
JSONObject keyword4 = new JSONObject();
JSONObject remark = new JSONObject();
first.put("value", "张三");
keyword1.put("value", "13999999999");
keyword2.put("value", "2019-10-28 11:49:20");
keyword3.put("value", "客户拜访");
keyword4.put("value", "请及时办理。");
data.put("first", first);
data.put("keyword1", keyword1);
data.put("keyword2", keyword2);
data.put("keyword3", keyword3);
data.put("keyword4", keyword4);
data.put("remark", remark);
jsonObject.put("data", data);
String string = null;
try {
string = HttpClientUtils.sendPostJsonStr(postUrl, jsonObject.toJSONString());
} catch (IOException e) {
e.printStackTrace();
}
JSONObject result = JSON.parseObject(string);
int errcode = result.getIntValue("errcode");
if (errcode == 0) {
// 发送成功
System.out.println("发送成功");
} else {
// 发送失败
System.out.println("发送失败");
}
}
工具类代码:
package com.ruoyi.common.utils.wx;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* HttpClient工具类
*
* @author ruoyi
*/
public class HttpClientUtils
{
/**
* 以jsonString形式发送HttpPost的Json请求,String形式返回响应结果
*
* @param url
* @param jsonString
* @return
*/
public static String sendPostJsonStr(String url, String jsonString) throws IOException {
if (jsonString == null || jsonString.isEmpty()) {
return sendPost(url);
}
String resp = "";
StringEntity entityStr = new StringEntity(jsonString,
ContentType.create("text/plain", "UTF-8"));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entityStr);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
resp = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity);
} catch (ClientProtocolException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
if (resp == null || resp.equals("")) {
return "";
}
return resp;
}
/**
* 发送不带参数的HttpPost请求
*
* @param url
* @return
*/
public static String sendPost(String url) throws IOException {
// 1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2.生成一个post请求
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
// 3.执行get请求并返回结果
response = httpclient.execute(httppost);
} catch (IOException e) {
System.err.println(e.getMessage());
}
// 4.处理结果,这里将结果返回为字符串
HttpEntity entity = response.getEntity();
String result = null;
try {
result = EntityUtils.toString(entity);
} catch (ParseException | IOException e) {
System.err.println(e.getMessage());
}
return result;
}
}
4.注意点 由于我的用本地的IP地址测得 所以就在获取access_token时返回码报错 40164 下面是报错的额原因
这个错误需要到公众开发平台 开发 – > 基本配置 上去修改
修改白名单 ,如果有多个url就以 Enter 符分割
这样就完成了,当然只是先测试下 这样差不多了 就需要在后台获取真实的数据了