mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	修改了部分接口字符串拼接的模式,从 + 改为StringBuffer
This commit is contained in:
		| @ -69,9 +69,9 @@ public interface WxMpConfigStorage { | |||||||
|  |  | ||||||
|   public String getSecret(); |   public String getSecret(); | ||||||
|  |  | ||||||
|     public String getPartnerId(); |   public String getPartnerId(); | ||||||
|  |    | ||||||
|     public String getPartnerKey(); |   public String getPartnerKey(); | ||||||
|  |  | ||||||
|   public String getToken(); |   public String getToken(); | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,232 +1,232 @@ | |||||||
| package me.chanjar.weixin.mp.api; | package me.chanjar.weixin.mp.api; | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.session.InternalSession; | import me.chanjar.weixin.common.session.InternalSession; | ||||||
| import me.chanjar.weixin.common.session.InternalSessionManager; | import me.chanjar.weixin.common.session.InternalSessionManager; | ||||||
| import me.chanjar.weixin.common.session.StandardSessionManager; | import me.chanjar.weixin.common.session.StandardSessionManager; | ||||||
| import me.chanjar.weixin.common.session.WxSessionManager; | import me.chanjar.weixin.common.session.WxSessionManager; | ||||||
| import me.chanjar.weixin.common.util.LogExceptionHandler; | import me.chanjar.weixin.common.util.LogExceptionHandler; | ||||||
| import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | ||||||
| import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; | import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; | ||||||
| import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; | import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.concurrent.ExecutionException; | import java.util.concurrent.ExecutionException; | ||||||
| import java.util.concurrent.ExecutorService; | import java.util.concurrent.ExecutorService; | ||||||
| import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | ||||||
| import java.util.concurrent.Future; | import java.util.concurrent.Future; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * <pre> |  * <pre> | ||||||
|  * 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 |  * 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | ||||||
|  *  |  *  | ||||||
|  * 说明: |  * 说明: | ||||||
|  * 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 |  * 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | ||||||
|  * 2. 默认情况下消息只会被处理一次,除非使用 {@link WxMpMessageRouterRule#next()} |  * 2. 默认情况下消息只会被处理一次,除非使用 {@link WxMpMessageRouterRule#next()} | ||||||
|  * 3. 规则的结束必须用{@link WxMpMessageRouterRule#end()}或者{@link WxMpMessageRouterRule#next()},否则不会生效 |  * 3. 规则的结束必须用{@link WxMpMessageRouterRule#end()}或者{@link WxMpMessageRouterRule#next()},否则不会生效 | ||||||
|  *  |  *  | ||||||
|  * 使用方法: |  * 使用方法: | ||||||
|  * WxMpMessageRouter router = new WxMpMessageRouter(); |  * WxMpMessageRouter router = new WxMpMessageRouter(); | ||||||
|  * router |  * router | ||||||
|  *   .rule() |  *   .rule() | ||||||
|  *       .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") |  *       .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | ||||||
|  *       .interceptor(interceptor, ...).handler(handler, ...) |  *       .interceptor(interceptor, ...).handler(handler, ...) | ||||||
|  *   .end() |  *   .end() | ||||||
|  *   .rule() |  *   .rule() | ||||||
|  *       // 另外一个匹配规则 |  *       // 另外一个匹配规则 | ||||||
|  *   .end() |  *   .end() | ||||||
|  * ; |  * ; | ||||||
|  *  |  *  | ||||||
|  * // 将WxXmlMessage交给消息路由器 |  * // 将WxXmlMessage交给消息路由器 | ||||||
|  * router.route(message); |  * router.route(message); | ||||||
|  *  |  *  | ||||||
|  * </pre> |  * </pre> | ||||||
|  * @author Daniel Qian |  * @author Daniel Qian | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class WxMpMessageRouter { | public class WxMpMessageRouter { | ||||||
|  |  | ||||||
|   protected final Logger log = LoggerFactory.getLogger(WxMpMessageRouter.class); |   protected final Logger log = LoggerFactory.getLogger(WxMpMessageRouter.class); | ||||||
|  |  | ||||||
|   private static final int DEFAULT_THREAD_POOL_SIZE = 100; |   private static final int DEFAULT_THREAD_POOL_SIZE = 100; | ||||||
|  |  | ||||||
|   private final List<WxMpMessageRouterRule> rules = new ArrayList<WxMpMessageRouterRule>(); |   private final List<WxMpMessageRouterRule> rules = new ArrayList<WxMpMessageRouterRule>(); | ||||||
|  |  | ||||||
|   private final WxMpService wxMpService; |   private final WxMpService wxMpService; | ||||||
|  |  | ||||||
|   private ExecutorService executorService; |   private ExecutorService executorService; | ||||||
|  |  | ||||||
|   private WxMessageDuplicateChecker messageDuplicateChecker; |   private WxMessageDuplicateChecker messageDuplicateChecker; | ||||||
|  |  | ||||||
|   private WxSessionManager sessionManager; |   private WxSessionManager sessionManager; | ||||||
|  |  | ||||||
|   private WxErrorExceptionHandler exceptionHandler; |   private WxErrorExceptionHandler exceptionHandler; | ||||||
|  |  | ||||||
|   public WxMpMessageRouter(WxMpService wxMpService) { |   public WxMpMessageRouter(WxMpService wxMpService) { | ||||||
|     this.wxMpService = wxMpService; |     this.wxMpService = wxMpService; | ||||||
|     this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); |     this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); | ||||||
|     this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker(); |     this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker(); | ||||||
|     this.sessionManager = new StandardSessionManager(); |     this.sessionManager = new StandardSessionManager(); | ||||||
|     this.exceptionHandler = new LogExceptionHandler(); |     this.exceptionHandler = new LogExceptionHandler(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 设置自定义的 {@link ExecutorService} |    * 设置自定义的 {@link ExecutorService} | ||||||
|    * 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100) |    * 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100) | ||||||
|    * </pre> |    * </pre> | ||||||
|    * @param executorService |    * @param executorService | ||||||
|    */ |    */ | ||||||
|   public void setExecutorService(ExecutorService executorService) { |   public void setExecutorService(ExecutorService executorService) { | ||||||
|     this.executorService = executorService; |     this.executorService = executorService; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker} |    * 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker} | ||||||
|    * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker} |    * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker} | ||||||
|    * </pre> |    * </pre> | ||||||
|    * @param messageDuplicateChecker |    * @param messageDuplicateChecker | ||||||
|    */ |    */ | ||||||
|   public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) { |   public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) { | ||||||
|     this.messageDuplicateChecker = messageDuplicateChecker; |     this.messageDuplicateChecker = messageDuplicateChecker; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager} |    * 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager} | ||||||
|    * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager} |    * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager} | ||||||
|    * </pre> |    * </pre> | ||||||
|    * @param sessionManager |    * @param sessionManager | ||||||
|    */ |    */ | ||||||
|   public void setSessionManager(WxSessionManager sessionManager) { |   public void setSessionManager(WxSessionManager sessionManager) { | ||||||
|     this.sessionManager = sessionManager; |     this.sessionManager = sessionManager; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler} |    * 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler} | ||||||
|    * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler} |    * 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler} | ||||||
|    * </pre> |    * </pre> | ||||||
|    * @param exceptionHandler |    * @param exceptionHandler | ||||||
|    */ |    */ | ||||||
|   public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) { |   public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) { | ||||||
|     this.exceptionHandler = exceptionHandler; |     this.exceptionHandler = exceptionHandler; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   List<WxMpMessageRouterRule> getRules() { |   List<WxMpMessageRouterRule> getRules() { | ||||||
|     return this.rules; |     return this.rules; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 开始一个新的Route规则 |    * 开始一个新的Route规则 | ||||||
|    * @return |    * @return | ||||||
|    */ |    */ | ||||||
|   public WxMpMessageRouterRule rule() { |   public WxMpMessageRouterRule rule() { | ||||||
|     return new WxMpMessageRouterRule(this); |     return new WxMpMessageRouterRule(this); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 处理微信消息 |    * 处理微信消息 | ||||||
|    * @param wxMessage |    * @param wxMessage | ||||||
|    */ |    */ | ||||||
|   public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage) { |   public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage) { | ||||||
|     if (isDuplicateMessage(wxMessage)) { |     if (isDuplicateMessage(wxMessage)) { | ||||||
|       // 如果是重复消息,那么就不做处理 |       // 如果是重复消息,那么就不做处理 | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     final List<WxMpMessageRouterRule> matchRules = new ArrayList<WxMpMessageRouterRule>(); |     final List<WxMpMessageRouterRule> matchRules = new ArrayList<WxMpMessageRouterRule>(); | ||||||
|     // 收集匹配的规则 |     // 收集匹配的规则 | ||||||
|     for (final WxMpMessageRouterRule rule : rules) { |     for (final WxMpMessageRouterRule rule : rules) { | ||||||
|       if (rule.test(wxMessage)) { |       if (rule.test(wxMessage)) { | ||||||
|         matchRules.add(rule); |         matchRules.add(rule); | ||||||
|         if(!rule.isReEnter()) { |         if(!rule.isReEnter()) { | ||||||
|           break; |           break; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (matchRules.size() == 0) { |     if (matchRules.size() == 0) { | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     WxMpXmlOutMessage res = null; |     WxMpXmlOutMessage res = null; | ||||||
|     final List<Future> futures = new ArrayList<Future>(); |     final List<Future> futures = new ArrayList<Future>(); | ||||||
|     for (final WxMpMessageRouterRule rule : matchRules) { |     for (final WxMpMessageRouterRule rule : matchRules) { | ||||||
|       // 返回最后一个非异步的rule的执行结果 |       // 返回最后一个非异步的rule的执行结果 | ||||||
|       if(rule.isAsync()) { |       if(rule.isAsync()) { | ||||||
|         futures.add( |         futures.add( | ||||||
|             executorService.submit(new Runnable() { |             executorService.submit(new Runnable() { | ||||||
|               public void run() { |               public void run() { | ||||||
|                 rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); |                 rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); | ||||||
|               } |               } | ||||||
|             }) |             }) | ||||||
|         ); |         ); | ||||||
|       } else { |       } else { | ||||||
|         res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); |         res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); | ||||||
|         // 在同步操作结束,session访问结束 |         // 在同步操作结束,session访问结束 | ||||||
|         log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); |         log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); | ||||||
|         sessionEndAccess(wxMessage); |         sessionEndAccess(wxMessage); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (futures.size() > 0) { |     if (futures.size() > 0) { | ||||||
|       executorService.submit(new Runnable() { |       executorService.submit(new Runnable() { | ||||||
|         @Override |         @Override | ||||||
|         public void run() { |         public void run() { | ||||||
|           for (Future future : futures) { |           for (Future future : futures) { | ||||||
|             try { |             try { | ||||||
|               future.get(); |               future.get(); | ||||||
|               log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); |               log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); | ||||||
|               // 异步操作结束,session访问结束 |               // 异步操作结束,session访问结束 | ||||||
|               sessionEndAccess(wxMessage); |               sessionEndAccess(wxMessage); | ||||||
|             } catch (InterruptedException e) { |             } catch (InterruptedException e) { | ||||||
|               log.error("Error happened when wait task finish", e); |               log.error("Error happened when wait task finish", e); | ||||||
|             } catch (ExecutionException e) { |             } catch (ExecutionException e) { | ||||||
|               log.error("Error happened when wait task finish", e); |               log.error("Error happened when wait task finish", e); | ||||||
|             } |             } | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
|       }); |       }); | ||||||
|     } |     } | ||||||
|     return res; |     return res; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) { |   protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) { | ||||||
|  |  | ||||||
|     String messageId = ""; |     StringBuffer messageId = new StringBuffer(); | ||||||
|     if (wxMessage.getMsgId() == null) { |     if (wxMessage.getMsgId() == null) { | ||||||
|       messageId = String.valueOf(wxMessage.getCreateTime()) |       messageId.append(wxMessage.getCreateTime()) | ||||||
|           + "-" + wxMessage.getFromUserName() |         .append("-").append(wxMessage.getFromUserName()) | ||||||
|           + "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) |         .append("-").append(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | ||||||
|           + "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) |         .append("-").append(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | ||||||
|       ; |       ; | ||||||
|     } else { |     } else { | ||||||
|       messageId = String.valueOf(wxMessage.getMsgId()); |       messageId.append(wxMessage.getMsgId()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (messageDuplicateChecker.isDuplicate(messageId)) { |     if (messageDuplicateChecker.isDuplicate(messageId.toString())) { | ||||||
|       return true; |       return true; | ||||||
|     } |     } | ||||||
|     return false; |     return false; | ||||||
|  |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 对session的访问结束 |    * 对session的访问结束 | ||||||
|    * @param wxMessage |    * @param wxMessage | ||||||
|    */ |    */ | ||||||
|   protected void sessionEndAccess(WxMpXmlMessage wxMessage) { |   protected void sessionEndAccess(WxMpXmlMessage wxMessage) { | ||||||
|  |  | ||||||
|     InternalSession session = ((InternalSessionManager)sessionManager).findSession(wxMessage.getFromUserName()); |     InternalSession session = ((InternalSessionManager)sessionManager).findSession(wxMessage.getFromUserName()); | ||||||
|     if (session != null) { |     if (session != null) { | ||||||
|       session.endAccess(); |       session.endAccess(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -728,8 +728,8 @@ public interface WxMpService { | |||||||
|    * @return |    * @return | ||||||
|  * @throws WxErrorException  |  * @throws WxErrorException  | ||||||
|    */ |    */ | ||||||
|   Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) throws WxErrorException;  	 |   Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) throws WxErrorException; | ||||||
|   	 |  | ||||||
|   /** |   /** | ||||||
|    * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 |    * 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 | ||||||
|    * 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 |    * 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | ||||||
|  | |||||||
| @ -100,9 +100,10 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     if (wxMpConfigStorage.isAccessTokenExpired()) { |     if (wxMpConfigStorage.isAccessTokenExpired()) { | ||||||
|       synchronized (globalAccessTokenRefreshLock) { |       synchronized (globalAccessTokenRefreshLock) { | ||||||
|         if (wxMpConfigStorage.isAccessTokenExpired()) { |         if (wxMpConfigStorage.isAccessTokenExpired()) { | ||||||
|           String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" |           String url = new StringBuffer() | ||||||
|               + "&appid=" + wxMpConfigStorage.getAppId() |               .append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential") | ||||||
|               + "&secret=" + wxMpConfigStorage.getSecret(); |               .append("&appid=").append(wxMpConfigStorage.getAppId()) | ||||||
|  |               .append("&secret=").append(wxMpConfigStorage.getSecret()).toString(); | ||||||
|           try { |           try { | ||||||
|             HttpGet httpGet = new HttpGet(url); |             HttpGet httpGet = new HttpGet(url); | ||||||
|             if (httpProxy != null) { |             if (httpProxy != null) { | ||||||
| @ -505,29 +506,31 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { |   public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { | ||||||
|     String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; |     StringBuffer url = new StringBuffer(); | ||||||
|     url += "appid=" + wxMpConfigStorage.getAppId(); |     url.append("https://open.weixin.qq.com/connect/oauth2/authorize?"); | ||||||
|     url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectURI); |     url.append("appid=").append(wxMpConfigStorage.getAppId()); | ||||||
|     url += "&response_type=code"; |     url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI)); | ||||||
|     url += "&scope=" + scope; |     url.append("&response_type=code"); | ||||||
|  |     url.append("&scope=").append(scope); | ||||||
|     if (state != null) { |     if (state != null) { | ||||||
|       url += "&state=" + state; |       url.append("&state=").append(state); | ||||||
|     } |     } | ||||||
|     url += "#wechat_redirect"; |     url.append("#wechat_redirect"); | ||||||
|     return url; |     return url.toString(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { |   public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"; |     StringBuffer url = new StringBuffer(); | ||||||
|     url += "appid=" + wxMpConfigStorage.getAppId(); |     url.append("https://api.weixin.qq.com/sns/oauth2/access_token?"); | ||||||
|     url += "&secret=" + wxMpConfigStorage.getSecret(); |     url.append("appid=").append(wxMpConfigStorage.getAppId()); | ||||||
|     url += "&code=" + code; |     url.append("&secret=").append(wxMpConfigStorage.getSecret()); | ||||||
|     url += "&grant_type=authorization_code"; |     url.append("&code=").append(code); | ||||||
|  |     url.append("&grant_type=authorization_code"); | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | ||||||
|       String responseText = executor.execute(getHttpclient(), httpProxy, url, null); |       String responseText = executor.execute(getHttpclient(), httpProxy, url.toString(), null); | ||||||
|       return WxMpOAuth2AccessToken.fromJson(responseText); |       return WxMpOAuth2AccessToken.fromJson(responseText); | ||||||
|     } catch (ClientProtocolException e) { |     } catch (ClientProtocolException e) { | ||||||
|       throw new RuntimeException(e); |       throw new RuntimeException(e); | ||||||
| @ -538,14 +541,15 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { |   public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"; |     StringBuffer url = new StringBuffer(); | ||||||
|     url += "appid=" + wxMpConfigStorage.getAppId(); |     url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?"); | ||||||
|     url += "&grant_type=refresh_token"; |     url.append("appid=").append(wxMpConfigStorage.getAppId()); | ||||||
|     url += "&refresh_token=" + refreshToken; |     url.append("&grant_type=refresh_token"); | ||||||
|  |     url.append("&refresh_token=").append(refreshToken); | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | ||||||
|       String responseText = executor.execute(getHttpclient(), httpProxy, url, null); |       String responseText = executor.execute(getHttpclient(), httpProxy, url.toString(), null); | ||||||
|       return WxMpOAuth2AccessToken.fromJson(responseText); |       return WxMpOAuth2AccessToken.fromJson(responseText); | ||||||
|     } catch (ClientProtocolException e) { |     } catch (ClientProtocolException e) { | ||||||
|       throw new RuntimeException(e); |       throw new RuntimeException(e); | ||||||
| @ -556,18 +560,19 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException { |   public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException { | ||||||
|     String url = "https://api.weixin.qq.com/sns/userinfo?"; |     StringBuffer url = new StringBuffer(); | ||||||
|     url += "access_token=" + oAuth2AccessToken.getAccessToken(); |     url.append("https://api.weixin.qq.com/sns/userinfo?"); | ||||||
|     url += "&openid=" + oAuth2AccessToken.getOpenId(); |     url.append("access_token=").append(oAuth2AccessToken.getAccessToken()); | ||||||
|  |     url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | ||||||
|     if (lang == null) { |     if (lang == null) { | ||||||
|       url += "&lang=zh_CN"; |       url.append("&lang=zh_CN"); | ||||||
|     } else { |     } else { | ||||||
|       url += "&lang=" + lang; |       url.append("&lang=").append(lang); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | ||||||
|       String responseText = executor.execute(getHttpclient(), httpProxy, url, null); |       String responseText = executor.execute(getHttpclient(), httpProxy, url.toString(), null); | ||||||
|       return WxMpUser.fromJson(responseText); |       return WxMpUser.fromJson(responseText); | ||||||
|     } catch (ClientProtocolException e) { |     } catch (ClientProtocolException e) { | ||||||
|       throw new RuntimeException(e); |       throw new RuntimeException(e); | ||||||
| @ -578,13 +583,14 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) { |   public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) { | ||||||
|     String url = "https://api.weixin.qq.com/sns/auth?"; |     StringBuffer url = new StringBuffer(); | ||||||
|     url += "access_token=" + oAuth2AccessToken.getAccessToken(); |     url.append("https://api.weixin.qq.com/sns/auth?"); | ||||||
|     url += "&openid=" + oAuth2AccessToken.getOpenId(); |     url.append("access_token=").append(oAuth2AccessToken.getAccessToken()); | ||||||
|  |     url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); |       RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | ||||||
|       executor.execute(getHttpclient(), httpProxy, url, null); |       executor.execute(getHttpclient(), httpProxy, url.toString(), null); | ||||||
|     } catch (ClientProtocolException e) { |     } catch (ClientProtocolException e) { | ||||||
|       throw new RuntimeException(e); |       throw new RuntimeException(e); | ||||||
|     } catch (IOException e) { |     } catch (IOException e) { | ||||||
| @ -984,7 +990,7 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|    |    | ||||||
|   @Override |   @Override | ||||||
|   public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature) { |   public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature) { | ||||||
| 	  return signature.equals(WxCryptUtil.createSign(kvm, wxMpConfigStorage.getPartnerKey())); |     return signature.equals(WxCryptUtil.createSign(kvm, wxMpConfigStorage.getPartnerKey())); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|  | |||||||
| @ -30,15 +30,15 @@ import java.util.UUID; | |||||||
| public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> { | public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> { | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { |   public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,  | ||||||
|  |       WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { | ||||||
|     if (ticket != null) { |     if (ticket != null) { | ||||||
|       if (uri.indexOf('?') == -1) { |       if (uri.indexOf('?') == -1) { | ||||||
|         uri += '?'; |         uri += '?'; | ||||||
|       } |       } | ||||||
|       uri += uri.endsWith("?") ?  |       uri += uri.endsWith("?")  | ||||||
|           "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")  |           ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")  | ||||||
|           :  |           : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | ||||||
|           "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     HttpGet httpGet = new HttpGet(uri); |     HttpGet httpGet = new HttpGet(uri); | ||||||
| @ -59,7 +59,7 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi | |||||||
|       InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); |       InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); | ||||||
|  |  | ||||||
|       return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); |       return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | ||||||
|     }finally { |     } finally { | ||||||
|       httpGet.releaseConnection(); |       httpGet.releaseConnection(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | |||||||
| @ -1,18 +1,15 @@ | |||||||
| package me.chanjar.weixin.mp.util.json; | package me.chanjar.weixin.mp.util.json; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Type; | ||||||
|  |  | ||||||
| import com.google.gson.JsonDeserializationContext; | import com.google.gson.JsonDeserializationContext; | ||||||
| import com.google.gson.JsonDeserializer; | import com.google.gson.JsonDeserializer; | ||||||
| import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
| import com.google.gson.JsonParseException; | import com.google.gson.JsonParseException; | ||||||
| import com.google.gson.reflect.TypeToken; |  | ||||||
| import me.chanjar.weixin.common.util.json.GsonHelper; | import me.chanjar.weixin.common.util.json.GsonHelper; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpCard; | import me.chanjar.weixin.mp.bean.WxMpCard; | ||||||
| import org.slf4j.Logger; |  | ||||||
| import org.slf4j.LoggerFactory; |  | ||||||
|  |  | ||||||
| import java.lang.reflect.Type; |  | ||||||
| import java.util.List; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Created by YuJian on 15/11/11. |  * Created by YuJian on 15/11/11. | ||||||
|  | |||||||
| @ -1,20 +1,17 @@ | |||||||
| package me.chanjar.weixin.mp.util.json; | package me.chanjar.weixin.mp.util.json; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Type; | ||||||
|  |  | ||||||
| import com.google.gson.JsonDeserializationContext; | import com.google.gson.JsonDeserializationContext; | ||||||
| import com.google.gson.JsonDeserializer; | import com.google.gson.JsonDeserializer; | ||||||
| import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
| import com.google.gson.JsonParseException; | import com.google.gson.JsonParseException; | ||||||
| import com.google.gson.reflect.TypeToken; | import com.google.gson.reflect.TypeToken; | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.util.json.GsonHelper; | import me.chanjar.weixin.common.util.json.GsonHelper; | ||||||
| import me.chanjar.weixin.mp.bean.WxMpCard; | import me.chanjar.weixin.mp.bean.WxMpCard; | ||||||
| import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | ||||||
| import org.slf4j.Logger; |  | ||||||
| import org.slf4j.LoggerFactory; |  | ||||||
|  |  | ||||||
| import java.lang.reflect.Type; |  | ||||||
| import java.text.ParseException; |  | ||||||
| import java.util.List; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Created by YuJian on 15/11/11. |  * Created by YuJian on 15/11/11. | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 tianmu
					tianmu