前提条件获取
要知道开发者ID—AppId,开发者密码—AppSecret,接口凭证—access_token,用户的openid
不知道的参考这边文章:微信获取公众号上的用户openid-----详细教程
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
POST数据示例如下:
{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"miniprogram":{
"appid":"xiaochengxuappid12345",
"pagepath":"index?foo=bar"
},
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"keyword2": {
"value":"39.8元",
"color":"#173177"
},
"keyword3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}
TemplateController
我这里参数是静态的,可以自己做成动态的数据来进行交互
package com.example.template.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.template.common.Constast;
import com.example.template.service.TemplateService;
import com.example.template.vo.WeixinUserListVo;
import com.example.template.vo.WxOpenidInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/a")
public class TemplateController {
private static final Logger logger = LoggerFactory.getLogger(TemplateController.class);
@Autowired
private TemplateService templateService;
//获取access_token,access_token是公众号的全局唯一接口调用凭据
public static Constast getAccessToken() {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> map = new HashMap<>();
map.put("AppId", Constast.AppId);
map.put("AppSecret", Constast.AppSecret);
//调用微信公众号接口
ResponseEntity<String> forEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={AppId}&secret={AppSecret}", String.class, map);
String body = forEntity.getBody();
JSONObject jsonObject = JSON.parseObject(body);
String access_token = jsonObject.getString("access_token");
String expires_in = jsonObject.getString("expires_in");//时效--7200s
Constast constast = new Constast();
constast.setAccess_token(access_token);
return constast;
}
@RequestMapping("/b")
public void sendMessage(){
//获取最新的access_token
String accessToken = TemplateController.getAccessToken().getAccess_token();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
RestTemplate restTemplate = new RestTemplate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
JSONObject miniprogram = new JSONObject();
miniprogram.put("appid",Constast.XcxAppId);
miniprogram.put("pagepath",Constast.PagePath);
JSONObject first = new JSONObject();
first.put("value","您好,您的爱车暂无新的违章记录");
JSONObject keyword1 = new JSONObject();
keyword1.put("value","川AXXXXX");
JSONObject keyword2 = new JSONObject();
keyword2.put("value",sdf.format(new Date()));
JSONObject keyword3 = new JSONObject();
keyword3.put("value","0");
JSONObject remark = new JSONObject();
remark.put("value","点击“详情”查看违章详细信息");
JSONObject data = new JSONObject();
data.put("first",first);
data.put("keyword1",keyword1);
data.put("keyword2",keyword2);
data.put("keyword3",keyword3);
data.put("remark",remark);
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser",Constast.HuHaoAppId);
jsonObject.put("template_id",Constast.TemplateId);
jsonObject.put("miniprogram",miniprogram);
jsonObject.put("data",data);
String s = restTemplate.postForObject(url, jsonObject, String.class);
System.out.println(s);
}
}
启动项目:http://localhost:8888/a/b
控制台:{“errcode”:0,“errmsg”:“ok”,“msgid”:1713714113454915587}
手机公众号: