添加获得模板列表的接口方法,并重构模板消息相关类包结构 for issue #63

This commit is contained in:
Binary Wang 2016-10-18 00:07:14 +08:00
parent d7298ab790
commit 6dc0481d1d
10 changed files with 196 additions and 36 deletions

View File

@ -1,8 +1,11 @@
package me.chanjar.weixin.mp.api;
import java.util.List;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
/**
* <pre>
@ -24,7 +27,7 @@ public interface WxMpTemplateMsgService {
*
* @return 是否成功
*/
boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException;
boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException;
/***
* <pre>
@ -34,7 +37,7 @@ public interface WxMpTemplateMsgService {
*
* @return wxMpIndustry
*/
WxMpIndustry getIndustry() throws WxErrorException;
WxMpTemplateIndustry getIndustry() throws WxErrorException;
/**
* <pre>
@ -57,4 +60,16 @@ public interface WxMpTemplateMsgService {
* @return templateId 模板Id
*/
String addTemplate(String shortTemplateId) throws WxErrorException;
/**
* <pre>
* 获取模板列表
* 获取已添加至帐号下所有模板列表可在MP中查看模板列表信息为方便第三方开发者提供通过接口调用的方式来获取帐号下所有模板信息
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
* 接口地址格式https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN
* </pre>
*
* @return templateId 模板Id
*/
List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException;
}

View File

@ -1,5 +1,7 @@
package me.chanjar.weixin.mp.api.impl;
import java.util.List;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
@ -7,8 +9,9 @@ import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
/**
* <pre>
@ -38,7 +41,7 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
}
@Override
public boolean setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
public boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException {
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
throw new IllegalArgumentException("行业Id不能为空请核实");
@ -50,10 +53,10 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
}
@Override
public WxMpIndustry getIndustry() throws WxErrorException {
public WxMpTemplateIndustry getIndustry() throws WxErrorException {
String url = API_URL_PREFIX + "/get_industry";
String responseContent = this.wxMpService.get(url, null);
return WxMpIndustry.fromJson(responseContent);
return WxMpTemplateIndustry.fromJson(responseContent);
}
@Override
@ -70,4 +73,10 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
throw new WxErrorException(WxError.fromJson(responseContent));
}
@Override
public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException {
String url = API_URL_PREFIX + "/get_all_private_template";
return WxMpTemplate.fromJson(this.wxMpService.get(url, null));
}
}

View File

@ -0,0 +1,126 @@
package me.chanjar.weixin.mp.bean.template;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
/**
* <pre>
* 模板列表信息
* Created by Binary Wang on 2016-10-17.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
public class WxMpTemplate {
private static final JsonParser JSON_PARSER = new JsonParser();
public static List<WxMpTemplate> fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(JSON_PARSER.parse(json).getAsJsonObject().get("template_list"),
new TypeToken<List<WxMpTemplate>>() {
}.getType());
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
/**
* template_id
* 模板ID
*/
@SerializedName("template_id")
private String templateId;
/**
* title
* 模板标题
*/
@SerializedName("title")
private String title;
/**
* primary_industry
* 模板所属行业的一级行业
*/
@SerializedName("primary_industry")
private String primaryIndustry;
/**
* deputy_industry
* 模板所属行业的二级行业
*/
@SerializedName("deputy_industry")
private String deputyIndustry;
/**
* content
* 模板内容
*/
@SerializedName("content")
private String content;
/**
* example
* 模板示例
*/
@SerializedName("example")
private String example;
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrimaryIndustry() {
return primaryIndustry;
}
public void setPrimaryIndustry(String primaryIndustry) {
this.primaryIndustry = primaryIndustry;
}
public String getDeputyIndustry() {
return deputyIndustry;
}
public void setDeputyIndustry(String deputyIndustry) {
this.deputyIndustry = deputyIndustry;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getExample() {
return example;
}
public void setExample(String example) {
this.example = example;
}
}

View File

@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.bean;
package me.chanjar.weixin.mp.bean.template;
import java.io.Serializable;

View File

@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.bean;
package me.chanjar.weixin.mp.bean.template;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
@ -10,15 +10,15 @@ import java.io.Serializable;
/**
* @author miller
*/
public class WxMpIndustry implements Serializable {
public class WxMpTemplateIndustry implements Serializable {
private static final long serialVersionUID = -7700398224795914722L;
private Industry primaryIndustry;
private Industry secondIndustry;
public WxMpIndustry() {
public WxMpTemplateIndustry() {
}
public WxMpIndustry(Industry primaryIndustry, Industry secondIndustry) {
public WxMpTemplateIndustry(Industry primaryIndustry, Industry secondIndustry) {
this.primaryIndustry = primaryIndustry;
this.secondIndustry = secondIndustry;
}
@ -81,8 +81,8 @@ public class WxMpIndustry implements Serializable {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
public static WxMpIndustry fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(json, WxMpIndustry.class);
public static WxMpTemplateIndustry fromJson(String json) {
return WxMpGsonBuilder.create().fromJson(json, WxMpTemplateIndustry.class);
}
public String toJson() {

View File

@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.bean;
package me.chanjar.weixin.mp.bean.template;
import java.io.Serializable;
import java.util.ArrayList;

View File

@ -5,7 +5,6 @@ import com.google.gson.GsonBuilder;
import me.chanjar.weixin.mp.bean.WxMpCard;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
@ -13,7 +12,6 @@ import me.chanjar.weixin.mp.bean.WxMpMassTagMessage;
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate;
import me.chanjar.weixin.mp.bean.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult;
@ -31,6 +29,8 @@ import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserBlacklistGetResult;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
public class WxMpGsonBuilder {
@ -68,7 +68,7 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpTemplateIndustry.class, new WxMpIndustryGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpUserBlacklistGetResult.class, new WxUserBlacklistGetResultGsonAdapter());
}

View File

@ -2,7 +2,7 @@ package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import java.lang.reflect.Type;
@ -10,9 +10,9 @@ import java.lang.reflect.Type;
* @author miller
*/
public class WxMpIndustryGsonAdapter
implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> {
implements JsonSerializer<WxMpTemplateIndustry>, JsonDeserializer<WxMpTemplateIndustry> {
@Override
public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type,
public JsonElement serialize(WxMpTemplateIndustry wxMpIndustry, Type type,
JsonSerializationContext jsonSerializationContext) {
JsonObject json = new JsonObject();
json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId());
@ -21,10 +21,10 @@ public class WxMpIndustryGsonAdapter
}
@Override
public WxMpIndustry deserialize(JsonElement jsonElement, Type type,
public WxMpTemplateIndustry deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
WxMpIndustry wxMpIndustry = new WxMpIndustry();
WxMpTemplateIndustry wxMpIndustry = new WxMpTemplateIndustry();
JsonObject primaryIndustry = jsonElement.getAsJsonObject()
.get("primary_industry").getAsJsonObject();
wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry));
@ -34,8 +34,8 @@ public class WxMpIndustryGsonAdapter
return wxMpIndustry;
}
private static WxMpIndustry.Industry convertFromJson(JsonObject json) {
WxMpIndustry.Industry industry = new WxMpIndustry.Industry();
private static WxMpTemplateIndustry.Industry convertFromJson(JsonObject json) {
WxMpTemplateIndustry.Industry industry = new WxMpTemplateIndustry.Industry();
industry.setFirstClass(GsonHelper.getString(json, "first_class"));
industry.setSecondClass(GsonHelper.getString(json, "second_class"));
return industry;

View File

@ -12,8 +12,9 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import java.lang.reflect.Type;

View File

@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.Guice;
@ -12,9 +13,10 @@ import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.ApiTestModule;
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.bean.WxMpIndustry;
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
/**
* <pre>
@ -45,15 +47,15 @@ public class WxMpTemplateMsgServiceImplTest {
@Test
public void testGetIndustry() throws Exception {
final WxMpIndustry industry = this.wxService.getTemplateMsgService().getIndustry();
final WxMpTemplateIndustry industry = this.wxService.getTemplateMsgService().getIndustry();
Assert.assertNotNull(industry);
System.out.println(industry);
}
@Test
public void testSetIndustry() throws Exception {
WxMpIndustry industry = new WxMpIndustry(new WxMpIndustry.Industry("1"),
new WxMpIndustry.Industry("04"));
WxMpTemplateIndustry industry = new WxMpTemplateIndustry(new WxMpTemplateIndustry.Industry("1"),
new WxMpTemplateIndustry.Industry("04"));
boolean result = this.wxService.getTemplateMsgService().setIndustry(industry);
Assert.assertTrue(result);
}
@ -65,4 +67,11 @@ public class WxMpTemplateMsgServiceImplTest {
System.err.println(result);
}
@Test
public void testGetAllPrivateTemplate() throws Exception {
List<WxMpTemplate> result = this.wxService.getTemplateMsgService().getAllPrivateTemplate();
Assert.assertNotNull(result);
System.err.println(result);
}
}