mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-21 02:57:37 +08:00
🎨 优化重构微信小程序订阅消息模板设置相关方法
This commit is contained in:
@@ -36,6 +36,7 @@ public class ApacheSimplePostRequestExecutor extends SimplePostRequestExecutor<C
|
|||||||
|
|
||||||
if (postEntity != null) {
|
if (postEntity != null) {
|
||||||
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
|
StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
|
||||||
|
entity.setContentType("application/json; charset=utf-8");
|
||||||
httpPost.setEntity(entity);
|
httpPost.setEntity(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -86,6 +86,11 @@ public interface WxMaService {
|
|||||||
*/
|
*/
|
||||||
String post(String url, String postData) throws WxErrorException;
|
String post(String url, String postData) throws WxErrorException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
|
||||||
|
*/
|
||||||
|
String post(String url, Object obj) throws WxErrorException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* Service没有实现某个API的时候,可以用这个,
|
* Service没有实现某个API的时候,可以用这个,
|
||||||
|
@@ -16,6 +16,7 @@ import me.chanjar.weixin.common.util.crypto.SHA1;
|
|||||||
import me.chanjar.weixin.common.util.http.*;
|
import me.chanjar.weixin.common.util.http.*;
|
||||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||||
|
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
@@ -206,6 +207,11 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
|
|||||||
return execute(SimplePostRequestExecutor.create(this), url, postData);
|
return execute(SimplePostRequestExecutor.create(this), url, postData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String post(String url, Object obj) throws WxErrorException {
|
||||||
|
return this.execute(SimplePostRequestExecutor.create(this), url, WxGsonBuilder.create().toJson(obj));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
|
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
|
||||||
*/
|
*/
|
||||||
|
@@ -9,12 +9,9 @@ import com.google.gson.JsonParser;
|
|||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
@@ -26,16 +23,14 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMaTemplateLibraryListResult getPubTemplateTitleList(Integer[] ids, int start, int limit) throws WxErrorException {
|
public WxMaTemplateLibraryListResult getPubTemplateTitleList(Integer[] ids, int start, int limit) throws WxErrorException {
|
||||||
ImmutableMap<String, ? extends Serializable> params = ImmutableMap.of("ids", StringUtils.join(ids, ","),
|
return WxMaTemplateLibraryListResult.fromJson(this.wxMaService.post(GET_PUB_TEMPLATE_TITLE_LIST_URL,
|
||||||
"start", start, "limit", limit);
|
ImmutableMap.of("ids", StringUtils.join(ids, ","),
|
||||||
String responseText = this.wxMaService.post(GET_PUB_TEMPLATE_TITLE_LIST_URL, WxGsonBuilder.create().toJson(params));
|
"start", start, "limit", limit)));
|
||||||
return WxMaTemplateLibraryListResult.fromJson(responseText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException {
|
public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException {
|
||||||
String responseText = this.wxMaService.post(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL,
|
String responseText = this.wxMaService.post(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL, ImmutableMap.of("tid", id));
|
||||||
WxGsonBuilder.create().toJson(ImmutableMap.of("tid", id)));
|
|
||||||
return WxMaGsonBuilder.create().fromJson(new JsonParser().parse(responseText).getAsJsonObject()
|
return WxMaGsonBuilder.create().fromJson(new JsonParser().parse(responseText).getAsJsonObject()
|
||||||
.getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() {
|
.getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() {
|
||||||
}.getType());
|
}.getType());
|
||||||
@@ -43,9 +38,9 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException {
|
public String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException {
|
||||||
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, WxGsonBuilder.create().toJson(ImmutableMap.of("tid", id,
|
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, ImmutableMap.of("tid", id,
|
||||||
"kidList", keywordIdList.toArray(),
|
"kidList", keywordIdList.toArray(),
|
||||||
"sceneDesc", sceneDesc)));
|
"sceneDesc", sceneDesc));
|
||||||
return new JsonParser().parse(responseText).getAsJsonObject().get("priTmplId").getAsString();
|
return new JsonParser().parse(responseText).getAsJsonObject().get("priTmplId").getAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,8 +54,7 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean delTemplate(String templateId) throws WxErrorException {
|
public boolean delTemplate(String templateId) throws WxErrorException {
|
||||||
Map<String, String> params = ImmutableMap.of("priTmplId", templateId);
|
this.wxMaService.post(TEMPLATE_DEL_URL, ImmutableMap.of("priTmplId", templateId));
|
||||||
this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -38,7 +38,7 @@ public class WxMaSubscribeServiceImplTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddTemplate() throws WxErrorException {
|
public void testAddTemplate() throws WxErrorException {
|
||||||
final String templateId = this.wxService.getSubscribeService().addTemplate("1", Lists.newArrayList(1), "");
|
final String templateId = this.wxService.getSubscribeService().addTemplate("1", Lists.newArrayList(1), "hhaa");
|
||||||
System.out.println(templateId);
|
System.out.println(templateId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user