Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 微信第三方平台 接口 #368

Merged
merged 4 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
768 changes: 387 additions & 381 deletions pom.xml

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions weixin-java-open/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
消息机制未实现,下面为通知回调中设置的代码部分
```
@RestController
@RequestMapping("notify")
public class NotifyController extends WechatThridBaseController {
@Autowired
protected WxOpenServiceDemo wxOpenService;
@RequestMapping("receive_ticket")
public Object receiveTicket(@RequestBody(required = false) String requestBody, @RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce, @RequestParam("signature") String signature,
@RequestParam(name = "encrypt_type", required = false) String encType,
@RequestParam(name = "msg_signature", required = false) String msgSignature) {
this.logger.info(
"\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
signature, encType, msgSignature, timestamp, nonce, requestBody);

if (!StringUtils.equalsIgnoreCase("aes", encType) || !wxOpenService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}

// aes加密的消息
WxOpenXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature);
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
String out = null;
try {
out = wxOpenService.getWxOpenComponentService().route(inMessage);
} catch (WxErrorException e) {
throw new ResponseException(ErrorCodeEnum.ERROR, e);
}

this.logger.debug("\n组装回复信息:{}", out);

return out;
}
@RequestMapping("{appId}/callback")
public Object callback(@RequestBody(required = false)String requestBody,
@PathVariable ("appId") String appId,
@RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("openid") String openid,
@RequestParam("encrypt_type") String encType,
@RequestParam("msg_signature") String msgSignature) {
this.logger.info(
"\n接收微信请求:[appId=[{}], openid=[{}], signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
appId, openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
logger.info("query:"+getHttpServletRequest().getQueryString()+"\nbody:"+requestBody);
if (!StringUtils.equalsIgnoreCase("aes", encType) || !wxOpenService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}

String out = "";
// aes加密的消息
WxMpXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedMpXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature);
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
//wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId);
return out;
}
}
```
97 changes: 97 additions & 0 deletions weixin-java-open/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.8.6.BETA</version>
</parent>
<artifactId>weixin-java-open</artifactId>
<name>WeiXin Java Tools - Open</name>
<description>微信开放平台Java SDK</description>
<developers>
<developer>
<name>007</name>
<email>[email protected]</email>
<url>https://github.com/007gzs</url>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-http</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package me.chanjar.weixin.open.api;

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.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;

/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public interface WxOpenComponentService {

String API_COMPONENT_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
String API_CREATE_PREAUTHCODE_URL = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode";
String API_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth";
String API_AUTHORIZER_TOKEN_URL = "https://api.weixin.qq.com /cgi-bin/component/api_authorizer_token";
String API_GET_AUTHORIZER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info";
String API_GET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option";
String API_SET_AUTHORIZER_OPTION_URL = "https://api.weixin.qq.com/cgi-bin/component/ api_set_authorizer_option";


String COMPONENT_LOGIN_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
String CONNECT_OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect";

/**
* 用code换取oauth2的access token
*/
String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s";
/**
* 刷新oauth2的access token
*/
String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s&component_appid==%s";
WxMpService getWxMpServiceByAppid(String appid);

WxOpenConfigStorage getWxOpenConfigStorage();

boolean checkSignature(String timestamp, String nonce, String signature);

String getComponentAccessToken(boolean forceRefresh) throws WxErrorException;

/**
* 获取用户授权页URL(来路URL和成功跳转URL 的域名都需要为三方平台设置的 登录授权的发起页域名)
*/
String getPreAuthUrl(String redirectURI) throws WxErrorException;

String route(WxOpenXmlMessage wxMessage) throws WxErrorException;

/**
* 使用授权码换取公众号或小程序的接口调用凭据和授权信息
*/
WxOpenQueryAuthResult getQueryAuth(String authorizationCode) throws WxErrorException;

/**
* 获取授权方的帐号基本信息
*/
WxOpenAuthorizerInfoResult getAuthorizerInfo(String authorizerAppid) throws WxErrorException;

/**
* 获取授权方的选项设置信息
*/
WxOpenAuthorizerOptionResult getAuthorizerOption(String authorizerAppid, String optionName) throws WxErrorException;

/**
* 设置授权方的选项信息
*/
WxError setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException;

String getAuthorizerAccessToken(String appid, boolean forceRefresh) throws WxErrorException;

WxMpOAuth2AccessToken oauth2getAccessToken(String appid, String code) throws WxErrorException;

boolean checkSignature(String appId, String timestamp, String nonce, String signature);

WxMpOAuth2AccessToken oauth2refreshAccessToken(String appid, String refreshToken) throws WxErrorException;

String oauth2buildAuthorizationUrl(String appid, String redirectURI, String scope, String state);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package me.chanjar.weixin.open.api;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;

/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public interface WxOpenConfigStorage {

void setComponentAppId(String componentAppId);

void setComponentAppSecret(String componentAppSecret);

void setComponentToken(String componentToken);

void setComponentAesKey(String componentAesKey);

String getComponentAppId();
String getComponentAppSecret();
String getComponentToken();
String getComponentAesKey();
String getComponentVerifyTicket();
void setComponentVerifyTicket(String componentVerifyTicket);
String getComponentAccessToken();
boolean isComponentAccessTokenExpired();
void updateComponentAccessTokent(WxOpenComponentAccessToken componentAccessToken);
WxMpConfigStorage getWxMpConfigStorage(String appId);
/**
* 应该是线程安全的
*
* @param componentAccessToken 新的accessToken值
* @param expiresInSeconds 过期时间,以秒为单位
*/
void updateComponentAccessTokent(String componentAccessToken, int expiresInSeconds);

/**
* 是否自动刷新token
*/
boolean autoRefreshToken();


String getAuthorizerRefreshToken(String appId);
void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken);
String getAuthorizerAccessToken(String appId);


boolean isAuthorizerAccessTokenExpired(String appId);

/**
* 强制将access token过期掉
*/
void expireAuthorizerAccessToken(String appId);

/**
* 应该是线程安全的
*
* @param authorizerAccessToken 要更新的WxAccessToken对象
*/
void updateAuthorizerAccessToken(String appId, WxOpenAuthorizerAccessToken authorizerAccessToken);

/**
* 应该是线程安全的
*
* @param authorizerAccessToken 新的accessToken值
* @param expiresInSeconds 过期时间,以秒为单位
*/
void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds);

String getJsapiTicket(String appId);

boolean isJsapiTicketExpired(String appId);

/**
* 强制将jsapi ticket过期掉
*/
void expireJsapiTicket(String appId);

/**
* 应该是线程安全的
*
* @param jsapiTicket 新的jsapi ticket值
* @param expiresInSeconds 过期时间,以秒为单位
*/
void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds);

String getCardApiTicket(String appId);


boolean isCardApiTicketExpired(String appId);

/**
* 强制将卡券api ticket过期掉
*/
void expireCardApiTicket(String appId);

/**
* 应该是线程安全的
*
* @param cardApiTicket 新的cardApi ticket值
* @param expiresInSeconds 过期时间,以秒为单位
*/
void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.chanjar.weixin.open.api;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
* @author <a href="https://github.com/007gzs">007</a>
*/
public interface WxOpenService {
WxOpenComponentService getWxOpenComponentService();
WxOpenConfigStorage getWxOpenConfigStorage();
void setWxOpenConfigStorage(WxOpenConfigStorage wxOpenConfigStorage);
/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
*/
String get(String url, String queryParam) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求
*/
String post(String url, String postData) throws WxErrorException;

}
Loading