Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Wechat-Group/weixin-java-tools i…
Browse files Browse the repository at this point in the history
…nto develop

* 'develop' of github.com:Wechat-Group/weixin-java-tools: (132 commits)
  发布临时测试版本2.8.1.BETA
  修正一个错误的类属性命名
  修复单元测试
  清理代码
  pay模块显式指定commons-lang3,以便用户项目可能会优先使用所依赖设置的版本
  清理代码,避免过度依赖容易引起冲突的类库代码
  精简代码
  Fix issue binarywang#335: 重构WxMpInRedisConfigStorage,改用连接池
  binarywang#136 修复WxMenuRule反序列化的问题tag_id为空的问题
  Update readme.md
  Update readme.md
  修改版本号为正式版本2.8.0
  binarywang#320 增加“拉取订单评价数据“接口方法
  binarywang#319 增加“退款结果通知“处理方法,并优化调整微信支付相关代码
  binarywang#322 为me.chanjar.weixin.mp.bean下的bean类实现Serializable接口,并增加serialVersionUID
  binarywang#324: 修复分布式刷新access_token冲突问题
  binarywang#324: 修复分布式刷新access_token冲突问题
  微信支付增加获取微信的请求和响应数据的方法getWxApiData(),方便使用者获取使用该数据
  微信支付优化部分代码,方便扩展
  微信支付接口抽取部分常量
  ...
  • Loading branch information
comeonc committed Sep 19, 2017
2 parents fd508b2 + 7b20300 commit 0b6ab42
Show file tree
Hide file tree
Showing 192 changed files with 3,637 additions and 1,224 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.7.6.BETA</version>
<version>2.8.1.BETA</version>
<packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description>
Expand Down Expand Up @@ -152,7 +152,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -288,7 +288,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.7-SNAPSHOT</version>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
30 changes: 14 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@

#### 声明: ***本项目Fork自chanjarster/weixin-java-tools,但由于原项目已停止维护,故单独维护和发布,且发布到maven上的groupId也会不同,详细信息见下文。***

***新人提示:本项目仅是一个开发工具包(即SDK),未提供Web实现,建议使用maven或gradle引用本项目即可使用本SDK提供的各种功能,详情可参考下文中提到的Demo项目或本项目中的部分单元测试代码;另外微信开发新手请务必阅读wiki首页的常见问题部分,可以少走很多弯路,节省不少时间。***
***新人提示:本项目仅是一个开发工具包(即SDK),未提供Web实现,建议使用maven或gradle引用本项目即可使用本SDK提供的各种功能,详情可参考下文中提到的Demo项目或本项目中的部分单元测试代码;另外微信开发新手请务必阅读[Wiki首页](https://github.com/Wechat-Group/weixin-java-tools/wiki)的常见问题部分,可以少走很多弯路,节省不少时间。***

## Demo项目列表
* https://github.com/wechat-group/weixin-java-miniapp-demo (微信小程序Demo)
* https://github.com/wechat-group/weixin-java-pay-demo (微信支付Demo)
* https://github.com/wechat-group/weixin-java-cp-demo (企业号Demo)
* https://github.com/wechat-group/weixin-java-mp-demo (公众号Demo,使用Spring MVC实现)
* https://github.com/wechat-group/weixin-java-mp-demo-springboot (公众号Demo,使用Spring Boot实现)
* https://github.com/binarywang/weixin-java-miniapp-demo (微信小程序Demo)
* https://github.com/binarywang/weixin-java-pay-demo (微信支付Demo)
* https://github.com/binarywang/weixin-java-cp-demo (企业号/企业微信Demo)
* https://github.com/binarywang/weixin-java-mp-demo (公众号Demo,使用Spring MVC实现)
* https://github.com/binarywang/weixin-java-mp-demo-springboot (公众号Demo,使用Spring Boot实现)
* https://github.com/binarywang/weixin-java-mp-multi-demo (支持多公众号)
* https://github.com/wechat-group/weixin-java-tools-springmvc (公众号Demo,内含部分微信支付代码)
* https://github.com/wechat-group/weixin-java-mp-multi-demo (支持多公众号)

---------------------------------
### 其他信息
1. 最新更新:**2017-6-25 发布[【2.7.0正式版】](https://github.com/Wechat-Group/weixin-java-tools/releases)**
### 重要提示信息(部分为新手必读)
1. 最新更新:**2017-9-3 发布[【2.8.0正式版】](https://github.com/Wechat-Group/weixin-java-tools/releases)**
1. 开源中国网站的本项目介绍的首页链接地址:https://www.oschina.net/p/weixin-java-tools-new
1. 自2.6.0版本开始,微信支付相关功能抽出独立为一个模块,详细使用方式请参考相关demo;
1. 自2.7.0版本开始,支持微信小程序,具体可以参考相关demo;
1. SDK详细开发文档请查阅 [【Wiki】](https://github.com/wechat-group/weixin-java-tools/wiki),部分文档可能未能及时更新,如有发现,可以及时上报或者自行修改。
1. 各个模块的Javadoc可以在线查看(有可能是最新的测试版本的,请注意观察版本号):[weixin-java-miniapp](https://binarywang.github.io/weixin-java-miniapp-javadoc/)[weixin-java-pay](https://binarywang.github.io/weixin-java-pay-javadoc/)[weixin-java-mp](https://binarywang.github.io/weixin-java-mp-javadoc/)[weixin-java-common](https://binarywang.github.io/weixin-java-common-javadoc/)[weixin-java-cp](https://binarywang.github.io/weixin-java-cp-javadoc/)
1. 本SDK要求的最低JDK版本是7,还在使用JDK6的用户请参考[【此项目】]( https://github.com/binarywang/weixin-java-tools-for-jdk6) ,而其他更早的JDK版本则需要自己改造实现。
Expand All @@ -30,7 +28,7 @@
1. **捐助渠道已开通,如有意向请点击[【支付宝二维码】](alipay_qrcode.jpg)捐赠,或者直接前往[【托管于码云的项目首页】](http://git.oschina.net/binary/weixin-java-tools),在评论区上方可以找到“捐助”按钮。非常感谢各位捐助的同学!**

---------------------------------
## SDK使用交流方式说明
## 使用交流方式说明
1. QQ群: [![加入QQ群](https://img.shields.io/badge/QQ群-343954419-blue.svg)](http://shang.qq.com/wpa/qunwpa?idkey=731dc3e7ea31ebe25376cc1a791445468612c63fd0e9e05399b088ec81fd9e15)[![加入QQ群](https://img.shields.io/badge/QQ群-343954419-blue.svg)](http://jq.qq.com/?_wv=1027&k=40lRskK),推荐点击按钮入群,当然如果无法成功操作,请自行搜索群号343954419进行添加 )
1. 由于群容量有限,即将爆满,故开启付费入群模式以保证只有真实交流需求的人进入,如果确实因为各种原因无法付费入群的,请联系群主说明原因即可入群;并为保证群的活跃度,本群将不定期清理长时间不活跃的同学;
1. 微信群: 因微信群已达到100人限制,故如有想加入微信群的,请入QQ群后联系管理员,提供微信号以便邀请加入;
Expand Down Expand Up @@ -60,7 +58,7 @@
<dependency>
<groupId>com.github.binarywang</groupId>
 <artifactId>weixin-java-miniapp</artifactId>
 <version>2.7.0</version>
 <version>2.8.0</version>
</dependency>
```

Expand All @@ -70,7 +68,7 @@
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
</dependency>
```

Expand All @@ -80,7 +78,7 @@
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
</dependency>
```

Expand All @@ -90,6 +88,6 @@
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>
</dependency>
```
2 changes: 1 addition & 1 deletion weixin-java-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.7.6.BETA</version>
<version>2.8.1.BETA</version>
</parent>

<artifactId>weixin-java-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package me.chanjar.weixin.common.bean.menu;

import com.google.gson.annotations.SerializedName;
import me.chanjar.weixin.common.util.ToStringUtils;

import java.io.Serializable;

public class WxMenuRule implements Serializable {
private static final long serialVersionUID = -4587181819499286670L;

/**
* 变态的微信接口,反序列化时这里反人类的使用和序列化时不一样的名字
*/
@SerializedName(value = "tag_id", alternate = "group_id")
private String tagId;
private String sex;
private String country;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.chanjar.weixin.common.bean.result;

import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

Expand Down Expand Up @@ -52,7 +51,7 @@ public void setThumbMediaId(String thumbMediaId) {

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
return ToStringUtils.toSimpleString(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ protected DocumentBuilder initialValue() {
protected String appidOrCorpid;

public WxCryptUtil() {
super();
}

/**
Expand Down Expand Up @@ -106,7 +105,7 @@ private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) {
private static String genRandomStr() {
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 16; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
Expand Down Expand Up @@ -148,12 +147,11 @@ public String encrypt(String plainText) {
String encryptedXml = encrypt(genRandomStr(), plainText);

// 生成安全签名
String timeStamp = Long.toString(System.currentTimeMillis() / 1000l);
String timeStamp = Long.toString(System.currentTimeMillis() / 1000L);
String nonce = genRandomStr();

String signature = SHA1.gen(this.token, timeStamp, nonce, encryptedXml);
String result = generateXml(encryptedXml, signature, timeStamp, nonce);
return result;
return generateXml(encryptedXml, signature, timeStamp, nonce);
}

/**
Expand Down Expand Up @@ -194,9 +192,7 @@ protected String encrypt(String randomStr, String plainText) {
byte[] encrypted = cipher.doFinal(unencrypted);

// 使用BASE64对加密后的字符串进行编码
String base64Encrypted = base64.encodeToString(encrypted);

return base64Encrypted;
return base64.encodeToString(encrypted);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -228,8 +224,7 @@ public String decrypt(String msgSignature, String timeStamp, String nonce, Strin
}

// 解密
String result = decrypt(cipherText);
return result;
return decrypt(cipherText);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package me.chanjar.weixin.common.util.http;

import jodd.http.HttpResponse;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import okhttp3.Response;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <pre>
* 三种http框架的response代理类,方便提取公共方法
* Created by Binary Wang on 2017-8-3.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class HttpResponseProxy {
private CloseableHttpResponse apacheHttpResponse;
private HttpResponse joddHttpResponse;
private Response okHttpResponse;

public HttpResponseProxy(CloseableHttpResponse apacheHttpResponse) {
this.apacheHttpResponse = apacheHttpResponse;
}

public HttpResponseProxy(HttpResponse joddHttpResponse) {
this.joddHttpResponse = joddHttpResponse;
}

public HttpResponseProxy(Response okHttpResponse) {
this.okHttpResponse = okHttpResponse;
}

public String getFileName() throws WxErrorException {
//由于对象只能由一个构造方法实现,因此三个response对象必定且只有一个不为空
if (this.apacheHttpResponse != null) {
return this.getFileName(this.apacheHttpResponse);
}

if (this.joddHttpResponse != null) {
return this.getFileName(this.joddHttpResponse);
}

if (this.okHttpResponse != null) {
return this.getFileName(this.okHttpResponse);
}

//cannot happen
return null;
}

private String getFileName(CloseableHttpResponse response) throws WxErrorException {
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
if (contentDispositionHeader == null || contentDispositionHeader.length == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

return this.extractFileNameFromContentString(contentDispositionHeader[0].getValue());
}

private String getFileName(HttpResponse response) throws WxErrorException {
String content = response.header("Content-disposition");
return this.extractFileNameFromContentString(content);
}

private String getFileName(Response response) throws WxErrorException {
String content = response.header("Content-disposition");
return this.extractFileNameFromContentString(content);
}

private String extractFileNameFromContentString(String content) throws WxErrorException {
if (content == null || content.length() == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(content);
if (m.matches()) {
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.HttpResponseProxy;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
Expand All @@ -17,15 +19,12 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by ecoolper on 2017/5/5.
*/
public class ApacheMediaDownloadRequestExecutor extends MediaDownloadRequestExecutor<CloseableHttpClient, HttpHost> {


public ApacheMediaDownloadRequestExecutor(RequestHttp requestHttp, File tmpDirFile) {
super(requestHttp, tmpDirFile);
}
Expand Down Expand Up @@ -57,31 +56,17 @@ public File execute(String uri, String queryParam) throws WxErrorException, IOEx
}
}

String fileName = getFileName(response);
String fileName = new HttpResponseProxy(response).getFileName();
if (StringUtils.isBlank(fileName)) {
return null;
}

String[] nameAndExt = fileName.split("\\.");
return FileUtils.createTmpFile(inputStream, nameAndExt[0], nameAndExt[1], super.tmpDirFile);
return FileUtils.createTmpFile(inputStream, FilenameUtils.getBaseName(fileName), FilenameUtils.getExtension(fileName),
super.tmpDirFile);

} finally {
httpGet.releaseConnection();
}
}

private String getFileName(CloseableHttpResponse response) throws WxErrorException {
Header[] contentDispositionHeader = response.getHeaders("Content-disposition");
if (contentDispositionHeader == null || contentDispositionHeader.length == 0) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

Pattern p = Pattern.compile(".*filename=\"(.*)\"");
Matcher m = p.matcher(contentDispositionHeader[0].getValue());
if (m.matches()) {
return m.group(1);
}
throw new WxErrorException(WxError.newBuilder().setErrorMsg("无法获取到文件名").build());
}

}
Loading

0 comments on commit 0b6ab42

Please sign in to comment.