# Conflicts: # pom.xml # weixin-java-common/pom.xml # weixin-java-cp/pom.xml # weixin-java-mp/pom.xmlmaster
@@ -57,4 +57,4 @@ | |||
非常欢迎和感谢对本项目发起Pull Request的同学,不过本项目基于[git flow](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow)开发流程,因此在发起Pull Request的时候请选择develop分支。 | |||
且本项目代码风格是用2个空格代表一个tab,因此在发起PR时注意一下,否则很容易发生在IDE格式化代码后与原代码产生大量diff,这样我在阅读PR的时候就很困难。 | |||
且本项目代码风格是用2个空格代表一个tab,因此在发起PR时注意一下,否则很容易发生在IDE格式化代码后与原代码产生大量diff,这样我在阅读PR的时候就很困难。 |
@@ -5,11 +5,11 @@ | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.3.4</version> | |||
<version>1.3.5</version> | |||
<packaging>pom</packaging> | |||
<name>WeiXin Java Tools - Parent</name> | |||
<description>微信公众号、企业号上级POM</description> | |||
<url>https://github.com/chanjarster/weixin-java-tools</url> | |||
<url>https://github.com/binarywang/weixin-java-tools</url> | |||
<licenses> | |||
<license> | |||
@@ -23,18 +23,23 @@ | |||
<name>Daniel Qian</name> | |||
<email>chanjarster@gmail.com</email> | |||
</developer> | |||
<developer> | |||
<name>Binary Wang</name> | |||
<email>binarywang@gmail.com</email> | |||
</developer> | |||
</developers> | |||
<scm> | |||
<connection>scm:git:https://github.com/chanjarster/weixin-java-tools.git</connection> | |||
<developerConnection>scm:git:git@github.com:chanjarster/weixin-java-tools.git</developerConnection> | |||
<url>https://github.com/chanjarster/weixin-java-tools</url> | |||
<connection>scm:git:https://github.com/binarywang/weixin-java-tools.git</connection> | |||
<developerConnection>scm:git:git@github.com:binarywang/weixin-java-tools.git</developerConnection> | |||
<url>https://github.com/binarywang/weixin-java-tools</url> | |||
</scm> | |||
<modules> | |||
<module>weixin-java-common</module> | |||
<module>weixin-java-cp</module> | |||
<module>weixin-java-mp</module> | |||
<module>spring-demo</module> | |||
</modules> | |||
<properties> | |||
@@ -144,6 +149,16 @@ | |||
</distributionManagement> | |||
<profiles> | |||
<profile> | |||
<id>doclint-java8-disable</id> | |||
<activation> | |||
<jdk>[1.8,)</jdk> | |||
</activation> | |||
<properties> | |||
<javadoc.opts>-Xdoclint:none</javadoc.opts> | |||
</properties> | |||
</profile> | |||
<profile> | |||
<id>release</id> | |||
<build> | |||
@@ -174,6 +189,7 @@ | |||
</execution> | |||
</executions> | |||
<configuration> | |||
<additionalparam>${javadoc.opts}</additionalparam> | |||
<charset>UTF-8</charset> | |||
<locale>zh_CN</locale> | |||
</configuration> | |||
@@ -0,0 +1,25 @@ | |||
<?xml version="1.0"?> | |||
<project | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<parent> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.3.5-SNAPSHOT</version> | |||
</parent> | |||
<artifactId>spring-demo</artifactId> | |||
<packaging>war</packaging> | |||
<name>WeiXin Java Tools - demo with spring</name> | |||
<description>spring demo</description> | |||
<url>https://github.com/binarywang/weixin-java-tools</url> | |||
<dependencies> | |||
<dependency> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>weixin-java-common</artifactId> | |||
<version>${project.version}</version> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -0,0 +1,55 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | |||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" | |||
version="3.0"> | |||
<display-name>api</display-name> | |||
<context-param> | |||
<param-name>contextConfigLocation</param-name> | |||
<param-value>classpath:applicationContext.xml</param-value> | |||
</context-param> | |||
<filter> | |||
<filter-name>encodingFilter</filter-name> | |||
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> | |||
<init-param> | |||
<param-name>encoding</param-name> | |||
<param-value>UTF-8</param-value> | |||
</init-param> | |||
</filter> | |||
<filter-mapping> | |||
<filter-name>encodingFilter</filter-name> | |||
<url-pattern>/*</url-pattern> | |||
</filter-mapping> | |||
<filter> | |||
<filter-name>springSecurityFilterChain</filter-name> | |||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> | |||
</filter> | |||
<filter-mapping> | |||
<filter-name>springSecurityFilterChain</filter-name> | |||
<url-pattern>/v1/admin/*</url-pattern> | |||
</filter-mapping> | |||
<listener> | |||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | |||
</listener> | |||
<listener> | |||
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> | |||
</listener> | |||
<servlet> | |||
<description>spring mvc servlet</description> | |||
<servlet-name>springMvc</servlet-name> | |||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> | |||
<init-param> | |||
<param-name>contextConfigLocation</param-name> | |||
<param-value>classpath:spring-servlet-common.xml</param-value> | |||
</init-param> | |||
<load-on-startup>1</load-on-startup> | |||
</servlet> | |||
<servlet-mapping> | |||
<servlet-name>springMvc</servlet-name> | |||
<url-pattern>/</url-pattern> | |||
</servlet-mapping> | |||
<session-config> | |||
<session-timeout>30</session-timeout> | |||
</session-config> | |||
</web-app> |
@@ -6,7 +6,7 @@ | |||
<parent> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.3.4</version> | |||
<version>1.3.5</version> | |||
</parent> | |||
<artifactId>weixin-java-common</artifactId> | |||
@@ -27,31 +27,26 @@ | |||
<dependency> | |||
<groupId>org.testng</groupId> | |||
<artifactId>testng</artifactId> | |||
<version>6.8.7</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.mockito</groupId> | |||
<artifactId>mockito-all</artifactId> | |||
<version>1.9.5</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.google.inject</groupId> | |||
<artifactId>guice</artifactId> | |||
<version>3.0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-server</artifactId> | |||
<version>9.3.0.M0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-servlet</artifactId> | |||
<version>9.3.0.M0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
@@ -5,6 +5,7 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import java.io.Serializable; | |||
public class WxAccessToken implements Serializable { | |||
private static final long serialVersionUID = 8709719312922168909L; | |||
private String accessToken; | |||
@@ -10,6 +10,8 @@ import java.io.Serializable; | |||
*/ | |||
public class WxCardApiSignature implements Serializable { | |||
private static final long serialVersionUID = 158176707226975979L; | |||
private String appId; | |||
private String cardId; | |||
@@ -6,6 +6,8 @@ import java.io.Serializable; | |||
* jspai signature | |||
*/ | |||
public class WxJsapiSignature implements Serializable { | |||
private static final long serialVersionUID = -1116808193154384804L; | |||
private String appid; | |||
private String noncestr; | |||
@@ -16,6 +16,8 @@ import java.util.List; | |||
*/ | |||
public class WxMenu implements Serializable { | |||
private static final long serialVersionUID = -7083914585539687746L; | |||
private List<WxMenuButton> buttons = new ArrayList<WxMenuButton>(); | |||
private WxMenuRule matchRule; | |||
@@ -129,7 +131,7 @@ public class WxMenu implements Serializable { | |||
} | |||
public static class WxMenuRule { | |||
private String groupId; | |||
private String tagId; | |||
private String sex; | |||
private String country; | |||
private String province; | |||
@@ -137,12 +139,12 @@ public class WxMenu implements Serializable { | |||
private String clientPlatformType; | |||
private String language; | |||
public String getGroupId() { | |||
return groupId; | |||
public String getTagId() { | |||
return tagId; | |||
} | |||
public void setGroupId(String groupId) { | |||
this.groupId = groupId; | |||
public void setTagId(String tagId) { | |||
this.tagId = tagId; | |||
} | |||
public String getSex() { | |||
@@ -196,7 +198,7 @@ public class WxMenu implements Serializable { | |||
@Override | |||
public String toString() { | |||
return "matchrule:{" + | |||
"group_id='" + groupId + '\'' + | |||
"tag_id='" + tagId + '\'' + | |||
", sex='" + sex + '\'' + | |||
", country" + country + '\'' + | |||
", province" + province + '\'' + | |||
@@ -5,13 +5,14 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import java.io.Serializable; | |||
/** | |||
* 微信错误码说明 | |||
* http://mp.weixin.qq.com/wiki/index.php?title=全局返回码说明 | |||
* 微信错误码说明,请阅读: <a href="http://mp.weixin.qq.com/wiki/10/6380dc743053a91c544ffd2b7c959166.html">全局返回码说明</a> | |||
* @author Daniel Qian | |||
* | |||
*/ | |||
public class WxError implements Serializable { | |||
private static final long serialVersionUID = 7869786563361406291L; | |||
private int errorCode; | |||
private String errorMsg; | |||
@@ -52,4 +53,30 @@ public class WxError implements Serializable { | |||
return "微信错误: errcode=" + errorCode + ", errmsg=" + errorMsg + "\njson:" + json; | |||
} | |||
public static Builder newBuilder(){ | |||
return new Builder(); | |||
} | |||
public static class Builder{ | |||
private int errorCode; | |||
private String errorMsg; | |||
public Builder setErrorCode(int errorCode) { | |||
this.errorCode = errorCode; | |||
return this; | |||
} | |||
public Builder setErrorMsg(String errorMsg) { | |||
this.errorMsg = errorMsg; | |||
return this; | |||
} | |||
public WxError build(){ | |||
WxError wxError = new WxError(); | |||
wxError.setErrorCode(this.errorCode); | |||
wxError.setErrorMsg(this.errorMsg); | |||
return wxError; | |||
} | |||
} | |||
} |
@@ -5,7 +5,8 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import java.io.Serializable; | |||
public class WxMediaUploadResult implements Serializable { | |||
private static final long serialVersionUID = 330834334738622341L; | |||
private String type; | |||
private String mediaId; | |||
private String thumbMediaId; | |||
@@ -1,11 +1,10 @@ | |||
package me.chanjar.weixin.common.util.crypto; | |||
import org.apache.commons.codec.digest.DigestUtils; | |||
import java.security.MessageDigest; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.Arrays; | |||
import org.apache.commons.codec.digest.DigestUtils; | |||
/** | |||
* Created by Daniel Qian on 14/10/19. | |||
*/ | |||
@@ -83,7 +83,7 @@ public class WxCryptUtil { | |||
String encryptedXml = encrypt(genRandomStr(), plainText); | |||
// 生成安全签名 | |||
String timeStamp = timeStamp = Long.toString(System.currentTimeMillis() / 1000l); | |||
String timeStamp = Long.toString(System.currentTimeMillis() / 1000l); | |||
String nonce = genRandomStr(); | |||
try { | |||
@@ -5,7 +5,6 @@ import java.io.IOException; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import org.apache.http.impl.client.HttpClients; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
@@ -62,7 +62,7 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ | |||
protected JsonObject convertToJson(WxMenu.WxMenuRule menuRule){ | |||
JsonObject matchRule = new JsonObject(); | |||
matchRule.addProperty("group_id",menuRule.getGroupId()); | |||
matchRule.addProperty("tag_id",menuRule.getTagId()); | |||
matchRule.addProperty("sex",menuRule.getSex()); | |||
matchRule.addProperty("country",menuRule.getCountry()); | |||
matchRule.addProperty("province",menuRule.getProvince()); | |||
@@ -1,19 +1,15 @@ | |||
package me.chanjar.weixin.common.util.xml; | |||
import java.io.Writer; | |||
import com.thoughtworks.xstream.XStream; | |||
import com.thoughtworks.xstream.converters.basic.DoubleConverter; | |||
import com.thoughtworks.xstream.converters.basic.FloatConverter; | |||
import com.thoughtworks.xstream.converters.basic.IntConverter; | |||
import com.thoughtworks.xstream.core.util.QuickWriter; | |||
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; | |||
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; | |||
import com.thoughtworks.xstream.io.xml.XppDriver; | |||
import com.thoughtworks.xstream.security.NoTypePermission; | |||
import com.thoughtworks.xstream.security.NullPermission; | |||
import com.thoughtworks.xstream.security.PrimitiveTypePermission; | |||
import java.io.Writer; | |||
public class XStreamInitializer { | |||
public static XStream getInstance() { | |||
@@ -67,7 +67,7 @@ public class WxMenuTest { | |||
menu.getButtons().add(button1); | |||
WxMenu.WxMenuRule wxMenuRule = new WxMenu.WxMenuRule(); | |||
wxMenuRule.setGroupId("2"); | |||
wxMenuRule.setTagId("2"); | |||
wxMenuRule.setSex("1"); | |||
wxMenuRule.setCountry("中国"); | |||
wxMenuRule.setProvince("广东"); | |||
@@ -6,7 +6,7 @@ | |||
<parent> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.3.4</version> | |||
<version>1.3.5</version> | |||
</parent> | |||
<artifactId>weixin-java-cp</artifactId> | |||
@@ -27,31 +27,26 @@ | |||
<dependency> | |||
<groupId>org.testng</groupId> | |||
<artifactId>testng</artifactId> | |||
<version>6.8.7</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.mockito</groupId> | |||
<artifactId>mockito-all</artifactId> | |||
<version>1.9.5</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.google.inject</groupId> | |||
<artifactId>guice</artifactId> | |||
<version>3.0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-server</artifactId> | |||
<version>9.3.0.M0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-servlet</artifactId> | |||
<version>9.3.0.M0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
@@ -6,7 +6,7 @@ | |||
<parent> | |||
<groupId>com.github.binarywang</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.3.4</version> | |||
<version>1.3.5</version> | |||
</parent> | |||
<artifactId>weixin-java-mp</artifactId> | |||
<name>WeiXin Java Tools - MP</name> | |||
@@ -26,31 +26,26 @@ | |||
<dependency> | |||
<groupId>org.testng</groupId> | |||
<artifactId>testng</artifactId> | |||
<version>6.8.7</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.mockito</groupId> | |||
<artifactId>mockito-all</artifactId> | |||
<version>1.9.5</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.google.inject</groupId> | |||
<artifactId>guice</artifactId> | |||
<version>3.0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-server</artifactId> | |||
<version>9.3.0.M0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.eclipse.jetty</groupId> | |||
<artifactId>jetty-servlet</artifactId> | |||
<version>9.3.0.M0</version> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
@@ -69,9 +69,9 @@ public interface WxMpConfigStorage { | |||
public String getSecret(); | |||
public String getPartnerId(); | |||
public String getPartnerKey(); | |||
public String getPartnerId(); | |||
public String getPartnerKey(); | |||
public String getToken(); | |||
@@ -44,29 +44,35 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
protected volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
@Override | |||
public String getAccessToken() { | |||
return this.accessToken; | |||
} | |||
@Override | |||
public boolean isAccessTokenExpired() { | |||
return System.currentTimeMillis() > this.expiresTime; | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(WxAccessToken accessToken) { | |||
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
} | |||
@Override | |||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | |||
this.accessToken = accessToken; | |||
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public void expireAccessToken() { | |||
this.expiresTime = 0; | |||
} | |||
@Override | |||
public String getJsapiTicket() { | |||
return jsapiTicket; | |||
return this.jsapiTicket; | |||
} | |||
public void setJsapiTicket(String jsapiTicket) { | |||
@@ -74,23 +80,26 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
} | |||
public long getJsapiTicketExpiresTime() { | |||
return jsapiTicketExpiresTime; | |||
return this.jsapiTicketExpiresTime; | |||
} | |||
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) { | |||
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime; | |||
} | |||
@Override | |||
public boolean isJsapiTicketExpired() { | |||
return System.currentTimeMillis() > this.jsapiTicketExpiresTime; | |||
} | |||
@Override | |||
public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { | |||
this.jsapiTicket = jsapiTicket; | |||
// 预留200秒的时间 | |||
this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public void expireJsapiTicket() { | |||
this.jsapiTicketExpiresTime = 0; | |||
} | |||
@@ -98,36 +107,44 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
/** | |||
* 卡券api_ticket | |||
*/ | |||
@Override | |||
public String getCardApiTicket() { | |||
return cardApiTicket; | |||
return this.cardApiTicket; | |||
} | |||
@Override | |||
public boolean isCardApiTicketExpired() { | |||
return System.currentTimeMillis() > this.cardApiTicketExpiresTime; | |||
} | |||
@Override | |||
public synchronized void updateCardApiTicket(String cardApiTicket, int expiresInSeconds) { | |||
this.cardApiTicket = cardApiTicket; | |||
// 预留200秒的时间 | |||
this.cardApiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l; | |||
} | |||
@Override | |||
public void expireCardApiTicket() { | |||
this.cardApiTicketExpiresTime = 0; | |||
} | |||
@Override | |||
public String getAppId() { | |||
return this.appId; | |||
} | |||
@Override | |||
public String getSecret() { | |||
return this.secret; | |||
} | |||
@Override | |||
public String getToken() { | |||
return this.token; | |||
} | |||
@Override | |||
public long getExpiresTime() { | |||
return this.expiresTime; | |||
} | |||
@@ -144,8 +161,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
this.token = token; | |||
} | |||
@Override | |||
public String getAesKey() { | |||
return aesKey; | |||
return this.aesKey; | |||
} | |||
public void setAesKey(String aesKey) { | |||
@@ -169,32 +187,36 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
this.oauth2redirectUri = oauth2redirectUri; | |||
} | |||
@Override | |||
public String getHttp_proxy_host() { | |||
return http_proxy_host; | |||
return this.http_proxy_host; | |||
} | |||
public void setHttp_proxy_host(String http_proxy_host) { | |||
this.http_proxy_host = http_proxy_host; | |||
} | |||
@Override | |||
public int getHttp_proxy_port() { | |||
return http_proxy_port; | |||
return this.http_proxy_port; | |||
} | |||
public void setHttp_proxy_port(int http_proxy_port) { | |||
this.http_proxy_port = http_proxy_port; | |||
} | |||
@Override | |||
public String getHttp_proxy_username() { | |||
return http_proxy_username; | |||
return this.http_proxy_username; | |||
} | |||
public void setHttp_proxy_username(String http_proxy_username) { | |||
this.http_proxy_username = http_proxy_username; | |||
} | |||
@Override | |||
public String getHttp_proxy_password() { | |||
return http_proxy_password; | |||
return this.http_proxy_password; | |||
} | |||
public void setHttp_proxy_password(String http_proxy_password) { | |||
@@ -204,29 +226,29 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
@Override | |||
public String toString() { | |||
return "WxMpInMemoryConfigStorage{" + | |||
"appId='" + appId + '\'' + | |||
", secret='" + secret + '\'' + | |||
", token='" + token + '\'' + | |||
", partnerId='" + partnerId + '\'' + | |||
", partnerKey='" + partnerKey + '\'' + | |||
", accessToken='" + accessToken + '\'' + | |||
", aesKey='" + aesKey + '\'' + | |||
", expiresTime=" + expiresTime + | |||
", http_proxy_host='" + http_proxy_host + '\'' + | |||
", http_proxy_port=" + http_proxy_port + | |||
", http_proxy_username='" + http_proxy_username + '\'' + | |||
", http_proxy_password='" + http_proxy_password + '\'' + | |||
", jsapiTicket='" + jsapiTicket + '\'' + | |||
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' + | |||
", cardApiTicket='" + cardApiTicket + '\'' + | |||
", cardApiTicketExpiresTime='" + cardApiTicketExpiresTime + '\'' + | |||
", tmpDirFile='" + tmpDirFile + '\'' + | |||
"appId='" + this.appId + '\'' + | |||
", secret='" + this.secret + '\'' + | |||
", token='" + this.token + '\'' + | |||
", partnerId='" + this.partnerId + '\'' + | |||
", partnerKey='" + this.partnerKey + '\'' + | |||
", accessToken='" + this.accessToken + '\'' + | |||
", aesKey='" + this.aesKey + '\'' + | |||
", expiresTime=" + this.expiresTime + | |||
", http_proxy_host='" + this.http_proxy_host + '\'' + | |||
", http_proxy_port=" + this.http_proxy_port + | |||
", http_proxy_username='" + this.http_proxy_username + '\'' + | |||
", http_proxy_password='" + this.http_proxy_password + '\'' + | |||
", jsapiTicket='" + this.jsapiTicket + '\'' + | |||
", jsapiTicketExpiresTime='" + this.jsapiTicketExpiresTime + '\'' + | |||
", cardApiTicket='" + this.cardApiTicket + '\'' + | |||
", cardApiTicketExpiresTime='" + this.cardApiTicketExpiresTime + '\'' + | |||
", tmpDirFile='" + this.tmpDirFile + '\'' + | |||
'}'; | |||
} | |||
@Override | |||
public String getPartnerId() { | |||
return partnerId; | |||
return this.partnerId; | |||
} | |||
public void setPartnerId(String partnerId) { | |||
@@ -235,7 +257,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
@Override | |||
public String getPartnerKey() { | |||
return partnerKey; | |||
return this.partnerKey; | |||
} | |||
public void setPartnerKey(String partnerKey) { | |||
@@ -253,11 +275,11 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
@Override | |||
public SSLContext getSSLContext() { | |||
return sslContext; | |||
return this.sslContext; | |||
} | |||
public void setSSLContext(SSLContext context) { | |||
sslContext = context; | |||
this.sslContext = context; | |||
} | |||
@Override | |||
@@ -1,232 +1,232 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import me.chanjar.weixin.common.session.InternalSession; | |||
import me.chanjar.weixin.common.session.InternalSessionManager; | |||
import me.chanjar.weixin.common.session.StandardSessionManager; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.LogExceptionHandler; | |||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; | |||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; | |||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | |||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.concurrent.ExecutionException; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.concurrent.Future; | |||
/** | |||
* <pre> | |||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | |||
* | |||
* 说明: | |||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | |||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxMpMessageRouterRule#next()} | |||
* 3. 规则的结束必须用{@link WxMpMessageRouterRule#end()}或者{@link WxMpMessageRouterRule#next()},否则不会生效 | |||
* | |||
* 使用方法: | |||
* WxMpMessageRouter router = new WxMpMessageRouter(); | |||
* router | |||
* .rule() | |||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | |||
* .interceptor(interceptor, ...).handler(handler, ...) | |||
* .end() | |||
* .rule() | |||
* // 另外一个匹配规则 | |||
* .end() | |||
* ; | |||
* | |||
* // 将WxXmlMessage交给消息路由器 | |||
* router.route(message); | |||
* | |||
* </pre> | |||
* @author Daniel Qian | |||
* | |||
*/ | |||
public class WxMpMessageRouter { | |||
protected final Logger log = LoggerFactory.getLogger(WxMpMessageRouter.class); | |||
private static final int DEFAULT_THREAD_POOL_SIZE = 100; | |||
private final List<WxMpMessageRouterRule> rules = new ArrayList<WxMpMessageRouterRule>(); | |||
private final WxMpService wxMpService; | |||
private ExecutorService executorService; | |||
private WxMessageDuplicateChecker messageDuplicateChecker; | |||
private WxSessionManager sessionManager; | |||
private WxErrorExceptionHandler exceptionHandler; | |||
public WxMpMessageRouter(WxMpService wxMpService) { | |||
this.wxMpService = wxMpService; | |||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); | |||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker(); | |||
this.sessionManager = new StandardSessionManager(); | |||
this.exceptionHandler = new LogExceptionHandler(); | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link ExecutorService} | |||
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100) | |||
* </pre> | |||
* @param executorService | |||
*/ | |||
public void setExecutorService(ExecutorService executorService) { | |||
this.executorService = executorService; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker} | |||
* </pre> | |||
* @param messageDuplicateChecker | |||
*/ | |||
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) { | |||
this.messageDuplicateChecker = messageDuplicateChecker; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager} | |||
* </pre> | |||
* @param sessionManager | |||
*/ | |||
public void setSessionManager(WxSessionManager sessionManager) { | |||
this.sessionManager = sessionManager; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler} | |||
* </pre> | |||
* @param exceptionHandler | |||
*/ | |||
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) { | |||
this.exceptionHandler = exceptionHandler; | |||
} | |||
List<WxMpMessageRouterRule> getRules() { | |||
return this.rules; | |||
} | |||
/** | |||
* 开始一个新的Route规则 | |||
* @return | |||
*/ | |||
public WxMpMessageRouterRule rule() { | |||
return new WxMpMessageRouterRule(this); | |||
} | |||
/** | |||
* 处理微信消息 | |||
* @param wxMessage | |||
*/ | |||
public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage) { | |||
if (isDuplicateMessage(wxMessage)) { | |||
// 如果是重复消息,那么就不做处理 | |||
return null; | |||
} | |||
final List<WxMpMessageRouterRule> matchRules = new ArrayList<WxMpMessageRouterRule>(); | |||
// 收集匹配的规则 | |||
for (final WxMpMessageRouterRule rule : rules) { | |||
if (rule.test(wxMessage)) { | |||
matchRules.add(rule); | |||
if(!rule.isReEnter()) { | |||
break; | |||
} | |||
} | |||
} | |||
if (matchRules.size() == 0) { | |||
return null; | |||
} | |||
WxMpXmlOutMessage res = null; | |||
final List<Future> futures = new ArrayList<Future>(); | |||
for (final WxMpMessageRouterRule rule : matchRules) { | |||
// 返回最后一个非异步的rule的执行结果 | |||
if(rule.isAsync()) { | |||
futures.add( | |||
executorService.submit(new Runnable() { | |||
public void run() { | |||
rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); | |||
} | |||
}) | |||
); | |||
} else { | |||
res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); | |||
// 在同步操作结束,session访问结束 | |||
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); | |||
sessionEndAccess(wxMessage); | |||
} | |||
} | |||
if (futures.size() > 0) { | |||
executorService.submit(new Runnable() { | |||
@Override | |||
public void run() { | |||
for (Future future : futures) { | |||
try { | |||
future.get(); | |||
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); | |||
// 异步操作结束,session访问结束 | |||
sessionEndAccess(wxMessage); | |||
} catch (InterruptedException e) { | |||
log.error("Error happened when wait task finish", e); | |||
} catch (ExecutionException e) { | |||
log.error("Error happened when wait task finish", e); | |||
} | |||
} | |||
} | |||
}); | |||
} | |||
return res; | |||
} | |||
protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) { | |||
String messageId = ""; | |||
if (wxMessage.getMsgId() == null) { | |||
messageId = String.valueOf(wxMessage.getCreateTime()) | |||
+ "-" + wxMessage.getFromUserName() | |||
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | |||
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | |||
; | |||
} else { | |||
messageId = String.valueOf(wxMessage.getMsgId()); | |||
} | |||
if (messageDuplicateChecker.isDuplicate(messageId)) { | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* 对session的访问结束 | |||
* @param wxMessage | |||
*/ | |||
protected void sessionEndAccess(WxMpXmlMessage wxMessage) { | |||
InternalSession session = ((InternalSessionManager)sessionManager).findSession(wxMessage.getFromUserName()); | |||
if (session != null) { | |||
session.endAccess(); | |||
} | |||
} | |||
} | |||
package me.chanjar.weixin.mp.api; | |||
import me.chanjar.weixin.common.session.InternalSession; | |||
import me.chanjar.weixin.common.session.InternalSessionManager; | |||
import me.chanjar.weixin.common.session.StandardSessionManager; | |||
import me.chanjar.weixin.common.session.WxSessionManager; | |||
import me.chanjar.weixin.common.util.LogExceptionHandler; | |||
import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker; | |||
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker; | |||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | |||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.concurrent.ExecutionException; | |||
import java.util.concurrent.ExecutorService; | |||
import java.util.concurrent.Executors; | |||
import java.util.concurrent.Future; | |||
/** | |||
* <pre> | |||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | |||
* | |||
* 说明: | |||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | |||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link WxMpMessageRouterRule#next()} | |||
* 3. 规则的结束必须用{@link WxMpMessageRouterRule#end()}或者{@link WxMpMessageRouterRule#next()},否则不会生效 | |||
* | |||
* 使用方法: | |||
* WxMpMessageRouter router = new WxMpMessageRouter(); | |||
* router | |||
* .rule() | |||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | |||
* .interceptor(interceptor, ...).handler(handler, ...) | |||
* .end() | |||
* .rule() | |||
* // 另外一个匹配规则 | |||
* .end() | |||
* ; | |||
* | |||
* // 将WxXmlMessage交给消息路由器 | |||
* router.route(message); | |||
* | |||
* </pre> | |||
* @author Daniel Qian | |||
* | |||
*/ | |||
public class WxMpMessageRouter { | |||
protected final Logger log = LoggerFactory.getLogger(WxMpMessageRouter.class); | |||
private static final int DEFAULT_THREAD_POOL_SIZE = 100; | |||
private final List<WxMpMessageRouterRule> rules = new ArrayList<WxMpMessageRouterRule>(); | |||
private final WxMpService wxMpService; | |||
private ExecutorService executorService; | |||
private WxMessageDuplicateChecker messageDuplicateChecker; | |||
private WxSessionManager sessionManager; | |||
private WxErrorExceptionHandler exceptionHandler; | |||
public WxMpMessageRouter(WxMpService wxMpService) { | |||
this.wxMpService = wxMpService; | |||
this.executorService = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE); | |||
this.messageDuplicateChecker = new WxMessageInMemoryDuplicateChecker(); | |||
this.sessionManager = new StandardSessionManager(); | |||
this.exceptionHandler = new LogExceptionHandler(); | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link ExecutorService} | |||
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100) | |||
* </pre> | |||
* @param executorService | |||
*/ | |||
public void setExecutorService(ExecutorService executorService) { | |||
this.executorService = executorService; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker} | |||
* </pre> | |||
* @param messageDuplicateChecker | |||
*/ | |||
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) { | |||
this.messageDuplicateChecker = messageDuplicateChecker; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager} | |||
* </pre> | |||
* @param sessionManager | |||
*/ | |||
public void setSessionManager(WxSessionManager sessionManager) { | |||
this.sessionManager = sessionManager; | |||
} | |||
/** | |||
* <pre> | |||
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler} | |||
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler} | |||
* </pre> | |||
* @param exceptionHandler | |||
*/ | |||
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) { | |||
this.exceptionHandler = exceptionHandler; | |||
} | |||
List<WxMpMessageRouterRule> getRules() { | |||
return this.rules; | |||
} | |||
/** | |||
* 开始一个新的Route规则 | |||
* @return | |||
*/ | |||
public WxMpMessageRouterRule rule() { | |||
return new WxMpMessageRouterRule(this); | |||
} | |||
/** | |||
* 处理微信消息 | |||
* @param wxMessage | |||
*/ | |||
public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage) { | |||
if (isDuplicateMessage(wxMessage)) { | |||
// 如果是重复消息,那么就不做处理 | |||
return null; | |||
} | |||
final List<WxMpMessageRouterRule> matchRules = new ArrayList<WxMpMessageRouterRule>(); | |||
// 收集匹配的规则 | |||
for (final WxMpMessageRouterRule rule : rules) { | |||
if (rule.test(wxMessage)) { | |||
matchRules.add(rule); | |||
if(!rule.isReEnter()) { | |||
break; | |||
} | |||
} | |||
} | |||
if (matchRules.size() == 0) { | |||
return null; | |||
} | |||
WxMpXmlOutMessage res = null; | |||
final List<Future> futures = new ArrayList<Future>(); | |||
for (final WxMpMessageRouterRule rule : matchRules) { | |||
// 返回最后一个非异步的rule的执行结果 | |||
if(rule.isAsync()) { | |||
futures.add( | |||
executorService.submit(new Runnable() { | |||
public void run() { | |||
rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); | |||
} | |||
}) | |||
); | |||
} else { | |||
res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler); | |||
// 在同步操作结束,session访问结束 | |||
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); | |||
sessionEndAccess(wxMessage); | |||
} | |||
} | |||
if (futures.size() > 0) { | |||
executorService.submit(new Runnable() { | |||
@Override | |||
public void run() { | |||
for (Future future : futures) { | |||
try { | |||
future.get(); | |||
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); | |||
// 异步操作结束,session访问结束 | |||
sessionEndAccess(wxMessage); | |||
} catch (InterruptedException e) { | |||
log.error("Error happened when wait task finish", e); | |||
} catch (ExecutionException e) { | |||
log.error("Error happened when wait task finish", e); | |||
} | |||
} | |||
} | |||
}); | |||
} | |||
return res; | |||
} | |||
protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) { | |||
StringBuffer messageId = new StringBuffer(); | |||
if (wxMessage.getMsgId() == null) { | |||
messageId.append(wxMessage.getCreateTime()) | |||
.append("-").append(wxMessage.getFromUserName()) | |||
.append("-").append(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | |||
.append("-").append(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | |||
; | |||
} else { | |||
messageId.append(wxMessage.getMsgId()); | |||
} | |||
if (messageDuplicateChecker.isDuplicate(messageId.toString())) { | |||
return true; | |||
} | |||
return false; | |||
} | |||
/** | |||
* 对session的访问结束 | |||
* @param wxMessage | |||
*/ | |||
protected void sessionEndAccess(WxMpXmlMessage wxMessage) { | |||
InternalSession session = ((InternalSessionManager)sessionManager).findSession(wxMessage.getFromUserName()); | |||
if (session != null) { | |||
session.endAccess(); | |||
} | |||
} | |||
} |
@@ -94,7 +94,8 @@ public interface WxMpService { | |||
/** | |||
* <pre> | |||
* 上传多媒体文件 | |||
* 新增临时素材 | |||
* 本接口即为原“上传多媒体文件”接口。 | |||
* | |||
* 上传的多媒体文件有格式和大小限制,如下: | |||
* 图片(image): 1M,支持JPG格式 | |||
@@ -102,7 +103,7 @@ public interface WxMpService { | |||
* 视频(video):10MB,支持MP4格式 | |||
* 缩略图(thumb):64KB,支持JPG格式 | |||
* | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/15/2d353966323806a202cd2deaafe8e557.html">新增临时素材</a> | |||
* </pre> | |||
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
@@ -241,6 +242,7 @@ public interface WxMpService { | |||
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException; | |||
/** | |||
* 新增临时素材 | |||
* @see #mediaUpload(String, String, InputStream) | |||
* @param mediaType | |||
* @param file | |||
@@ -250,9 +252,10 @@ public interface WxMpService { | |||
/** | |||
* <pre> | |||
* 下载多媒体文件 | |||
* 获取临时素材 | |||
* 本接口即为原“下载多媒体文件”接口。 | |||
* 根据微信文档,视频文件下载不了,会返回null | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/9/677a85e3f3849af35de54bb5516c2521.html">获取临时素材</a> | |||
* </pre> | |||
* @params media_id | |||
* @return 保存到本地的临时文件 | |||
@@ -351,7 +354,7 @@ public interface WxMpService { | |||
* @throws WxErrorException | |||
*/ | |||
public void menuDelete(String menuid) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 自定义菜单查询接口 | |||
@@ -361,7 +364,7 @@ public interface WxMpService { | |||
* @throws WxErrorException | |||
*/ | |||
public WxMenu menuGet() throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 测试个性化菜单匹配结果 | |||
@@ -465,7 +468,7 @@ public interface WxMpService { | |||
/** | |||
* <pre> | |||
* 换取临时二维码ticket | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a> | |||
* </pre> | |||
* @param scene_id 参数。 | |||
* @param expire_seconds 过期秒数,默认60秒,最小60秒,最大1800秒 | |||
@@ -477,7 +480,7 @@ public interface WxMpService { | |||
/** | |||
* <pre> | |||
* 换取永久二维码ticket | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a> | |||
* </pre> | |||
* @param scene_id 参数。永久二维码时最大值为100000(目前参数只支持1--100000) | |||
* @return | |||
@@ -488,7 +491,7 @@ public interface WxMpService { | |||
/** | |||
* <pre> | |||
* 换取永久字符串二维码ticket | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a> | |||
* </pre> | |||
* | |||
* @param scene_str 参数。字符串类型长度现在为1到64 | |||
@@ -500,7 +503,7 @@ public interface WxMpService { | |||
/** | |||
* <pre> | |||
* 换取二维码图片文件,jpg格式 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a> | |||
* </pre> | |||
* @param ticket 二维码ticket | |||
* @return | |||
@@ -508,10 +511,21 @@ public interface WxMpService { | |||
*/ | |||
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 换取二维码图片url地址(可以选择是否生成压缩的网址) | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a> | |||
* </pre> | |||
* @param ticket 二维码ticket | |||
* @param needShortUrl 是否需要压缩的二维码地址 | |||
* @return | |||
* @throws WxErrorException | |||
*/ | |||
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 换取二维码图片url地址 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码 | |||
* 详情请见: <a href="http://mp.weixin.qq.com/wiki/18/167e7d94df85d8389df6c94a7a8f78ba.html">生成带参数的二维码</a> | |||
* </pre> | |||
* @param ticket 二维码ticket | |||
* @return | |||
@@ -723,7 +737,7 @@ public interface WxMpService { | |||
/** | |||
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | |||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | |||
* | |||
* | |||
* @param parameters | |||
* All required/optional parameters for weixin payment | |||
* @return | |||
@@ -732,97 +746,114 @@ public interface WxMpService { | |||
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters); | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 | |||
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* @param parameters | |||
* the required or optional parameters | |||
* @return | |||
* @throws WxErrorException | |||
* @throws WxErrorException | |||
*/ | |||
Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) throws WxErrorException; | |||
Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数 | |||
* 该接口调用“统一下单”接口,并拼装NATIVE发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* @param openId 支付人openId | |||
* tradeType 交易类型 NATIVE (其他交易类型JSAPI,APP,WAP) | |||
* @param productId 商户商品ID | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @return | |||
* @throws WxErrorException | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getJSSDKPayInfo(Map<String, String>) instead | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl) throws WxErrorException; | |||
Map<String, String> getNativePayInfo(String productId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException; | |||
/** | |||
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 | |||
* @param transactionId | |||
* @param outTradeNo | |||
*/ | |||
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo); | |||
/** | |||
* 该接口调用“统一下单”接口,并拼装JSAPI发起支付请求需要的参数 | |||
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82 | |||
* tradeType 交易类型 JSAPI(其他交易类型NATIVE,APP,WAP) | |||
* @param openId 支付人openId | |||
* @param outTradeNo 商户端对应订单号 | |||
* @param amt 金额(单位元) | |||
* @param body 商品描述 | |||
* @param ip 发起支付的客户端IP | |||
* @param notifyUrl 通知地址 | |||
* @return | |||
* @throws WxErrorException | |||
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPayInfo(Map<String, String>) instead | |||
*/ | |||
@Deprecated | |||
Map<String, String> getJsapiPayInfo(String openId, String outTradeNo, double amt, String body, String ip, String notifyUrl) throws WxErrorException; | |||
/** | |||
* 读取支付结果通知 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 | |||
* @param xmlData | |||
* @return | |||
*/ | |||
WxMpPayCallback getJSSDKCallbackData(String xmlData); | |||
/** | |||
* 微信支付-申请退款 | |||
* 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 | |||
* @param parameters 需要传入的退款参数的Map。以下几项为参数的必须项:<br/> | |||
* <li/> transaction_id | |||
* <li/> out_trade_no (仅在上述transaction_id为空时是必须项) | |||
* <li/> out_refund_no | |||
* <li/> total_fee | |||
* <li/> refund_fee | |||
* @return 退款操作结果 | |||
* @throws WxErrorException | |||
*/ | |||
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 计算Map键值对是否和签名相符, | |||
* 按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的 格式(即 key1=value1&key2=value2...)拼接成字符串 | |||
* </pre> | |||
* @param kvm | |||
* @param signature | |||
* @return | |||
*/ | |||
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature); | |||
/** | |||
* 发送微信红包给个人用户 | |||
* | |||
* 需要传入的必填参数如下: | |||
* mch_billno//商户订单号 | |||
* send_name//商户名称 | |||
* re_openid//用户openid | |||
* total_amount//红包总额 | |||
* total_num//红包发放总人数 | |||
* wishing//红包祝福语 | |||
* client_ip//服务器Ip地址 | |||
* act_name//活动名称 | |||
* remark //备注 | |||
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5 | |||
* | |||
* 使用现金红包功能需要在xml配置文件中额外设置: | |||
* <partnerId></partnerId>微信商户平台ID | |||
* <partnerKey></partnerKey>商户平台设置的API密钥 | |||
* | |||
* @param parameters | |||
* @return | |||
* @throws WxErrorException | |||
*/ | |||
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 | |||
* @param transactionId | |||
* @param outTradeNo | |||
*/ | |||
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo); | |||
/** | |||
* 读取支付结果通知 | |||
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 | |||
* @param xmlData | |||
* @return | |||
*/ | |||
WxMpPayCallback getJSSDKCallbackData(String xmlData); | |||
/** | |||
* 微信支付-申请退款 | |||
* 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 | |||
* @param parameters 需要传入的退款参数的Map。以下几项为参数的必须项:<br/> | |||
* <li/> transaction_id | |||
* <li/> out_trade_no (仅在上述transaction_id为空时是必须项) | |||
* <li/> out_refund_no | |||
* <li/> total_fee | |||
* <li/> refund_fee | |||
* @return 退款操作结果 | |||
* @throws WxErrorException | |||
*/ | |||
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 计算Map键值对是否和签名相符, | |||
* 按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的 格式(即 key1=value1&key2=value2...)拼接成字符串 | |||
* </pre> | |||
* @param kvm | |||
* @param signature | |||
* @return | |||
*/ | |||
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature); | |||
/** | |||
* 发送微信红包给个人用户 | |||
* | |||
* 需要传入的必填参数如下: | |||
* mch_billno//商户订单号 | |||
* send_name//商户名称 | |||
* re_openid//用户openid | |||
* total_amount//红包总额 | |||
* total_num//红包发放总人数 | |||
* wishing//红包祝福语 | |||
* client_ip//服务器Ip地址 | |||
* act_name//活动名称 | |||
* remark //备注 | |||
* 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5 | |||
* | |||
* 使用现金红包功能需要在xml配置文件中额外设置: | |||
* <partnerId></partnerId>微信商户平台ID | |||
* <partnerKey></partnerKey>商户平台设置的API密钥 | |||
* | |||
* @param parameters | |||
* @return | |||
* @throws WxErrorException | |||
*/ | |||
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException; | |||
/** | |||
* 获得卡券api_ticket,不强制刷新卡券api_ticket | |||
@@ -914,7 +945,7 @@ public interface WxMpService { | |||
*/ | |||
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws | |||
WxErrorException; | |||
/** | |||
* 查看卡券详情接口 | |||
* 详见 https://mp.weixin.qq.com/wiki/14/8dd77aeaee85f922db5f8aa6386d385e.html#.E6.9F.A5.E7.9C.8B.E5.8D.A1.E5.88.B8.E8.AF.A6.E6.83.85 | |||
@@ -925,4 +956,49 @@ public interface WxMpService { | |||
* @throws WxErrorException | |||
*/ | |||
public String getCardDetail(String cardId) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 预览接口 | |||
* 详情请见:http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E9.A2.84.E8.A7.88.E6.8E.A5.E5.8F.A3.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91 | |||
* </pre> | |||
* @param wxMpMassPreviewMessage | |||
* @return wxMpMassSendResult | |||
* @throws WxErrorException | |||
*/ | |||
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | |||
/** | |||
* <pre> | |||
* 上传图文消息内的图片获取URL | |||
* 详情请见:http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E4.B8.8A.E4.BC.A0.E5.9B.BE.E6.96.87.E6.B6.88.E6.81.AF.E5.86.85.E7.9A.84.E5.9B.BE.E7.89.87.E8.8E.B7.E5.8F.96URL.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91 | |||
* </pre> | |||
* @param file | |||
* @return WxMediaImgUploadResult 返回图片url | |||
* @throws WxErrorException | |||
*/ | |||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 设置所属行业 | |||
* 官方文档中暂未告知响应内容 | |||
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A | |||
* </pre> | |||
* @param wxMpIndustry | |||
* @return JsonObject | |||
* @throws WxErrorException | |||
*/ | |||
String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException; | |||
/*** | |||
* <pre> | |||
* 获取设置的行业信息 | |||
* 详情请见:http://mp.weixin.qq.com/wiki/5/6dde9eaa909f83354e0094dc3ad99e05.html#.E8.AE.BE.E7.BD.AE.E6.89.80.E5.B1.9E.E8.A1.8C.E4.B8.9A | |||
* </pre> | |||
* | |||
* @return wxMpIndustry | |||
* @throws WxErrorException | |||
*/ | |||
WxMpIndustry getIndustry() throws WxErrorException; | |||
} |
@@ -50,7 +50,6 @@ import java.io.InputStream; | |||
import java.io.StringReader; | |||
import java.io.UnsupportedEncodingException; | |||
import java.net.URLEncoder; | |||
import java.nio.charset.Charset; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.util.*; | |||
import java.util.Map.Entry; | |||
@@ -86,32 +85,36 @@ public class WxMpServiceImpl implements WxMpService { | |||
protected WxSessionManager sessionManager = new StandardSessionManager(); | |||
@Override | |||
public boolean checkSignature(String timestamp, String nonce, String signature) { | |||
try { | |||
return SHA1.gen(wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature); | |||
return SHA1.gen(this.wxMpConfigStorage.getToken(), timestamp, nonce).equals(signature); | |||
} catch (Exception e) { | |||
return false; | |||
} | |||
} | |||
@Override | |||
public String getAccessToken() throws WxErrorException { | |||
return getAccessToken(false); | |||
} | |||
@Override | |||
public String getAccessToken(boolean forceRefresh) throws WxErrorException { | |||
if (forceRefresh) { | |||
wxMpConfigStorage.expireAccessToken(); | |||
this.wxMpConfigStorage.expireAccessToken(); | |||
} | |||
if (wxMpConfigStorage.isAccessTokenExpired()) { | |||
synchronized (globalAccessTokenRefreshLock) { | |||
if (wxMpConfigStorage.isAccessTokenExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" | |||
+ "&appid=" + wxMpConfigStorage.getAppId() | |||
+ "&secret=" + wxMpConfigStorage.getSecret(); | |||
if (this.wxMpConfigStorage.isAccessTokenExpired()) { | |||
synchronized (this.globalAccessTokenRefreshLock) { | |||
if (this.wxMpConfigStorage.isAccessTokenExpired()) { | |||
String url = new StringBuffer() | |||
.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential") | |||
.append("&appid=").append(this.wxMpConfigStorage.getAppId()) | |||
.append("&secret=").append(this.wxMpConfigStorage.getSecret()).toString(); | |||
try { | |||
HttpGet httpGet = new HttpGet(url); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpGet.setConfig(config); | |||
} | |||
try (CloseableHttpResponse response = getHttpclient().execute(httpGet)) { | |||
@@ -121,7 +124,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
throw new WxErrorException(error); | |||
} | |||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | |||
wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
this.wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
}finally { | |||
httpGet.releaseConnection(); | |||
} | |||
@@ -133,33 +136,36 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
} | |||
return wxMpConfigStorage.getAccessToken(); | |||
return this.wxMpConfigStorage.getAccessToken(); | |||
} | |||
@Override | |||
public String getJsapiTicket() throws WxErrorException { | |||
return getJsapiTicket(false); | |||
} | |||
@Override | |||
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException { | |||
if (forceRefresh) { | |||
wxMpConfigStorage.expireJsapiTicket(); | |||
this.wxMpConfigStorage.expireJsapiTicket(); | |||
} | |||
if (wxMpConfigStorage.isJsapiTicketExpired()) { | |||
synchronized (globalJsapiTicketRefreshLock) { | |||
if (wxMpConfigStorage.isJsapiTicketExpired()) { | |||
if (this.wxMpConfigStorage.isJsapiTicketExpired()) { | |||
synchronized (this.globalJsapiTicketRefreshLock) { | |||
if (this.wxMpConfigStorage.isJsapiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); | |||
wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds); | |||
this.wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds); | |||
} | |||
} | |||
} | |||
return wxMpConfigStorage.getJsapiTicket(); | |||
return this.wxMpConfigStorage.getJsapiTicket(); | |||
} | |||
@Override | |||
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException { | |||
long timestamp = System.currentTimeMillis() / 1000; | |||
String noncestr = RandomUtils.getRandomStr(); | |||
@@ -172,7 +178,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
"url=" + url | |||
); | |||
WxJsapiSignature jsapiSignature = new WxJsapiSignature(); | |||
jsapiSignature.setAppid(wxMpConfigStorage.getAppId()); | |||
jsapiSignature.setAppid(this.wxMpConfigStorage.getAppId()); | |||
jsapiSignature.setTimestamp(timestamp); | |||
jsapiSignature.setNoncestr(noncestr); | |||
jsapiSignature.setUrl(url); | |||
@@ -183,11 +189,13 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public void customMessageSend(WxMpCustomMessage message) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; | |||
execute(new SimplePostRequestExecutor(), url, message.toJson()); | |||
} | |||
@Override | |||
public void menuCreate(WxMenu menu) throws WxErrorException { | |||
if (menu.getMatchRule() != null) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional"; | |||
@@ -198,16 +206,19 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public void menuDelete() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete"; | |||
execute(new SimpleGetRequestExecutor(), url, null); | |||
} | |||
@Override | |||
public void menuDelete(String menuid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional"; | |||
execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid); | |||
} | |||
@Override | |||
public WxMenu menuGet() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/get"; | |||
try { | |||
@@ -222,6 +233,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public WxMenu menuTryMatch(String userid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch"; | |||
try { | |||
@@ -236,25 +248,30 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { | |||
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); | |||
} | |||
@Override | |||
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { | |||
String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; | |||
String url = "https://api.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; | |||
return execute(new MediaUploadRequestExecutor(), url, file); | |||
} | |||
@Override | |||
public File mediaDownload(String media_id) throws WxErrorException { | |||
String url = "http://file.api.weixin.qq.com/cgi-bin/media/get"; | |||
return execute(new MediaDownloadRequestExecutor(wxMpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id); | |||
String url = "https://api.weixin.qq.com/cgi-bin/media/get"; | |||
return execute(new MediaDownloadRequestExecutor(this.wxMpConfigStorage.getTmpDirFile()), url, "media_id=" + media_id); | |||
} | |||
@Override | |||
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?type=" + mediaType; | |||
return execute(new MaterialUploadRequestExecutor(), url, material); | |||
} | |||
@Override | |||
public WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException { | |||
if (news == null || news.isEmpty()) { | |||
throw new IllegalArgumentException("news is empty!"); | |||
@@ -264,21 +281,25 @@ public class WxMpServiceImpl implements WxMpService { | |||
return WxMpMaterialUploadResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material"; | |||
return execute(new MaterialVoiceAndImageDownloadRequestExecutor(wxMpConfigStorage.getTmpDirFile()), url, media_id); | |||
return execute(new MaterialVoiceAndImageDownloadRequestExecutor(this.wxMpConfigStorage.getTmpDirFile()), url, media_id); | |||
} | |||
@Override | |||
public WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material"; | |||
return execute(new MaterialVideoInfoRequestExecutor(), url, media_id); | |||
} | |||
@Override | |||
public WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/get_material"; | |||
return execute(new MaterialNewsInfoRequestExecutor(), url, media_id); | |||
} | |||
@Override | |||
public boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/update_news"; | |||
String responseText = post(url, wxMpMaterialArticleUpdate.toJson()); | |||
@@ -290,11 +311,13 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public boolean materialDelete(String media_id) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/del_material"; | |||
return execute(new MaterialDeleteRequestExecutor(), url, media_id); | |||
} | |||
@Override | |||
public WxMpMaterialCountResult materialCount() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount"; | |||
String responseText = get(url, null); | |||
@@ -306,6 +329,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public WxMpMaterialNewsBatchGetResult materialNewsBatchGet(int offset, int count) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; | |||
Map<String, Object> params = new HashMap<>(); | |||
@@ -321,6 +345,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public WxMpMaterialFileBatchGetResult materialFileBatchGet(String type, int offset, int count) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; | |||
Map<String, Object> params = new HashMap<>(); | |||
@@ -336,30 +361,35 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
} | |||
@Override | |||
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, news.toJson()); | |||
return WxMpMassUploadResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException { | |||
String url = "http://file.api.weixin.qq.com/cgi-bin/media/uploadvideo"; | |||
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, video.toJson()); | |||
return WxMpMassUploadResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpMassSendResult massGroupMessageSend(WxMpMassGroupMessage message) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson()); | |||
return WxMpMassSendResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, message.toJson()); | |||
return WxMpMassSendResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpGroup groupCreate(String name) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/create"; | |||
JsonObject json = new JsonObject(); | |||
@@ -374,6 +404,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
return WxMpGroup.fromJson(responseContent); | |||
} | |||
@Override | |||
public List<WxMpGroup> groupGet() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/get"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
@@ -387,6 +418,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
}.getType()); | |||
} | |||
@Override | |||
public long userGetGroup(String openid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/getid"; | |||
JsonObject o = new JsonObject(); | |||
@@ -396,11 +428,13 @@ public class WxMpServiceImpl implements WxMpService { | |||
return GsonHelper.getAsLong(tmpJsonElement.getAsJsonObject().get("groupid")); | |||
} | |||
@Override | |||
public void groupUpdate(WxMpGroup group) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/update"; | |||
execute(new SimplePostRequestExecutor(), url, group.toJson()); | |||
} | |||
@Override | |||
public void userUpdateGroup(String openid, long to_groupid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/groups/members/update"; | |||
JsonObject json = new JsonObject(); | |||
@@ -409,6 +443,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
} | |||
@Override | |||
public void userUpdateRemark(String openid, String remark) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark"; | |||
JsonObject json = new JsonObject(); | |||
@@ -417,6 +452,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
execute(new SimplePostRequestExecutor(), url, json.toString()); | |||
} | |||
@Override | |||
public WxMpUser userInfo(String openid, String lang) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/user/info"; | |||
lang = lang == null ? "zh_CN" : lang; | |||
@@ -424,12 +460,14 @@ public class WxMpServiceImpl implements WxMpService { | |||
return WxMpUser.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpUserList userList(String next_openid) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/user/get"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, next_openid == null ? null : "next_openid=" + next_openid); | |||
return WxMpUserList.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpQrCodeTicket qrCodeCreateTmpTicket(int scene_id, Integer expire_seconds) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create"; | |||
JsonObject json = new JsonObject(); | |||
@@ -446,6 +484,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
return WxMpQrCodeTicket.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpQrCodeTicket qrCodeCreateLastTicket(int scene_id) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create"; | |||
JsonObject json = new JsonObject(); | |||
@@ -459,6 +498,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
return WxMpQrCodeTicket.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMpQrCodeTicket qrCodeCreateLastTicket(String scene_str) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create"; | |||
JsonObject json = new JsonObject(); | |||
@@ -472,24 +512,36 @@ public class WxMpServiceImpl implements WxMpService { | |||
return WxMpQrCodeTicket.fromJson(responseContent); | |||
} | |||
@Override | |||
public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException { | |||
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode"; | |||
return execute(new QrCodeRequestExecutor(), url, ticket); | |||
} | |||
@Override | |||
public String qrCodePictureUrl(String ticket) throws WxErrorException { | |||
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException { | |||
String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"; | |||
try { | |||
return String.format(url, URLEncoder.encode(ticket, Charsets.UTF_8.name())); | |||
String resultUrl = String.format(url, | |||
URLEncoder.encode(ticket, Charsets.UTF_8.name())); | |||
if(needShortUrl){ | |||
return this.shortUrl(resultUrl); | |||
} | |||
return resultUrl; | |||
} catch (UnsupportedEncodingException e) { | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg(e.getMessage()); | |||
WxError error = WxError.newBuilder().setErrorCode(-1) | |||
.setErrorMsg(e.getMessage()).build(); | |||
throw new WxErrorException(error); | |||
} | |||
} | |||
@Override | |||
public String qrCodePictureUrl(String ticket) throws WxErrorException { | |||
return qrCodePictureUrl(ticket, false); | |||
} | |||
@Override | |||
public String shortUrl(String long_url) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/shorturl"; | |||
JsonObject o = new JsonObject(); | |||
@@ -500,6 +552,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString(); | |||
} | |||
@Override | |||
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); | |||
@@ -510,6 +563,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
throw new WxErrorException(WxError.fromJson(responseContent)); | |||
} | |||
@Override | |||
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/semantic/semproxy/search"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, semanticQuery.toJson()); | |||
@@ -518,34 +572,36 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public String oauth2buildAuthorizationUrl(String scope, String state) { | |||
return this.oauth2buildAuthorizationUrl(wxMpConfigStorage.getOauth2redirectUri(), scope, state); | |||
return this.oauth2buildAuthorizationUrl(this.wxMpConfigStorage.getOauth2redirectUri(), scope, state); | |||
} | |||
@Override | |||
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { | |||
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; | |||
url += "appid=" + wxMpConfigStorage.getAppId(); | |||
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectURI); | |||
url += "&response_type=code"; | |||
url += "&scope=" + scope; | |||
StringBuffer url = new StringBuffer(); | |||
url.append("https://open.weixin.qq.com/connect/oauth2/authorize?"); | |||
url.append("appid=").append(this.wxMpConfigStorage.getAppId()); | |||
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI)); | |||
url.append("&response_type=code"); | |||
url.append("&scope=").append(scope); | |||
if (state != null) { | |||
url += "&state=" + state; | |||
url.append("&state=").append(state); | |||
} | |||
url += "#wechat_redirect"; | |||
return url; | |||
url.append("#wechat_redirect"); | |||
return url.toString(); | |||
} | |||
@Override | |||
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"; | |||
url += "appid=" + wxMpConfigStorage.getAppId(); | |||
url += "&secret=" + wxMpConfigStorage.getSecret(); | |||
url += "&code=" + code; | |||
url += "&grant_type=authorization_code"; | |||
StringBuffer url = new StringBuffer(); | |||
url.append("https://api.weixin.qq.com/sns/oauth2/access_token?"); | |||
url.append("appid=").append(this.wxMpConfigStorage.getAppId()); | |||
url.append("&secret=").append(this.wxMpConfigStorage.getSecret()); | |||
url.append("&code=").append(code); | |||
url.append("&grant_type=authorization_code"); | |||
try { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); | |||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
return WxMpOAuth2AccessToken.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
@@ -556,14 +612,15 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"; | |||
url += "appid=" + wxMpConfigStorage.getAppId(); | |||
url += "&grant_type=refresh_token"; | |||
url += "&refresh_token=" + refreshToken; | |||
StringBuffer url = new StringBuffer(); | |||
url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?"); | |||
url.append("appid=").append(this.wxMpConfigStorage.getAppId()); | |||
url.append("&grant_type=refresh_token"); | |||
url.append("&refresh_token=").append(refreshToken); | |||
try { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); | |||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
return WxMpOAuth2AccessToken.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
@@ -574,18 +631,19 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/sns/userinfo?"; | |||
url += "access_token=" + oAuth2AccessToken.getAccessToken(); | |||
url += "&openid=" + oAuth2AccessToken.getOpenId(); | |||
StringBuffer url = new StringBuffer(); | |||
url.append("https://api.weixin.qq.com/sns/userinfo?"); | |||
url.append("access_token=").append(oAuth2AccessToken.getAccessToken()); | |||
url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | |||
if (lang == null) { | |||
url += "&lang=zh_CN"; | |||
url.append("&lang=zh_CN"); | |||
} else { | |||
url += "&lang=" + lang; | |||
url.append("&lang=").append(lang); | |||
} | |||
try { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); | |||
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
return WxMpUser.fromJson(responseText); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
@@ -596,13 +654,14 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) { | |||
String url = "https://api.weixin.qq.com/sns/auth?"; | |||
url += "access_token=" + oAuth2AccessToken.getAccessToken(); | |||
url += "&openid=" + oAuth2AccessToken.getOpenId(); | |||
StringBuffer url = new StringBuffer(); | |||
url.append("https://api.weixin.qq.com/sns/auth?"); | |||
url.append("access_token=").append(oAuth2AccessToken.getAccessToken()); | |||
url.append("&openid=").append(oAuth2AccessToken.getOpenId()); | |||
try { | |||
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); | |||
executor.execute(getHttpclient(), httpProxy, url, null); | |||
executor.execute(getHttpclient(), this.httpProxy, url.toString(), null); | |||
} catch (ClientProtocolException e) { | |||
throw new RuntimeException(e); | |||
} catch (IOException e) { | |||
@@ -653,10 +712,12 @@ public class WxMpServiceImpl implements WxMpService { | |||
}.getType()); | |||
} | |||
@Override | |||
public String get(String url, String queryParam) throws WxErrorException { | |||
return execute(new SimpleGetRequestExecutor(), url, queryParam); | |||
} | |||
@Override | |||
public String post(String url, String postData) throws WxErrorException { | |||
return execute(new SimplePostRequestExecutor(), url, postData); | |||
} | |||
@@ -670,6 +731,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
* @return | |||
* @throws WxErrorException | |||
*/ | |||
@Override | |||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | |||
int retryTimes = 0; | |||
do { | |||
@@ -681,9 +743,9 @@ public class WxMpServiceImpl implements WxMpService { | |||
* -1 系统繁忙, 1000ms后重试 | |||
*/ | |||
if (error.getErrorCode() == -1) { | |||
int sleepMillis = retrySleepMillis * (1 << retryTimes); | |||
int sleepMillis = this.retrySleepMillis * (1 << retryTimes); | |||
try { | |||
log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1); | |||
this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis, retryTimes + 1); | |||
Thread.sleep(sleepMillis); | |||
} catch (InterruptedException e1) { | |||
throw new RuntimeException(e1); | |||
@@ -692,7 +754,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
throw e; | |||
} | |||
} | |||
} while (++retryTimes < maxRetryTimes); | |||
} while (++retryTimes < this.maxRetryTimes); | |||
throw new RuntimeException("微信服务端异常,超出重试次数"); | |||
} | |||
@@ -707,7 +769,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken; | |||
try { | |||
return executor.execute(getHttpclient(), httpProxy, uriWithAccessToken, data); | |||
return executor.execute(getHttpclient(), this.httpProxy, uriWithAccessToken, data); | |||
} catch (WxErrorException e) { | |||
WxError error = e.getError(); | |||
/* | |||
@@ -717,7 +779,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
*/ | |||
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) { | |||
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token | |||
wxMpConfigStorage.expireAccessToken(); | |||
this.wxMpConfigStorage.expireAccessToken(); | |||
return execute(executor, uri, data); | |||
} | |||
if (error.getErrorCode() != 0) { | |||
@@ -732,20 +794,21 @@ public class WxMpServiceImpl implements WxMpService { | |||
} | |||
protected CloseableHttpClient getHttpclient() { | |||
return httpClient; | |||
return this.httpClient; | |||
} | |||
@Override | |||
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) { | |||
this.wxMpConfigStorage = wxConfigProvider; | |||
ApacheHttpClientBuilder apacheHttpClientBuilder = wxMpConfigStorage.getApacheHttpClientBuilder(); | |||
ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage.getApacheHttpClientBuilder(); | |||
if (null == apacheHttpClientBuilder) { | |||
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get(); | |||
} | |||
apacheHttpClientBuilder.httpProxyHost(wxMpConfigStorage.getHttp_proxy_host()) | |||
.httpProxyPort(wxMpConfigStorage.getHttp_proxy_port()) | |||
.httpProxyUsername(wxMpConfigStorage.getHttp_proxy_username()) | |||
.httpProxyPassword(wxMpConfigStorage.getHttp_proxy_password()); | |||
apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttp_proxy_host()) | |||
.httpProxyPort(this.wxMpConfigStorage.getHttp_proxy_port()) | |||
.httpProxyUsername(this.wxMpConfigStorage.getHttp_proxy_username()) | |||
.httpProxyPassword(this.wxMpConfigStorage.getHttp_proxy_password()); | |||
if (wxConfigProvider.getSSLContext() != null){ | |||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( | |||
@@ -756,7 +819,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); | |||
} | |||
httpClient = apacheHttpClientBuilder.build(); | |||
this.httpClient = apacheHttpClientBuilder.build(); | |||
} | |||
@Override | |||
@@ -772,9 +835,9 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) { | |||
Map<String, String> packageParams = new HashMap<String, String>(); | |||
packageParams.put("appid", wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
@@ -786,16 +849,17 @@ public class WxMpServiceImpl implements WxMpService { | |||
return getPrepayId(packageParams); | |||
} | |||
@Override | |||
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
final SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters); | |||
packageParams.put("appid", wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | |||
final SortedMap<String, String> packageParams = new TreeMap<>(parameters); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("nonce_str", nonce_str); | |||
checkParameters(packageParams); | |||
String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()); | |||
String sign = WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey()); | |||
packageParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
@@ -805,8 +869,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder"); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
@@ -829,7 +893,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
"trade_type", }; | |||
private void checkParameters(Map<String, String> parameters) { | |||
for (String para : REQUIRED_ORDER_PARAMETERS) { | |||
for (String para : this.REQUIRED_ORDER_PARAMETERS) { | |||
if (!parameters.containsKey(para)) | |||
throw new IllegalArgumentException("Reqiured argument '" + para + "' is missing."); | |||
} | |||
@@ -839,25 +903,40 @@ public class WxMpServiceImpl implements WxMpService { | |||
throw new IllegalArgumentException("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'."); | |||
} | |||
@Override | |||
public Map<String, String> getJsapiPayInfo(String openId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{ | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", "JSAPI"); | |||
packageParams.put("openid", openId); | |||
return getPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getNativePayInfo(String productId,String outTradeNo, double amt, String body,String ip, String callbackUrl) throws WxErrorException{ | |||
Map<String, String> packageParams = new HashMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", (int) (amt * 100) + ""); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", "NATIVE"); | |||
packageParams.put("product_id", productId); | |||
return getPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getJSSDKPayInfo(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String callbackUrl) | |||
throws WxErrorException { | |||
Map<String, String> packageParams = new HashMap<String, String>(); | |||
packageParams.put("appid", wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("body", body); | |||
packageParams.put("out_trade_no", outTradeNo); | |||
packageParams.put("total_fee", String.format("%.0f", amt * 100)); | |||
packageParams.put("spbill_create_ip", ip); | |||
packageParams.put("notify_url", callbackUrl); | |||
packageParams.put("trade_type", tradeType); | |||
packageParams.put("openid", openId); | |||
return getJSSDKPayInfo(packageParams); | |||
} | |||
@Override | |||
public Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) throws WxErrorException { | |||
public Map<String, String> getPayInfo(Map<String, String> parameters) throws WxErrorException { | |||
WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId(parameters); | |||
if (!"SUCCESS".equalsIgnoreCase(wxMpPrepayIdResult.getReturn_code()) | |||
@@ -877,16 +956,18 @@ public class WxMpServiceImpl implements WxMpService { | |||
throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", wxMpPrepayIdResult.getErr_code(), wxMpPrepayIdResult.getErr_code_des())); | |||
} | |||
Map<String, String> payInfo = new HashMap<String, String>(); | |||
payInfo.put("appId", wxMpConfigStorage.getAppId()); | |||
Map<String, String> payInfo = new HashMap<>(); | |||
payInfo.put("appId", this.wxMpConfigStorage.getAppId()); | |||
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 | |||
payInfo.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000)); | |||
payInfo.put("nonceStr", System.currentTimeMillis() + ""); | |||
payInfo.put("package", "prepay_id=" + prepayId); | |||
payInfo.put("signType", "MD5"); | |||
payInfo.put("code_url",wxMpPrepayIdResult.getCode_url()); | |||
if("NATIVE".equals(parameters.get("trade_type"))){ | |||
payInfo.put("codeUrl", wxMpPrepayIdResult.getCode_url()); | |||
} | |||
String finalSign = WxCryptUtil.createSign(payInfo, wxMpConfigStorage.getPartnerKey()); | |||
String finalSign = WxCryptUtil.createSign(payInfo, this.wxMpConfigStorage.getPartnerKey()); | |||
payInfo.put("paySign", finalSign); | |||
return payInfo; | |||
} | |||
@@ -895,9 +976,9 @@ public class WxMpServiceImpl implements WxMpService { | |||
public WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
SortedMap<String, String> packageParams = new TreeMap<String, String>(); | |||
packageParams.put("appid", wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | |||
SortedMap<String, String> packageParams = new TreeMap<>(); | |||
packageParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
if (transactionId != null && !"".equals(transactionId.trim())) | |||
packageParams.put("transaction_id", transactionId); | |||
else if (outTradeNo != null && !"".equals(outTradeNo.trim())) | |||
@@ -905,7 +986,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
else | |||
throw new IllegalArgumentException("Either 'transactionId' or 'outTradeNo' must be given."); | |||
packageParams.put("nonce_str", nonce_str); | |||
packageParams.put("sign", WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey())); | |||
packageParams.put("sign", WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey())); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
for (Entry<String, String> para : packageParams.entrySet()) { | |||
@@ -914,14 +995,14 @@ public class WxMpServiceImpl implements WxMpService { | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/pay/orderquery"); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8); | |||
httpPost.setEntity(entity); | |||
try(CloseableHttpResponse response = httpClient.execute(httpPost)) { | |||
try(CloseableHttpResponse response = this.httpClient.execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
XStream xstream = XStreamInitializer.getInstance(); | |||
xstream.alias("xml", WxMpPayResult.class); | |||
@@ -947,12 +1028,12 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException { | |||
SortedMap<String, String> refundParams = new TreeMap<String, String>(parameters); | |||
refundParams.put("appid", wxMpConfigStorage.getAppId()); | |||
refundParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | |||
SortedMap<String, String> refundParams = new TreeMap<>(parameters); | |||
refundParams.put("appid", this.wxMpConfigStorage.getAppId()); | |||
refundParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
refundParams.put("nonce_str", System.currentTimeMillis() + ""); | |||
refundParams.put("op_user_id", wxMpConfigStorage.getPartnerId()); | |||
String sign = WxCryptUtil.createSign(refundParams, wxMpConfigStorage.getPartnerKey()); | |||
refundParams.put("op_user_id", this.wxMpConfigStorage.getPartnerId()); | |||
String sign = WxCryptUtil.createSign(refundParams, this.wxMpConfigStorage.getPartnerKey()); | |||
refundParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
@@ -962,8 +1043,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
@@ -990,7 +1071,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
return wxMpPayRefundResult; | |||
} catch (IOException e) { | |||
log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e); | |||
this.log.error(MessageFormatter.format("The exception was happened when sending refund '{}'.", request.toString()).getMessage(), e); | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
error.setErrorMsg("incorrect response."); | |||
@@ -1002,19 +1083,19 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm, String signature) { | |||
return signature.equals(WxCryptUtil.createSign(kvm, wxMpConfigStorage.getPartnerKey())); | |||
return signature.equals(WxCryptUtil.createSign(kvm, this.wxMpConfigStorage.getPartnerKey())); | |||
} | |||
@Override | |||
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException { | |||
String nonce_str = System.currentTimeMillis() + ""; | |||
SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters); | |||
packageParams.put("wxappid", wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", wxMpConfigStorage.getPartnerId()); | |||
SortedMap<String, String> packageParams = new TreeMap<>(parameters); | |||
packageParams.put("wxappid", this.wxMpConfigStorage.getAppId()); | |||
packageParams.put("mch_id", this.wxMpConfigStorage.getPartnerId()); | |||
packageParams.put("nonce_str", nonce_str); | |||
String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey()); | |||
String sign = WxCryptUtil.createSign(packageParams, this.wxMpConfigStorage.getPartnerKey()); | |||
packageParams.put("sign", sign); | |||
StringBuilder request = new StringBuilder("<xml>"); | |||
@@ -1024,8 +1105,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
request.append("</xml>"); | |||
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
if (this.httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
@@ -1038,7 +1119,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent); | |||
return wxMpRedpackResult; | |||
} catch (IOException e) { | |||
log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e); | |||
this.log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e); | |||
WxError error = new WxError(); | |||
error.setErrorCode(-1); | |||
throw new WxErrorException(error); | |||
@@ -1076,22 +1157,22 @@ public class WxMpServiceImpl implements WxMpService { | |||
@Override | |||
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException { | |||
if (forceRefresh) { | |||
wxMpConfigStorage.expireCardApiTicket(); | |||
this.wxMpConfigStorage.expireCardApiTicket(); | |||
} | |||
if (wxMpConfigStorage.isCardApiTicketExpired()) { | |||
synchronized (globalCardApiTicketRefreshLock) { | |||
if (wxMpConfigStorage.isCardApiTicketExpired()) { | |||
if (this.wxMpConfigStorage.isCardApiTicketExpired()) { | |||
synchronized (this.globalCardApiTicketRefreshLock) { | |||
if (this.wxMpConfigStorage.isCardApiTicketExpired()) { | |||
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | |||
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); | |||
String cardApiTicket = tmpJsonObject.get("ticket").getAsString(); | |||
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); | |||
wxMpConfigStorage.updateCardApiTicket(cardApiTicket, expiresInSeconds); | |||
this.wxMpConfigStorage.updateCardApiTicket(cardApiTicket, expiresInSeconds); | |||
} | |||
} | |||
} | |||
return wxMpConfigStorage.getCardApiTicket(); | |||
return this.wxMpConfigStorage.getCardApiTicket(); | |||
} | |||
/** | |||
@@ -1234,7 +1315,7 @@ public class WxMpServiceImpl implements WxMpService { | |||
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement, | |||
new TypeToken<WxMpCardResult>() { }.getType()); | |||
if (!cardResult.getErrorCode().equals("0")) { | |||
log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg()); | |||
this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg()); | |||
} | |||
} | |||
@@ -1259,4 +1340,33 @@ public class WxMpServiceImpl implements WxMpService { | |||
return responseContent; | |||
} | |||
@Override | |||
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception { | |||
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"; | |||
String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson()); | |||
return WxMpMassSendResult.fromJson(responseContent); | |||
} | |||
@Override | |||
public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg"; | |||
return execute(new MediaImgUploadRequestExecutor(), url, file); | |||
} | |||
@Override | |||
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException { | |||
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId() | |||
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) { | |||
throw new IllegalArgumentException("industry id is empty"); | |||
} | |||
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry"; | |||
return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson()); | |||
} | |||
@Override | |||
public WxMpIndustry getIndustry() throws WxErrorException { | |||
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry"; | |||
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | |||
return WxMpIndustry.fromJson(responseContent); | |||
} | |||
} |
@@ -0,0 +1,38 @@ | |||
package me.chanjar.weixin.mp.bean; | |||
import java.io.Serializable; | |||
/** | |||
* @author miller | |||
* 官方文档中,创建和获取的数据结构不一样。所以采用冗余字段的方式,实现相应的接口 | |||
*/ | |||
public class Industry implements Serializable { | |||
private static final long serialVersionUID = -1707184885588012142L; | |||
private String id; | |||
private String firstClass; | |||
private String secondClass; | |||
public String getId() { | |||
return id; | |||
} | |||
public void setId(String id) { | |||
this.id = id; | |||
} | |||
public String getFirstClass() { | |||
return firstClass; | |||
} | |||
public void setFirstClass(String firstClass) { | |||
this.firstClass = firstClass; | |||
} | |||
public String getSecondClass() { | |||
return secondClass; | |||
} | |||
public void setSecondClass(String secondClass) { | |||
this.secondClass = secondClass; | |||
} | |||
} |
@@ -0,0 +1,39 @@ | |||
package me.chanjar.weixin.mp.bean; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import java.io.Serializable; | |||
/** | |||
* @author miller | |||
*/ | |||
public class WxMpIndustry implements Serializable { | |||
private static final long serialVersionUID = -7700398224795914722L; | |||
private Industry primaryIndustry; | |||
private Industry secondIndustry; | |||
public static WxMpIndustry fromJson(String json) { | |||
return WxMpGsonBuilder.create().fromJson(json, WxMpIndustry.class); | |||
} | |||
public String toJson() { | |||
return WxMpGsonBuilder.create().toJson(this); | |||
} | |||
public Industry getPrimaryIndustry() { | |||
return primaryIndustry; | |||
} | |||
public void setPrimaryIndustry(Industry primaryIndustry) { | |||
this.primaryIndustry = primaryIndustry; | |||
} | |||
public Industry getSecondIndustry() { | |||
return secondIndustry; | |||
} | |||
public void setSecondIndustry(Industry secondIndustry) { | |||
this.secondIndustry = secondIndustry; | |||
} | |||
} |
@@ -0,0 +1,68 @@ | |||
package me.chanjar.weixin.mp.bean; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import java.io.Serializable; | |||
/** | |||
* @author miller | |||
*/ | |||
public class WxMpMassPreviewMessage implements Serializable { | |||
private String toWxUsername; | |||
private String msgType; | |||
private String content; | |||
private String mediaId; | |||
public WxMpMassPreviewMessage() { | |||
super(); | |||
} | |||
public String getToWxUsername() { | |||
return toWxUsername; | |||
} | |||
public void setToWxUsername(String toWxUsername) { | |||
this.toWxUsername = toWxUsername; | |||
} | |||
public String getMsgType() { | |||
return msgType; | |||
} | |||
/** | |||
* <pre> | |||
* 请使用 | |||
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_IMAGE} | |||
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_NEWS} | |||
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_TEXT} | |||
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VIDEO} | |||
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VOICE} | |||
* 如果msgtype和media_id不匹配的话,会返回系统繁忙的错误 | |||
* </pre> | |||
* | |||
* @param msgType | |||
*/ | |||
public void setMsgType(String msgType) { | |||
this.msgType = msgType; | |||
} | |||
public String getContent() { | |||
return content; | |||
} | |||
public void setContent(String content) { | |||
this.content = content; | |||
} | |||
public String getMediaId() { | |||
return mediaId; | |||
} | |||
public void setMediaId(String mediaId) { | |||
this.mediaId = mediaId; | |||
} | |||
public String toJson() { | |||
return WxMpGsonBuilder.INSTANCE.create().toJson(this); | |||
} | |||
} |
@@ -12,7 +12,7 @@ public class WxMpTemplateMessage implements Serializable { | |||
private String templateId; | |||
private String url; | |||
private String topColor; | |||
private List<WxMpTemplateData> datas = new ArrayList<WxMpTemplateData>(); | |||
private List<WxMpTemplateData> data = new ArrayList<WxMpTemplateData>(); | |||
public String getToUser() { | |||
return toUser; | |||
@@ -46,12 +46,16 @@ public class WxMpTemplateMessage implements Serializable { | |||
this.topColor = topColor; | |||
} | |||
public List<WxMpTemplateData> getDatas() { | |||
return datas; | |||
public List<WxMpTemplateData> getData() { | |||
return data; | |||
} | |||
public void setDatas(List<WxMpTemplateData> datas) { | |||
this.datas = datas; | |||
public void setData(List<WxMpTemplateData> data) { | |||
this.data = data; | |||
} | |||
public void addWxMpTemplateData(WxMpTemplateData datum) { | |||
this.data.add(datum); | |||
} | |||
public String toJson() { | |||
@@ -0,0 +1,24 @@ | |||
package me.chanjar.weixin.mp.bean.result; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import java.io.Serializable; | |||
/** | |||
* @author miller | |||
*/ | |||
public class WxMediaImgUploadResult implements Serializable { | |||
private String url; | |||
public static WxMediaImgUploadResult fromJson(String json) { | |||
return WxMpGsonBuilder.create().fromJson(json, WxMediaImgUploadResult.class); | |||
} | |||
public String getUrl() { | |||
return url; | |||
} | |||
public void setUrl(String url) { | |||
this.url = url; | |||
} | |||
} |
@@ -20,6 +20,7 @@ public class WxMpMassSendResult implements Serializable { | |||
private String errorCode; | |||
private String errorMsg; | |||
private String msgId; | |||
private String msgDataId; | |||
public String getErrorCode() { | |||
return errorCode; | |||
@@ -44,7 +45,15 @@ public class WxMpMassSendResult implements Serializable { | |||
public void setMsgId(String msgId) { | |||
this.msgId = msgId; | |||
} | |||
public String getMsgDataId() { | |||
return msgDataId; | |||
} | |||
public void setMsgDataId(String msgDataId) { | |||
this.msgDataId = msgDataId; | |||
} | |||
public static WxMpMassSendResult fromJson(String json) { | |||
return WxMpGsonBuilder.create().fromJson(json, WxMpMassSendResult.class); | |||
} | |||
@@ -30,22 +30,26 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri | |||
httpPost.setConfig(response); | |||
} | |||
if (material != null) { | |||
File file = material.getFile(); | |||
if (file == null || !file.exists()) { | |||
throw new FileNotFoundException(); | |||
} | |||
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); | |||
multipartEntityBuilder | |||
.addBinaryBody("media", file) | |||
.setMode(HttpMultipartMode.RFC6532); | |||
Map<String, String> form = material.getForm(); | |||
if (material.getForm() != null) { | |||
multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); | |||
} | |||
httpPost.setEntity(multipartEntityBuilder.build()); | |||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
if (material == null) { | |||
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法请求,material参数为空").build()); | |||
} | |||
File file = material.getFile(); | |||
if (file == null || !file.exists()) { | |||
throw new FileNotFoundException(); | |||
} | |||
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); | |||
multipartEntityBuilder | |||
.addBinaryBody("media", file) | |||
.setMode(HttpMultipartMode.RFC6532); | |||
Map<String, String> form = material.getForm(); | |||
if (material.getForm() != null) { | |||
multipartEntityBuilder.addTextBody("description", WxGsonBuilder.create().toJson(form)); | |||
} | |||
httpPost.setEntity(multipartEntityBuilder.build()); | |||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
@@ -55,7 +59,7 @@ public class MaterialUploadRequestExecutor implements RequestExecutor<WxMpMateri | |||
} else { | |||
return WxMpMaterialUploadResult.fromJson(responseContent); | |||
} | |||
}finally { | |||
} finally { | |||
httpPost.releaseConnection(); | |||
} | |||
} | |||
@@ -0,0 +1,51 @@ | |||
package me.chanjar.weixin.mp.util.http; | |||
import me.chanjar.weixin.common.bean.result.WxError; | |||
import me.chanjar.weixin.common.exception.WxErrorException; | |||
import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | |||
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult; | |||
import org.apache.http.HttpEntity; | |||
import org.apache.http.HttpHost; | |||
import org.apache.http.client.ClientProtocolException; | |||
import org.apache.http.client.config.RequestConfig; | |||
import org.apache.http.client.methods.CloseableHttpResponse; | |||
import org.apache.http.client.methods.HttpPost; | |||
import org.apache.http.entity.ContentType; | |||
import org.apache.http.entity.mime.HttpMultipartMode; | |||
import org.apache.http.entity.mime.MultipartEntityBuilder; | |||
import org.apache.http.impl.client.CloseableHttpClient; | |||
import java.io.File; | |||
import java.io.IOException; | |||
/** | |||
* @author miller | |||
*/ | |||
public class MediaImgUploadRequestExecutor implements RequestExecutor<WxMediaImgUploadResult, File> { | |||
@Override | |||
public WxMediaImgUploadResult execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, File data) throws WxErrorException, ClientProtocolException, IOException { | |||
HttpPost httpPost = new HttpPost(uri); | |||
if (httpProxy != null) { | |||
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); | |||
httpPost.setConfig(config); | |||
} | |||
if (data != null) { | |||
HttpEntity entity = MultipartEntityBuilder | |||
.create() | |||
.addBinaryBody("media", data) | |||
.setMode(HttpMultipartMode.RFC6532) | |||
.build(); | |||
httpPost.setEntity(entity); | |||
httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString()); | |||
} | |||
try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | |||
WxError error = WxError.fromJson(responseContent); | |||
if (error.getErrorCode() != 0) { | |||
throw new WxErrorException(error); | |||
} | |||
return WxMediaImgUploadResult.fromJson(responseContent); | |||
} | |||
} | |||
} |
@@ -30,15 +30,15 @@ import java.util.UUID; | |||
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> { | |||
@Override | |||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { | |||
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { | |||
if (ticket != null) { | |||
if (uri.indexOf('?') == -1) { | |||
uri += '?'; | |||
} | |||
uri += uri.endsWith("?") ? | |||
"ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") | |||
: | |||
"&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | |||
uri += uri.endsWith("?") | |||
? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") | |||
: "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); | |||
} | |||
HttpGet httpGet = new HttpGet(uri); | |||
@@ -59,7 +59,7 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi | |||
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); | |||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); | |||
}finally { | |||
} finally { | |||
httpGet.releaseConnection(); | |||
} | |||
@@ -0,0 +1,22 @@ | |||
package me.chanjar.weixin.mp.util.json; | |||
import com.google.gson.*; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult; | |||
import java.lang.reflect.Type; | |||
/** | |||
* @author miller | |||
*/ | |||
public class WxMediaImgUploadResultGsonAdapter implements JsonDeserializer<WxMediaImgUploadResult> { | |||
@Override | |||
public WxMediaImgUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |||
WxMediaImgUploadResult wxMediaImgUploadResult = new WxMediaImgUploadResult(); | |||
JsonObject jsonObject = json.getAsJsonObject(); | |||
if (null != jsonObject.get("url") && !jsonObject.get("url").isJsonNull()) { | |||
wxMediaImgUploadResult.setUrl(GsonHelper.getAsString(jsonObject.get("url"))); | |||
} | |||
return wxMediaImgUploadResult; | |||
} | |||
} |
@@ -1,18 +1,15 @@ | |||
package me.chanjar.weixin.mp.util.json; | |||
import java.lang.reflect.Type; | |||
import com.google.gson.JsonDeserializationContext; | |||
import com.google.gson.JsonDeserializer; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParseException; | |||
import com.google.gson.reflect.TypeToken; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.mp.bean.WxMpCard; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.lang.reflect.Type; | |||
import java.util.List; | |||
/** | |||
* Created by YuJian on 15/11/11. | |||
@@ -1,20 +1,17 @@ | |||
package me.chanjar.weixin.mp.util.json; | |||
import java.lang.reflect.Type; | |||
import com.google.gson.JsonDeserializationContext; | |||
import com.google.gson.JsonDeserializer; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParseException; | |||
import com.google.gson.reflect.TypeToken; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.mp.bean.WxMpCard; | |||
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import java.lang.reflect.Type; | |||
import java.text.ParseException; | |||
import java.util.List; | |||
/** | |||
* Created by YuJian on 15/11/11. | |||
@@ -40,6 +40,9 @@ public class WxMpGsonBuilder { | |||
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter()); | |||
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter()); | |||
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); | |||
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | |||
INSTANCE.registerTypeAdapter(WxMediaImgUploadResult.class, new WxMediaImgUploadResultGsonAdapter()); | |||
INSTANCE.registerTypeAdapter(WxMpIndustry.class, new WxMpIndustryGsonAdapter()); | |||
} | |||
public static Gson create() { | |||
@@ -0,0 +1,44 @@ | |||
package me.chanjar.weixin.mp.util.json; | |||
import com.google.gson.JsonDeserializationContext; | |||
import com.google.gson.JsonDeserializer; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonParseException; | |||
import com.google.gson.JsonSerializationContext; | |||
import com.google.gson.JsonSerializer; | |||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||
import me.chanjar.weixin.mp.bean.Industry; | |||
import me.chanjar.weixin.mp.bean.WxMpIndustry; | |||
import java.lang.reflect.Type; | |||
/** | |||
* @author miller | |||
*/ | |||
public class WxMpIndustryGsonAdapter implements JsonSerializer<WxMpIndustry>, JsonDeserializer<WxMpIndustry> { | |||
@Override | |||
public JsonElement serialize(WxMpIndustry wxMpIndustry, Type type, JsonSerializationContext jsonSerializationContext) { | |||
JsonObject json = new JsonObject(); | |||
json.addProperty("industry_id1", wxMpIndustry.getPrimaryIndustry().getId()); | |||
json.addProperty("industry_id2", wxMpIndustry.getSecondIndustry().getId()); | |||
return json; | |||
} | |||
@Override | |||
public WxMpIndustry deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | |||
WxMpIndustry wxMpIndustry = new WxMpIndustry(); | |||
JsonObject primaryIndustry = jsonElement.getAsJsonObject().get("primary_industry").getAsJsonObject(); | |||
wxMpIndustry.setPrimaryIndustry(convertFromJson(primaryIndustry)); | |||
JsonObject secondaryIndustry = jsonElement.getAsJsonObject().get("secondary_industry").getAsJsonObject(); | |||
wxMpIndustry.setSecondIndustry(convertFromJson(secondaryIndustry)); | |||
return wxMpIndustry; | |||
} | |||
private Industry convertFromJson(JsonObject json) { | |||
Industry industry = new Industry(); | |||
industry.setFirstClass(GsonHelper.getString(json, "first_class")); | |||
industry.setSecondClass(GsonHelper.getString(json, "second_class")); | |||
return industry; | |||
} | |||
} |
@@ -0,0 +1,48 @@ | |||
package me.chanjar.weixin.mp.util.json; | |||
import com.google.gson.JsonElement; | |||
import com.google.gson.JsonObject; | |||
import com.google.gson.JsonSerializationContext; | |||
import com.google.gson.JsonSerializer; | |||
import me.chanjar.weixin.common.api.WxConsts; | |||
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage; | |||
import java.lang.reflect.Type; | |||
/** | |||
* @author miller | |||
*/ | |||
public class WxMpMassPreviewMessageGsonAdapter implements JsonSerializer<WxMpMassPreviewMessage> { | |||
@Override | |||
public JsonElement serialize(WxMpMassPreviewMessage wxMpMassPreviewMessage, Type type, JsonSerializationContext jsonSerializationContext) { | |||
JsonObject jsonObject = new JsonObject(); | |||
jsonObject.addProperty("towxname", wxMpMassPreviewMessage.getToWxUsername()); | |||
if (WxConsts.MASS_MSG_NEWS.equals(wxMpMassPreviewMessage.getMsgType())) { | |||
JsonObject news = new JsonObject(); | |||
news.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | |||
jsonObject.add(WxConsts.MASS_MSG_NEWS, news); | |||
} | |||
if (WxConsts.MASS_MSG_TEXT.equals(wxMpMassPreviewMessage.getMsgType())) { | |||
JsonObject sub = new JsonObject(); | |||
sub.addProperty("content", wxMpMassPreviewMessage.getContent()); | |||
jsonObject.add(WxConsts.MASS_MSG_TEXT, sub); | |||
} | |||
if (WxConsts.MASS_MSG_VOICE.equals(wxMpMassPreviewMessage.getMsgType())) { | |||
JsonObject sub = new JsonObject(); | |||
sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | |||
jsonObject.add(WxConsts.MASS_MSG_VOICE, sub); | |||
} | |||
if (WxConsts.MASS_MSG_IMAGE.equals(wxMpMassPreviewMessage.getMsgType())) { | |||
JsonObject sub = new JsonObject(); | |||
sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | |||
jsonObject.add(WxConsts.MASS_MSG_IMAGE, sub); | |||
} | |||
if (WxConsts.MASS_MSG_VIDEO.equals(wxMpMassPreviewMessage.getMsgType())) { | |||
JsonObject sub = new JsonObject(); | |||
sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | |||
jsonObject.add(WxConsts.MASS_MSG_VIDEO, sub); | |||
} | |||
jsonObject.addProperty("msgtype", wxMpMassPreviewMessage.getMsgType()); | |||
return jsonObject; | |||
} | |||
} |
@@ -34,6 +34,9 @@ public class WxMpMassSendResultAdapter implements JsonDeserializer<WxMpMassSendR | |||
if (sendResultJsonObject.get("msg_id") != null && !sendResultJsonObject.get("msg_id").isJsonNull()) { | |||
sendResult.setMsgId(GsonHelper.getAsString(sendResultJsonObject.get("msg_id"))); | |||
} | |||
if (sendResultJsonObject.get("msg_data_id") != null && !sendResultJsonObject.get("msg_data_id").isJsonNull()) { | |||
sendResult.setMsgDataId(GsonHelper.getAsString(sendResultJsonObject.get("msg_data_id"))); | |||
} | |||
return sendResult; | |||
} | |||
@@ -30,16 +30,16 @@ public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTempla | |||
messageJson.addProperty("topcolor", message.getTopColor()); | |||
} | |||
JsonObject datas = new JsonObject(); | |||
messageJson.add("data", datas); | |||
JsonObject data = new JsonObject(); | |||
messageJson.add("data", data); | |||
for (WxMpTemplateData data : message.getDatas()) { | |||
for (WxMpTemplateData datum : message.getData()) { | |||
JsonObject dataJson = new JsonObject(); | |||
dataJson.addProperty("value", data.getValue()); | |||
if (data.getColor() != null) { | |||
dataJson.addProperty("color", data.getColor()); | |||
dataJson.addProperty("value", datum.getValue()); | |||
if (datum.getColor() != null) { | |||
dataJson.addProperty("color", datum.getColor()); | |||
} | |||
datas.add(data.getName(), dataJson); | |||
data.add(datum.getName(), dataJson); | |||
} | |||
return messageJson; | |||
@@ -20,9 +20,9 @@ public class WxMpBaseAPITest { | |||
protected WxMpServiceImpl wxService; | |||
public void testRefreshAccessToken() throws WxErrorException { | |||
WxMpConfigStorage configStorage = wxService.wxMpConfigStorage; | |||
WxMpConfigStorage configStorage = this.wxService.wxMpConfigStorage; | |||
String before = configStorage.getAccessToken(); | |||
wxService.getAccessToken(false); | |||
this.wxService.getAccessToken(false); | |||
String after = configStorage.getAccessToken(); | |||
Assert.assertNotEquals(before, after); | |||
@@ -26,21 +26,23 @@ public class WxMpMediaAPITest { | |||
@Inject | |||
protected WxMpServiceImpl wxService; | |||
private List<String> media_ids = new ArrayList<String>(); | |||
private List<String> media_ids = new ArrayList<>(); | |||
@Test(dataProvider="uploadMedia") | |||
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException { | |||
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName); | |||
WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream); | |||
Assert.assertNotNull(res.getType()); | |||
Assert.assertNotNull(res.getCreatedAt()); | |||
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); | |||
if (res.getMediaId() != null) { | |||
media_ids.add(res.getMediaId()); | |||
} | |||
if (res.getThumbMediaId() != null) { | |||
media_ids.add(res.getThumbMediaId()); | |||
try(InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)){ | |||
WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, inputStream); | |||
Assert.assertNotNull(res.getType()); | |||
Assert.assertNotNull(res.getCreatedAt()); | |||
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null); | |||
if (res.getMediaId() != null) { | |||
this.media_ids.add(res.getMediaId()); | |||
} | |||
if (res.getThumbMediaId() != null) { | |||
this.media_ids.add(res.getThumbMediaId()); | |||
} | |||
} | |||
} | |||
@@ -56,7 +58,7 @@ public class WxMpMediaAPITest { | |||
@Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia") | |||
public void testDownloadMedia(String media_id) throws WxErrorException { | |||
wxService.mediaDownload(media_id); | |||
this.wxService.mediaDownload(media_id); | |||
} | |||
@DataProvider | |||
@@ -22,23 +22,29 @@ public class WxMpQrCodeAPITest { | |||
protected WxMpServiceImpl wxService; | |||
public void testQrCodeCreateTmpTicket() throws WxErrorException { | |||
WxMpQrCodeTicket ticket = wxService.qrCodeCreateTmpTicket(1, null); | |||
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateTmpTicket(1, null); | |||
Assert.assertNotNull(ticket.getUrl()); | |||
Assert.assertNotNull(ticket.getTicket()); | |||
Assert.assertTrue(ticket.getExpire_seconds() != -1); | |||
} | |||
public void testQrCodeCreateLastTicket() throws WxErrorException { | |||
WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1); | |||
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1); | |||
Assert.assertNotNull(ticket.getUrl()); | |||
Assert.assertNotNull(ticket.getTicket()); | |||
Assert.assertTrue(ticket.getExpire_seconds() == -1); | |||
} | |||
public void testQrCodePicture() throws WxErrorException { | |||
WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1); | |||
File file = wxService.qrCodePicture(ticket); | |||
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1); | |||
File file = this.wxService.qrCodePicture(ticket); | |||
Assert.assertNotNull(file); | |||
} | |||
public void testQrCodePictureUrl() throws WxErrorException { | |||
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1); | |||
String url = this.wxService.qrCodePictureUrl(ticket.getTicket()); | |||
Assert.assertNotNull(url); | |||
} | |||
} |