| @@ -327,7 +327,7 @@ public interface WxMpService { | |||
| * | |||
| * @return WxMpGroupService | |||
| */ | |||
| WxMpGroupService getGroupService(); | |||
| /** | |||
| @@ -364,4 +364,11 @@ public interface WxMpService { | |||
| * @return WxMpDataCubeService | |||
| */ | |||
| WxMpDataCubeService getDataCubeService(); | |||
| /** | |||
| * 返回用户黑名单管理相关接口的方法实现类,以方便调用其各种借口 | |||
| * | |||
| * @return WxMpUserBlackListService | |||
| */ | |||
| WxMpUserBlackListService getBlackListService(); | |||
| } | |||
| @@ -0,0 +1,35 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUserBlackListGetResult; | |||
| import java.util.List; | |||
| /** | |||
| * @author miller | |||
| */ | |||
| public interface WxMpUserBlackListService { | |||
| /** | |||
| * <pre> | |||
| * 获取公众号的黑名单列表 | |||
| * 详情请见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1471422259_pJMWA&token=&lang=zh_CN | |||
| * </pre> | |||
| */ | |||
| WxMpUserBlackListGetResult blackList(String nextOpenid) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 拉黑用户 | |||
| * 详情请见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1471422259_pJMWA&token=&lang=zh_CN | |||
| * </pre> | |||
| */ | |||
| void pushToBlackList(List<String> openIdList) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 取消拉黑用户 | |||
| * 详情请见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1471422259_pJMWA&token=&lang=zh_CN | |||
| * </pre> | |||
| */ | |||
| void pullFromBlackList(List<String> openIdList) throws WxErrorException; | |||
| } | |||
| @@ -1,23 +1,9 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.conn.ssl.DefaultHostnameVerifier; | |||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
| import org.apache.http.impl.client.BasicResponseHandler; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.google.gson.JsonArray; | |||
| import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| @@ -42,6 +28,7 @@ import me.chanjar.weixin.mp.api.WxMpMenuService; | |||
| import me.chanjar.weixin.mp.api.WxMpPayService; | |||
| import me.chanjar.weixin.mp.api.WxMpQrcodeService; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserBlackListService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserTagService; | |||
| import me.chanjar.weixin.mp.bean.WxMpIndustry; | |||
| @@ -57,6 +44,18 @@ import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.conn.ssl.DefaultHostnameVerifier; | |||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
| import org.apache.http.impl.client.BasicResponseHandler; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.io.IOException; | |||
| public class WxMpServiceImpl implements WxMpService { | |||
| @@ -75,7 +74,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
| private final Object globalJsapiTicketRefreshLock = new Object(); | |||
| private WxMpConfigStorage configStorage; | |||
| private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this); | |||
| private WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this); | |||
| @@ -96,6 +95,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
| private WxMpDataCubeService dataCubeService = new WxMpDataCubeServiceImpl(this); | |||
| private WxMpUserBlackListService blackListService = new WxMpUserBlackListServiceImpl(this); | |||
| private CloseableHttpClient httpClient; | |||
| private HttpHost httpProxy; | |||
| @@ -312,7 +313,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
| if (state != null) { | |||
| url.append("&state=").append(state); | |||
| } | |||
| url.append("#wechat_redirect"); | |||
| return url.toString(); | |||
| } | |||
| @@ -474,7 +475,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
| throw new RuntimeException(e); | |||
| } | |||
| } | |||
| public HttpHost getHttpProxy() { | |||
| return this.httpProxy; | |||
| } | |||
| @@ -495,7 +496,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
| if (null == apacheHttpClientBuilder) { | |||
| apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get(); | |||
| } | |||
| apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost()) | |||
| .httpProxyPort(this.configStorage.getHttpProxyPort()) | |||
| .httpProxyUsername(this.configStorage.getHttpProxyUsername()) | |||
| @@ -580,4 +581,9 @@ public class WxMpServiceImpl implements WxMpService { | |||
| return this.dataCubeService; | |||
| } | |||
| @Override | |||
| public WxMpUserBlackListService getBlackListService() { | |||
| return this.blackListService; | |||
| } | |||
| } | |||
| @@ -0,0 +1,50 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import com.google.gson.Gson; | |||
| import com.google.gson.JsonObject; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserBlackListService; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUserBlackListGetResult; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| /** | |||
| * @author miller | |||
| */ | |||
| public class WxMpUserBlackListServiceImpl implements WxMpUserBlackListService { | |||
| private static final String API_BLACK_LIST_PREFIX = "https://api.weixin.qq.com/cgi-bin/tags/members"; | |||
| private WxMpService wxMpService; | |||
| public WxMpUserBlackListServiceImpl(WxMpService wxMpService) { | |||
| this.wxMpService = wxMpService; | |||
| } | |||
| @Override | |||
| public WxMpUserBlackListGetResult blackList(String nextOpenid) throws WxErrorException { | |||
| JsonObject jsonObject = new JsonObject(); | |||
| jsonObject.addProperty("begin_openid", nextOpenid); | |||
| String url = API_BLACK_LIST_PREFIX + "/getblacklist"; | |||
| String responseContent = this.wxMpService.execute(new SimplePostRequestExecutor(), url, jsonObject.toString()); | |||
| return WxMpUserBlackListGetResult.fromJson(responseContent); | |||
| } | |||
| @Override | |||
| public void pushToBlackList(List<String> openIdList) throws WxErrorException { | |||
| Map<String, Object> map = new HashMap<>(); | |||
| map.put("openid_list", openIdList); | |||
| String url = API_BLACK_LIST_PREFIX + "/batchblacklist"; | |||
| this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); | |||
| } | |||
| @Override | |||
| public void pullFromBlackList(List<String> openIdList) throws WxErrorException { | |||
| Map<String, Object> map = new HashMap<>(); | |||
| map.put("openid_list", openIdList); | |||
| String url = API_BLACK_LIST_PREFIX + "/batchunblacklist"; | |||
| this.wxMpService.execute(new SimplePostRequestExecutor(), url, new Gson().toJson(map)); | |||
| } | |||
| } | |||
| @@ -0,0 +1,57 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| /** | |||
| * @author miller | |||
| */ | |||
| public class WxMpUserBlackListGetResult { | |||
| protected int total = -1; | |||
| protected int count = -1; | |||
| protected List<String> openIds = new ArrayList<>(); | |||
| protected String nextOpenId; | |||
| public static WxMpUserBlackListGetResult fromJson(String json) { | |||
| return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpUserBlackListGetResult.class); | |||
| } | |||
| public int getTotal() { | |||
| return this.total; | |||
| } | |||
| public void setTotal(int total) { | |||
| this.total = total; | |||
| } | |||
| public int getCount() { | |||
| return this.count; | |||
| } | |||
| public void setCount(int count) { | |||
| this.count = count; | |||
| } | |||
| public List<String> getOpenIds() { | |||
| return this.openIds; | |||
| } | |||
| public void setOpenIds(List<String> openIds) { | |||
| this.openIds = openIds; | |||
| } | |||
| public String getNextOpenId() { | |||
| return this.nextOpenId; | |||
| } | |||
| public void setNextOpenId(String nextOpenId) { | |||
| this.nextOpenId = nextOpenId; | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| return WxMpGsonBuilder.INSTANCE.create().toJson(this); | |||
| } | |||
| } | |||
| @@ -45,6 +45,7 @@ public class WxMpGsonBuilder { | |||
| INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | |||
| INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter()); | |||
| INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter()); | |||
| INSTANCE.registerTypeAdapter(WxMpUserBlackListGetResult.class, new WxUserBlackListGetResultGsonAdapter()); | |||
| } | |||
| public static Gson create() { | |||
| @@ -0,0 +1,28 @@ | |||
| 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.result.WxMpUserBlackListGetResult; | |||
| import java.lang.reflect.Type; | |||
| /** | |||
| * @author miller | |||
| */ | |||
| public class WxUserBlackListGetResultGsonAdapter implements JsonDeserializer<WxMpUserBlackListGetResult> { | |||
| @Override | |||
| public WxMpUserBlackListGetResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |||
| JsonObject o = json.getAsJsonObject(); | |||
| WxMpUserBlackListGetResult wxMpUserBlackListGetResult = new WxMpUserBlackListGetResult(); | |||
| wxMpUserBlackListGetResult.setTotal(GsonHelper.getInteger(o, "total")); | |||
| wxMpUserBlackListGetResult.setCount(GsonHelper.getInteger(o, "count")); | |||
| wxMpUserBlackListGetResult.setNextOpenId(GsonHelper.getString(o, "next_openid")); | |||
| if (o.get("data") != null && !o.get("data").isJsonNull() && !o.get("data").getAsJsonObject().get("openid").isJsonNull()) { | |||
| JsonArray data = o.get("data").getAsJsonObject().get("openid").getAsJsonArray(); | |||
| for (int i = 0; i < data.size(); i++) { | |||
| wxMpUserBlackListGetResult.getOpenIds().add(GsonHelper.getAsString(data.get(i))); | |||
| } | |||
| } | |||
| return wxMpUserBlackListGetResult; | |||
| } | |||
| } | |||
| @@ -0,0 +1,48 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import me.chanjar.weixin.mp.api.ApiTestModule; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUserBlackListGetResult; | |||
| import org.testng.Assert; | |||
| import org.testng.annotations.Guice; | |||
| import org.testng.annotations.Test; | |||
| import javax.inject.Inject; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| /** | |||
| * @author miller | |||
| */ | |||
| @Test(groups = "userAPI") | |||
| @Guice(modules = ApiTestModule.class) | |||
| public class WxMpUserBlackListServiceImplTest { | |||
| //此处openid只是开发的时候测试用 使用者测试的时候请替换自己公众号的openid | |||
| private final String TEST_OPENID = "o9VAswOI0KSXFUtFHgk9Kb9Rtkys"; | |||
| @Inject | |||
| protected WxMpServiceImpl wxService; | |||
| @Test | |||
| public void testBlackList() throws Exception { | |||
| WxMpUserBlackListGetResult wxMpUserBlackListGetResult = this.wxService.getBlackListService().blackList(TEST_OPENID); | |||
| Assert.assertNotNull(wxMpUserBlackListGetResult); | |||
| Assert.assertFalse(wxMpUserBlackListGetResult.getCount() == -1); | |||
| Assert.assertFalse(wxMpUserBlackListGetResult.getTotal() == -1); | |||
| Assert.assertFalse(wxMpUserBlackListGetResult.getOpenIds().size() == -1); | |||
| System.out.println(wxMpUserBlackListGetResult); | |||
| } | |||
| @Test | |||
| public void testPushToBlackList() throws Exception { | |||
| List<String> openIdList = new ArrayList<>(); | |||
| openIdList.add(TEST_OPENID); | |||
| this.wxService.getBlackListService().pushToBlackList(openIdList); | |||
| } | |||
| @Test | |||
| public void testPullFromBlackList() throws Exception { | |||
| List<String> openIdList = new ArrayList<>(); | |||
| openIdList.add(TEST_OPENID); | |||
| this.wxService.getBlackListService().pullFromBlackList(openIdList); | |||
| } | |||
| } | |||