Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public interface WxMaService {
*/
WxMaQrcodeService getQrcodeService();

/**
* 返回模板配置相关接口方法的实现类对象, 以方便调用其各个接口
* @return WxMaTemplateService
*/
WxMaTemplateService getTemplateService();

/**
* 初始化http请求对象
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -259,4 +260,9 @@ public WxMaUserService getUserService() {
public WxMaQrcodeService getQrcodeService() {
return this.qrCodeService;
}

@Override
public WxMaTemplateService getTemplateService() {
return this.templateService;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading