新增JSTICKET支持
This commit is contained in:
parent
9f8af3f725
commit
766b8da3cb
@ -224,6 +224,8 @@ netty的代码没有放到maven中心仓库,也没什么意义,因为最终需
|
|||||||
|
|
||||||
+ **weixin4j-base**: 重构token实现机制
|
+ **weixin4j-base**: 重构token实现机制
|
||||||
|
|
||||||
|
+ **weixin4j-base**: 新增JSTICKET支持
|
||||||
|
|
||||||
接下来
|
接下来
|
||||||
------
|
------
|
||||||
* 公众号数据分析接口
|
* 公众号数据分析接口
|
||||||
|
|||||||
@ -46,3 +46,5 @@ weixin4j-base
|
|||||||
* 2015-01-10
|
* 2015-01-10
|
||||||
|
|
||||||
+ 重构token实现机制
|
+ 重构token实现机制
|
||||||
|
|
||||||
|
+ 新增JSTICKET支持
|
||||||
@ -27,6 +27,7 @@ public final class Consts {
|
|||||||
public static final String OAUTH_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
|
public static final String OAUTH_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
|
||||||
public static final String MP_ASSESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
|
public static final String MP_ASSESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
|
||||||
public static final String QY_ASSESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
public static final String QY_ASSESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
||||||
|
public static final String JS_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
|
||||||
|
|
||||||
public static final String UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
|
public static final String UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
|
||||||
public static final String MICROPAYURL = "https://api.mch.weixin.qq.com/pay/micropay";
|
public static final String MICROPAYURL = "https://api.mch.weixin.qq.com/pay/micropay";
|
||||||
|
|||||||
@ -6,6 +6,8 @@ import java.net.URLEncoder;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.type.AccountType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信账号信息
|
* 微信账号信息
|
||||||
*
|
*
|
||||||
@ -26,7 +28,7 @@ public abstract class WeixinAccount implements Serializable {
|
|||||||
// 安全模式下的加密密钥
|
// 安全模式下的加密密钥
|
||||||
private String encodingAesKey;
|
private String encodingAesKey;
|
||||||
|
|
||||||
public abstract String getTokenUrl();
|
public abstract AccountType getAccountType();
|
||||||
|
|
||||||
public WeixinAccount() {
|
public WeixinAccount() {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,9 @@ package com.foxinmy.weixin4j.model;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import com.foxinmy.weixin4j.type.AccountType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众平台信息
|
* 微信公众平台信息
|
||||||
*
|
*
|
||||||
@ -31,11 +34,6 @@ public class WeixinMpAccount extends WeixinAccount {
|
|||||||
// 微信支付版本号(如果无则按照mchId来做判断)
|
// 微信支付版本号(如果无则按照mchId来做判断)
|
||||||
private int version;
|
private int version;
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getTokenUrl() {
|
|
||||||
return String.format(Consts.MP_ASSESS_TOKEN_URL, getId(), getSecret());
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getOpenId() {
|
public String getOpenId() {
|
||||||
return openId;
|
return openId;
|
||||||
}
|
}
|
||||||
@ -135,6 +133,12 @@ public class WeixinMpAccount extends WeixinAccount {
|
|||||||
this.partnerKey = partnerKey;
|
this.partnerKey = partnerKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@JSONField(deserialize = false)
|
||||||
|
public AccountType getAccountType() {
|
||||||
|
return AccountType.MP;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "WeixinMpAccount [openId=" + openId + ", paySignKey="
|
return "WeixinMpAccount [openId=" + openId + ", paySignKey="
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
package com.foxinmy.weixin4j.model;
|
package com.foxinmy.weixin4j.model;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import com.foxinmy.weixin4j.type.AccountType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信企业号信息
|
* 微信企业号信息
|
||||||
*
|
*
|
||||||
@ -22,8 +25,9 @@ public class WeixinQyAccount extends WeixinAccount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getTokenUrl() {
|
@JSONField(deserialize = false)
|
||||||
return String.format(Consts.QY_ASSESS_TOKEN_URL, getId(), getSecret());
|
public AccountType getAccountType() {
|
||||||
|
return AccountType.QY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -0,0 +1,53 @@
|
|||||||
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||||
|
import com.foxinmy.weixin4j.http.Response;
|
||||||
|
import com.foxinmy.weixin4j.model.Consts;
|
||||||
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信JSTICKET创建者
|
||||||
|
*
|
||||||
|
* @className WeixinJSTicketCreator
|
||||||
|
* @author jy
|
||||||
|
* @date 2015年1月10日
|
||||||
|
* @since JDK 1.7
|
||||||
|
* @see <a
|
||||||
|
* href="http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95">JS
|
||||||
|
* TICKET</a>
|
||||||
|
*/
|
||||||
|
public class WeixinJSTicketCreator implements TokenCreator {
|
||||||
|
|
||||||
|
private final String appid;
|
||||||
|
private final TokenHolder weixinTokenHolder;
|
||||||
|
private final HttpRequest request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <font color="red">公众平台的access_token</font>
|
||||||
|
*
|
||||||
|
* @param weixinTokenHolder
|
||||||
|
*/
|
||||||
|
public WeixinJSTicketCreator(String appid, TokenHolder weixinTokenHolder) {
|
||||||
|
this.appid = appid;
|
||||||
|
this.weixinTokenHolder = weixinTokenHolder;
|
||||||
|
this.request = new HttpRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCacheKey() {
|
||||||
|
return String.format("%s_jskey", appid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Token createToken() throws WeixinException {
|
||||||
|
Response response = request.get(String.format(Consts.JS_TICKET_URL,
|
||||||
|
weixinTokenHolder.getToken().getAccessToken()));
|
||||||
|
JSONObject result = response.getAsJson();
|
||||||
|
Token token = new Token(result.getString("ticket"));
|
||||||
|
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||||
|
token.setTime(System.currentTimeMillis());
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import com.alibaba.fastjson.TypeReference;
|
|||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||||
import com.foxinmy.weixin4j.http.Response;
|
import com.foxinmy.weixin4j.http.Response;
|
||||||
|
import com.foxinmy.weixin4j.model.Consts;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||||
import com.foxinmy.weixin4j.type.AccountType;
|
import com.foxinmy.weixin4j.type.AccountType;
|
||||||
@ -25,25 +26,38 @@ import com.foxinmy.weixin4j.util.ConfigUtil;
|
|||||||
public class WeixinTokenCreator implements TokenCreator {
|
public class WeixinTokenCreator implements TokenCreator {
|
||||||
|
|
||||||
private final HttpRequest request;
|
private final HttpRequest request;
|
||||||
private final WeixinAccount weixinAccount;
|
private final String id;
|
||||||
|
private final String secret;
|
||||||
|
private final AccountType accountType;
|
||||||
|
|
||||||
public WeixinTokenCreator(AccountType accountType) {
|
public WeixinTokenCreator(AccountType accountType) {
|
||||||
this(ConfigUtil.getWeixinAccount(accountType.getClazz()));
|
WeixinAccount weixinAccount = ConfigUtil.getWeixinAccount(accountType
|
||||||
|
.getClazz());
|
||||||
|
this.id = weixinAccount.getId();
|
||||||
|
this.secret = weixinAccount.getSecret();
|
||||||
|
this.accountType = accountType;
|
||||||
|
this.request = new HttpRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeixinTokenCreator(WeixinAccount weixinAccount) {
|
public WeixinTokenCreator(String id, String secret, AccountType accountType) {
|
||||||
|
this.id = id;
|
||||||
|
this.secret = secret;
|
||||||
|
this.accountType = accountType;
|
||||||
this.request = new HttpRequest();
|
this.request = new HttpRequest();
|
||||||
this.weixinAccount = weixinAccount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
return weixinAccount.getId();
|
return String.format("%s_%s", accountType.name(), id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token createToken() throws WeixinException {
|
public Token createToken() throws WeixinException {
|
||||||
Response response = request.get(weixinAccount.getTokenUrl());
|
String tokenUrl = String.format(Consts.MP_ASSESS_TOKEN_URL, id, secret);
|
||||||
|
if (accountType == AccountType.QY) {
|
||||||
|
tokenUrl = String.format(Consts.QY_ASSESS_TOKEN_URL, id, secret);
|
||||||
|
}
|
||||||
|
Response response = request.get(tokenUrl);
|
||||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||||
});
|
});
|
||||||
token.setTime(System.currentTimeMillis());
|
token.setTime(System.currentTimeMillis());
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import java.util.List;
|
|||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.JsonResult;
|
import com.foxinmy.weixin4j.http.JsonResult;
|
||||||
import com.foxinmy.weixin4j.model.Button;
|
import com.foxinmy.weixin4j.model.Button;
|
||||||
import com.foxinmy.weixin4j.model.WeixinMpAccount;
|
|
||||||
import com.foxinmy.weixin4j.mp.api.CustomApi;
|
import com.foxinmy.weixin4j.mp.api.CustomApi;
|
||||||
import com.foxinmy.weixin4j.mp.api.GroupApi;
|
import com.foxinmy.weixin4j.mp.api.GroupApi;
|
||||||
import com.foxinmy.weixin4j.mp.api.HelperApi;
|
import com.foxinmy.weixin4j.mp.api.HelperApi;
|
||||||
@ -71,24 +70,13 @@ public class WeixinProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* appid,appsecret<br>
|
|
||||||
* <font color="red">将无法调用支付相关接口</font>
|
|
||||||
*
|
*
|
||||||
* @param appid
|
* @param appid
|
||||||
* @param appsecret
|
* @param appsecret
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(String appid, String appsecret) {
|
public WeixinProxy(String appid, String appsecret) {
|
||||||
this(new WeixinMpAccount(appid, appsecret));
|
this(new FileTokenHolder(new WeixinTokenCreator(appid, appsecret,
|
||||||
}
|
AccountType.MP)));
|
||||||
|
|
||||||
/**
|
|
||||||
* WeixinAccount对象
|
|
||||||
*
|
|
||||||
* @param weixinAccount
|
|
||||||
* 微信账户
|
|
||||||
*/
|
|
||||||
public WeixinProxy(WeixinMpAccount weixinAccount) {
|
|
||||||
this(new FileTokenHolder(new WeixinTokenCreator(weixinAccount)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -35,10 +35,11 @@ public abstract class PayApi extends MpApi {
|
|||||||
|
|
||||||
protected final TokenHolder tokenHolder;
|
protected final TokenHolder tokenHolder;
|
||||||
protected final WeixinMpAccount weixinAccount;
|
protected final WeixinMpAccount weixinAccount;
|
||||||
|
|
||||||
public PayApi(WeixinMpAccount weixinAccount) {
|
public PayApi(WeixinMpAccount weixinAccount) {
|
||||||
this.weixinAccount = weixinAccount;
|
this.weixinAccount = weixinAccount;
|
||||||
this.tokenHolder = new FileTokenHolder(new WeixinTokenCreator(
|
this.tokenHolder = new FileTokenHolder(new WeixinTokenCreator(
|
||||||
weixinAccount));
|
weixinAccount.getAccountType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -124,9 +124,11 @@ micropay_uri={mch_base_url}/pay/micropay
|
|||||||
# \u63a5\u53e3\u4e0a\u62a5
|
# \u63a5\u53e3\u4e0a\u62a5
|
||||||
pay_report_uri={mch_base_url}/payitil/report
|
pay_report_uri={mch_base_url}/payitil/report
|
||||||
|
|
||||||
|
|
||||||
# \u7edf\u4e00\u8ba2\u5355\u751f\u6210
|
# \u7edf\u4e00\u8ba2\u5355\u751f\u6210
|
||||||
unifiedorder_uri={mch_base_url}/pay/unifiedorder
|
unifiedorder_uri={mch_base_url}/pay/unifiedorder
|
||||||
# native\u652f\u4ed8\u94fe\u63a5
|
# native\u652f\u4ed8\u94fe\u63a5
|
||||||
nativepay_v2_uri=weixin://wxpay/bizpayurl?sign=%s&appid=%s&productid=%s×tamp=%s&noncestr=%s
|
nativepay_v2_uri=weixin://wxpay/bizpayurl?sign=%s&appid=%s&productid=%s×tamp=%s&noncestr=%s
|
||||||
nativepay_v3_uri=weixin://wxpay/bizpayurl?sign=%s&appid=%s&mch_id=%s&product_id=%s&time_stamp=%s&nonce_str=%s
|
nativepay_v3_uri=weixin://wxpay/bizpayurl?sign=%s&appid=%s&mch_id=%s&product_id=%s&time_stamp=%s&nonce_str=%s
|
||||||
|
|
||||||
|
# \u83b7\u53d6\u7528\u6237\u589e\u51cf\u6570\u636e
|
||||||
|
datacube_getusersummary_uri={api_base_url}/datacube/getusersummary?access_token=%s
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import java.util.List;
|
|||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.JsonResult;
|
import com.foxinmy.weixin4j.http.JsonResult;
|
||||||
import com.foxinmy.weixin4j.model.WeixinQyAccount;
|
|
||||||
import com.foxinmy.weixin4j.qy.api.DepartApi;
|
import com.foxinmy.weixin4j.qy.api.DepartApi;
|
||||||
import com.foxinmy.weixin4j.qy.api.HelperApi;
|
import com.foxinmy.weixin4j.qy.api.HelperApi;
|
||||||
import com.foxinmy.weixin4j.qy.api.TagApi;
|
import com.foxinmy.weixin4j.qy.api.TagApi;
|
||||||
@ -47,16 +46,8 @@ public class WeixinProxy {
|
|||||||
* @param corpsecret
|
* @param corpsecret
|
||||||
*/
|
*/
|
||||||
public WeixinProxy(String corpid, String corpsecret) {
|
public WeixinProxy(String corpid, String corpsecret) {
|
||||||
this(new WeixinQyAccount(corpid, corpsecret));
|
this(new FileTokenHolder(new WeixinTokenCreator(corpid, corpsecret,
|
||||||
}
|
AccountType.QY)));
|
||||||
|
|
||||||
/**
|
|
||||||
* WeixinAccount对象
|
|
||||||
*
|
|
||||||
* @param weixinAccount
|
|
||||||
*/
|
|
||||||
public WeixinProxy(WeixinQyAccount weixinAccount) {
|
|
||||||
this(new FileTokenHolder(new WeixinTokenCreator(weixinAccount)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user