重构Weixin4jSettings...

This commit is contained in:
jinyu
2016-02-02 19:48:30 +08:00
parent baff97e9db
commit 8a9f93e54e
39 changed files with 636 additions and 1020 deletions
-75
View File
@@ -36,81 +36,6 @@ weixin4j-qy
如何使用
--------
0.maven依赖(1.6.6,2015-12-31 released)
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-qy</artifactId>
<version>1.6.6</version>
</dependency>
1.需新增或拷贝`weixin4j.properties`文件到项目的`classpath`
weixin4j.properties说明
| 属性名 | 说明 |
| :---------- | :-------------- |
| weixin4j.account | 微信企业号信息 `json格式`(使用new WeixinProxy()缺省构造器时须填写) |
| weixin4j.token.path | 使用FileTokenStorager时token保存的物理路径(非必须填写) |
| weixin4j.media.path | 调用媒体接口时保存媒体文件的物理路径(非必须填写) |
| weixin4j.bill.path | 调用下载对账单接口保存文件的物理路径(非必须填写) |
| weixin4j.certificate.file | 调用某些接口(支付相关)强制需要auth的ca授权文件(非必须填写) |
| weixin4j.user.oauth.redirect.uri | 企业号用户身份授权后重定向的url(OauthApi接口) |
| weixin4j.third.oauth.redirect.uri | 企业号第三方提供商授权后重定向的url(OauthApi接口) |
| weixin4j.suite.oauth.redirect.uri | 企业号第三方应用套件授权后重定向的url(OauthApi接口) |
示例(properties中换行用右斜杆\\)
weixin4j.account={"id":"corpid","secret":"corpsecret",\
"suites":[{"id":"应用套件的id","secret":"应用套件的secret"}],\
"providerSecret":"第三方提供商secret(企业号登陆)",\
"chatSecret":"消息服务secret(企业号消息服务,暂时没用到)",\
"mchId":"微信商户号 微信支付时需要填入",\
"certificateKey":"加载支付证书文件的密码 如果不填写则默认获取mchId作为密码",\
"paySignKey":"微信支付中调用API的密钥 微信支付时需要填入"}
# 使用FileTokenStorager时token的存放路径(如果不填则默认为Weixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.token.path=/tmp/weixin4j/token
# 二维码保存路径(如果不填则默认为Weixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.qrcode.path=/tmp/weixin4j/qrcode
# 媒体文件保存路径(如果不填则默认为Weixin4jConst#DEFAULT_MEDIA_PATH)
weixin4j.media.path=/tmp/weixin4j/media
# 对账单保存路径(如果不填则默认为Weixin4jConst#DEFAULT_BILL_PATH)
weixin4j.bill.path=/tmp/weixin4j/bill
# ca证书存放的完整路径
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
# classpath路径下可以这么写(如果不填则默认为Weixin4jConst#DEFAULT_CAFILE_PATH)
# weixin4j.certificate.file=classpath:xxxxx.pfx
# 企业号用户身份授权后重定向的url(在使用OauthApi时填写)
weixin4j.user.oauth.redirect.uri=
# 企业号第三方提供商授权后重定向的url(在使用OauthApi时填写)
weixin4j.third.oauth.redirect.uri=
# 企业号第三方应用套件授权后重定向的url(在使用OauthApi时填写)
weixin4j.suite.oauth.redirect.uri=
2.实例化微信企业号接口代理对象,调用具体的API方法
// 微信企业号API 使用classpath的weixin4j.properties
WeixinProxy weixinProxy = new WeixinProxy();
// 直接传入企业号信息
// weixinProxy = new WeixinProxy(corpid,corpsecret);
weixinProxy.getUser(userid);
// 微信支付API 使用classpath的weixin4j.properties
WeixinPayProxy weixinPayProxy = new WeixinPayProxy();
// 直接构造WexinAccount对象
// weixinPayProxy = new WeixinPayProxy(weixinAccount);
weixinPayProxy.orderQuery(idQuery);
// 微信第三方应用API 使用classpath的weixin4j.properties
WeixinSuiteProxy weixinSuiteProxy = new WeixinSuiteProxy();
// 直接传入套件信息
//weixinSuiteProxy = new WeixinSuiteProxy(suiteId,suiteSecret);
weixinSuiteProxy.api().getOAuthInfo(authCorpid);
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenStorager`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用[RedisTokenStorager](../weixin4j-base/src/main/java/com/foxinmy/weixin4j/token/RedisTokenStorager.java).
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenStorager());
> // weixinProxy = new WeixinProxy(new RedisTokenStorager(corpid,corpsecret));
[更新LOG](./CHANGE.md)
----------------------
@@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.qy;
import java.io.File;
import java.io.InputStream;
import java.util.List;
@@ -48,11 +47,11 @@ import com.foxinmy.weixin4j.qy.type.ChatType;
import com.foxinmy.weixin4j.qy.type.InviteType;
import com.foxinmy.weixin4j.qy.type.KfType;
import com.foxinmy.weixin4j.qy.type.UserStatus;
import com.foxinmy.weixin4j.settings.Weixin4jSettings;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.tuple.MpArticle;
import com.foxinmy.weixin4j.type.MediaType;
import com.foxinmy.weixin4j.type.TicketType;
import com.foxinmy.weixin4j.util.Weixin4jSettings;
/**
* 微信企业号接口实现
@@ -90,13 +89,13 @@ public class WeixinProxy {
/**
*
* @param settings
* 配置信息
* @see com.foxinmy.weixin4j.settings.Weixin4jSettings
* 微信配置信息
* @see com.foxinmy.weixin4j.util.Weixin4jSettings
*/
public WeixinProxy(Weixin4jSettings settings) {
this(new TokenHolder(new WeixinTokenCreator(settings.getAccount()
.getId(), settings.getAccount().getSecret()),
settings.getTokenStorager()));
this(new TokenHolder(new WeixinTokenCreator(settings.getWeixinAccount()
.getId(), settings.getWeixinAccount().getSecret()),
settings.getTokenStorager0()));
this.settings = settings;
}
@@ -116,8 +115,8 @@ public class WeixinProxy {
TokenHolder suiteTokenHolder) {
this(new TokenHolder(new WeixinTokenSuiteCreator(perCodeHolder,
suiteTokenHolder), suiteTokenHolder.getTokenStorager()));
this.settings = new Weixin4jSettings(perCodeHolder.getAuthCorpId(),
null);
this.settings = new Weixin4jSettings(new WeixinAccount(
perCodeHolder.getAuthCorpId(), null));
}
/**
@@ -156,7 +155,7 @@ public class WeixinProxy {
* @return
*/
public WeixinAccount getWeixinAccount() {
return this.settings.getAccount();
return this.settings.getWeixinAccount();
}
/**
@@ -345,25 +344,6 @@ public class WeixinProxy {
return mediaApi.uploadMedia(agentid, is, fileName);
}
/**
* 下载媒体文件
*
* @param agentid
* 企业应用Id(<font color="red">大于0时视为获取永久媒体文件</font>)
* @param mediaId
* 存储在微信服务器上的媒体标识
* @return 写入硬盘后的文件对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.qy.api.MediaApi
* @see com.foxinmy.weixin4j.type.MediaType
* @see {@link #downloadMedia(int,String)}
*/
public File downloadMediaFile(int agentid, String mediaId)
throws WeixinException {
return mediaApi.downloadMediaFile(agentid, mediaId,
settings.getMediaPath());
}
/**
* 下载媒体文件
*
@@ -3,7 +3,6 @@ package com.foxinmy.weixin4j.qy;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.qy.api.ProviderApi;
@@ -11,14 +10,11 @@ import com.foxinmy.weixin4j.qy.api.SuiteApi;
import com.foxinmy.weixin4j.qy.model.OUserInfo;
import com.foxinmy.weixin4j.qy.model.WeixinQyAccount;
import com.foxinmy.weixin4j.qy.suite.SuiteTicketHolder;
import com.foxinmy.weixin4j.qy.suite.Weixin4jSuiteSettings;
import com.foxinmy.weixin4j.qy.token.WeixinProviderTokenCreator;
import com.foxinmy.weixin4j.qy.type.LoginTargetType;
import com.foxinmy.weixin4j.settings.Weixin4jSettings;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.token.TokenStorager;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 微信第三方应用接口实现
@@ -36,72 +32,52 @@ public class WeixinSuiteProxy {
private Map<String, SuiteApi> suiteMap;
private ProviderApi providerApi;
private final Weixin4jSuiteSettings suiteSettings;
public WeixinSuiteProxy() {
this(Weixin4jSettings.DEFAULT_TOKEN_PATH);
this(new Weixin4jSuiteSettings());
}
/**
*
* @param tokenPath
* 使用文件存储token的保存路径
* @param suiteSettings
* 套件信息配置
*/
public WeixinSuiteProxy(String tokenPath) {
this(new FileTokenStorager(tokenPath));
}
/**
*
* @param tokenStorager
* token存储
*/
public WeixinSuiteProxy(TokenStorager tokenStorager) {
this(tokenStorager, JSON.parseObject(
Weixin4jConfigUtil.getValue("account"), WeixinQyAccount.class));
}
/**
*
* @param tokenStorager
* token存储
* @param suites
* 套件信息 必填项
*/
public WeixinSuiteProxy(TokenStorager tokenStorager,
WeixinQyAccount weixinAccount) {
this(tokenStorager, weixinAccount.getId(), weixinAccount
.getProviderSecret(), weixinAccount.suitesToArray());
}
/**
*
* @param tokenStorager
* token存储
* @param providerCorpId
* 服务商的企业号ID <font color="red">使用服务商API时必填项</font>
* @param providerSecret
* 服务商secret <font color="red">使用服务商API时必填项</font>
* @param suites
* 套件信息 <font color="red">使用套件API时必填项</font>
*/
public WeixinSuiteProxy(TokenStorager tokenStorager, String providerCorpId,
String providerSecret, WeixinAccount... suites) {
if (suites != null) {
public WeixinSuiteProxy(Weixin4jSuiteSettings suiteSettings) {
this.suiteSettings = suiteSettings;
if (suiteSettings.getWeixinAccount().getSuiteAccounts() != null) {
this.suiteMap = new HashMap<String, SuiteApi>();
for (WeixinAccount suite : suites) {
for (WeixinAccount suite : suiteSettings.getWeixinAccount()
.getSuiteAccounts()) {
this.suiteMap.put(suite.getId(), new SuiteApi(
new SuiteTicketHolder(suite.getId(), suite.getSecret(),
tokenStorager)));
this.suiteMap.put(null, suiteMap.get(suites[0].getId()));
suiteSettings.getTokenStorager0())));
this.suiteMap.put(
null,
suiteMap.get(suiteSettings.getWeixinAccount()
.getSuiteAccounts().get(0).getId()));
}
}
if (StringUtil.isNotBlank(providerCorpId)
&& StringUtil.isNotBlank(providerSecret)) {
if (StringUtil.isNotBlank(suiteSettings.getWeixinAccount().getId())
&& StringUtil.isNotBlank(suiteSettings.getWeixinAccount()
.getProviderSecret())) {
this.providerApi = new ProviderApi(new TokenHolder(
new WeixinProviderTokenCreator(providerCorpId,
providerSecret), tokenStorager));
new WeixinProviderTokenCreator(suiteSettings
.getWeixinAccount().getId(), suiteSettings
.getWeixinAccount().getProviderSecret()),
suiteSettings.getTokenStorager0()));
}
}
/**
* 企业号信息
*
* @return
*/
public WeixinQyAccount getWeixinAccount() {
return this.suiteSettings.getWeixinAccount();
}
/**
* 只关注第一个套件获取API(如果只有一个套件
*
@@ -1,12 +1,8 @@
package com.foxinmy.weixin4j.qy.api;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
@@ -190,57 +186,6 @@ public class MediaApi extends QyApi {
}
}
/**
* 下载媒体文件
*
* @param agentid
* 企业应用Id(<font color="red">大于0时视为获取永久媒体文件</font>)
* @param mediaId
* 存储在微信服务器上的媒体标识
* @param mediaPath
* 媒体素材保存路径
* @return 写入硬盘后的文件对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.type.MediaType
* @see {@link #downloadMedia(int,String)}
*/
public File downloadMediaFile(int agentid, String mediaId, String mediaPath)
throws WeixinException {
final String prefixName = String.format("%d_%s.", agentid, mediaId);
File[] files = new File(mediaPath).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(prefixName);
}
});
if (files.length > 0) {
return files[0];
}
MediaDownloadResult result = downloadMedia(agentid, mediaId);
File file = new File(mediaPath + File.separator + result.getFileName());
OutputStream os = null;
try {
if (file.createNewFile()) {
os = new FileOutputStream(file);
os.write(result.getContent());
} else {
throw new WeixinException(String.format("create file fail:%s",
file.getAbsolutePath()));
}
} catch (IOException e) {
throw new WeixinException(e);
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
;
}
}
return file;
}
/**
* 下载媒体文件
*
@@ -23,7 +23,7 @@ public class WeixinQyAccount extends WeixinAccount {
/**
* 多个应用套件信息
*/
private List<WeixinAccount> suites;
private List<WeixinAccount> suiteAccounts;
/**
* 第三方提供商secret(企业号登陆)
*/
@@ -36,32 +36,30 @@ public class WeixinQyAccount extends WeixinAccount {
/**
*
* @param corpid
* 企业ID
* 企业ID 必填
* @param corpsecret
* 管理组的凭证密钥
* @param suiteId
* 应用套件的id
* @param suiteSecret
* 应用套件的secret
* 管理组的凭证密钥 使用普通接口(WeixinProxy对象)必须填写
* @param suites
* 应用套件集合 使用套件接口(WeixinSuiteProxy#SuiteApi)必须填写
* @param providerSecret
* 第三方提供商secret(企业号登陆)
* 第三方提供商secret(企业号登陆) 使用服务商接口(WeixinSuiteProxy#ProviderApi)必填项
* @param chatSecret
* 消息服务secret(企业号聊天)
* 消息服务secret(企业号聊天) 暂无用途
*/
@JSONCreator
public WeixinQyAccount(@JSONField(name = "id") String corpid,
@JSONField(name = "secret") String corpsecret,
@JSONField(name = "suites") List<WeixinAccount> suites,
@JSONField(name = "suites") List<WeixinAccount> suiteAccounts,
@JSONField(name = "providerSecret") String providerSecret,
@JSONField(name = "chatSecret") String chatSecret) {
super(corpid, corpsecret);
this.suites = suites;
this.suiteAccounts = suiteAccounts;
this.providerSecret = providerSecret;
this.chatSecret = chatSecret;
}
public List<WeixinAccount> getSuites() {
return suites;
public List<WeixinAccount> getSuiteAccounts() {
return suiteAccounts;
}
public String getProviderSecret() {
@@ -72,15 +70,15 @@ public class WeixinQyAccount extends WeixinAccount {
return chatSecret;
}
public WeixinAccount[] suitesToArray() {
return suites != null ? suites
.toArray(new WeixinAccount[suites.size()]) : null;
public WeixinAccount[] suiteAccountsToArray() {
return suiteAccounts != null ? suiteAccounts
.toArray(new WeixinAccount[suiteAccounts.size()]) : null;
}
@Override
public String toString() {
return "WeixinQyAccount [" + super.toString() + ", suites=" + suites
+ ", providerSecret=" + providerSecret + ", chatSecret="
+ chatSecret + "]";
return "WeixinQyAccount [" + super.toString() + ", suiteAccounts="
+ suiteAccounts + ", providerSecret=" + providerSecret
+ ", chatSecret=" + chatSecret + "]";
}
}
@@ -0,0 +1,124 @@
package com.foxinmy.weixin4j.qy.suite;
import java.util.Arrays;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.qy.model.WeixinQyAccount;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenStorager;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 微信第三方套件配置相关
*
* @className Weixin4jSuiteSettings
* @author jy
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jSuiteSettings {
/**
* 微信企业号信息
*/
private final WeixinQyAccount weixinAccount;
/**
* Http参数
*/
private HttpParams httpParams;
/**
* token存储方式 默认为FileTokenStorager
*/
private TokenStorager tokenStorager;
/**
* 系统临时目录
*/
private String tmpdir;
/**
* 默认使用weixin4j.properties配置的信息
*/
public Weixin4jSuiteSettings() {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinQyAccount.class));
}
/**
*
* @param providerCorpId
* 服务商的企业号ID <font color="red">使用服务商API时必填项</font>
* @param providerSecret
* 服务商secret <font color="red">使用服务商API时必填项</font>
* @param suites
* 套件信息 <font color="red">使用套件API时必填项</font>
*/
public Weixin4jSuiteSettings(String providerCorpId, String providerSecret,
WeixinAccount... suites) {
this.weixinAccount = new WeixinQyAccount(providerCorpId, null,
Arrays.asList(suites), providerSecret, null);
}
private Weixin4jSuiteSettings(WeixinQyAccount weixinAccount) {
this.weixinAccount = weixinAccount;
}
public WeixinQyAccount getWeixinAccount() {
return weixinAccount;
}
public HttpParams getHttpParams() {
return httpParams;
}
public HttpParams getHttpParams0() {
if (httpParams == null) {
return new HttpParams();
}
return httpParams;
}
public String getTmpdir() {
return tmpdir;
}
public String getTmpdir0() {
if (StringUtil.isBlank(tmpdir)) {
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
System.getProperty("java.io.tmpdir"));
}
return tmpdir;
}
public TokenStorager getTokenStorager() {
return tokenStorager;
}
public TokenStorager getTokenStorager0() {
if (tokenStorager == null) {
return new FileTokenStorager(getTmpdir0());
}
return tokenStorager;
}
public void setHttpParams(HttpParams httpParams) {
this.httpParams = httpParams;
}
public void setTmpdir(String tmpdir) {
this.tmpdir = tmpdir;
}
public void setTokenStorager(TokenStorager tokenStorager) {
this.tokenStorager = tokenStorager;
}
@Override
public String toString() {
return "Weixin4jSuiteSettings [weixinAccount=" + weixinAccount
+ ", httpParams=" + httpParams + ",tokenStorager="
+ tokenStorager + ", tmpdir=" + tmpdir + "]";
}
}
@@ -42,7 +42,7 @@ public class WeixinSuitePreCodeCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_qy_suite_precode_%s", suiteId);
return String.format("weixin4j_qy_suite_precode_%s", suiteId);
}
@Override
@@ -36,7 +36,7 @@ public class WeixinSuiteTokenCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_qy_suite_token_%s", ticketHolder.getSuiteId());
return String.format("weixin4j_qy_suite_token_%s", ticketHolder.getSuiteId());
}
@Override
@@ -42,7 +42,7 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_qy_token_suite_%s:%s",
return String.format("weixin4j_qy_token_suite_%s:%s",
perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId()
);
@@ -40,7 +40,7 @@ public class WeixinProviderTokenCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_qy_provider_token_%s", corpid);
return String.format("weixin4j_qy_provider_token_%s", corpid);
}
@Override
@@ -45,7 +45,7 @@ public class WeixinTicketCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_qy_ticket_%s_%s", corpid, ticketType.name());
return String.format("weixin4j_qy_ticket_%s_%s", corpid, ticketType.name());
}
@Override
@@ -40,7 +40,7 @@ public class WeixinTokenCreator implements TokenCreator {
@Override
public String getCacheKey() {
return String.format("wx_qy_token_%s", corpid);
return String.format("weixin4j_qy_token_%s", corpid);
}
@Override
@@ -1,5 +1,6 @@
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
# \u4f01\u4e1a\u53f7\u4fe1\u606f
# \u4f01\u4e1a\u53f7\u4fe1\u606f \u8bf7\u6309\u9700\u586b\u5199
weixin4j.account={"id":"wx5132afc5da26d661","secret":"GsnKLVDI1pWArdB60Ze4iP2cwFvcW5KCAs2vLJldipilmSYxtbkcAiBcGSHHvu_I",\
"suites":[{"id":"\u5e94\u7528\u5957\u4ef6\u7684id","secret":"\u5e94\u7528\u5957\u4ef6\u7684secret"}],\
"providerSecret":"\u7b2c\u4e09\u65b9\u63d0\u4f9b\u5546secret(\u4f01\u4e1a\u53f7\u767b\u9646)",\
@@ -8,16 +9,15 @@ weixin4j.account={"id":"wx5132afc5da26d661","secret":"GsnKLVDI1pWArdB60Ze4iP2cwF
"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",\
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
# \u4f7f\u7528FileTokenStorager\u65f6token\u7684\u5b58\u653e\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_TOKEN_PATH)
weixin4j.token.path=/tmp/weixin4j/token
# \u5a92\u4f53\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_MEDIA_PATH)
weixin4j.media.path=/tmp/weixin4j/media
# \u5bf9\u8d26\u5355\u4fdd\u5b58\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_BILL_PATH)
weixin4j.bill.path=/tmp/weixin4j/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)
# 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
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_CAFILE_PATH)
# weixin4j.certificate.file=classpath:xxxxx.pfx
# \u4f01\u4e1a\u53f7\u7528\u6237\u8eab\u4efd\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
weixin4j.user.oauth.redirect.uri=
@@ -44,12 +44,11 @@ public class MediaTest extends TokenTest {
@Test
public void download() throws WeixinException, IOException {
File file = mediaApi
.downloadMediaFile(
MediaDownloadResult result = mediaApi
.downloadMedia(
0,
"1y0NWE5ochkfOoiyJsPwQ3Wg7gsyRHNp8SveqhGXY_1rOH7OcOMwfHDg8KH6s88osq59AfS3BX-MBBKvERB7Bvw",
"/tmp/weixin4j/media");
Assert.assertTrue(file.exists());
"1y0NWE5ochkfOoiyJsPwQ3Wg7gsyRHNp8SveqhGXY_1rOH7OcOMwfHDg8KH6s88osq59AfS3BX-MBBKvERB7Bvw");
Assert.assertTrue(result.getContent().length > 0);
}
@Test
@@ -1,39 +1,39 @@
package com.foxinmy.weixin4j.qy.test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* token测试
*
* @className TokenTest
* @author jy.hu
* @date 2014年4月10日
* @since JDK 1.6
*/
public class TokenTest {
protected TokenHolder tokenHolder;
@Before
public void setUp() {
WeixinAccount weixinAccount = Weixin4jConfigUtil.getWeixinAccount();
tokenHolder = new TokenHolder(new WeixinTokenCreator(
weixinAccount.getId(), weixinAccount.getSecret()),
new FileTokenStorager(Weixin4jConfigUtil.getValue("token.path",
"/tmp/weixin4j/token")));
}
@Test
public void test() throws WeixinException {
Assert.assertNotNull(tokenHolder.getToken());
}
}
package com.foxinmy.weixin4j.qy.test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.qy.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* token测试
*
* @className TokenTest
* @author jy.hu
* @date 2014年4月10日
* @since JDK 1.6
*/
public class TokenTest {
protected TokenHolder tokenHolder;
@Before
public void setUp() {
WeixinAccount weixinAccount = Weixin4jConfigUtil.getWeixinAccount();
tokenHolder = new TokenHolder(new WeixinTokenCreator(
weixinAccount.getId(), weixinAccount.getSecret()),
new FileTokenStorager(Weixin4jConfigUtil.getValue("token.path",
"/tmp/weixin4j/token")));
}
@Test
public void test() throws WeixinException {
Assert.assertNotNull(tokenHolder.getToken());
}
}