| @@ -121,6 +121,12 @@ public interface WxMaService { | |||
| */ | |||
| WxMaQrcodeService getQrcodeService(); | |||
| /** | |||
| * 返回模板配置相关接口方法的实现类对象, 以方便调用其各个接口 | |||
| * @return WxMaTemplateService | |||
| */ | |||
| WxMaTemplateService getTemplateService(); | |||
| /** | |||
| * 初始化http请求对象 | |||
| */ | |||
| @@ -0,0 +1,89 @@ | |||
| package cn.binarywang.wx.miniapp.api; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import java.util.List; | |||
| public interface WxMaTemplateService { | |||
| //获取小程序模板库标题列表 | |||
| String TEMPLATE_LIBRARY_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list"; | |||
| //获取模板库某个模板标题下关键词库 | |||
| String TEMPLATE_LIBRARY_KEYWORD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get"; | |||
| //组合模板并添加至帐号下的个人模板库 | |||
| String TEMPLATE_ADD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add"; | |||
| //获取帐号下已存在的模板列表 | |||
| String TEMPLATE_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list"; | |||
| //删除帐号下的某个模板 | |||
| String TEMPLATE_DEL_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del"; | |||
| /** | |||
| * <pre> | |||
| * 获取小程序模板库标题列表 | |||
| * | |||
| * 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a> | |||
| * 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * @param offset | |||
| * @param count | |||
| * @return | |||
| */ | |||
| WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取模板库某个模板标题下关键词库 | |||
| * | |||
| * 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a> | |||
| * 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * @param id | |||
| * @return | |||
| */ | |||
| WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 组合模板并添加至帐号下的个人模板库 | |||
| * | |||
| * 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a> | |||
| * 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * @param id | |||
| * @param keywordIdList | |||
| * @return | |||
| */ | |||
| WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取帐号下已存在的模板列表 | |||
| * | |||
| * 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a> | |||
| * 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * @param offset | |||
| * @param count | |||
| * @return | |||
| */ | |||
| WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 删除帐号下的某个模板 | |||
| * | |||
| * 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a> | |||
| * 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * @param templateId | |||
| */ | |||
| boolean delTemplate(String templateId) throws WxErrorException; | |||
| } | |||
| @@ -38,6 +38,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
| private WxMaMediaService materialService = new WxMaMediaServiceImpl(this); | |||
| private WxMaUserService userService = new WxMaUserServiceImpl(this); | |||
| private WxMaQrcodeService qrCodeService = new WxMaQrcodeServiceImpl(this); | |||
| private WxMaTemplateService templateService = new WxMaTemplateServiceImpl(this); | |||
| private int retrySleepMillis = 1000; | |||
| private int maxRetryTimes = 5; | |||
| @@ -259,4 +260,9 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||
| public WxMaQrcodeService getQrcodeService() { | |||
| return this.qrCodeService; | |||
| } | |||
| @Override | |||
| public WxMaTemplateService getTemplateService() { | |||
| return this.templateService; | |||
| } | |||
| } | |||
| @@ -0,0 +1,101 @@ | |||
| package cn.binarywang.wx.miniapp.api.impl; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.api.WxMaTemplateService; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| public class WxMaTemplateServiceImpl implements WxMaTemplateService { | |||
| private WxMaService wxMaService; | |||
| public WxMaTemplateServiceImpl(WxMaService wxMaService){ | |||
| this.wxMaService = wxMaService; | |||
| } | |||
| @Override | |||
| public WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException { | |||
| Map<String, Integer> params = new HashMap<>(); | |||
| params.put("offset", offset); | |||
| params.put("count", count); | |||
| String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_LIST_URL, WxGsonBuilder.create().toJson(params)); | |||
| WxError wxError = WxError.fromJson(responseText); | |||
| if(wxError.getErrorCode() == 0){ | |||
| return WxMaTemplateLibraryListResult.fromJson(responseText); | |||
| }else { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException { | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("id", id); | |||
| String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_KEYWORD_URL, WxGsonBuilder.create().toJson(params)); | |||
| WxError wxError = WxError.fromJson(responseText); | |||
| if(wxError.getErrorCode() == 0){ | |||
| return WxMaTemplateLibraryGetResult.fromJson(responseText); | |||
| }else { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException { | |||
| Map<String, Object> params = new HashMap<>(); | |||
| params.put("id", id); | |||
| params.put("keyword_id_list", keywordIdList.toArray()); | |||
| String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, WxGsonBuilder.create().toJson(params)); | |||
| WxError wxError = WxError.fromJson(responseText); | |||
| if(wxError.getErrorCode() == 0){ | |||
| return WxMaTemplateAddResult.fromJson(responseText); | |||
| }else { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException { | |||
| Map<String, Integer> params = new HashMap<>(); | |||
| params.put("offset", offset); | |||
| params.put("count", count); | |||
| String responseText = this.wxMaService.post(TEMPLATE_LIST_URL, WxGsonBuilder.create().toJson(params)); | |||
| WxError wxError = WxError.fromJson(responseText); | |||
| if(wxError.getErrorCode() == 0){ | |||
| return WxMaTemplateListResult.fromJson(responseText); | |||
| }else { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } | |||
| @Override | |||
| public boolean delTemplate(String templateId) throws WxErrorException { | |||
| Map<String, String> params = new HashMap<>(); | |||
| params.put("template_id", templateId); | |||
| String responseText = this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params)); | |||
| WxError wxError = WxError.fromJson(responseText); | |||
| if(wxError.getErrorCode() == 0){ | |||
| return true; | |||
| }else { | |||
| throw new WxErrorException(wxError); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,20 @@ | |||
| package cn.binarywang.wx.miniapp.bean.template; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import lombok.Data; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import java.io.Serializable; | |||
| @Data | |||
| public class WxMaTemplateAddResult implements Serializable{ | |||
| private static final long serialVersionUID = 872250961973834465L; | |||
| @SerializedName("template_id") | |||
| private String templateId; | |||
| public static WxMaTemplateAddResult fromJson(String json){ | |||
| return WxGsonBuilder.create().fromJson(json, WxMaTemplateAddResult.class); | |||
| } | |||
| } | |||
| @@ -0,0 +1,31 @@ | |||
| package cn.binarywang.wx.miniapp.bean.template; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import lombok.Data; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.List; | |||
| @Data | |||
| public class WxMaTemplateLibraryGetResult implements Serializable{ | |||
| private static final long serialVersionUID = -190847592776636744L; | |||
| private String id; | |||
| private String title; | |||
| @SerializedName("keyword_list") | |||
| private List<KeywordInfo> keywordList; | |||
| @Data | |||
| public static class KeywordInfo{ | |||
| @SerializedName("keyword_id") | |||
| private int keywordId; | |||
| private String name; | |||
| private String example; | |||
| } | |||
| public static WxMaTemplateLibraryGetResult fromJson(String json){ | |||
| return WxGsonBuilder.create().fromJson(json, WxMaTemplateLibraryGetResult.class); | |||
| } | |||
| } | |||
| @@ -0,0 +1,28 @@ | |||
| package cn.binarywang.wx.miniapp.bean.template; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import lombok.Data; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.List; | |||
| @Data | |||
| public class WxMaTemplateLibraryListResult implements Serializable{ | |||
| private static final long serialVersionUID = -2780782521447602209L; | |||
| @SerializedName("total_count") | |||
| private int totalCount; | |||
| private List<TemplateItem> list; | |||
| public static WxMaTemplateLibraryListResult fromJson(String json){ | |||
| return WxGsonBuilder.create().fromJson(json, WxMaTemplateLibraryListResult.class); | |||
| } | |||
| @Data | |||
| public static class TemplateItem{ | |||
| private String id; | |||
| private String title; | |||
| } | |||
| } | |||
| @@ -0,0 +1,29 @@ | |||
| package cn.binarywang.wx.miniapp.bean.template; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import lombok.Data; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.List; | |||
| @Data | |||
| public class WxMaTemplateListResult implements Serializable{ | |||
| private static final long serialVersionUID = -7430535579782184537L; | |||
| private List<TemplateInfo> list; | |||
| public static WxMaTemplateListResult fromJson(String json){ | |||
| return WxGsonBuilder.create().fromJson(json, WxMaTemplateListResult.class); | |||
| } | |||
| @Data | |||
| public static class TemplateInfo{ | |||
| @SerializedName("template_id") | |||
| private String templateId; | |||
| private String title; | |||
| private String content; | |||
| private String example; | |||
| } | |||
| } | |||
| @@ -0,0 +1,70 @@ | |||
| package cn.binarywang.wx.miniapp.api.impl; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult; | |||
| import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult; | |||
| import cn.binarywang.wx.miniapp.test.ApiTestModule; | |||
| import com.google.inject.Inject; | |||
| import org.assertj.core.util.Lists; | |||
| import org.testng.Assert; | |||
| import org.testng.annotations.Guice; | |||
| import org.testng.annotations.Test; | |||
| import java.util.List; | |||
| @Test | |||
| @Guice(modules = ApiTestModule.class) | |||
| public class WxMaTemplateServiceImplTest { | |||
| @Inject | |||
| protected WxMaService wxService; | |||
| @Test | |||
| public void testFindTemplateLibraryList() throws Exception { | |||
| WxMaTemplateLibraryListResult result = this.wxService.getTemplateService().findTemplateLibraryList(0, 20); | |||
| Assert.assertEquals(20, result.getList().size()); | |||
| } | |||
| @Test | |||
| public void testFindTemplateLibraryKeywordList() throws Exception { | |||
| WxMaTemplateLibraryGetResult result = this.wxService.getTemplateService().findTemplateLibraryKeywordList("AT0004"); | |||
| Assert.assertEquals("AT0004", result.getId()); | |||
| Assert.assertEquals("交易提醒", result.getTitle()); | |||
| Assert.assertEquals(100, result.getKeywordList().size()); | |||
| } | |||
| @Test | |||
| public void testAddTemplate() throws Exception{ | |||
| List<Integer> list = Lists.newArrayList(); | |||
| list.add(1); | |||
| list.add(20); | |||
| list.add(84); | |||
| WxMaTemplateAddResult result = this.wxService.getTemplateService().addTemplate("AT0004", list); | |||
| Assert.assertNotNull(result.getTemplateId()); | |||
| System.out.println(result); | |||
| } | |||
| @Test | |||
| public void testFindTemplateList() throws Exception{ | |||
| WxMaTemplateListResult result = this.wxService.getTemplateService().findTemplateList(0, 20); | |||
| System.out.println(result); | |||
| } | |||
| @Test | |||
| public void testDelTemplate() throws Exception { | |||
| //add | |||
| List<Integer> list = Lists.newArrayList(); | |||
| list.add(1); | |||
| list.add(20); | |||
| list.add(84); | |||
| WxMaTemplateAddResult result = this.wxService.getTemplateService().addTemplate("AT0004", list); | |||
| //delete | |||
| this.wxService.getTemplateService().delTemplate(result.getTemplateId()); | |||
| } | |||
| } | |||