1、文档需要的参数
2、 前段展示数据
?3、后端代码
public class SendMessage1 {
/*
* 发送订阅消息
* */
@Async
public Object pushOneUser(Repair record ,String WechatOpenid){
return push(record ,WechatOpenid);
}
public Object push( Repair record ,String WechatOpenid) {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + getAccessToken();
JSONObject miniprogram = new JSONObject();
miniprogram.put("appid",".................");
miniprogram.put("pagepath","pages/index/index1");
JSONObject data = new JSONObject();
JSONObject first = new JSONObject();
JSONObject keyword1 = new JSONObject();
JSONObject keyword2 = new JSONObject();
JSONObject keyword3 = new JSONObject();
JSONObject remark = new JSONObject();
first.put("value","有新的维修订单");
first.put("color","#173177");
keyword1.put("value",record.getContentsRepair());
keyword2.put("value",record.getOrderDate());
keyword3.put("value",record.getLocationAddress());
remark.put("value","点击“详情”查看详细信息");
remark.put("color","#173177");
data.put("first", first);
data.put("keyword1",keyword1);
data.put("keyword2",keyword3);
data.put("keyword3",keyword2);
data.put("remark",remark);
JSONObject mpTemplateMSG = new JSONObject();
mpTemplateMSG.put("appid",".............");
mpTemplateMSG.put("template_id","...............................");
mpTemplateMSG.put("url","http://weixin.qq.com/download");
mpTemplateMSG.put("miniprogram",miniprogram);
mpTemplateMSG.put("data",data);
JSONObject jsonObject = new JSONObject();
jsonObject.put("touser",WechatOpenid);
jsonObject.put("mp_template_msg",mpTemplateMSG);
ResponseEntity<String> responseEntity =
restTemplate.postForEntity( url,jsonObject, String.class);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
System.out.println(object.getString("errcode"));
System.out.println(object.getString("errmsg"));
return 0;
}
public String getAccessToken() {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> params = new HashMap<>();
params.put("APPID", "..............."); //
params.put("APPSECRET", "....................."); //
ResponseEntity<String> responseEntity = restTemplate.getForEntity(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
String Access_Token = object.getString("access_token");
String expires_in = object.getString("expires_in");
System.out.println("Access_Token:" + Access_Token);
System.out.println("object:" + object);
return Access_Token;
}
}
完了、就这么简单。希望可以帮到你。。。。
|