重构Weixin4jSettings...
This commit is contained in:
@@ -47,7 +47,6 @@ 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.settings.Weixin4jSettings;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.tuple.MassTuple;
|
||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||
@@ -55,6 +54,7 @@ import com.foxinmy.weixin4j.tuple.MpVideo;
|
||||
import com.foxinmy.weixin4j.tuple.Tuple;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
import com.foxinmy.weixin4j.type.TicketType;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jSettings;
|
||||
|
||||
/**
|
||||
* 微信公众平台接口实现
|
||||
@@ -92,13 +92,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;
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ public class WeixinProxy {
|
||||
* @return
|
||||
*/
|
||||
public WeixinAccount getWeixinAccount() {
|
||||
return this.settings.getAccount();
|
||||
return this.settings.getWeixinAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,29 +222,6 @@ public class WeixinProxy {
|
||||
return mediaApi.uploadMedia(isMaterial, is, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载媒体文件
|
||||
* <p>
|
||||
* 正常情况下返回表头如Content-Type: image/jpeg,否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @param mediaId
|
||||
* 存储在微信服务器上的媒体标识
|
||||
* @param isMaterial
|
||||
* 是否下载永久素材
|
||||
* @return 写入硬盘后的文件对象
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see {@link #downloadMedia(String)}
|
||||
*/
|
||||
public File downloadMediaFile(String mediaId, boolean isMaterial)
|
||||
throws WeixinException {
|
||||
return mediaApi.downloadMediaFile(mediaId, isMaterial,
|
||||
settings.getMediaPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载媒体文件
|
||||
*
|
||||
@@ -1192,19 +1169,6 @@ public class WeixinProxy {
|
||||
public QRResult createQR(QRParameter parameter) throws WeixinException {
|
||||
return qrApi.createQR(parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成带参数的二维码
|
||||
*
|
||||
* @return 硬盘存储的文件对象
|
||||
* @param parameter
|
||||
* 二维码参数
|
||||
* @throws WeixinException
|
||||
* @see {@link #createQR(QRParameter)}
|
||||
*/
|
||||
public File createQRFile(QRParameter parameter) throws WeixinException {
|
||||
return qrApi.createQRFile(parameter, settings.getQrcodePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所属行业(每月可修改行业1次,账号仅可使用所属行业中相关的模板)
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
package com.foxinmy.weixin4j.mp.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.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
@@ -228,63 +224,6 @@ public class MediaApi extends MpApi {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载媒体素材
|
||||
* <p>
|
||||
* 正常情况下返回表头如Content-Type: image/jpeg,否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @param mediaId
|
||||
* 存储在微信服务器上的媒体标识
|
||||
* @param isMaterial
|
||||
* 是否下载永久素材
|
||||
* @param mediaPath
|
||||
* 媒体素材保存路径
|
||||
* @return 写入硬盘后的文件对象
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html">下载临时媒体文件</a>
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
|
||||
* @see {@link #downloadMedia(String,boolean)}
|
||||
*/
|
||||
public File downloadMediaFile(String mediaId, boolean isMaterial,
|
||||
String mediaPath) throws WeixinException {
|
||||
final String prefixName = String.format("%s.", 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(mediaId, isMaterial);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载媒体素材
|
||||
*
|
||||
|
||||
@@ -40,11 +40,7 @@ import com.foxinmy.weixin4j.mp.payment.v2.RefundRecordV2;
|
||||
import com.foxinmy.weixin4j.mp.payment.v2.RefundResultV2;
|
||||
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.payment.PayRequest;
|
||||
import com.foxinmy.weixin4j.settings.Weixin4jSettings;
|
||||
import com.foxinmy.weixin4j.settings.Weixin4jPaySettings;
|
||||
import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.token.TokenStorager;
|
||||
import com.foxinmy.weixin4j.type.BillType;
|
||||
import com.foxinmy.weixin4j.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.type.RefundType;
|
||||
@@ -54,6 +50,7 @@ import com.foxinmy.weixin4j.util.DigestUtil;
|
||||
import com.foxinmy.weixin4j.util.MapUtil;
|
||||
import com.foxinmy.weixin4j.util.RandomUtil;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jSettings;
|
||||
import com.foxinmy.weixin4j.xml.ListsuffixResultDeserializer;
|
||||
|
||||
/**
|
||||
@@ -67,27 +64,22 @@ import com.foxinmy.weixin4j.xml.ListsuffixResultDeserializer;
|
||||
*/
|
||||
public class Pay2Api extends MpApi {
|
||||
|
||||
private final Weixin4jPaySettings settings;
|
||||
private final Weixin4jSettings settings;
|
||||
private final TokenHolder tokenHolder;
|
||||
|
||||
public Pay2Api() {
|
||||
this(new Weixin4jPaySettings());
|
||||
this(new Weixin4jSettings());
|
||||
}
|
||||
|
||||
public Pay2Api(Weixin4jPaySettings settings) {
|
||||
this(settings, new FileTokenStorager(
|
||||
Weixin4jSettings.DEFAULT_TOKEN_PATH));
|
||||
}
|
||||
|
||||
public Pay2Api(Weixin4jPaySettings settings, TokenStorager tokenStorager) {
|
||||
public Pay2Api(Weixin4jSettings settings) {
|
||||
this.tokenHolder = new TokenHolder(new WeixinTokenCreator(settings
|
||||
.getWeixinAccount().getId(), settings.getWeixinAccount()
|
||||
.getSecret()), settings.getTokenStorager0());
|
||||
this.settings = settings;
|
||||
this.tokenHolder = new TokenHolder(
|
||||
new WeixinTokenCreator(settings.getPayAccount().getId(),
|
||||
settings.getPayAccount().getSecret()), tokenStorager);
|
||||
}
|
||||
|
||||
public WeixinPayAccount getPayAccount() {
|
||||
return this.settings.getPayAccount();
|
||||
return this.settings.getWeixinPayAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,7 +134,7 @@ public class Pay2Api extends MpApi {
|
||||
double totalFee, String notifyUrl, String createIp, String attach,
|
||||
Date timeStart, Date timeExpire, double transportFee,
|
||||
double productFee, String goodsTag) {
|
||||
PayPackageV2 payPackage = new PayPackageV2(settings.getPayAccount()
|
||||
PayPackageV2 payPackage = new PayPackageV2(getPayAccount()
|
||||
.getPartnerId(), body, outTradeNo, totalFee, notifyUrl,
|
||||
createIp);
|
||||
payPackage.setAttach(attach);
|
||||
@@ -458,8 +450,8 @@ public class Pay2Api extends MpApi {
|
||||
String formatBillDate = DateUtil.fortmat2yyyyMMdd(billDate);
|
||||
String fileName = String.format("%s_%s_%s.txt", formatBillDate,
|
||||
billType.name().toLowerCase(), getPayAccount().getId());
|
||||
File file = new File(String.format("%s/%s", settings.getBillPath(),
|
||||
fileName));
|
||||
File file = new File(String.format("%s/weixin4j_bill_%s",
|
||||
settings.getTmpdir0(), fileName));
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.foxinmy.weixin4j.mp.api;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
@@ -62,49 +59,4 @@ public class QrApi extends MpApi {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成带参数的二维码
|
||||
* <p>
|
||||
* 二维码分为临时跟永久两种,扫描时触发推送带参数事件
|
||||
* </p>
|
||||
*
|
||||
* @param parameter
|
||||
* 二维码参数
|
||||
* @param qrcodePath
|
||||
* 二维码保存路径
|
||||
* @return 硬盘存储的文件对象
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html">二维码</a>
|
||||
* @see #createQR(QRParameter)
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRParameter
|
||||
*/
|
||||
public File createQRFile(QRParameter parameter, String qrcodePath)
|
||||
throws WeixinException {
|
||||
String filename = String.format("%s_%s_%d.jpg", parameter.getQrType()
|
||||
.name(), parameter.getSceneValue(), parameter
|
||||
.getExpireSeconds());
|
||||
File file = new File(qrcodePath + File.separator + filename);
|
||||
if (parameter.getQrType().ordinal() > 0 && file.exists()) {
|
||||
return file;
|
||||
}
|
||||
QRResult qrResult = createQR(parameter);
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = new FileOutputStream(file);
|
||||
os.write(qrResult.getContent());
|
||||
} catch (IOException e) {
|
||||
throw new WeixinException(e);
|
||||
} finally {
|
||||
try {
|
||||
if (os != null) {
|
||||
os.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
NotifyMessage: (客服消息)[http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html#.E5.AE.A2.E6.9C.8D.E6.8E.A5.E5.8F.A3-.E5.8F.91.E6.B6.88.E6.81.AF]
|
||||
|
||||
TemplateMessage: (模板消息)[http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html]
|
||||
@@ -48,7 +48,7 @@ public class WeixinTicketCreator implements TokenCreator {
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("wx_mp_ticket_%s_%s", appid, ticketType.name());
|
||||
return String.format("weixin4j_mp_ticket_%s_%s", appid, ticketType.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -40,7 +40,7 @@ public class WeixinTokenCreator implements TokenCreator {
|
||||
|
||||
@Override
|
||||
public String getCacheKey() {
|
||||
return String.format("wx_mp_token_%s", appid);
|
||||
return String.format("weixin4j_mp_token_%s", appid);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
|
||||
# \u516c\u4f17\u53f7\u4fe1\u606f
|
||||
|
||||
# \u516c\u4f17\u53f7\u4fe1\u606f \u8bf7\u6309\u9700\u586b\u5199
|
||||
weixin4j.account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
|
||||
"mchId":"V3.x\u7248\u672c\u4e0b\u7684\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"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",\
|
||||
"partnerId":"V2\u7248\u672c\u4e0b\u7684\u8d22\u4ed8\u901a\u7684\u5546\u6237\u53f7 \u8001\u7248\u672c\u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"partnerKey":"V2\u7248\u672c\u4e0b\u7684\u8d22\u4ed8\u901a\u5546\u6237\u6743\u9650\u5bc6\u94a5Key \u8001\u7248\u672c\u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"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
|
||||
# \u4e8c\u7ef4\u7801\u4fdd\u5b58\u8def\u5f84(\u5982\u679c\u4e0d\u586b\u5219\u9ed8\u8ba4\u4e3aWeixin4jConst#DEFAULT_TOKEN_PATH)
|
||||
weixin4j.qrcode.path=/tmp/weixin4j/qrcode
|
||||
# \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
|
||||
|
||||
# \u7528\u6237oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
|
||||
weixin4j.user.oauth.redirect.uri=
|
||||
Reference in New Issue
Block a user