Browse Source

优化部分代码

master
Binary Wang 7 years ago
parent
commit
8d6cfce13c
8 changed files with 29 additions and 39 deletions
  1. +11
    -4
      weixin-java-common/src/main/java/me/chanjar/weixin/common/util/BeanUtils.java
  2. +5
    -13
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java
  3. +2
    -8
      weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaMediaServiceImpl.java
  4. +1
    -2
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java
  5. +3
    -8
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceOkHttpImpl.java
  6. +1
    -1
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java
  7. +5
    -2
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/util/SignUtils.java
  8. +1
    -1
      weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java

+ 11
- 4
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/BeanUtils.java View File

@@ -43,15 +43,20 @@ public class BeanUtils {
boolean isAccessible = field.isAccessible(); boolean isAccessible = field.isAccessible();
field.setAccessible(true); field.setAccessible(true);
if (field.isAnnotationPresent(Required.class)) { if (field.isAnnotationPresent(Required.class)) {
if (field.get(bean) == null || (field.get(bean) instanceof String && StringUtils.isBlank(field.get(bean).toString()))) {
//两种情况,一种是值为null,另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
// 两种情况,一种是值为null,
// 另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
boolean isRequiredMissing = field.get(bean) == null
|| (field.get(bean) instanceof String
&& StringUtils.isBlank(field.get(bean).toString())
);
if (isRequiredMissing) {
requiredFields.add(field.getName()); requiredFields.add(field.getName());
} }
} }
field.setAccessible(isAccessible); field.setAccessible(isAccessible);
} catch (SecurityException | IllegalArgumentException } catch (SecurityException | IllegalArgumentException
| IllegalAccessException e) { | IllegalAccessException e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} }
} }


@@ -83,11 +88,13 @@ public class BeanUtils {


if (field.isAnnotationPresent(XStreamAlias.class)) { if (field.isAnnotationPresent(XStreamAlias.class)) {
result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString()); result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString());
} else {
result.put(field.getName(), field.get(bean).toString());
} }


field.setAccessible(isAccessible); field.setAccessible(isAccessible);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} }


} }


+ 5
- 13
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java View File

@@ -7,14 +7,10 @@ import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import okhttp3.*; import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.IOException; import java.io.IOException;


public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> { public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

protected OkHttpClient httpClient; protected OkHttpClient httpClient;
protected OkHttpProxyInfo httpProxy; protected OkHttpProxyInfo httpProxy;


@@ -36,7 +32,7 @@ public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient,


@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
logger.debug("WxCpServiceOkHttpImpl is running");
this.log.debug("WxCpServiceOkHttpImpl is running");
if (this.configStorage.isAccessTokenExpired() || forceRefresh) { if (this.configStorage.isAccessTokenExpired() || forceRefresh) {
synchronized (this.globalAccessTokenRefreshLock) { synchronized (this.globalAccessTokenRefreshLock) {
if (this.configStorage.isAccessTokenExpired()) { if (this.configStorage.isAccessTokenExpired()) {
@@ -47,18 +43,14 @@ public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient,
OkHttpClient client = getRequestHttpClient(); OkHttpClient client = getRequestHttpClient();
//请求的request //请求的request
Request request = new Request.Builder().url(url).get().build(); Request request = new Request.Builder().url(url).get().build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
String resultContent = null; String resultContent = null;
try { try {
Response response = client.newCall(request).execute();
resultContent = response.body().string(); resultContent = response.body().string();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
this.log.error(e.getMessage(), e);
} }

WxError error = WxError.fromJson(resultContent); WxError error = WxError.fromJson(resultContent);
if (error.getErrorCode() != 0) { if (error.getErrorCode() != 0) {
throw new WxErrorException(error); throw new WxErrorException(error);
@@ -74,7 +66,7 @@ public class WxCpServiceOkHttpImpl extends WxCpServiceAbstractImpl<OkHttpClient,


@Override @Override
public void initHttp() { public void initHttp() {
logger.debug("WxCpServiceOkHttpImpl initHttp");
this.log.debug("WxCpServiceOkHttpImpl initHttp");
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
//设置代理 //设置代理
if (httpProxy != null) { if (httpProxy != null) {


+ 2
- 8
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaMediaServiceImpl.java View File

@@ -9,8 +9,6 @@ import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -22,8 +20,6 @@ import java.util.UUID;
* @author <a href="https://github.com/binarywang">Binary Wang</a> * @author <a href="https://github.com/binarywang">Binary Wang</a>
*/ */
public class WxMaMediaServiceImpl implements WxMaMediaService { public class WxMaMediaServiceImpl implements WxMaMediaService {
private final Logger log = LoggerFactory.getLogger(this.getClass());

private WxMaService wxMaService; private WxMaService wxMaService;


public WxMaMediaServiceImpl(WxMaService wxMaService) { public WxMaMediaServiceImpl(WxMaService wxMaService) {
@@ -35,8 +31,7 @@ public class WxMaMediaServiceImpl implements WxMaMediaService {
try { try {
return this.uploadMedia(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); return this.uploadMedia(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
} }
} }


@@ -53,8 +48,7 @@ public class WxMaMediaServiceImpl implements WxMaMediaService {
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("wxma").toFile()); .create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("wxma").toFile());
return this.wxMaService.execute(executor, MEDIA_GET_URL, "media_id=" + mediaId); return this.wxMaService.execute(executor, MEDIA_GET_URL, "media_id=" + mediaId);
} catch (IOException e) { } catch (IOException e) {
this.log.error(e.getMessage(), e);
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
} }
} }




+ 1
- 2
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImpl.java View File

@@ -37,8 +37,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
try { try {
return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build());
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
} }
} }




+ 3
- 8
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceOkHttpImpl.java View File

@@ -8,16 +8,11 @@ import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import okhttp3.*; import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.IOException; import java.io.IOException;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;


public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> { public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private OkHttpClient httpClient; private OkHttpClient httpClient;
private OkHttpProxyInfo httpProxy; private OkHttpProxyInfo httpProxy;


@@ -38,7 +33,7 @@ public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient,


@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
logger.debug("WxMpServiceOkHttpImpl is running");
this.log.debug("WxMpServiceOkHttpImpl is running");
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock(); Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
try { try {
lock.lock(); lock.lock();
@@ -59,7 +54,7 @@ public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient,
accessToken.getExpiresIn()); accessToken.getExpiresIn());
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
this.log.error(e.getMessage(), e);
} finally { } finally {
lock.unlock(); lock.unlock();
} }
@@ -68,7 +63,7 @@ public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient,


@Override @Override
public void initHttp() { public void initHttp() {
logger.debug("WxMpServiceOkHttpImpl initHttp");
this.log.debug("WxMpServiceOkHttpImpl initHttp");
WxMpConfigStorage configStorage = this.getWxMpConfigStorage(); WxMpConfigStorage configStorage = this.getWxMpConfigStorage();


if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) { if (configStorage.getHttpProxyHost() != null && configStorage.getHttpProxyPort() > 0) {


+ 1
- 1
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayBaseRequest.java View File

@@ -190,7 +190,7 @@ public abstract class WxPayBaseRequest {
* 3、生成签名,并设置进去 * 3、生成签名,并设置进去
* </pre> * </pre>
* *
* @param config 支付配置对象,用于读取相应系统配置信息
* @param config 支付配置对象,用于读取相应系统配置信息
* @param isIgnoreSignType 签名时,是否忽略signType * @param isIgnoreSignType 签名时,是否忽略signType
*/ */
public void checkAndSign(WxPayConfig config, boolean isIgnoreSignType) throws WxPayException { public void checkAndSign(WxPayConfig config, boolean isIgnoreSignType) throws WxPayException {


+ 5
- 2
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/util/SignUtils.java View File

@@ -6,6 +6,8 @@ import me.chanjar.weixin.common.util.BeanUtils;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
@@ -23,11 +25,12 @@ import java.util.TreeMap;
* </pre> * </pre>
*/ */
public class SignUtils { public class SignUtils {
private static final Logger log = LoggerFactory.getLogger(SignUtils.class);


/** /**
* 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3) * 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3)
* *
* @param xmlBean Bean需要标记有XML注解
* @param xmlBean Bean里的属性如果存在XML注解,则使用其作为key,否则使用变量名
* @param signType 签名类型,如果为空,则默认为MD5 * @param signType 签名类型,如果为空,则默认为MD5
* @param signKey 签名Key * @param signKey 签名Key
* @param isIgnoreSignType 签名时,是否忽略signType * @param isIgnoreSignType 签名时,是否忽略signType
@@ -81,7 +84,7 @@ public class SignUtils {
byte[] bytes = hmacSHA256.doFinal(message.getBytes()); byte[] bytes = hmacSHA256.doFinal(message.getBytes());
return Hex.encodeHexString(bytes).toUpperCase(); return Hex.encodeHexString(bytes).toUpperCase();
} catch (NoSuchAlgorithmException | InvalidKeyException e) { } catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} }


return null; return null;


+ 1
- 1
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java View File

@@ -67,7 +67,7 @@ public class WxPayServiceAbstractImplTest {
} }


@Test @Test
public void testCreateOrder_jssdk() throws Exception {
public void testCreateOrder_jsapi() throws Exception {
WxPayMpOrderResult result = this.payService WxPayMpOrderResult result = this.payService
.createOrder(WxPayUnifiedOrderRequest.newBuilder() .createOrder(WxPayUnifiedOrderRequest.newBuilder()
.body("我去") .body("我去")


Loading…
Cancel
Save