删除Weixin4jSettings配置类 & fixed #89 #90 #91

This commit is contained in:
jinyu
2016-08-22 13:07:06 +08:00
parent 4420adca99
commit 0356484f44
42 changed files with 1098 additions and 853 deletions
@@ -3,17 +3,17 @@ package com.foxinmy.weixin4j.mp;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.mp.api.ComponentApi;
import com.foxinmy.weixin4j.mp.model.WeixinMpAccount;
import com.foxinmy.weixin4j.mp.type.URLConsts;
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
import com.foxinmy.weixin4j.token.TicketManager;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.StringUtil;
@@ -38,41 +38,66 @@ public class WeixinComponentProxy {
*/
private Map<String, ComponentApi> componentMap;
/**
* 配置相关
* 微信账号信息
*/
private final Weixin4jSettings<WeixinMpAccount> settings;
private final WeixinMpAccount weixinMpAccount;
/**
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
* 微信第三方组件接口实现(使用weixin4j.properties配置的account#components账号信息,
* 使用FileCacheStorager文件方式缓存TOKEN)
*/
public WeixinComponentProxy() {
this(new Weixin4jSettings<WeixinMpAccount>(
JSON.parseObject(Weixin4jConfigUtil.getValue("account"), WeixinMpAccount.class)));
this(new FileCacheStorager<Token>());
}
/**
*
* @param settings
* 配置信息
* 微信第三方组件接口实现(使用weixin4j.properties配置的account#components账号信息)
*
* @param cacheStorager
* token管理
*/
public WeixinComponentProxy(Weixin4jSettings<WeixinMpAccount> settings) {
this.settings = settings;
List<WeixinAccount> components = settings.getAccount().getComponents();
this.componentMap = new HashMap<String, ComponentApi>(components.size());
for (WeixinAccount component : components) {
this.componentMap.put(component.getId(), new ComponentApi(
new TicketManager(component.getId(), component.getSecret(), settings.getCacheStorager0())));
}
this.componentMap.put(null, componentMap.get(components.get(0).getId()));
public WeixinComponentProxy(CacheStorager<Token> cacheStorager) {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinMpAccount.class), cacheStorager);
}
/**
* 公众号信息
* 微信第三方组件接口实现
*
* @param weixinMpAccount
* 账号信息
* @param cacheStorager
* token管理
*/
public WeixinComponentProxy(WeixinMpAccount weixinMpAccount,
CacheStorager<Token> cacheStorager) {
if (weixinMpAccount == null) {
throw new IllegalArgumentException(
"weixinPayAccount must not be empty");
}
if (cacheStorager == null) {
throw new IllegalArgumentException(
"cacheStorager must not be empty");
}
this.weixinMpAccount = weixinMpAccount;
this.componentMap = new HashMap<String, ComponentApi>(weixinMpAccount
.getComponents().size());
for (WeixinAccount component : weixinMpAccount.getComponents()) {
this.componentMap.put(component.getId(), new ComponentApi(
new TicketManager(component.getId(), component.getSecret(),
cacheStorager)));
}
this.componentMap.put(null, componentMap.get(weixinMpAccount
.getComponents().get(0).getId()));
}
/**
* 获取微信账号信息
*
* @return
*/
public WeixinMpAccount getWeixinAccount() {
return this.settings.getAccount();
public WeixinMpAccount getWeixinMpAccount() {
return this.weixinMpAccount;
}
/**
@@ -103,17 +128,20 @@ public class WeixinComponentProxy {
* @param componentId
* 组件ID
* @return 预授权码
* @see #cacheComponentTicket(String, String)
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
* @throws WeixinException
*/
public String getPreComponentTicket(String componentId) throws WeixinException {
Token token = component(componentId).getTicketManager().getTicket();
public String getPreComponentTicket(String componentId)
throws WeixinException {
ComponentApi component = component(componentId);
Token token = component.getTicketManager().getTicket();
if (token == null || StringUtil.isBlank(token.getAccessToken())) {
throw new WeixinException("maybe oauth first?");
}
return token.getAccessToken();
return component.getPreCodeManager().getAccessToken();
}
/**
@@ -125,27 +153,34 @@ public class WeixinComponentProxy {
* 组件ticket内容
* @throws WeixinException
*/
public void cacheComponentTicket(String componentId, String componentTicket) throws WeixinException {
component(componentId).getTicketManager().cachingTicket(componentTicket);
public void cacheComponentTicket(String componentId, String componentTicket)
throws WeixinException {
component(componentId).getTicketManager()
.cachingTicket(componentTicket);
}
/**
* 应用组件授权 <font color="red">需先缓存ticket</font>
*
* @see {@link #getComponentAuthorizeURL(String, String,String)}
* 应用组件授权 <font color="red">需先缓存ticket</font> <li>
* redirectUri默认填写weixin4j.properties#component.oauth.redirect.uri <li>
* state默认填写state
*
* @param componentId
* 组件ID
* @see {@link #cacheComponentTicket(String, String)}
* @see {@link #getComponentAuthorizationURL(String, String,String)}
* @return 请求授权的URL
* @throws WeixinException
*/
public String getComponentAuthorizeURL(String componentId) throws WeixinException {
String redirectUri = Weixin4jConfigUtil.getValue("component.oauth.redirect.uri");
return getComponentAuthorizeURL(componentId, redirectUri, "state");
public String getComponentAuthorizationURL(String componentId)
throws WeixinException {
String redirectUri = Weixin4jConfigUtil
.getValue("component.oauth.redirect.uri");
return getComponentAuthorizationURL(componentId, redirectUri, "state");
}
/**
* 应用组件授权 <font color="red">需先缓存ticket</font>
* 应用组件授权 <font
* color="red">需先缓存ticket,在授权完成之后需要调用ComponentApi#exchangeAuthInfo方法
* ,否则无法缓存token相关导致后续的组件接口调用失败</font>
*
* @param componentId
* 组件ID
@@ -153,16 +188,21 @@ public class WeixinComponentProxy {
* 授权后重定向url
* @param state
* 回调后原样返回
* @see #cacheComponentTicket(String, String)
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTicketManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPreCodeManager()
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#exchangeAuthInfo(String)
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN">应用组件授权</a>
* @return 请求授权的URL
* @throws WeixinException
*/
public String getComponentAuthorizeURL(String componentId, String redirectUri, String state)
throws WeixinException {
public String getComponentAuthorizationURL(String componentId,
String redirectUri, String state) throws WeixinException {
try {
return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId, getPreComponentTicket(componentId),
return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId,
getPreComponentTicket(componentId),
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
} catch (UnsupportedEncodingException e) {
;
@@ -181,8 +221,8 @@ public class WeixinComponentProxy {
* @return
*/
public WeixinProxy getWeixinProxy(String componentId, String authAppId) {
return new WeixinProxy(component(componentId).getRefreshTokenManager(authAppId),
component(componentId).getTokenManager());
return new WeixinProxy(component(componentId).getRefreshTokenManager(
authAppId), component(componentId).getTokenManager());
}
public final static String VERSION = "1.7.1";
@@ -4,9 +4,12 @@ import java.io.InputStream;
import java.util.Date;
import java.util.List;
import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
import com.foxinmy.weixin4j.model.Button;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.model.card.CardCoupon;
import com.foxinmy.weixin4j.model.card.CardCoupons;
@@ -28,6 +31,7 @@ import com.foxinmy.weixin4j.mp.api.MassApi;
import com.foxinmy.weixin4j.mp.api.MediaApi;
import com.foxinmy.weixin4j.mp.api.MenuApi;
import com.foxinmy.weixin4j.mp.api.NotifyApi;
import com.foxinmy.weixin4j.mp.api.OauthApi;
import com.foxinmy.weixin4j.mp.api.QrApi;
import com.foxinmy.weixin4j.mp.api.TagApi;
import com.foxinmy.weixin4j.mp.api.TmplApi;
@@ -56,8 +60,8 @@ import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.mp.type.DatacubeType;
import com.foxinmy.weixin4j.mp.type.IndustryType;
import com.foxinmy.weixin4j.mp.type.Lang;
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
import com.foxinmy.weixin4j.token.PerTicketManager;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.tuple.MassTuple;
import com.foxinmy.weixin4j.tuple.MpArticle;
@@ -77,7 +81,10 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
* @see <a href="http://mp.weixin.qq.com/wiki/index.php">api文档</a>
*/
public class WeixinProxy {
/**
* 授权API
*/
private final OauthApi oauthApi;
/**
* 媒体素材API
*/
@@ -131,64 +138,94 @@ public class WeixinProxy {
*/
private final CardApi cardApi;
/**
* token实现
* token管理
*/
private final TokenManager tokenManager;
/**
* 配置信息
* 账号信息
*/
private Weixin4jSettings<WeixinAccount> settings;
private final WeixinAccount weixinAccount;
/**
* token存储
*/
private final CacheStorager<Token> cacheStorager;
/**
* 默认使用文件方式保存token、使用weixin4j.properties配置的账号信息
* 微信接口实现(使用weixin4j.properties配置的account账号信息,
* 使用FileCacheStorager文件方式缓存TOKEN)
*/
public WeixinProxy() {
this(new Weixin4jSettings<WeixinAccount>(
Weixin4jConfigUtil.getWeixinAccount()));
this(new FileCacheStorager<Token>());
}
/**
*
* @param settings
* 微信配置信息
* @see com.foxinmy.weixin4j.setting.Weixin4jSettings
* 微信接口实现(使用weixin4j.properties配置的account账号信息)
*
* @param cacheStorager
* token管理
*/
public WeixinProxy(Weixin4jSettings<WeixinAccount> settings) {
this(new TokenManager(new WeixinTokenCreator(settings.getAccount()
.getId(), settings.getAccount().getSecret()),
settings.getCacheStorager0()));
this.settings = settings;
public WeixinProxy(CacheStorager<Token> cacheStorager) {
this(Weixin4jConfigUtil.getWeixinAccount(), cacheStorager);
}
/**
* 第三方组件(永久刷新令牌机制)
* 微信接口实现
*
* @param weixinAccount
* 账号信息
* @param cacheStorager
* token管理
*/
public WeixinProxy(WeixinAccount weixinAccount,
CacheStorager<Token> cacheStorager) {
this(weixinAccount, new WeixinTokenCreator(weixinAccount.getId(),
weixinAccount.getSecret()), cacheStorager);
}
/**
* 第三方组件方式创建微信接口实现(永久刷新令牌机制)
*
* @param perTicketManager
* 第三方组件永久刷新token
* {@link com.foxinmy.weixin4j.mp.api.ComponentApi#getPerCodeManager(String)}
* @param componentTokenManager
* 第三方组件凭证token
* {@link com.foxinmy.weixin4j.mp.api.ComponentApi#getTokenManager}
* @see com.foxinmy.weixin4j.mp.api.ComponentApi
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getPerCodeManager(String)
* @see com.foxinmy.weixin4j.mp.api.ComponentApi#getTokenManager
*/
public WeixinProxy(PerTicketManager perTicketManager,
TokenManager componentTokenManager) {
this(new TokenManager(new WeixinTokenComponentCreator(perTicketManager,
componentTokenManager), perTicketManager.getCacheStorager()));
this.settings = new Weixin4jSettings<WeixinAccount>(new WeixinAccount(
perTicketManager.getAuthAppId(), null));
this.settings.setCacheStorager(perTicketManager.getCacheStorager());
this(new WeixinAccount(perTicketManager.getThirdId(),
perTicketManager.getThirdSecret()),
new WeixinTokenComponentCreator(perTicketManager,
componentTokenManager), perTicketManager
.getCacheStorager());
}
/**
* 注意:TokenCreator 需为 <font color="red">WeixinTokenCreator</font>
*
* @see com.foxinmy.weixin4j.mp.token.WeixinTokenCreator
* 微信接口实现
*
* @param settings
* 配置信息
* @param tokenManager
* token管理
*/
private WeixinProxy(TokenManager tokenManager) {
this.tokenManager = tokenManager;
private WeixinProxy(WeixinAccount weixinAccount, TokenCreator tokenCreator,
CacheStorager<Token> cacheStorager) {
if (weixinAccount == null) {
throw new IllegalArgumentException("settings must not be empty");
}
if (tokenCreator == null) {
throw new IllegalArgumentException("tokenCreator must not be empty");
}
if (cacheStorager == null) {
throw new IllegalArgumentException(
"cacheStorager must not be empty");
}
this.tokenManager = new TokenManager(tokenCreator, cacheStorager);
this.weixinAccount = weixinAccount;
this.cacheStorager = cacheStorager;
this.oauthApi = new OauthApi(weixinAccount);
this.mediaApi = new MediaApi(tokenManager);
this.notifyApi = new NotifyApi(tokenManager);
this.customApi = new CustomApi(tokenManager);
@@ -210,7 +247,7 @@ public class WeixinProxy {
* @return
*/
public WeixinAccount getWeixinAccount() {
return this.settings.getAccount();
return weixinAccount;
}
/**
@@ -222,6 +259,16 @@ public class WeixinProxy {
return this.tokenManager;
}
/**
* 获取oauth授权API
*
* @see com.foxinmy.weixin4j.mp.api.OauthApi
* @return
*/
public OauthApi getOauthApi() {
return oauthApi;
}
/**
* 获取JSSDK Ticket的tokenManager
*
@@ -230,9 +277,8 @@ public class WeixinProxy {
* @return
*/
public TokenManager getTicketManager(TicketType ticketType) {
return new TokenManager(new WeixinTicketCreator(getWeixinAccount()
.getId(), ticketType, this.tokenManager),
this.settings.getCacheStorager0());
return new TokenManager(new WeixinTicketCreator(weixinAccount.getId(),
ticketType, this.tokenManager), this.cacheStorager);
}
/**
@@ -1537,7 +1583,7 @@ public class WeixinProxy {
* @return 操作结果
* @throws WeixinException
*/
public ApiResult clearQuota() throws WeixinException {
public ApiResult clearQuota() throws WeixinException {
return helperApi.clearQuota(weixinAccount.getId());
}
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.mp.api;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
@@ -14,12 +16,15 @@ import com.foxinmy.weixin4j.mp.component.WeixinComponentPreCodeCreator;
import com.foxinmy.weixin4j.mp.component.WeixinComponentTokenCreator;
import com.foxinmy.weixin4j.mp.component.WeixinTokenComponentCreator;
import com.foxinmy.weixin4j.mp.model.AuthorizerOption;
import com.foxinmy.weixin4j.mp.model.OauthToken;
import com.foxinmy.weixin4j.mp.model.AuthorizerOption.AuthorizerOptionName;
import com.foxinmy.weixin4j.mp.model.ComponentAuthInfo;
import com.foxinmy.weixin4j.token.PerTicketManager;
import com.foxinmy.weixin4j.token.TicketManager;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.util.Consts;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 第三方应用组件
@@ -101,6 +106,110 @@ public class ComponentApi extends MpApi {
ticketManager.getCacheStorager());
}
/**
* 第三方组件代替授权公众号发起网页授权:获取code <li>
* redirectUri默认填写weixin4j.properties#component.user.oauth.redirect.uri <li>
* scope默认填写snsapi_base <li>
* state默认填写state
*
* @param authAppId
* 公众号的appid
* @see #getAuthorizationURL(String, String, String, String)
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">第三方组件代替授权公众号发起网页授权</a>
*/
public String getUserAuthorizationURL(String authAppId) {
String redirectUri = Weixin4jConfigUtil
.getValue("component.user.oauth.redirect.uri");
return getUserAuthorizationURL(authAppId, redirectUri, "snsapi_base",
"state");
}
/**
* 第三方组件代替授权公众号发起网页授权:获取code
*
* @param authAppId
* 公众号的appid
* @param redirectUri
* 重定向地址,这里填写的应是服务开发方的回调地址
* @param scope
* 应用授权作用域,snsapi_base/snsapi_userinfo
* @param state
* 重定向后会带上state参数,开发者可以填写任意参数值,最多128字节
* @return oauth授权URL
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN">第三方组件代替授权公众号发起网页授权</a>
*/
public String getUserAuthorizationURL(String authAppId, String redirectUri,
String scope, String state) {
String sns_component_user_auth_uri = getRequestUri("sns_component_user_auth_uri");
try {
return String.format(sns_component_user_auth_uri, authAppId,
URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope,
state, this.ticketManager.getThirdId());
} catch (UnsupportedEncodingException e) {
;
}
return "";
}
/**
* 第三方组件代替授权公众号发起网页授权:换取token
*
* @param authAppId
* 公众号的appid
* @param code
* 用户同意授权获取的code
* @return token信息
* @see #getUserAuthorizationURL(String, String, String, String)
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @throws WeixinException
*/
public OauthToken getAuthorizationToken(String authAppId, String code)
throws WeixinException {
String sns_component_user_token_uri = getRequestUri("sns_component_user_token_uri");
String accessToken = tokenManager.getAccessToken();
WeixinResponse response = weixinExecutor.get(String.format(
sns_component_user_token_uri, authAppId, code,
ticketManager.getThirdId(), accessToken));
JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"),
result.getLongValue("expires_in") * 1000l);
token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token"));
return token;
}
/**
* 第三方组件代替授权公众号发起网页授权:刷新token
*
* @param authAppId
* 公众号的appid
* @param refreshToken
* 填写通过access_token获取到的refresh_token参数
* @return token信息
* @see #getAuthorizationToken(String, String)
* @see OauthApi#getAuthorizationUser(OauthToken)
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @throws WeixinException
*/
public OauthToken refreshAuthorizationToken(String authAppId,
String refreshToken) throws WeixinException {
String sns_component_token_refresh_uri = getRequestUri("sns_component_token_refresh_uri");
String accessToken = tokenManager.getAccessToken();
WeixinResponse response = weixinExecutor.get(String.format(
sns_component_token_refresh_uri, authAppId,
ticketManager.getThirdId(), accessToken, refreshToken));
JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"),
result.getLongValue("expires_in") * 1000l);
token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token"));
return token;
}
/**
* 使用授权码换取公众号的接口调用凭据和授权信息:用于使用授权码换取授权公众号的授权信息,
* 并换取authorizer_access_token和authorizer_refresh_token。
@@ -111,6 +220,7 @@ public class ComponentApi extends MpApi {
* @param authCode
* 授权code
* @return 第三方组件授权信息
* @see {@link com.foxinmy.weixin4j.mp.WeixinComponentProxy#getComponentAuthorizeURL(String, String, String)}
* @see com.foxinmy.weixin4j.mp.model.ComponentAuthInfo
* @throws WeixinException
*/
@@ -169,22 +279,23 @@ public class ComponentApi extends MpApi {
WeixinResponse response = weixinExecutor.post(
String.format(component_get_authorizer_uri,
tokenManager.getAccessToken()), obj.toJSONString());
obj = response.getAsJson().getJSONObject("authorizer_info");
ComponentAuthInfo info = JSON
.toJavaObject(obj, ComponentAuthInfo.class);
info.setServiceType(obj.getJSONObject("service_type_info").getIntValue(
obj = response.getAsJson();
JSONObject auth = obj.getJSONObject("authorizer_info");
ComponentAuthInfo info = JSON.toJavaObject(auth,
ComponentAuthInfo.class);
info.setServiceType(auth.getJSONObject("service_type_info")
.getIntValue("id"));
info.setVerifyType(auth.getJSONObject("verify_type_info").getIntValue(
"id"));
info.setVerifyType(obj.getJSONObject("verify_type_info").getIntValue(
"id"));
JSONArray privilegesObj = obj.getJSONObject("authorization_info")
.getJSONArray("func_info");
auth = obj.getJSONObject("authorization_info");
JSONArray privilegesObj = auth.getJSONArray("func_info");
List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
for (int i = 0; i < privilegesObj.size(); i++) {
privileges.add(privilegesObj.getJSONObject(i)
.getJSONObject("funcscope_category").getInteger("id"));
}
info.setPrivileges(privileges);
info.setAppId(authAppId);
info.setAppId(auth.getString("appid"));
return info;
}
@@ -21,8 +21,6 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
* @author jinyu(foxinmy@gmail.com)
* @date 2015年3月6日
* @since JDK 1.6
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN">微信登陆</a>
*/
public class OauthApi extends MpApi {
@@ -45,20 +43,23 @@ public class OauthApi extends MpApi {
}
/**
* 公众号base静默oauth授权:重定向URL使用weixin4j.properties#user.oauth.redirect.uri
* 公众号网页获取用户资料oauth授权:请求code<li>
* redirectUri默认填写weixin4j.properties#user.oauth.redirect.uri <li>
* scope默认填写snsapi_base <li>
* state默认填写state
*
* @see {@link #getAuthorizeURL(String, String,String)}
* @see {@link #getUserAuthorizationURL(String, String,String)}
*
* @return 请求授权的URL
*/
public String getAuthorizeURL() {
public String getUserAuthorizationURL() {
String redirectUri = Weixin4jConfigUtil
.getValue("user.oauth.redirect.uri");
return getAuthorizeURL(redirectUri, "state", "snsapi_base");
return getUserAuthorizationURL(redirectUri, "state", "snsapi_base");
}
/**
* 请求CODE
* 公众号网页获取用户资料oauth授权:请求code
*
* @param redirectUri
* 重定向地址<br>
@@ -86,7 +87,8 @@ public class OauthApi extends MpApi {
* ,这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。<br>
* @return 请求授权的URL
*/
public String getAuthorizeURL(String redirectUri, String state, String scope) {
public String getUserAuthorizationURL(String redirectUri, String state,
String scope) {
String sns_user_auth_uri = getRequestUri("sns_user_auth_uri");
try {
return String.format(sns_user_auth_uri, account.getId(),
@@ -99,15 +101,17 @@ public class OauthApi extends MpApi {
}
/**
* code换取token
* 公众号网页获取用户资料oauth授权:code换取token
*
* @param code
* 用户同意授权获取的code,
* code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次
* ,5分钟未被使用自动过期。
* @return oauthtoken信息
* @see #getUserAuthorizationURL(String, String,String)
* @see #getAuthorizationUser(OauthToken)
*/
public OauthToken getOauthToken(String code) throws WeixinException {
public OauthToken getAuthorizationToken(String code) throws WeixinException {
String user_token_uri = getRequestUri("sns_user_token_uri");
WeixinResponse response = weixinExecutor.get(String.format(
user_token_uri, account.getId(), account.getSecret(), code));
@@ -122,23 +126,29 @@ public class OauthApi extends MpApi {
}
/**
* 刷新token由于access_token拥有较短的有效期,当access_token超时后,可以使用refresh_token进行刷新,
* refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
* 公众号网页获取用户资料oauth授权:刷新token由于access_token拥有较短的有效期,当access_token超时后,
* 可以使用refresh_token进行刷新, refresh_token有效期为30天,当refresh_token失效之后,需要用户重新授权。
*
*
* @param refreshToken
* 填写通过access_token获取到的refresh_token参数
* {@link #getOauthToken(String)}
* @see {@link #getAuthorizationToken(String)}
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @return oauthtoken信息
*/
public OauthToken refreshToken(String refreshToken) throws WeixinException {
public OauthToken refreshAuthorizationToken(String refreshToken)
throws WeixinException {
String sns_token_refresh_uri = getRequestUri("sns_token_refresh_uri");
WeixinResponse response = weixinExecutor.get(String.format(
sns_token_refresh_uri, account.getId(), refreshToken));
return response.getAsObject(new TypeReference<OauthToken>() {
});
JSONObject result = response.getAsJson();
OauthToken token = new OauthToken(result.getString("access_token"),
result.getLongValue("expires_in") * 1000l);
token.setUnionId(result.getString("unionid"));
token.setOpenId(result.getString("openid"));
token.setScope(result.getString("scope"));
token.setRefreshToken(result.getString("refresh_token"));
return token;
}
/**
@@ -150,7 +160,7 @@ public class OauthApi extends MpApi {
* 用户标识
* @return 验证结果
*/
public boolean authAccessToken(String oauthToken, String openId) {
public boolean verifyAuthorizationToken(String oauthToken, String openId) {
String sns_auth_token_uri = getRequestUri("sns_auth_token_uri");
try {
weixinExecutor.get(String.format(sns_auth_token_uri, oauthToken,
@@ -163,7 +173,7 @@ public class OauthApi extends MpApi {
}
/**
* oauth获取用户信息(需scope为 snsapi_userinfo)
* oauth授权获取用户信息(需scope为 snsapi_userinfo)
*
* @param token
* 授权信息(token&openid)
@@ -173,11 +183,11 @@ public class OauthApi extends MpApi {
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&lang=zh_CN">授权获取用户信息</a>
* @see com.foxinmy.weixin4j.mp.model.User
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @see {@link #getOauthToken(String)}
* @see {@link #getUser(String,Sring,Lang)}
* @see {@link #getAuthorizationUser(String,Sring,Lang)}
*/
public User getUser(OauthToken token) throws WeixinException {
return getUser(token.getAccessToken(), token.getOpenId(), Lang.zh_CN);
public User getAuthorizationUser(OauthToken token) throws WeixinException {
return getAuthorizationUser(token.getAccessToken(), token.getOpenId(),
Lang.zh_CN);
}
/**
@@ -193,10 +203,11 @@ public class OauthApi extends MpApi {
* @throws WeixinException
* @see <a
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&lang=zh_CN">授权获取用户信息</a>
* @see {@link #getAuthorizationToken(String)}
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @see com.foxinmy.weixin4j.mp.model.User
*/
public User getUser(String oauthToken, String openid, Lang lang)
public User getAuthorizationUser(String oauthToken, String openid, Lang lang)
throws WeixinException {
String user_info_uri = getRequestUri("sns_user_info_uri");
WeixinResponse response = weixinExecutor.get(String.format(
@@ -207,28 +218,32 @@ public class OauthApi extends MpApi {
}
/**
* 微信开放平台oauth授权:重定向URL使用weixin4j.properties#user.oauth.redirect.uri
*
* @see {@link #getOpenAuthorizeURL(String, String)}
* 微信开放平台oauth授权(扫码登陆)<li>
* redirectUri默认填写weixin4j.properties#open.user.oauth.redirect.uri <li>
* state默认填写state
*
* @see {@link #getOpenAuthorizationURL(String, String)}
* @return 请求授权的URL
*/
public String getOpenAuthorizeURL() {
public String getOpenAuthorizationURL() {
String redirectUri = Weixin4jConfigUtil
.getValue("user.oauth.redirect.uri");
return getOpenAuthorizeURL(redirectUri, "state");
.getValue("open.user.oauth.redirect.uri");
return getOpenAuthorizationURL(redirectUri, "state");
}
/**
* 微信开放平台oauth授权:请求CODE
* 微信开放平台oauth授权(扫码登陆):请求CODE
*
* @param redirectUri
* 重定向地址 域名与审核时填写的授权域名一致
* @param state
* 用于保持请求和回调的状态,授权请求后原样带回给第三方
* @return 请求授权的URL
* @see <a
* href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN">网站扫描登陆oauth授权</a>
* @see #getAuthorizationToken(String)
*/
public String getOpenAuthorizeURL(String redirectUri, String state) {
public String getOpenAuthorizationURL(String redirectUri, String state) {
String open_user_auth_uri = getRequestUri("open_user_auth_uri");
try {
return String.format(open_user_auth_uri, account.getId(),
@@ -25,6 +25,8 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.entity.FormUrlEntity;
import com.foxinmy.weixin4j.http.weixin.ApiResult;
@@ -38,7 +40,6 @@ import com.foxinmy.weixin4j.mp.oldpayment.WeixinOldPayAccount;
import com.foxinmy.weixin4j.mp.oldpayment.WeixinOldPaymentSignature;
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.payment.PayRequest;
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
import com.foxinmy.weixin4j.sign.WeixinPaymentSignature;
import com.foxinmy.weixin4j.sign.WeixinSignature;
import com.foxinmy.weixin4j.token.TokenManager;
@@ -66,44 +67,80 @@ import com.foxinmy.weixin4j.xml.ListsuffixResultDeserializer;
*/
public class PayOldApi extends MpApi {
private final Weixin4jSettings<WeixinOldPayAccount> settings;
private final WeixinOldPayAccount weixinAccount;
private final WeixinOldPayAccount weixinPayAccount;
private final TokenManager tokenManager;
private final WeixinSignature weixinMD5Signature;
private final WeixinOldPaymentSignature weixinOldSignature;
private final WeixinOldPaymentSignature weixinSignature;
/**
* 默认使用weixin4j.properties配置信息
* 支付对象(使用weixin4j.properties配置的account商户信息,使用FileCacheStorager文件方式缓存TOKEN)
*/
public PayOldApi() {
this(new Weixin4jSettings<WeixinOldPayAccount>(JSON.parseObject(
Weixin4jConfigUtil.getValue("account"),
WeixinOldPayAccount.class)));
this(new FileCacheStorager<Token>());
}
/**
*
* @param settings
* 微信配置信息
* 支付对象(使用weixin4j.properties配置的account商户信息)
*/
public PayOldApi(Weixin4jSettings<WeixinOldPayAccount> settings) {
this.settings = settings;
this.weixinAccount = settings.getAccount();
public PayOldApi(CacheStorager<Token> cacheStorager) {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinOldPayAccount.class), cacheStorager);
}
/**
* 支付对象
*
* @param weixinPayAccount
* 商户信息
* @param cacheStorager
* token管理
*/
public PayOldApi(WeixinOldPayAccount weixinPayAccount,
CacheStorager<Token> cacheStorager) {
if (weixinPayAccount == null) {
throw new IllegalArgumentException(
"weixinPayAccount must not be empty");
}
if (cacheStorager == null) {
throw new IllegalArgumentException(
"cacheStorager must not be empty");
}
this.weixinPayAccount = weixinPayAccount;
this.tokenManager = new TokenManager(new WeixinTokenCreator(
weixinAccount.getId(), weixinAccount.getSecret()),
settings.getCacheStorager0());
weixinPayAccount.getId(), weixinPayAccount.getSecret()),
cacheStorager);
this.weixinMD5Signature = new WeixinPaymentSignature(
weixinAccount.getPartnerKey());
this.weixinOldSignature = new WeixinOldPaymentSignature(
weixinAccount.getPaySignKey(), weixinAccount.getPartnerKey());
weixinPayAccount.getPartnerKey());
this.weixinSignature = new WeixinOldPaymentSignature(
weixinPayAccount.getPaySignKey(),
weixinPayAccount.getPartnerKey());
}
/**
* 返回商户信息
*
* @return
*/
public WeixinOldPayAccount getWeixinPayAccount() {
return this.weixinAccount;
return weixinPayAccount;
}
/**
* token管理
*
* @return
*/
public TokenManager getTokenManager() {
return this.tokenManager;
}
/**
* 返回签名对象
*
* @return
*/
public WeixinOldPaymentSignature getWeixinPaymentSignature() {
return this.weixinOldSignature;
return this.weixinSignature;
}
/**
@@ -124,7 +161,7 @@ public class PayOldApi extends MpApi {
public String createPayJsRequestJson(String body, String outTradeNo,
double totalFee, String notifyUrl, String createIp) {
PayPackageV2 payPackage = new PayPackageV2(
weixinAccount.getPartnerId(), body, outTradeNo, totalFee,
weixinPayAccount.getPartnerId(), body, outTradeNo, totalFee,
notifyUrl, createIp);
return createPayJsRequestJson(payPackage);
}
@@ -137,9 +174,9 @@ public class PayOldApi extends MpApi {
* @return 支付json串
*/
public String createPayJsRequestJson(PayPackageV2 payPackage) {
PayRequest payRequest = new PayRequest(weixinAccount.getId(),
weixinOldSignature.sign(payPackage));
payRequest.setPaySign(weixinOldSignature.sign(payRequest));
PayRequest payRequest = new PayRequest(weixinPayAccount.getId(),
weixinSignature.sign(payPackage));
payRequest.setPaySign(weixinSignature.sign(payRequest));
payRequest.setSignType(SignType.SHA1);
return JSON.toJSONString(payRequest);
}
@@ -155,14 +192,14 @@ public class PayOldApi extends MpApi {
Map<String, String> map = new HashMap<String, String>();
String timestamp = DateUtil.timestamp2string();
String noncestr = RandomUtil.generateString(16);
map.put("appid", weixinAccount.getId());
map.put("appid", weixinPayAccount.getId());
map.put("timestamp", timestamp);
map.put("noncestr", noncestr);
map.put("productid", productId);
map.put("appkey", weixinAccount.getPaySignKey());
String sign = weixinOldSignature.sign(map);
map.put("appkey", weixinPayAccount.getPaySignKey());
String sign = weixinSignature.sign(map);
String nativepay_uri = getRequestUri("nativepay_old_uri");
return String.format(nativepay_uri, sign, weixinAccount.getId(),
return String.format(nativepay_uri, sign, weixinPayAccount.getId(),
productId, timestamp, noncestr);
}
@@ -182,23 +219,23 @@ public class PayOldApi extends MpApi {
StringBuilder sb = new StringBuilder();
sb.append(idQuery.getType().getName()).append("=")
.append(idQuery.getId());
sb.append("&partner=").append(weixinAccount.getPartnerId());
sb.append("&partner=").append(weixinPayAccount.getPartnerId());
String part = sb.toString();
sb.append("&key=").append(weixinAccount.getPartnerKey());
sb.append("&key=").append(weixinPayAccount.getPartnerKey());
String sign = DigestUtil.MD5(sb.toString()).toUpperCase();
sb.delete(0, sb.length());
sb.append(part).append("&sign=").append(sign);
String timestamp = DateUtil.timestamp2string();
JSONObject obj = new JSONObject();
obj.put("appid", weixinAccount.getId());
obj.put("appkey", weixinAccount.getPaySignKey());
obj.put("appid", weixinPayAccount.getId());
obj.put("appkey", weixinPayAccount.getPaySignKey());
obj.put("package", sb.toString());
obj.put("timestamp", timestamp);
String signature = weixinOldSignature.sign(obj);
String signature = weixinSignature.sign(obj);
obj.clear();
obj.put("appid", weixinAccount.getId());
obj.put("appid", weixinPayAccount.getId());
obj.put("package", sb.toString());
obj.put("timestamp", timestamp);
obj.put("app_signature", signature);
@@ -259,7 +296,7 @@ public class PayOldApi extends MpApi {
// 填写为 1.0 时,操作员密码为明文
// 填写为 1.1 时,操作员密码为 MD5(密码)值
map.put("service_version", "1.1");
map.put("partner", weixinAccount.getPartnerId());
map.put("partner", weixinPayAccount.getPartnerId());
map.put("out_refund_no", outRefundNo);
map.put("total_fee",
Integer.toString(DateUtil.formatYuan2Fen(totalFee)));
@@ -267,7 +304,7 @@ public class PayOldApi extends MpApi {
Integer.toString(DateUtil.formatYuan2Fen(refundFee)));
map.put(idQuery.getType().getName(), idQuery.getId());
if (StringUtil.isBlank(opUserId)) {
opUserId = weixinAccount.getPartnerId();
opUserId = weixinPayAccount.getPartnerId();
}
map.put("op_user_id", opUserId);
if (mopara != null && !mopara.isEmpty()) {
@@ -280,7 +317,7 @@ public class PayOldApi extends MpApi {
KeyStore ks = null;
String jksPwd = "";
File jksFile = new File(String.format("%s%stenpay_cacert.jks",
settings.getTmpdir0(), File.separator));
System.getProperty("java.io.tmpdir"), File.separator));
// create jks ca
if (!jksFile.exists()) {
CertificateFactory cf = CertificateFactory
@@ -307,8 +344,8 @@ public class PayOldApi extends MpApi {
KeyManagerFactory kmf = KeyManagerFactory
.getInstance(Consts.SunX509);
ks = KeyStore.getInstance(Consts.PKCS12);
ks.load(certificate, weixinAccount.getPartnerId().toCharArray());
kmf.init(ks, weixinAccount.getPartnerId().toCharArray());
ks.load(certificate, weixinPayAccount.getPartnerId().toCharArray());
kmf.init(ks, weixinPayAccount.getPartnerId().toCharArray());
ctx = SSLContext.getInstance(Consts.TLS);
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(),
@@ -442,7 +479,7 @@ public class PayOldApi extends MpApi {
String formatBillDate = DateUtil.fortmat2yyyyMMdd(billDate);
String fileName = String.format("weixin4j_bill_%s_%s_%s.txt",
formatBillDate, billType.name().toLowerCase(),
weixinAccount.getId());
weixinPayAccount.getId());
File file = new File(String.format("%s%s%s", billPath, File.separator,
fileName));
if (file.exists()) {
@@ -451,12 +488,12 @@ public class PayOldApi extends MpApi {
String downloadbill_uri = getRequestUri("downloadbill_old_uri");
Map<String, String> map = new HashMap<String, String>();
map.put("spid", weixinAccount.getPartnerId());
map.put("spid", weixinPayAccount.getPartnerId());
map.put("trans_time", DateUtil.fortmat2yyyy_MM_dd(billDate));
map.put("stamp", DateUtil.timestamp2string());
map.put("cft_signtype", "0");
map.put("mchtype", Integer.toString(billType.getVal()));
map.put("key", weixinAccount.getPartnerKey());
map.put("key", weixinPayAccount.getPartnerKey());
String sign = DigestUtil.MD5(MapUtil.toJoinString(map, false, false));
map.put("sign", sign.toLowerCase());
WeixinResponse response = weixinExecutor.get(String.format("%s?%s",
@@ -507,7 +544,7 @@ public class PayOldApi extends MpApi {
String refundquery_uri = getRequestUri("refundquery_old_uri");
Map<String, String> map = new HashMap<String, String>();
map.put("input_charset", Consts.UTF_8.name());
map.put("partner", weixinAccount.getPartnerId());
map.put("partner", weixinPayAccount.getPartnerId());
map.put(idQuery.getType().getName(), idQuery.getId());
String sign = weixinMD5Signature.sign(map);
map.put("sign", sign.toLowerCase());
@@ -540,15 +577,15 @@ public class PayOldApi extends MpApi {
Token token = tokenManager.getCache();
Map<String, String> map = new HashMap<String, String>();
map.put("appid", weixinAccount.getId());
map.put("appkey", weixinAccount.getPaySignKey());
map.put("appid", weixinPayAccount.getId());
map.put("appkey", weixinPayAccount.getPaySignKey());
map.put("openid", openId);
map.put("transid", transid);
map.put("out_trade_no", outTradeNo);
map.put("deliver_timestamp", DateUtil.timestamp2string());
map.put("deliver_status", status ? "1" : "0");
map.put("deliver_msg", statusMsg);
map.put("app_signature", weixinOldSignature.sign(map));
map.put("app_signature", weixinSignature.sign(map));
map.put("sign_method", SignType.SHA1.name().toLowerCase());
WeixinResponse response = weixinExecutor.post(
@@ -9,12 +9,23 @@ tenpay_base_url=http://mch.tenpay.com
tenpay_ssl_base_url=https://mch.tenpay.com
tenpay_gw_base_url=https://gw.tenpay.com
# \u7f51\u9875\u6388\u6743\u83b7\u53d6\u7528\u6237\u4fe1\u606f
# \u7f51\u9875oauth\u6388\u6743URL
sns_user_auth_uri=https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect
sns_user_token_uri=https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code
sns_token_refresh_uri=https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s
sns_auth_token_uri=https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s
sns_user_info_uri=https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u4ee3\u516c\u4f17\u53f7\u7f51\u9875oauth\u6388\u6743URL
sns_component_user_auth_uri=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
# \u7f51\u9875oauth\u6388\u6743\u83b7\u53d6token
sns_user_token_uri={api_base_url}/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u4ee3\u516c\u4f17\u53f7\u7f51\u9875oauth\u6388\u6743\u83b7\u53d6token
sns_component_user_token_uri={api_base_url}/sns/oauth2/component/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s
# \u7f51\u9875oauth\u6388\u6743\u5237\u65b0token
sns_token_refresh_uri={api_base_url}/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u4ee3\u516c\u4f17\u53f7\u7f51\u9875oauth\u6388\u6743\u5237\u65b0token
sns_component_token_refresh_uri={api_base_url}/sns/oauth2/component/refresh_token?appid=%s&grant_type=refresh_token&component_appid=%s&component_access_token=%s&refresh_token=%s
# \u7f51\u9875oauthoauth\u6388\u6743\u9a8c\u8bc1token
sns_auth_token_uri={api_base_url}/sns/auth?access_token=%s&openid=%s
# \u7f51\u9875oauth\u6388\u6743\u83b7\u53d6\u7528\u6237\u4fe1\u606f
sns_user_info_uri={api_base_url}/sns/userinfo?access_token=%s&openid=%s&lang=%s
# \u5f00\u653e\u5e73\u53f0\u626b\u7801\u767b\u9646\u6388\u6743
open_user_auth_uri=https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect
# \u76f4\u63a5\u83b7\u53d6\u7528\u6237\u4fe1\u606f
@@ -18,7 +18,7 @@ public class OauthToken extends Token {
/**
* 用户的openid
*/
@JSONField(name = "openId")
@JSONField(name = "openid")
private String openId;
/**
* 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段
@@ -12,8 +12,7 @@ import com.foxinmy.weixin4j.model.WeixinAccount;
* @className WeixinMpAccount
* @author jinyu
* @date Jul 6, 2016
* @since JDK 1.8
* @see
* @since JDK 1.6
*/
public class WeixinMpAccount extends WeixinAccount {
@@ -14,15 +14,18 @@ public final class URLConsts {
/**
* 公众平台获取token的url
*/
public static final String ASSESS_TOKEN_URL = BASE_URL + "/token?grant_type=client_credential&appid=%s&secret=%s";
public static final String ASSESS_TOKEN_URL = BASE_URL
+ "/token?grant_type=client_credential&appid=%s&secret=%s";
/**
* 公众平台jssdk获取token的url
*/
public static final String JS_TICKET_URL = BASE_URL + "/ticket/getticket?access_token=%s&type=%s";
public static final String JS_TICKET_URL = BASE_URL
+ "/ticket/getticket?access_token=%s&type=%s";
/**
* 开放平台获取token的url
*/
public static final String COMPONENT_TOKEN_URL = BASE_URL + "/component/api_component_token";
public static final String COMPONENT_TOKEN_URL = BASE_URL
+ "/component/api_component_token";
/**
* 开放平台获取预授权码的url
*/
@@ -36,6 +39,5 @@ public final class URLConsts {
/**
* 开放平台oauth授权的url
*/
public static final String COMPONENT_OAUTH_URL = BASE_URL
+ "/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
public static final String COMPONENT_OAUTH_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";
}
@@ -5,20 +5,21 @@ weixin4j.account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed0
"components":[{"id":"\u5e94\u7528\u7ec4\u4ef6\u7684id","secret":"\u5e94\u7528\u7ec4\u4ef6\u7684secret"}],\
"mchId":"\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
"certificateKey":"\u52a0\u8f7d\u652f\u4ed8\u8bc1\u4e66\u6587\u4ef6\u7684\u5bc6\u7801 \u5982\u679c\u4e0d\u586b\u5199\u5219\u9ed8\u8ba4\u83b7\u53d6mchId\u4f5c\u4e3a\u5bc6\u7801",\
"certificateFile":"\u5fae\u4fe1\u652f\u4ed8\u67d0\u4e9b\u63a5\u53e3(\u9000\u6b3e\u7b49)\u9700\u8981\u7684ca\u8bc1\u4e66\u5b58\u653e\u7684\u8def\u5f84,classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199classpath:xxxxx.p12,\u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6classpath\u6839\u76ee\u5f55\u4e0b\u7684ca.p12\u6587\u4ef6",\
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
# weixin4j\u7684\u4e34\u65f6\u76ee\u5f55
# \u53ef\u80fd\u5b58\u653etoken\u6587\u4ef6\u3001\u4e8c\u7ef4\u7801\u6587\u4ef6\u3001\u5a92\u4f53\u6587\u4ef6\u3001\u5bf9\u8d26\u5355\u6587\u4ef6\u7b49
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6java.io.tmpdir\u4e34\u65f6\u76ee\u5f55
weixin4j.tmpdir=
# \u5fae\u4fe1\u652f\u4ed8\u67d0\u4e9b\u63a5\u53e3\u9700\u8981\u7684ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
# weixin4j.certificate.file=classpath:xxxxx.p12
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6classpath\u6839\u76ee\u5f55\u4e0b\u7684ca.p12\u6587\u4ef6
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
# \u7528\u6237oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
# \u516c\u4f17\u53f7\u5fae\u4fe1\u7f51\u9875oauth\u6388\u6743\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199,\u4e5f\u53ef\u81ea\u5b9a\u4e49\u4f20\u5165)
# \u8be6\u89c1\uff1ahttps://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842&token=&lang=zh_CN
weixin4j.user.oauth.redirect.uri=
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528ComponentApi\u65f6\u586b\u5199)
weixin4j.component.oauth.redirect.uri=
# \u7f51\u7ad9\u626b\u63cf\u767b\u9646oauth\u6388\u6743\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199,\u4e5f\u53ef\u81ea\u5b9a\u4e49\u4f20\u5165)
# \u8be6\u89c1\uff1ahttps://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
weixin4j.open.user.oauth.redirect.uri=
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u6388\u6743\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528ComponentApi\u65f6\u586b\u5199,\u4e5f\u53ef\u81ea\u5b9a\u4e49\u4f20\u5165)
# \u8be6\u89c1\uff1ahttps://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=zh_CN
weixin4j.component.oauth.redirect.uri=
# \u7b2c\u4e09\u65b9\u7ec4\u4ef6\u4ee3\u66ff\u6388\u6743\u516c\u4f17\u53f7\u53d1\u8d77\u7f51\u9875\u6388\u6743\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528ComponentApi\u65f6\u586b\u5199,\u4e5f\u53ef\u81ea\u5b9a\u4e49\u4f20\u5165)
# \u8be6\u89c1\uff1ahttps://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN
weixin4j.component.user.oauth.redirect.uri=
@@ -44,6 +44,6 @@ public class HelperTest extends TokenTest {
@Test
public void clearQuota() throws WeixinException {
System.err.println(helperApi.clearQuota(settings.getAccount().getId()));
System.err.println(helperApi.clearQuota(weixinAccount.getId()));
}
}
@@ -7,10 +7,11 @@ import java.util.Calendar;
import org.junit.Test;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.mp.api.PayOldApi;
import com.foxinmy.weixin4j.mp.oldpayment.WeixinOldPayAccount;
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.IdType;
@@ -24,14 +25,14 @@ import com.foxinmy.weixin4j.type.IdType;
* @see
*/
public class PayTest {
protected final static PayOldApi PAY2;
protected final static Weixin4jSettings<WeixinOldPayAccount> settings;
private final static PayOldApi PAY2;
private final static WeixinOldPayAccount WEIXIN_OLD_PAY_ACCOUNT;
static {
settings = new Weixin4jSettings<WeixinOldPayAccount>(
new WeixinOldPayAccount("请填入v2版本的appid", "请填入v2版本的appSecret",
"请填入v2版本的paysignkey", "请填入v2版本的partnerId",
"请填入v2版本的partnerKey"));
PAY2 = new PayOldApi(settings);
WEIXIN_OLD_PAY_ACCOUNT = new WeixinOldPayAccount("请填入v2版本的appid",
"请填入v2版本的appSecret", "请填入v2版本的paysignkey", "请填入v2版本的partnerId",
"请填入v2版本的partnerKey");
PAY2 = new PayOldApi(WEIXIN_OLD_PAY_ACCOUNT,
new FileCacheStorager<Token>());
}
/**
* 商户证书文件
@@ -4,10 +4,11 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
@@ -22,15 +23,14 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
public class TokenTest {
protected TokenManager tokenManager;
protected Weixin4jSettings<WeixinAccount> settings;
protected WeixinAccount weixinAccount;
@Before
public void setUp() {
this.settings = new Weixin4jSettings<WeixinAccount>(
Weixin4jConfigUtil.getWeixinAccount());
tokenManager = new TokenManager(new WeixinTokenCreator(settings
.getAccount().getId(), settings.getAccount().getSecret()),
settings.getCacheStorager0());
this.weixinAccount = Weixin4jConfigUtil.getWeixinAccount();
tokenManager = new TokenManager(new WeixinTokenCreator(
weixinAccount.getId(), weixinAccount.getSecret()),
new FileCacheStorager<Token>());
}
@Test
@@ -0,0 +1,37 @@
package com.foxinmy.weixin4j.mp.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.mp.WeixinProxy;
import com.foxinmy.weixin4j.mp.model.Menu;
/**
* 微信接口测试
*
* @className WeixinProxyTest
* @author jinyu(foxinmy@gmail.com)
* @date 2016年8月18日
* @since JDK 1.6
* @see
*/
public class WeixinProxyTest {
private WeixinProxy weixinProxy;
@Before
public void init() {
this.weixinProxy = new WeixinProxy();
// Weixin4jSettings<WeixinAccount> settings = new
// Weixin4jSettings<WeixinAccount>(RediscacheStorager);
// this.weixinProxy= new WeixinProxy(settings);
}
@Test
public void test() throws WeixinException{
List<Menu> buttons = weixinProxy.getAllMenu();
System.err.println(buttons);
}
}