mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	重构部分代码,抽取重复代码为单独方法,并调整部分代码顺序
This commit is contained in:
		| @ -21,6 +21,7 @@ import org.apache.http.HttpHost; | |||||||
| import org.apache.http.client.config.RequestConfig; | import org.apache.http.client.config.RequestConfig; | ||||||
| import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; | ||||||
| import org.apache.http.client.methods.HttpGet; | 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.conn.ssl.SSLConnectionSocketFactory; | ||||||
| import org.apache.http.impl.client.BasicResponseHandler; | import org.apache.http.impl.client.BasicResponseHandler; | ||||||
| import org.apache.http.impl.client.CloseableHttpClient; | import org.apache.http.impl.client.CloseableHttpClient; | ||||||
| @ -209,6 +210,13 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     return WxMpMassSendResult.fromJson(responseContent); |     return WxMpMassSendResult.fromJson(responseContent); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception { | ||||||
|  |     String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"; | ||||||
|  |     String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson()); | ||||||
|  |     return WxMpMassSendResult.fromJson(responseContent); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public String shortUrl(String long_url) throws WxErrorException { |   public String shortUrl(String long_url) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/shorturl"; |     String url = "https://api.weixin.qq.com/cgi-bin/shorturl"; | ||||||
| @ -226,11 +234,30 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); |     String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); | ||||||
|     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); |     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||||
|     final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); |     final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); | ||||||
|     if (jsonObject.get("errcode").getAsInt() == 0) |     if (jsonObject.get("errcode").getAsInt() == 0){ | ||||||
|       return jsonObject.get("msgid").getAsString(); |       return jsonObject.get("msgid").getAsString(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     throw new WxErrorException(WxError.fromJson(responseContent)); |     throw new WxErrorException(WxError.fromJson(responseContent)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException { | ||||||
|  |     if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId() | ||||||
|  |             || null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) { | ||||||
|  |       throw new IllegalArgumentException("industry id is empty"); | ||||||
|  |     } | ||||||
|  |     String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry"; | ||||||
|  |     return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public WxMpIndustry getIndustry() throws WxErrorException { | ||||||
|  |     String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry"; | ||||||
|  |     String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | ||||||
|  |     return WxMpIndustry.fromJson(responseContent); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException { |   public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/semantic/semproxy/search"; |     String url = "https://api.weixin.qq.com/semantic/semproxy/search"; | ||||||
| @ -253,6 +280,16 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     return url.toString(); |     return url.toString(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException { | ||||||
|  |     try { | ||||||
|  |       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | ||||||
|  |       String responseText = executor.execute(this.getHttpclient(), this.httpProxy, url.toString(), null); | ||||||
|  |       return WxMpOAuth2AccessToken.fromJson(responseText); | ||||||
|  |     } catch (IOException e) { | ||||||
|  |       throw new RuntimeException(e); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { |   public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { | ||||||
|     StringBuilder url = new StringBuilder(); |     StringBuilder url = new StringBuilder(); | ||||||
| @ -262,13 +299,7 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     url.append("&code=").append(code); |     url.append("&code=").append(code); | ||||||
|     url.append("&grant_type=authorization_code"); |     url.append("&grant_type=authorization_code"); | ||||||
|  |  | ||||||
|     try { |     return this.getOAuth2AccessToken(url); | ||||||
|       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |  | ||||||
|       String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); |  | ||||||
|       return WxMpOAuth2AccessToken.fromJson(responseText); |  | ||||||
|     } catch (IOException e) { |  | ||||||
|       throw new RuntimeException(e); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -279,13 +310,7 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     url.append("&grant_type=refresh_token"); |     url.append("&grant_type=refresh_token"); | ||||||
|     url.append("&refresh_token=").append(refreshToken); |     url.append("&refresh_token=").append(refreshToken); | ||||||
|  |  | ||||||
|     try { |     return this.getOAuth2AccessToken(url); | ||||||
|       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |  | ||||||
|       String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); |  | ||||||
|       return WxMpOAuth2AccessToken.fromJson(responseText); |  | ||||||
|     } catch (IOException e) { |  | ||||||
|       throw new RuntimeException(e); |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
| @ -440,10 +465,7 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|     if (this.wxMpConfigStorage.getSSLContext() != null){ |     if (this.wxMpConfigStorage.getSSLContext() != null){ | ||||||
|       SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( |       SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( | ||||||
|           this.wxMpConfigStorage.getSSLContext(), |           this.wxMpConfigStorage.getSSLContext(), new String[] { "TLSv1" }, null, new DefaultHostnameVerifier()); | ||||||
|           new String[] { "TLSv1" }, |  | ||||||
|           null, |  | ||||||
|           SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); |  | ||||||
|       apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); |       apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @ -460,36 +482,11 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     this.retrySleepMillis = retrySleepMillis; |     this.retrySleepMillis = retrySleepMillis; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public void setMaxRetryTimes(int maxRetryTimes) { |   public void setMaxRetryTimes(int maxRetryTimes) { | ||||||
|     this.maxRetryTimes = maxRetryTimes; |     this.maxRetryTimes = maxRetryTimes; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |  | ||||||
|   public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception { |  | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"; |  | ||||||
|     String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson()); |  | ||||||
|     return WxMpMassSendResult.fromJson(responseContent); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @Override |  | ||||||
|   public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException { |  | ||||||
|     if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId() |  | ||||||
|         || null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) { |  | ||||||
|       throw new IllegalArgumentException("industry id is empty"); |  | ||||||
|     } |  | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry"; |  | ||||||
|     return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson()); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @Override |  | ||||||
|   public WxMpIndustry getIndustry() throws WxErrorException { |  | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry"; |  | ||||||
|     String responseContent = execute(new SimpleGetRequestExecutor(), url, null); |  | ||||||
|     return WxMpIndustry.fromJson(responseContent); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKefuService getKefuService() { |   public WxMpKefuService getKefuService() { | ||||||
|     return this.kefuService; |     return this.kefuService; | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 BinaryWang
					BinaryWang