重构token实现机制
This commit is contained in:
parent
5b40a3728c
commit
9f8af3f725
@ -220,8 +220,16 @@ netty的代码没有放到maven中心仓库,也没什么意义,因为最终需
|
||||
|
||||
+ **weixin4j-qy**: 新增批量删除员工接口
|
||||
|
||||
* 2015-01-10
|
||||
|
||||
+ **weixin4j-base**: 重构token实现机制
|
||||
|
||||
接下来
|
||||
------
|
||||
* 公众号数据分析接口
|
||||
|
||||
* 公众号服务应用
|
||||
|
||||
* 企业号第三方应用
|
||||
|
||||
* 微信小店
|
||||
|
||||
@ -42,3 +42,7 @@ weixin4j-base
|
||||
* 2015-01-04
|
||||
|
||||
+ ConfigUtil类新增获取classpath目录下的资源路径的方法
|
||||
|
||||
* 2015-01-10
|
||||
|
||||
+ 重构token实现机制
|
||||
@ -100,13 +100,18 @@ public class HttpRequest {
|
||||
StringBuilder sb = new StringBuilder(url);
|
||||
if (parameters != null && parameters.length > 0) {
|
||||
if (url.indexOf("?") < 0) {
|
||||
sb.append(String.format("?%s=%s", parameters[0].getName(),
|
||||
parameters[0].getValue()));
|
||||
sb.append("?");
|
||||
} else {
|
||||
sb.append("&");
|
||||
}
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
sb.append(String.format("%s=%s", parameters[0].getName(),
|
||||
parameters[0].getValue()));
|
||||
if (parameters.length > 1) {
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
sb.append(parameters[i].toGetPara());
|
||||
}
|
||||
}
|
||||
}
|
||||
return doRequest(new HttpGet(sb.toString()));
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,6 @@ package com.foxinmy.weixin4j.model;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
/**
|
||||
* access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token,正常情况下access_token有效期为7200秒,
|
||||
@ -15,9 +14,9 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html">微信公众平台获取token</a>
|
||||
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号的主动模式</a>
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号的主动模式</a>
|
||||
*/
|
||||
@XStreamAlias("app-token")
|
||||
public class Token implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -7564855472419104084L;
|
||||
@ -28,6 +27,14 @@ public class Token implements Serializable {
|
||||
private int expiresIn;
|
||||
private long time;
|
||||
|
||||
public Token() {
|
||||
|
||||
}
|
||||
|
||||
public Token(String accessToken) {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
public String getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
@ -1,34 +0,0 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
* 获取weixin.properties中的id&secret信息
|
||||
*
|
||||
* @className AbstractTokenHolder
|
||||
* @author jy
|
||||
* @date 2014年10月6日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public abstract class AbstractTokenHolder implements TokenHolder {
|
||||
|
||||
protected final HttpRequest request = new HttpRequest();
|
||||
protected final WeixinAccount weixinAccount;
|
||||
|
||||
public AbstractTokenHolder(AccountType accountType) {
|
||||
this.weixinAccount = ConfigUtil
|
||||
.getWeixinAccount(accountType.getClazz());
|
||||
}
|
||||
|
||||
public AbstractTokenHolder(WeixinAccount weixinAccount) {
|
||||
this.weixinAccount = weixinAccount;
|
||||
}
|
||||
|
||||
public WeixinAccount getAccount() {
|
||||
return weixinAccount;
|
||||
}
|
||||
}
|
||||
@ -6,79 +6,59 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.Response;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.xml.XmlStream;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
/**
|
||||
* 基于文件保存的Token获取类
|
||||
* 用FILE保存TOKEN
|
||||
*
|
||||
* @className FileTokenHolder
|
||||
* @author jy.hu
|
||||
* @date 2014年9月27日
|
||||
* @author jy
|
||||
* @date 2015年1月9日
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html">微信公众平台获取token说明</a>
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
* @see com.foxinmy.weixin4j.token.TokenCreator
|
||||
* @see com.foxinmy.weixin4j.token.WeixinTokenCreator
|
||||
*/
|
||||
public class FileTokenHolder extends AbstractTokenHolder {
|
||||
public class FileTokenHolder implements TokenHolder {
|
||||
private final XStream xstream;
|
||||
private final String tokenPath;
|
||||
private final TokenCreator tokenCretor;
|
||||
|
||||
public FileTokenHolder(AccountType accountType) {
|
||||
super(accountType);
|
||||
public FileTokenHolder(TokenCreator tokenCretor) {
|
||||
this(ConfigUtil.getValue("token_path"), tokenCretor);
|
||||
}
|
||||
|
||||
public FileTokenHolder(WeixinAccount weixinAccount) {
|
||||
super(weixinAccount);
|
||||
public FileTokenHolder(String tokenPath, TokenCreator tokenCretor) {
|
||||
this.tokenPath = tokenPath;
|
||||
this.tokenCretor = tokenCretor;
|
||||
xstream = new XStream();
|
||||
xstream.ignoreUnknownElements();
|
||||
xstream.autodetectAnnotations(true);
|
||||
xstream.alias("xml", Token.class);
|
||||
xstream.processAnnotations(Token.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token
|
||||
* <p>
|
||||
* 正常情况下返回{"access_token":"ACCESS_TOKEN","expires_in":7200},否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @return token对象
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
@Override
|
||||
public Token getToken() throws WeixinException {
|
||||
String id = weixinAccount.getId();
|
||||
if (StringUtils.isBlank(id)
|
||||
|| StringUtils.isBlank(weixinAccount.getSecret())) {
|
||||
throw new IllegalArgumentException("id or secret not be null!");
|
||||
}
|
||||
File token_file = new File(String.format("%s/token_%s.xml",
|
||||
ConfigUtil.getValue("token_path"), id));
|
||||
File token_file = new File(String.format("%s/%s.xml", tokenPath,
|
||||
tokenCretor.getCacheKey()));
|
||||
Token token = null;
|
||||
Calendar ca = Calendar.getInstance();
|
||||
long now_time = ca.getTimeInMillis();
|
||||
try {
|
||||
if (token_file.exists()) {
|
||||
token = XmlStream.get(new FileInputStream(token_file),
|
||||
Token.class);
|
||||
token = (Token) xstream
|
||||
.fromXML(new FileInputStream(token_file));
|
||||
long expire_time = token.getTime()
|
||||
+ (token.getExpiresIn() * 1000) - 2;
|
||||
if (expire_time > now_time) {
|
||||
return token;
|
||||
}
|
||||
}
|
||||
Response response = request.get(weixinAccount.getTokenUrl());
|
||||
token = response.getAsObject(new TypeReference<Token>() {
|
||||
});
|
||||
token.setTime(now_time);
|
||||
XmlStream.to(token, new FileOutputStream(token_file));
|
||||
token = tokenCretor.createToken();
|
||||
xstream.toXML(token, new FileOutputStream(token_file));
|
||||
} catch (IOException e) {
|
||||
throw new WeixinException(e.getMessage());
|
||||
}
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
@ -9,82 +7,69 @@ import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
|
||||
/**
|
||||
* 基于redis保存的Token获取类
|
||||
* 用REDIS保存TOKEN
|
||||
*
|
||||
* @className RedisTokenHolder
|
||||
* @author jy.hu
|
||||
* @date 2014年9月27日
|
||||
* @author jy
|
||||
* @date 2015年1月9日
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html">微信公众平台获取token说明</a>
|
||||
* @see <a href=
|
||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8"
|
||||
* >微信企业号获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
* @see com.foxinmy.weixin4j.token.TokenCreator
|
||||
* @see com.foxinmy.weixin4j.token.WeixinTokenCreator
|
||||
*/
|
||||
public class RedisTokenHolder extends AbstractTokenHolder {
|
||||
public class RedisTokenHolder implements TokenHolder {
|
||||
|
||||
private JedisPool jedisPool;
|
||||
private final TokenCreator tokenCretor;
|
||||
|
||||
private void createPool(String host, int port) {
|
||||
JedisPoolConfig poolConfig = new JedisPoolConfig();
|
||||
poolConfig.setMaxTotal(50);
|
||||
poolConfig.setMaxIdle(5);
|
||||
poolConfig.setMaxWaitMillis(2000);
|
||||
poolConfig.setTestOnBorrow(false);
|
||||
poolConfig.setTestOnReturn(true);
|
||||
this.jedisPool = new JedisPool(poolConfig, host, port);
|
||||
public final static int MAX_TOTAL = 50;
|
||||
public final static int MAX_IDLE = 5;
|
||||
public final static int MAX_WAIT_MILLIS = 2000;
|
||||
public final static boolean TEST_ON_BORROW = false;
|
||||
public final static boolean TEST_ON_RETURN = true;
|
||||
|
||||
public RedisTokenHolder(TokenCreator tokenCretor) {
|
||||
this("localhost", 6379, tokenCretor);
|
||||
}
|
||||
|
||||
public RedisTokenHolder(String host, int port, AccountType accountType) {
|
||||
super(accountType);
|
||||
createPool(host, port);
|
||||
public RedisTokenHolder(String host, int port, TokenCreator tokenCretor) {
|
||||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||
jedisPoolConfig.setMaxTotal(MAX_TOTAL);
|
||||
jedisPoolConfig.setMaxIdle(MAX_IDLE);
|
||||
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
|
||||
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
|
||||
jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN);
|
||||
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
||||
this.tokenCretor = tokenCretor;
|
||||
}
|
||||
|
||||
public RedisTokenHolder(AccountType accountType) {
|
||||
this("localhost", 6379, accountType);
|
||||
public RedisTokenHolder(String host, int port,
|
||||
JedisPoolConfig jedisPoolConfig, TokenCreator tokenCretor) {
|
||||
this(new JedisPool(jedisPoolConfig, host, port), tokenCretor);
|
||||
}
|
||||
|
||||
public RedisTokenHolder(WeixinAccount weixinAccount) {
|
||||
this("localhost", 6379, weixinAccount);
|
||||
}
|
||||
|
||||
public RedisTokenHolder(String host, int port, WeixinAccount weixinAccount) {
|
||||
super(weixinAccount);
|
||||
createPool(host, port);
|
||||
public RedisTokenHolder(JedisPool jedisPool, TokenCreator tokenCretor) {
|
||||
this.jedisPool = jedisPool;
|
||||
this.tokenCretor = tokenCretor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token getToken() throws WeixinException {
|
||||
String id = weixinAccount.getId();
|
||||
if (StringUtils.isBlank(id)
|
||||
|| StringUtils.isBlank(weixinAccount.getSecret())) {
|
||||
throw new IllegalArgumentException("id or secret not be null!");
|
||||
}
|
||||
Token token = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
Calendar now = Calendar.getInstance();
|
||||
String key = String.format("token:%s", id);
|
||||
String accessToken = jedis.get(key);
|
||||
String cacheKey = tokenCretor.getCacheKey();
|
||||
String accessToken = jedis.get(cacheKey);
|
||||
if (StringUtils.isBlank(accessToken)) {
|
||||
token = request.get(weixinAccount.getTokenUrl()).getAsObject(
|
||||
new TypeReference<Token>() {
|
||||
});
|
||||
jedis.setex(key, token.getExpiresIn(),
|
||||
token = tokenCretor.createToken();
|
||||
jedis.setex(cacheKey, (int) token.getExpiresIn(),
|
||||
token.getAccessToken());
|
||||
token.setTime(now.getTimeInMillis());
|
||||
} else {
|
||||
token = new Token();
|
||||
token.setAccessToken(accessToken);
|
||||
token = new Token(accessToken);
|
||||
}
|
||||
} catch (JedisException e) {
|
||||
jedisPool.returnBrokenResource(jedis);
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
|
||||
/**
|
||||
* TOKEN创建者
|
||||
*
|
||||
* @className TokenCreator
|
||||
* @author jy
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public interface TokenCreator {
|
||||
/**
|
||||
* 返回缓存KEY的名称
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCacheKey();
|
||||
|
||||
/**
|
||||
* 创建token
|
||||
*
|
||||
* @return
|
||||
* @throws MeetException
|
||||
*/
|
||||
public Token createToken() throws WeixinException;
|
||||
}
|
||||
@ -2,21 +2,18 @@ package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
|
||||
/**
|
||||
* 获取Token接口
|
||||
* token持有者
|
||||
*
|
||||
* @className TokenHolder
|
||||
* @author jy.hu
|
||||
* @date 2014年9月27日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
* @see com.foxinmy.weixin4j.token.AbstractTokenHolder
|
||||
* @see com.foxinmy.weixin4j.token.FileTokenHolder
|
||||
* @see com.foxinmy.weixin4j.token.RedisTokenHolder
|
||||
*/
|
||||
public interface TokenHolder {
|
||||
public WeixinAccount getAccount();
|
||||
public Token getToken() throws WeixinException;
|
||||
}
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.http.Response;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
* 微信TOKEN创建者
|
||||
*
|
||||
* @className WeixinTokenCreator
|
||||
* @author jy
|
||||
* @date 2015年1月10日
|
||||
* @since JDK 1.7
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html">微信公众平台获取token说明</a>
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号获取token说明</a>
|
||||
* @see com.foxinmy.weixin4j.model.Token
|
||||
*/
|
||||
public class WeixinTokenCreator implements TokenCreator {
|
||||
|
||||
private final HttpRequest request;
|
||||
private final WeixinAccount weixinAccount;
|
||||
|
||||
public WeixinTokenCreator(AccountType accountType) {
|
||||
this(ConfigUtil.getWeixinAccount(accountType.getClazz()));
|
||||
}
|
||||
|
||||
public WeixinTokenCreator(WeixinAccount weixinAccount) {
|
||||
this.request = new HttpRequest();
|
||||
this.weixinAccount = weixinAccount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return weixinAccount.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token createToken() throws WeixinException {
|
||||
Response response = request.get(weixinAccount.getTokenUrl());
|
||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||
});
|
||||
token.setTime(System.currentTimeMillis());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@ -15,9 +15,6 @@ import com.foxinmy.weixin4j.mp.type.BillType;
|
||||
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.mp.type.IdType;
|
||||
import com.foxinmy.weixin4j.mp.type.RefundType;
|
||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
/**
|
||||
@ -35,11 +32,8 @@ public class WeixinPayProxy {
|
||||
private final Pay2Api pay2Api;
|
||||
private final Pay3Api pay3Api;
|
||||
|
||||
/**
|
||||
* 默认采用文件存放Token信息
|
||||
*/
|
||||
public WeixinPayProxy() {
|
||||
this(new FileTokenHolder(AccountType.MP));
|
||||
this(ConfigUtil.getWeixinMpAccount());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,18 +43,9 @@ public class WeixinPayProxy {
|
||||
* 微信账户
|
||||
*/
|
||||
public WeixinPayProxy(WeixinMpAccount weixinAccount) {
|
||||
this(new FileTokenHolder(weixinAccount));
|
||||
}
|
||||
|
||||
/**
|
||||
* TokenHolder对象
|
||||
*
|
||||
* @param tokenHolder
|
||||
*/
|
||||
public WeixinPayProxy(TokenHolder tokenHolder) {
|
||||
this.pay2Api = new Pay2Api(tokenHolder);
|
||||
this.pay3Api = new Pay3Api(tokenHolder);
|
||||
int version = ((WeixinMpAccount) tokenHolder.getAccount()).getVersion();
|
||||
this.pay2Api = new Pay2Api(weixinAccount);
|
||||
this.pay3Api = new Pay3Api(weixinAccount);
|
||||
int version = weixinAccount.getVersion();
|
||||
if (version == 2) {
|
||||
this.payApi = this.pay2Api;
|
||||
} else if (version == 3) {
|
||||
|
||||
@ -37,6 +37,7 @@ import com.foxinmy.weixin4j.msg.model.MpArticle;
|
||||
import com.foxinmy.weixin4j.msg.model.Video;
|
||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
|
||||
@ -66,7 +67,7 @@ public class WeixinProxy {
|
||||
* 默认采用文件存放Token信息
|
||||
*/
|
||||
public WeixinProxy() {
|
||||
this(new FileTokenHolder(AccountType.MP));
|
||||
this(new FileTokenHolder(new WeixinTokenCreator(AccountType.MP)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -87,7 +88,7 @@ public class WeixinProxy {
|
||||
* 微信账户
|
||||
*/
|
||||
public WeixinProxy(WeixinMpAccount weixinAccount) {
|
||||
this(new FileTokenHolder(weixinAccount));
|
||||
this(new FileTokenHolder(new WeixinTokenCreator(weixinAccount)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -544,8 +545,9 @@ public class WeixinProxy {
|
||||
* @see com.foxinmy.weixin4j.mp.model.OauthToken
|
||||
* @see com.foxinmy.weixin4j.mp.api.UserApi
|
||||
*/
|
||||
public OauthToken getOauthToken(String code) throws WeixinException {
|
||||
return userApi.getOauthToken(code);
|
||||
public OauthToken getOauthToken(String code, String appid, String appsecret)
|
||||
throws WeixinException {
|
||||
return userApi.getOauthToken(code, appid, appsecret);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -8,7 +8,6 @@ import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.Response;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinMpAccount;
|
||||
import com.foxinmy.weixin4j.mp.model.SemQuery;
|
||||
import com.foxinmy.weixin4j.mp.model.SemResult;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
@ -33,7 +32,8 @@ public class HelperApi extends MpApi {
|
||||
/**
|
||||
* 长链接转短链接
|
||||
*
|
||||
* @param url 待转换的链接
|
||||
* @param url
|
||||
* 待转换的链接
|
||||
* @return 短链接
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
@ -65,10 +65,8 @@ public class HelperApi extends MpApi {
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public SemResult semantic(SemQuery semQuery) throws WeixinException {
|
||||
WeixinMpAccount weixinAccount = (WeixinMpAccount) tokenHolder.getAccount();
|
||||
String semantic_uri = getRequestUri("semantic_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
semQuery.appid(weixinAccount.getId());
|
||||
Response response = request.post(
|
||||
String.format(semantic_uri, token.getAccessToken()),
|
||||
semQuery.toJson());
|
||||
|
||||
@ -37,6 +37,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.Response;
|
||||
import com.foxinmy.weixin4j.http.SSLHttpRequest;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinMpAccount;
|
||||
import com.foxinmy.weixin4j.mp.payment.PayUtil;
|
||||
import com.foxinmy.weixin4j.mp.payment.RefundConverter;
|
||||
import com.foxinmy.weixin4j.mp.payment.v2.Order;
|
||||
@ -48,7 +49,6 @@ import com.foxinmy.weixin4j.mp.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.mp.type.RefundType;
|
||||
import com.foxinmy.weixin4j.mp.type.SignType;
|
||||
import com.foxinmy.weixin4j.mp.util.ExcelUtil;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.DateUtil;
|
||||
import com.foxinmy.weixin4j.util.MapUtil;
|
||||
@ -66,8 +66,8 @@ public class Pay2Api extends PayApi {
|
||||
|
||||
private final HelperApi helperApi;
|
||||
|
||||
public Pay2Api(TokenHolder tokenHolder) {
|
||||
super(tokenHolder);
|
||||
public Pay2Api(WeixinMpAccount weixinAccount) {
|
||||
super(weixinAccount);
|
||||
this.helperApi = new HelperApi(tokenHolder);
|
||||
}
|
||||
|
||||
@ -180,7 +180,7 @@ public class Pay2Api extends PayApi {
|
||||
}
|
||||
String sign = PayUtil
|
||||
.paysignMd5(map, weixinAccount.getPartnerKey());
|
||||
map.put("sign", sign.toLowerCase());
|
||||
map.put("sign", sign.toUpperCase());
|
||||
|
||||
SSLContext ctx = null;
|
||||
KeyStore ks = null;
|
||||
@ -232,6 +232,7 @@ public class Pay2Api extends PayApi {
|
||||
}
|
||||
}
|
||||
}
|
||||
System.err.println(response.getAsString());
|
||||
return response.getAsObject(new TypeReference<RefundResult>() {
|
||||
});
|
||||
}
|
||||
|
||||
@ -28,6 +28,7 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.Response;
|
||||
import com.foxinmy.weixin4j.http.SSLHttpRequest;
|
||||
import com.foxinmy.weixin4j.http.XmlResult;
|
||||
import com.foxinmy.weixin4j.model.WeixinMpAccount;
|
||||
import com.foxinmy.weixin4j.mp.payment.PayUtil;
|
||||
import com.foxinmy.weixin4j.mp.payment.RefundConverter;
|
||||
import com.foxinmy.weixin4j.mp.payment.v3.ApiResult;
|
||||
@ -38,7 +39,6 @@ import com.foxinmy.weixin4j.mp.type.BillType;
|
||||
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.mp.type.IdType;
|
||||
import com.foxinmy.weixin4j.mp.util.ExcelUtil;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.DateUtil;
|
||||
import com.foxinmy.weixin4j.util.RandomUtil;
|
||||
@ -54,8 +54,8 @@ import com.foxinmy.weixin4j.util.RandomUtil;
|
||||
*/
|
||||
public class Pay3Api extends PayApi {
|
||||
|
||||
public Pay3Api(TokenHolder tokenHolder) {
|
||||
super(tokenHolder);
|
||||
public Pay3Api(WeixinMpAccount weixinAccount) {
|
||||
super(weixinAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -16,7 +16,9 @@ import com.foxinmy.weixin4j.mp.payment.v3.ApiResult;
|
||||
import com.foxinmy.weixin4j.mp.type.BillType;
|
||||
import com.foxinmy.weixin4j.mp.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.mp.type.SignType;
|
||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.util.DateUtil;
|
||||
|
||||
/**
|
||||
@ -30,12 +32,13 @@ import com.foxinmy.weixin4j.util.DateUtil;
|
||||
* @see com.foxinmy.weixin4j.mp.api.Pay3Api
|
||||
*/
|
||||
public abstract class PayApi extends MpApi {
|
||||
|
||||
protected final TokenHolder tokenHolder;
|
||||
protected final WeixinMpAccount weixinAccount;
|
||||
|
||||
public PayApi(TokenHolder tokenHolder) {
|
||||
this.tokenHolder = tokenHolder;
|
||||
this.weixinAccount = (WeixinMpAccount) tokenHolder.getAccount();
|
||||
public PayApi(WeixinMpAccount weixinAccount) {
|
||||
this.weixinAccount = weixinAccount;
|
||||
this.tokenHolder = new FileTokenHolder(new WeixinTokenCreator(
|
||||
weixinAccount));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -10,7 +10,6 @@ import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.JsonResult;
|
||||
import com.foxinmy.weixin4j.http.Response;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.model.WeixinAccount;
|
||||
import com.foxinmy.weixin4j.mp.model.Following;
|
||||
import com.foxinmy.weixin4j.mp.model.OauthToken;
|
||||
import com.foxinmy.weixin4j.mp.model.User;
|
||||
@ -39,17 +38,19 @@ public class UserApi extends MpApi {
|
||||
*
|
||||
* @param code
|
||||
* 用户授权后返回的code
|
||||
* @param appid
|
||||
* @param appsecret
|
||||
* @return token对象
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">获取用户token</a>
|
||||
* @see com.foxinmy.weixin4j.mp.model.OauthToken
|
||||
*/
|
||||
public OauthToken getOauthToken(String code) throws WeixinException {
|
||||
WeixinAccount weixinAccount = tokenHolder.getAccount();
|
||||
public OauthToken getOauthToken(String code, String appid, String appsecret)
|
||||
throws WeixinException {
|
||||
String user_token_uri = getRequestUri("sns_user_token_uri");
|
||||
Response response = request.get(String.format(user_token_uri,
|
||||
weixinAccount.getId(), weixinAccount.getSecret(), code));
|
||||
Response response = request.get(String.format(user_token_uri, appid,
|
||||
appsecret, code));
|
||||
|
||||
return response.getAsObject(new TypeReference<OauthToken>() {
|
||||
});
|
||||
|
||||
@ -20,4 +20,4 @@ bill_path=/tmp/weixin/bill
|
||||
# ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84 (V2\u7248\u672c\u540e\u7f00\u4e3a*.pfx,V3\u7248\u672c\u540e\u7f00\u4e3a*.p12)
|
||||
ca_file=/tmp/weixin/xxxxx.p12
|
||||
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
|
||||
ca_file=classpath:xxxxx.pfx
|
||||
# ca_file=classpath:xxxxx.pfx
|
||||
@ -7,6 +7,7 @@ import org.junit.Test;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
|
||||
/**
|
||||
@ -23,7 +24,7 @@ public class TokenTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
tokenHolder = new FileTokenHolder(AccountType.MP);
|
||||
tokenHolder = new FileTokenHolder(new WeixinTokenCreator(AccountType.MP));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@ -16,5 +16,7 @@ qr_path=/tmp/weixin/qr
|
||||
media_path=/tmp/weixin/media
|
||||
# \u5bf9\u8d26\u5355\u4fdd\u5b58\u8def\u5f84
|
||||
bill_path=/tmp/weixin/bill
|
||||
# ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84
|
||||
ca_file=/tmp/weixin/xxxxx.p12 | xxxxx.pfx
|
||||
# ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84 (V2\u7248\u672c\u540e\u7f00\u4e3a*.pfx,V3\u7248\u672c\u540e\u7f00\u4e3a*.p12)
|
||||
ca_file=/tmp/weixin/xxxxx.p12
|
||||
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
|
||||
# ca_file=classpath:xxxxx.pfx
|
||||
@ -15,6 +15,7 @@ import com.foxinmy.weixin4j.qy.model.User;
|
||||
import com.foxinmy.weixin4j.qy.type.UserStatus;
|
||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
|
||||
/**
|
||||
@ -36,7 +37,7 @@ public class WeixinProxy {
|
||||
* 默认采用文件存放Token信息
|
||||
*/
|
||||
public WeixinProxy() {
|
||||
this(new FileTokenHolder(AccountType.QY));
|
||||
this(new FileTokenHolder(new WeixinTokenCreator(AccountType.QY)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +56,7 @@ public class WeixinProxy {
|
||||
* @param weixinAccount
|
||||
*/
|
||||
public WeixinProxy(WeixinQyAccount weixinAccount) {
|
||||
this(new FileTokenHolder(weixinAccount));
|
||||
this(new FileTokenHolder(new WeixinTokenCreator(weixinAccount)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -7,6 +7,7 @@ import org.junit.Test;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.token.FileTokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.type.AccountType;
|
||||
|
||||
/**
|
||||
@ -23,7 +24,8 @@ public class TokenTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
tokenHolder = new FileTokenHolder(AccountType.QY);
|
||||
tokenHolder = new FileTokenHolder(
|
||||
new WeixinTokenCreator(AccountType.QY));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user