小程序 发送模版消息
@Autowired
private RedisTemplate redisTemplate;
@Override
public void getaccessToken() { 可设置定时任务,避免token过期
//获取access_token
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=" + appId + "&secret=" + appSecret;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Map> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, Map.class);
Map body = responseEntity.getBody();
//存入redis
ValueOperations<String, Object> value = redisTemplate.opsForValue();
value.set("accessToken", body.get("access_token"));
//获取 这个 key 的值
System.out.println(value.get("accessToken"));
}
private String getUrl(String accessToken) { 公共方法
//获取access_token
String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send" +
"?access_token=" + accessToken;
return url;
}
// 消息发送
@Override
public void sendMessage(SendMessageDTO sendMessageDTO) {
if(CheckParam.isNull(sendMessageDTO.getCuserId())){
throw new McExceptionUtil("用户信息不能为空");
}
ValueOperations<String, Object> value = redisTemplate.opsForValue();
String accessToken = (String) value.get("accessToken");
String hotelFormId = (String) value.get("formId");
Optional<Cuser> cuserId = cuserRepository.findById(sendMessageDTO.getCuserId());
Cuser cuseropenId = cuserId.get();
String url = getUrl(accessToken);
//拼接推送的模版
WxMessage wxMessage = new WxMessage();
wxMessage.setTouser(cuseropenId.getOpenId());//用户openid
wxMessage.setTemplate_id(hotelOrder);//模版id
wxMessage.setForm_id(hotelFormId);//formid
Map<String, TemplateData> templateDataMap = new HashMap<>();
TemplateData templateData1 = new TemplateData();
templateData1.setValue(sendMessageDTO.getOrderId());
templateData1.setColor("#000555");
templateDataMap.put("keyword1", templateData1);
TemplateData templateData2 = new TemplateData();
templateData2.setValue(sendMessageDTO.getStatus());
templateData2.setColor("#088888");
templateDataMap.put("keyword2", templateData2);
TemplateData templateData3 = new TemplateData();
templateData3.setValue(sendMessageDTO.getHotelName());
templateData3.setColor("#000333");
templateDataMap.put("keyword3", templateData3);
TemplateData templateData4 = new TemplateData();
templateData4.setValue(sendMessageDTO.getHotelRoom());
templateData4.setColor("#000000");
templateDataMap.put("keyword4", templateData4);
TemplateData templateData5 = new TemplateData();
templateData5.setValue(sendMessageDTO.getStratTime()+"--"+sendMessageDTO.getEndTime());
templateData5.setColor("#000000");
templateDataMap.put("keyword5", templateData5);
TemplateData templateData6 = new TemplateData();
templateData6.setValue(sendMessageDTO.getPayMoney());
templateData6.setColor("#000000");
templateDataMap.put("keyword6", templateData6);
TemplateData templateData7 = new TemplateData();
templateData7.setValue(sendMessageDTO.getName());
templateData7.setColor("#000000");
templateDataMap.put("keyword7", templateData7);
TemplateData templateData8 = new TemplateData();
templateData8.setValue(sendMessageDTO.getSubcode());
templateData8.setColor("#000000");
templateDataMap.put("keyword8", templateData8);
TemplateData templateData9 = new TemplateData();
templateData9.setValue(sendMessageDTO.getPoint());
templateData9.setColor("#000000");
templateDataMap.put("keyword9", templateData9);
wxMessage.setData(templateDataMap);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity =
restTemplate.postForEntity(url, wxMessage, String.class);
System.out.println(XinyuanJSONUtil.gSonString(responseEntity.getBody()));
return;
实体:
@Data
public class WxMessage {
private String touser;//用户openid
private String template_id;//模版id
private String page = "index";//默认跳到小程序首页
private String form_id;//收集到的用户formid
private String emphasis_keyword = "keyword1.DATA";//放大那个推送字段
private Map<String, TemplateData> data;//推送文字
}