Ver a proveniência

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

master
Binary Wang há 8 anos
ascendente
cometimento
6dc0481d1d
10 ficheiros alterados com 196 adições e 36 eliminações
  1. +20
    -5
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java
  2. +14
    -5
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java
  3. +126
    -0
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplate.java
  4. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateData.java
  5. +6
    -6
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateIndustry.java
  6. +1
    -1
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java
  7. +3
    -3
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java
  8. +7
    -7
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpIndustryGsonAdapter.java
  9. +3
    -2
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpTemplateMessageGsonAdapter.java
  10. +15
    -6
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java

+ 20
- 5
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java Ver ficheiro

@@ -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>
@@ -53,8 +56,20 @@ public interface WxMpTemplateMsgService {
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
* </pre>
*@param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
* @param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
* @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;
}

+ 14
- 5
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java Ver ficheiro

@@ -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));
}

}

+ 126
- 0
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplate.java Ver ficheiro

@@ -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;
}

}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateData.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateData.java Ver ficheiro

@@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.bean;
package me.chanjar.weixin.mp.bean.template;

import java.io.Serializable;


weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpIndustry.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateIndustry.java Ver ficheiro

@@ -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() {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/WxMpTemplateMessage.java → weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java Ver ficheiro

@@ -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;

+ 3
- 3
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpGsonBuilder.java Ver ficheiro

@@ -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());
}



+ 7
- 7
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpIndustryGsonAdapter.java Ver ficheiro

@@ -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;


+ 3
- 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpTemplateMessageGsonAdapter.java Ver ficheiro

@@ -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;



+ 15
- 6
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImplTest.java Ver ficheiro

@@ -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);
}

}

Carregando…
Cancelar
Guardar