Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage
ylbtech-Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage |
1.返回顶部 |
package com.ylbtech.common.utils.miniprogram; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 模板消息
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.addTemplate.html
*/
public class TemplateMessage { /**
* 组合模板并添加至帐号下的个人模板库
* @param access_token 接口调用凭证
* @param id 模板标题id,可通过接口获取,也可登录小程序后台查看获取
* @param keyword_id_list 开发者自行组合好的模板关键词列表,关键词顺序可以自由搭配(例如[3,5,4]或[4,5,3]),最多支持10个关键词组合
* @param restTemplate
* @return
*/
public static String addTemplate(String access_token, String id, List<Integer> keyword_id_list, RestTemplate restTemplate) { restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 防止中文乱码
String url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token="+access_token;
//请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//请求体
Map<String, Object> requestParam = new HashMap<>();
requestParam.put("id", id);
requestParam.put("keyword_id_list",keyword_id_list); //封装成一个请求对象
HttpEntity entity = new HttpEntity(requestParam, headers);
//发送数据方法
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, entity, String.class); //得到返回的数据body
return forEntity.getBody();
} /**
* 删除帐号下的某个模板
* @param access_token 接口调用凭证
* @param template_id 要删除的模板id
* @param restTemplate
* @return
*/
public static String deleteTemplate(String access_token, String template_id, RestTemplate restTemplate) { restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 防止中文乱码
String url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token="+access_token;
//请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//请求体
Map<String, Object> requestParam = new HashMap<>();
requestParam.put("template_id", template_id); //封装成一个请求对象
HttpEntity entity = new HttpEntity(requestParam, headers);
//发送数据方法
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, entity, String.class); //得到返回的数据body
return forEntity.getBody();
} /**
* 获取模板库某个模板标题下关键词库
* @param access_token 接口调用凭证
* @param id 模板标题id,可通过接口获取,也可登录小程序后台查看获取
* @param restTemplate
* @return
*/
public static String getTemplateLibraryById(String access_token, String id, RestTemplate restTemplate) { restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 防止中文乱码
String url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token="+access_token;
//请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//请求体
Map<String, Object> requestParam = new HashMap<>();
requestParam.put("id", id); //封装成一个请求对象
HttpEntity entity = new HttpEntity(requestParam, headers);
//发送数据方法
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, entity, String.class); //得到返回的数据body
return forEntity.getBody();
} /**
* 获取小程序模板库标题列表
* @param access_token 接口调用凭证
* @param offset 用于分页,表示从offset开始。从 0 开始计数
* @param count 用于分页,表示拉取count条记录。最大为 20
* @param restTemplate
* @return
*/
public static String getTemplateLibraryList(String access_token, Integer offset, Integer count, RestTemplate restTemplate) { restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 防止中文乱码
String url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token="+access_token;
//请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//请求体
Map<String, Object> requestParam = new HashMap<>();
requestParam.put("offset", offset);
requestParam.put("count", count); //封装成一个请求对象
HttpEntity entity = new HttpEntity(requestParam, headers);
//发送数据方法
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, entity, String.class); //得到返回的数据body
return forEntity.getBody();
} /**
* 发送模板消息
* @param access_token 接口调用凭证
* @param touser 接收者(用户)的 openid
* @param template_id 所需下发的模板消息的id
* @param form_id 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
* @param restTemplate
* @return
*/
public static String send(String access_token,String touser, String template_id, String form_id, RestTemplate restTemplate) { restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 防止中文乱码
String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+access_token;
//请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//请求体
Map<String, Object> requestParam = new HashMap<>();
requestParam.put("touser", touser);
requestParam.put("template_id", template_id);
//requestParam.put("page", "index");
requestParam.put("form_id", form_id);
//requestParam.put("data", "");
//requestParam.put("emphasis_keyword", ""); //封装成一个请求对象
HttpEntity entity = new HttpEntity(requestParam, headers);
//发送数据方法
ResponseEntity<String> forEntity = restTemplate.postForEntity(url, entity, String.class); //得到返回的数据body
return forEntity.getBody();
}
}
2.返回顶部 |
3.返回顶部 |
4.返回顶部 |
5.返回顶部 |
6.返回顶部 |
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage的更多相关文章
- Java-Class-Miniprogram:com.common.utils.miniprogram.Auth
ylbtech-Java-Class-miniprogram:com.common.utils.miniprogram.Auth 1.返回顶部 1.1. package com.ylbtech.com ...
- 启动服务报错:nested exception is java.lang.NoSuchMethodError: org.apache.cxf.common.jaxb.JAXBUtils.closeUnmarshaller(Ljavax/xml/bind/Unmarshaller;)V
1.启动tomcat时报错:Error creating bean with name 'payInfService': Invocation of init method failed; neste ...
- Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class
ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶 ...
- Java虚拟机9:Java类加载机制
前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后才能运行和使用.而虚拟机如何加载这些.class文件?.class文件的信息 ...
- Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataPro
1.错误描述 信息: MLog clients using java 1.4+ standard logging. 2014-7-12 19:29:20 com.mchange.v2.c3p0.C3P ...
- Java虚拟机13:Java类加载机制
前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后才能运行和使用.而虚拟机如何加载这些.class文件?.class文件的信息 ...
- Java日期类:Date和Calendar的使用
总是使用这两个类,总是需要百度.还不如一次全部整理完. 一.介绍: Date 类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年 ...
- Java进阶学习:将文件上传到七牛云中
Java进阶学习:将文件上传到七牛云中 通过本文,我们将讲述如何利用七牛云官方SDK,将我们的本地文件传输到其存储空间中去. JavaSDK:https://developer.qiniu.com/k ...
- 基于JavaMail的Java邮件发送:复杂邮件发送
参考:http://blog.csdn.net/xietansheng/article/details/51722660package com.bfd.ftp.utils;import java.ut ...
随机推荐
- Unity3D开发——LeRunning的人物角色信息的显示
///////////////////////2015/08/22/////////////// //////////////////////by xbw/////////////////// ...
- [学习笔记]JavaScript基础
JavaScript概述 1. JavaScript定义 JavaScript是Netscape公司开发的一种基于对象和事件驱动的脚本语言.它是弱类型语言.仅仅能由浏览器解释运行. 当中: 脚本语言: ...
- 浅析Linux字符设备驱动程序内核机制
前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对 ...
- js的类库
prototype.js https://github.com/sstephenson/prototype moment js https://github.com/moment/moment thr ...
- Bing Maps进阶系列七:Bing Maps功能导航菜单华丽的变身
Bing Maps进阶系列七:Bing Maps功能导航菜单华丽的变身 Bing Maps Silverlight Control所提供的功能导航是非常强大的,在设计上对扩展的支持非常好,提供了许多用 ...
- golang函数——可以为类型(包括内置数据类型)定义函数,类似类方法,同时支持多返回值
不可或缺的函数,在Go中定义函数的方式如下: func (p myType ) funcName ( a, b int , c string ) ( r , s int ) { return } 通过 ...
- bzoj 5127 数据校验
题目大意: 一个数列a 对于 a 的一个区间 [l, r],若对于该区间 [l, r] 内的任意一个非空连续子区间,该子区间内出现过的数值在整数上值域连续,则称 [l, r]为合法区间 m次询问 每 ...
- 【RAID在数据库存储上的应用 】
随着单块磁盘在数据安全.性能.容量上呈现出的局限,磁盘阵列(Redundant Arrays of Inexpensive/Independent Disks,RAID)出现了,RAID把多块独立的磁 ...
- struts2基础学习----struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- JQuery 总结
JQuery官方网站 http://jquery.com/ 1.JQuery概念 A.Jquery是一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器,jQu ...