| @@ -1,28 +1,49 @@ | |||
| *.class | |||
| test-output | |||
| # Mobile Tools for Java (J2ME) | |||
| .mtj.tmp/ | |||
| # Package Files # | |||
| *.jar | |||
| *.war | |||
| *.ear | |||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | |||
| hs_err_pid* | |||
| target | |||
| bin | |||
| .project | |||
| .classpath | |||
| .settings | |||
| sw-pom.xml | |||
| *.iml | |||
| test-config.xml | |||
| .idea | |||
| /.gradle/ | |||
| /gradle/ | |||
| *.bat | |||
| /gradlew | |||
| *.class | |||
| test-output | |||
| # Mobile Tools for Java (J2ME) | |||
| .mtj.tmp/ | |||
| # Package Files # | |||
| *.jar | |||
| *.war | |||
| *.ear | |||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | |||
| hs_err_pid* | |||
| target | |||
| bin | |||
| .project | |||
| .classpath | |||
| .settings | |||
| sw-pom.xml | |||
| *.iml | |||
| test-config.xml | |||
| .idea | |||
| /.gradle/ | |||
| /gradle/ | |||
| *.bat | |||
| /gradlew | |||
| # OSX | |||
| # Icon must end with two \r | |||
| Icon | |||
| ._* | |||
| .DS_Store | |||
| .AppleDouble | |||
| .LSOverride | |||
| .DocumentRevisions-V100 | |||
| .fseventsd | |||
| .Spotlight-V100 | |||
| .TemporaryItems | |||
| .Trashes | |||
| .VolumeIcon.icns | |||
| .AppleDB | |||
| .AppleDesktop | |||
| Network Trash Folder | |||
| Temporary Items | |||
| .apdisk | |||
| @@ -6,7 +6,7 @@ | |||
| 1. ***本项目Fork自chanjarster/weixin-java-tools,但由于原项目已停止维护,故单独维护和发布,且发布到maven上的groupId也会不同,详细信息见下文。*** | |||
| 1. ***自2.0.0版本以来,公众号的接口调整比较大,主要是为了解决主接口类过于庞大不方便管理的问题,将接口实现代码按模块进行拆分。*** | |||
| 1. 本SDK要求的最低JDK版本是7,为满足少量还在使用JDK6的用户的需求,特意抽出独立的代码分支项目,请参考 https://github.com/binarywang/weixin-java-tools-for-jdk6 ,其他更早的JDK版本则需要自己改造实现; | |||
| 1. 最新更新:2016-09-30 发布2.2.0正式版! | |||
| 1. 最新更新:2016-10-31 发布2.3.0正式版! | |||
| =========== | |||
| @@ -36,12 +36,12 @@ maven: | |||
| <dependency> | |||
| <groupId>com.github.binarywang</groupId> | |||
| <artifactId>weixin-java-mp</artifactId> | |||
| <version>2.2.0</version> | |||
| <version>2.3.0</version> | |||
| </dependency> | |||
| ``` | |||
| gradle: | |||
| ```groovy | |||
| compile 'com.github.binarywang:weixin-java-mp:2.2.0' | |||
| compile 'com.github.binarywang:weixin-java-mp:2.3.0' | |||
| ``` | |||
| * 企业号: | |||
| @@ -51,12 +51,12 @@ maven: | |||
| <dependency> | |||
| <groupId>com.github.binarywang</groupId> | |||
| <artifactId>weixin-java-cp</artifactId> | |||
| <version>2.2.0</version> | |||
| <version>2.3.0</version> | |||
| </dependency> | |||
| ``` | |||
| gradle: | |||
| ```groovy | |||
| compile 'com.github.binarywang:weixin-java-cp:2.2.0' | |||
| compile 'com.github.binarywang:weixin-java-cp:2.3.0' | |||
| ``` | |||
| =========== | |||
| @@ -2,7 +2,7 @@ allprojects { | |||
| apply plugin: 'maven' | |||
| group = 'com.github.binarywang' | |||
| version = '2.2.0' | |||
| version = '2.3.0' | |||
| } | |||
| subprojects { | |||
| @@ -20,12 +20,9 @@ subprojects { | |||
| dependencies { | |||
| compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.10' | |||
| compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version:'4.5' | |||
| compile group: 'org.apache.httpcomponents', name: 'httpmime', version:'4.5' | |||
| compile group: 'org.jodd', name: 'jodd-http', version:'3.6.7' | |||
| compile group: 'com.google.code.gson', name: 'gson', version:'2.7' | |||
| compile group: 'com.google.guava', name: 'guava', version:'19.0' | |||
| compile group: 'org.jooq', name: 'joor', version:'0.9.6' | |||
| compile group: 'commons-codec', name: 'commons-codec', version:'1.10' | |||
| compile group: 'commons-io', name: 'commons-io', version:'2.5' | |||
| compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.4' | |||
| @@ -5,11 +5,11 @@ | |||
| <modelVersion>4.0.0</modelVersion> | |||
| <groupId>com.github.binarywang</groupId> | |||
| <artifactId>weixin-java-parent</artifactId> | |||
| <version>2.2.0</version> | |||
| <version>2.3.0</version> | |||
| <packaging>pom</packaging> | |||
| <name>WeiXin Java Tools - Parent</name> | |||
| <description>微信公众号、企业号上级POM</description> | |||
| <url>https://github.com/binarywang/weixin-java-tools</url> | |||
| <url>https://github.com/wechat-group/weixin-java-tools</url> | |||
| <licenses> | |||
| <license> | |||
| @@ -22,23 +22,51 @@ | |||
| <developer> | |||
| <name>Daniel Qian</name> | |||
| <email>chanjarster@gmail.com</email> | |||
| <url>https://github.com/chanjarster</url> | |||
| </developer> | |||
| <developer> | |||
| <name>Binary Wang</name> | |||
| <email>binarywang@gmail.com</email> | |||
| <url>https://github.com/binarywang</url> | |||
| </developer> | |||
| <developer> | |||
| <name>gaigeshen</name> | |||
| <email>gaigeshen@qq.com</email> | |||
| <url>https://github.com/gaigeshen</url> | |||
| </developer> | |||
| <developer> | |||
| <name>Liu Mingbo</name> | |||
| <email>liumingbo2008@gmail.com</email> | |||
| <url>https://github.com/FirenzesEagle</url> | |||
| </developer> | |||
| <developer> | |||
| <name>kakotor</name> | |||
| <email>kakotor@gmail.com</email> | |||
| <url>https://github.com/kakotor</url> | |||
| </developer> | |||
| <developer> | |||
| <name>xiong</name> | |||
| <email>zhaoxiong.tan@gmail.com</email> | |||
| <url>https://github.com/ZhaoxiongTan</url> | |||
| </developer> | |||
| <developer> | |||
| <name>LiuJunGuang</name> | |||
| <email>aimilin@yeah.net</email> | |||
| <url>https://github.com/aimilin6688</url> | |||
| </developer> | |||
| </developers> | |||
| <scm> | |||
| <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> | |||
| <connection>scm:git:https://github.com/wechat-group/weixin-java-tools.git</connection> | |||
| <developerConnection>scm:git:git@github.com:wechat-group/weixin-java-tools.git</developerConnection> | |||
| <url>https://github.com/wechat-group/weixin-java-tools</url> | |||
| </scm> | |||
| <modules> | |||
| <module>weixin-java-common</module> | |||
| <module>weixin-java-cp</module> | |||
| <module>weixin-java-mp</module> | |||
| <module>weixin-java-osgi</module> | |||
| </modules> | |||
| <properties> | |||
| @@ -48,16 +76,13 @@ | |||
| <httpclient.version>4.5</httpclient.version> | |||
| <slf4j.version>1.7.10</slf4j.version> | |||
| <logback.version>1.1.2</logback.version> | |||
| <jodd-http.version>3.6.7</jodd-http.version> | |||
| <jedis.version>2.9.0</jedis.version> | |||
| <gson.version>2.7</gson.version> | |||
| <guava.version>19.0</guava.version> | |||
| <joor.version>0.9.6</joor.version> | |||
| <commons-lang3.version>3.4</commons-lang3.version> | |||
| <commons-lang3.version>3.5</commons-lang3.version> | |||
| <commons-io.version>2.5</commons-io.version> | |||
| <commons-codec.version>1.10</commons-codec.version> | |||
| <jetty.version>9.3.0.M0</jetty.version> | |||
| <jetty-new.version>9.3.10.v20160621</jetty-new.version> | |||
| <jetty.version>9.3.0.RC0</jetty.version> | |||
| </properties> | |||
| <dependencies> | |||
| @@ -72,21 +97,11 @@ | |||
| <version>${logback.version}</version> | |||
| <scope>test</scope> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.apache.httpcomponents</groupId> | |||
| <artifactId>fluent-hc</artifactId> | |||
| <version>${httpclient.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.apache.httpcomponents</groupId> | |||
| <artifactId>httpmime</artifactId> | |||
| <version>${httpclient.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.jodd</groupId> | |||
| <artifactId>jodd-http</artifactId> | |||
| <version>${jodd-http.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.google.code.gson</groupId> | |||
| <artifactId>gson</artifactId> | |||
| @@ -111,16 +126,12 @@ | |||
| <groupId>redis.clients</groupId> | |||
| <artifactId>jedis</artifactId> | |||
| <version>${jedis.version}</version> | |||
| <scope>provided</scope> | |||
| <optional>true</optional> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.google.guava</groupId> | |||
| <artifactId>guava</artifactId> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.jooq</groupId> | |||
| <artifactId>joor</artifactId> | |||
| </dependency> | |||
| </dependencies> | |||
| <dependencyManagement> | |||
| @@ -166,11 +177,6 @@ | |||
| <artifactId>guava</artifactId> | |||
| <version>${guava.version}</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>org.jooq</groupId> | |||
| <artifactId>joor</artifactId> | |||
| <version>${joor.version}</version> | |||
| </dependency> | |||
| </dependencies> | |||
| </dependencyManagement> | |||
| @@ -300,5 +306,4 @@ | |||
| </plugins> | |||
| </build> | |||
| </project> | |||
| @@ -6,7 +6,7 @@ | |||
| <parent> | |||
| <groupId>com.github.binarywang</groupId> | |||
| <artifactId>weixin-java-parent</artifactId> | |||
| <version>2.2.0</version> | |||
| <version>2.3.0</version> | |||
| </parent> | |||
| <artifactId>weixin-java-common</artifactId> | |||
| @@ -23,14 +23,15 @@ public class WxConsts { | |||
| /////////////////////// | |||
| // 主动发送消息(即客服消息)的消息类型 | |||
| /////////////////////// | |||
| public static final String CUSTOM_MSG_TEXT = "text"; | |||
| public static final String CUSTOM_MSG_IMAGE = "image"; | |||
| public static final String CUSTOM_MSG_VOICE = "voice"; | |||
| public static final String CUSTOM_MSG_VIDEO = "video"; | |||
| public static final String CUSTOM_MSG_MUSIC = "music"; | |||
| public static final String CUSTOM_MSG_NEWS = "news"; | |||
| public static final String CUSTOM_MSG_FILE = "file"; | |||
| public static final String CUSTOM_MSG_WXCARD = "wxcard"; | |||
| public static final String CUSTOM_MSG_TEXT = "text";//文本消息 | |||
| public static final String CUSTOM_MSG_IMAGE = "image";//图片消息 | |||
| public static final String CUSTOM_MSG_VOICE = "voice";//语音消息 | |||
| public static final String CUSTOM_MSG_VIDEO = "video";//视频消息 | |||
| public static final String CUSTOM_MSG_MUSIC = "music";//音乐消息 | |||
| public static final String CUSTOM_MSG_NEWS = "news";//图文消息(点击跳转到外链) | |||
| public static final String CUSTOM_MSG_MPNEWS = "mpnews";//图文消息(点击跳转到图文消息页面) | |||
| public static final String CUSTOM_MSG_FILE = "file";//发送文件(CP专用) | |||
| public static final String CUSTOM_MSG_WXCARD = "wxcard";//卡券消息 | |||
| public static final String CUSTOM_MSG_TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service"; | |||
| public static final String CUSTOM_MSG_SAFE_NO = "0"; | |||
| public static final String CUSTOM_MSG_SAFE_YES = "1"; | |||
| @@ -63,6 +64,7 @@ public class WxConsts { | |||
| * 群发反馈消息代码所对应的文字描述 | |||
| */ | |||
| public static final Map<String, String> MASS_ST_2_DESC = new HashMap<>(); | |||
| /////////////////////// | |||
| // 微信端推送过来的事件类型 | |||
| /////////////////////// | |||
| @@ -94,6 +96,31 @@ public class WxConsts { | |||
| public static final String EVT_KF_CLOSE_SESSION = "kf_close_session"; // 客服关闭会话 | |||
| public static final String EVT_KF_SWITCH_SESSION = "kf_switch_session"; // 客服转接会话 | |||
| public static final String EVT_POI_CHECK_NOTIFY = "poi_check_notify"; //门店审核事件推送 | |||
| //以下为微信认证事件 | |||
| /** | |||
| * 资质认证成功 | |||
| */ | |||
| public static final String EVT_QUALIFICATION_VERIFY_SUCCESS = "qualification_verify_success"; | |||
| /** | |||
| * 资质认证失败 | |||
| */ | |||
| public static final String EVT_QUALIFICATION_VERIFY_FAIL = "qualification_verify_fail"; | |||
| /** | |||
| * 名称认证成功 | |||
| */ | |||
| public static final String EVT_NAMING_VERIFY_SUCCESS = "naming_verify_success"; | |||
| /** | |||
| * 名称认证失败 | |||
| */ | |||
| public static final String EVT_NAMING_VERIFY_FAIL = "naming_verify_fail"; | |||
| /** | |||
| * 年审通知 | |||
| */ | |||
| public static final String EVT_ANNUAL_RENEW = "annual_renew"; | |||
| /** | |||
| * 认证过期失效通知 | |||
| */ | |||
| public static final String EVT_VERIFY_EXPIRED = "verify_expired"; | |||
| /////////////////////// | |||
| // 上传多媒体文件的类型 | |||
| @@ -167,7 +194,7 @@ public class WxConsts { | |||
| * 弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息 | |||
| */ | |||
| public static final String OAUTH2_SCOPE_USER_INFO = "snsapi_userinfo"; | |||
| /** | |||
| * 网页应用登录授权作用域 snsapi_login | |||
| */ | |||
| @@ -1,7 +1,6 @@ | |||
| package me.chanjar.weixin.common.bean; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import java.io.Serializable; | |||
| @@ -35,7 +34,7 @@ public class WxCardApiSignature implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String getAppId() { | |||
| @@ -1,11 +1,10 @@ | |||
| package me.chanjar.weixin.common.bean.menu; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| public class WxMenuButton { | |||
| private String type; | |||
| @@ -18,10 +17,9 @@ public class WxMenuButton { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, | |||
| ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String getType() { | |||
| return this.type; | |||
| } | |||
| @@ -69,4 +67,4 @@ public class WxMenuButton { | |||
| public void setMediaId(String mediaId) { | |||
| this.mediaId = mediaId; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,7 +1,6 @@ | |||
| package me.chanjar.weixin.common.bean.menu; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| public class WxMenuRule { | |||
| private String tagId; | |||
| @@ -70,6 +69,6 @@ public class WxMenuRule { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| } | |||
| @@ -13,6 +13,11 @@ public class WxErrorException extends Exception { | |||
| this.error = error; | |||
| } | |||
| public WxErrorException(WxError error, Throwable cause) { | |||
| super(error.toString(), cause); | |||
| this.error = error; | |||
| } | |||
| public WxError getError() { | |||
| return this.error; | |||
| } | |||
| @@ -0,0 +1,88 @@ | |||
| package me.chanjar.weixin.common.util; | |||
| import com.google.common.collect.Lists; | |||
| import com.google.common.collect.Maps; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import me.chanjar.weixin.common.annotation.Required; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import java.lang.reflect.Field; | |||
| import java.util.ArrayList; | |||
| import java.util.Arrays; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| /** | |||
| * <pre> | |||
| * bean操作的一些工具类 | |||
| * Created by Binary Wang on 2016-10-21. | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * </pre> | |||
| */ | |||
| public class BeanUtils { | |||
| /** | |||
| * 检查bean里标记为@Required的field是否为空,为空则抛异常 | |||
| * @param bean 要检查的bean对象 | |||
| * @throws WxErrorException | |||
| */ | |||
| public static void checkRequiredFields(Object bean) throws WxErrorException { | |||
| List<String> nullFields = Lists.newArrayList(); | |||
| List<Field> fields = new ArrayList<>( Arrays.asList(bean.getClass().getDeclaredFields())); | |||
| fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields())); | |||
| for (Field field : fields) { | |||
| try { | |||
| boolean isAccessible = field.isAccessible(); | |||
| field.setAccessible(true); | |||
| if (field.isAnnotationPresent(Required.class) | |||
| && field.get(bean) == null) { | |||
| nullFields.add(field.getName()); | |||
| } | |||
| field.setAccessible(isAccessible); | |||
| } catch (SecurityException | IllegalArgumentException | |||
| | IllegalAccessException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| if (!nullFields.isEmpty()) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("必填字段 " + nullFields + " 必须提供值").build()); | |||
| } | |||
| } | |||
| /** | |||
| * 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象 | |||
| * @param bean 包含@XStreamAlias的xml bean对象 | |||
| * @return map对象 | |||
| */ | |||
| public static Map<String, String> xmlBean2Map(Object bean) { | |||
| Map<String, String> result = Maps.newHashMap(); | |||
| List<Field> fields = new ArrayList<>( Arrays.asList(bean.getClass().getDeclaredFields())); | |||
| fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields())); | |||
| for (Field field : fields) { | |||
| try { | |||
| boolean isAccessible = field.isAccessible(); | |||
| field.setAccessible(true); | |||
| if (field.get(bean) == null) { | |||
| field.setAccessible(isAccessible); | |||
| continue; | |||
| } | |||
| if (field.isAnnotationPresent(XStreamAlias.class)) { | |||
| result.put(field.getAnnotation(XStreamAlias.class).value(), | |||
| field.get(bean).toString()); | |||
| } | |||
| field.setAccessible(isAccessible); | |||
| } catch (SecurityException | IllegalArgumentException | |||
| | IllegalAccessException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| return result; | |||
| } | |||
| } | |||
| @@ -1,98 +0,0 @@ | |||
| package me.chanjar.weixin.common.util; | |||
| /** | |||
| * copy from apache-commons-lang3 | |||
| */ | |||
| public class StringUtils { | |||
| /** | |||
| * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p> | |||
| * <p> | |||
| * <pre> | |||
| * StringUtils.isBlank(null) = true | |||
| * StringUtils.isBlank("") = true | |||
| * StringUtils.isBlank(" ") = true | |||
| * StringUtils.isBlank("bob") = false | |||
| * StringUtils.isBlank(" bob ") = false | |||
| * </pre> | |||
| * | |||
| * @param cs the CharSequence to check, may be null | |||
| * @return {@code true} if the CharSequence is null, empty or whitespace | |||
| * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence) | |||
| */ | |||
| public static boolean isBlank(CharSequence cs) { | |||
| int strLen; | |||
| if (cs == null || (strLen = cs.length()) == 0) { | |||
| return true; | |||
| } | |||
| for (int i = 0; i < strLen; i++) { | |||
| if (Character.isWhitespace(cs.charAt(i)) == false) { | |||
| return false; | |||
| } | |||
| } | |||
| return true; | |||
| } | |||
| /** | |||
| * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p> | |||
| * <p> | |||
| * <pre> | |||
| * StringUtils.isNotBlank(null) = false | |||
| * StringUtils.isNotBlank("") = false | |||
| * StringUtils.isNotBlank(" ") = false | |||
| * StringUtils.isNotBlank("bob") = true | |||
| * StringUtils.isNotBlank(" bob ") = true | |||
| * </pre> | |||
| * | |||
| * @param cs the CharSequence to check, may be null | |||
| * @return {@code true} if the CharSequence is | |||
| * not empty and not null and not whitespace | |||
| * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence) | |||
| */ | |||
| public static boolean isNotBlank(CharSequence cs) { | |||
| return !StringUtils.isBlank(cs); | |||
| } | |||
| /** | |||
| * <p>Checks if a CharSequence is empty ("") or null.</p> | |||
| * <p> | |||
| * <pre> | |||
| * StringUtils.isEmpty(null) = true | |||
| * StringUtils.isEmpty("") = true | |||
| * StringUtils.isEmpty(" ") = false | |||
| * StringUtils.isEmpty("bob") = false | |||
| * StringUtils.isEmpty(" bob ") = false | |||
| * </pre> | |||
| * <p> | |||
| * <p>NOTE: This method changed in Lang version 2.0. | |||
| * It no longer trims the CharSequence. | |||
| * That functionality is available in isBlank().</p> | |||
| * | |||
| * @param cs the CharSequence to check, may be null | |||
| * @return {@code true} if the CharSequence is empty or null | |||
| * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence) | |||
| */ | |||
| public static boolean isEmpty(CharSequence cs) { | |||
| return cs == null || cs.length() == 0; | |||
| } | |||
| /** | |||
| * <p>Checks if a CharSequence is not empty ("") and not null.</p> | |||
| * <p> | |||
| * <pre> | |||
| * StringUtils.isNotEmpty(null) = false | |||
| * StringUtils.isNotEmpty("") = false | |||
| * StringUtils.isNotEmpty(" ") = true | |||
| * StringUtils.isNotEmpty("bob") = true | |||
| * StringUtils.isNotEmpty(" bob ") = true | |||
| * </pre> | |||
| * | |||
| * @param cs the CharSequence to check, may be null | |||
| * @return {@code true} if the CharSequence is not empty and not null | |||
| * @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence) | |||
| */ | |||
| public static boolean isNotEmpty(CharSequence cs) { | |||
| return !StringUtils.isEmpty(cs); | |||
| } | |||
| } | |||
| @@ -0,0 +1,61 @@ | |||
| package me.chanjar.weixin.common.util; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| /** | |||
| * <pre> | |||
| * 自定义的ToString方法,用于产生去掉空值属性的字符串 | |||
| * Created by Binary Wang on 2016-10-27. | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * </pre> | |||
| */ | |||
| public class ToStringUtils { | |||
| public static final ToStringStyle THE_STYLE = new SimpleMultiLineToStringStyle(); | |||
| private static class SimpleMultiLineToStringStyle extends ToStringStyle { | |||
| private static final long serialVersionUID = 4645306494220335355L; | |||
| private static final String LINE_SEPARATOR = "\n"; | |||
| private static final String NULL_TEXT = "<null>"; | |||
| public SimpleMultiLineToStringStyle() { | |||
| super(); | |||
| this.setContentStart("["); | |||
| this.setFieldSeparator(LINE_SEPARATOR + " "); | |||
| this.setFieldSeparatorAtStart(true); | |||
| this.setContentEnd(LINE_SEPARATOR + "]"); | |||
| this.setNullText(NULL_TEXT); | |||
| this.setUseShortClassName(true); | |||
| this.setUseIdentityHashCode(false); | |||
| } | |||
| } | |||
| /** | |||
| * 用于产生去掉空值属性并以换行符分割各属性键值的toString字符串 | |||
| * @param obj | |||
| */ | |||
| public static String toSimpleString(Object obj) { | |||
| String toStringResult = ToStringBuilder.reflectionToString(obj, THE_STYLE); | |||
| String[] split = toStringResult.split(SimpleMultiLineToStringStyle.LINE_SEPARATOR); | |||
| StringBuilder result = new StringBuilder(); | |||
| for (String string : split) { | |||
| if (string.endsWith(SimpleMultiLineToStringStyle.NULL_TEXT)) { | |||
| continue; | |||
| } | |||
| result.append(string + SimpleMultiLineToStringStyle.LINE_SEPARATOR); | |||
| } | |||
| if (result.length() == 0) { | |||
| return ""; | |||
| } | |||
| //如果没有非空的属性,就输出 <all null properties> | |||
| if (StringUtils.countMatches(result, SimpleMultiLineToStringStyle.LINE_SEPARATOR) == 2) { | |||
| return result.toString().split(SimpleMultiLineToStringStyle.LINE_SEPARATOR)[0] | |||
| + "<all null values>]"; | |||
| } | |||
| return result.deleteCharAt(result.length() - 1).toString(); | |||
| } | |||
| } | |||
| @@ -1,300 +1,300 @@ | |||
| /** | |||
| * 对公众平台发送给公众账号的消息加解密示例代码. | |||
| * | |||
| * @copyright Copyright (c) 1998-2014 Tencent Inc. | |||
| * <p> | |||
| * 针对org.apache.commons.codec.binary.Base64, | |||
| * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) | |||
| * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi | |||
| */ | |||
| // ------------------------------------------------------------------------ | |||
| /** | |||
| * 针对org.apache.commons.codec.binary.Base64, | |||
| * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) | |||
| * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi | |||
| */ | |||
| package me.chanjar.weixin.common.util.crypto; | |||
| import java.io.StringReader; | |||
| import java.nio.charset.Charset; | |||
| import java.util.Arrays; | |||
| import java.util.Random; | |||
| import javax.crypto.Cipher; | |||
| import javax.crypto.spec.IvParameterSpec; | |||
| import javax.crypto.spec.SecretKeySpec; | |||
| import javax.xml.parsers.DocumentBuilder; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import org.apache.commons.codec.binary.Base64; | |||
| import org.w3c.dom.Document; | |||
| import org.w3c.dom.Element; | |||
| import org.xml.sax.InputSource; | |||
| public class WxCryptUtil { | |||
| private static final Base64 base64 = new Base64(); | |||
| private static final Charset CHARSET = Charset.forName("utf-8"); | |||
| private static final ThreadLocal<DocumentBuilder> builderLocal = new ThreadLocal<DocumentBuilder>() { | |||
| @Override | |||
| protected DocumentBuilder initialValue() { | |||
| try { | |||
| return DocumentBuilderFactory.newInstance().newDocumentBuilder(); | |||
| } catch (ParserConfigurationException exc) { | |||
| throw new IllegalArgumentException(exc); | |||
| } | |||
| } | |||
| }; | |||
| protected byte[] aesKey; | |||
| protected String token; | |||
| protected String appidOrCorpid; | |||
| public WxCryptUtil() { | |||
| super(); | |||
| } | |||
| /** | |||
| * 构造函数 | |||
| * | |||
| * @param token 公众平台上,开发者设置的token | |||
| * @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey | |||
| * @param appidOrCorpid 公众平台appid/corpid | |||
| */ | |||
| public WxCryptUtil(String token, String encodingAesKey, | |||
| String appidOrCorpid) { | |||
| this.token = token; | |||
| this.appidOrCorpid = appidOrCorpid; | |||
| this.aesKey = Base64.decodeBase64(encodingAesKey + "="); | |||
| } | |||
| static String extractEncryptPart(String xml) { | |||
| try { | |||
| DocumentBuilder db = builderLocal.get(); | |||
| Document document = db.parse(new InputSource(new StringReader(xml))); | |||
| Element root = document.getDocumentElement(); | |||
| return root.getElementsByTagName("Encrypt").item(0).getTextContent(); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| } | |||
| /** | |||
| * 将公众平台回复用户的消息加密打包. | |||
| * <ol> | |||
| * <li>对要发送的消息进行AES-CBC加密</li> | |||
| * <li>生成安全签名</li> | |||
| * <li>将消息密文和安全签名打包成xml格式</li> | |||
| * </ol> | |||
| * | |||
| * @param plainText 公众平台待回复用户的消息,xml格式的字符串 | |||
| * @return 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串 | |||
| */ | |||
| public String encrypt(String plainText) { | |||
| // 加密 | |||
| String encryptedXml = encrypt(genRandomStr(), plainText); | |||
| // 生成安全签名 | |||
| String timeStamp = Long.toString(System.currentTimeMillis() / 1000l); | |||
| String nonce = genRandomStr(); | |||
| String signature = SHA1.gen(this.token, timeStamp, nonce, encryptedXml); | |||
| String result = generateXml(encryptedXml, signature, timeStamp, nonce); | |||
| return result; | |||
| } | |||
| /** | |||
| * 对明文进行加密. | |||
| * | |||
| * @param plainText 需要加密的明文 | |||
| * @return 加密后base64编码的字符串 | |||
| */ | |||
| protected String encrypt(String randomStr, String plainText) { | |||
| ByteGroup byteCollector = new ByteGroup(); | |||
| byte[] randomStringBytes = randomStr.getBytes(CHARSET); | |||
| byte[] plainTextBytes = plainText.getBytes(CHARSET); | |||
| byte[] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder( | |||
| plainTextBytes.length); | |||
| byte[] appIdBytes = this.appidOrCorpid.getBytes(CHARSET); | |||
| // randomStr + networkBytesOrder + text + appid | |||
| byteCollector.addBytes(randomStringBytes); | |||
| byteCollector.addBytes(bytesOfSizeInNetworkOrder); | |||
| byteCollector.addBytes(plainTextBytes); | |||
| byteCollector.addBytes(appIdBytes); | |||
| // ... + pad: 使用自定义的填充方式对明文进行补位填充 | |||
| byte[] padBytes = PKCS7Encoder.encode(byteCollector.size()); | |||
| byteCollector.addBytes(padBytes); | |||
| // 获得最终的字节流, 未加密 | |||
| byte[] unencrypted = byteCollector.toBytes(); | |||
| try { | |||
| // 设置加密模式为AES的CBC模式 | |||
| Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); | |||
| SecretKeySpec keySpec = new SecretKeySpec(this.aesKey, "AES"); | |||
| IvParameterSpec iv = new IvParameterSpec(this.aesKey, 0, 16); | |||
| cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); | |||
| // 加密 | |||
| byte[] encrypted = cipher.doFinal(unencrypted); | |||
| // 使用BASE64对加密后的字符串进行编码 | |||
| String base64Encrypted = base64.encodeToString(encrypted); | |||
| return base64Encrypted; | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| } | |||
| /** | |||
| * 检验消息的真实性,并且获取解密后的明文. | |||
| * <ol> | |||
| * <li>利用收到的密文生成安全签名,进行签名验证</li> | |||
| * <li>若验证通过,则提取xml中的加密消息</li> | |||
| * <li>对消息进行解密</li> | |||
| * </ol> | |||
| * | |||
| * @param msgSignature 签名串,对应URL参数的msg_signature | |||
| * @param timeStamp 时间戳,对应URL参数的timestamp | |||
| * @param nonce 随机串,对应URL参数的nonce | |||
| * @param encryptedXml 密文,对应POST请求的数据 | |||
| * @return 解密后的原文 | |||
| */ | |||
| public String decrypt(String msgSignature, String timeStamp, String nonce, | |||
| String encryptedXml) { | |||
| // 密钥,公众账号的app corpSecret | |||
| // 提取密文 | |||
| String cipherText = extractEncryptPart(encryptedXml); | |||
| // 验证安全签名 | |||
| String signature = SHA1.gen(this.token, timeStamp, nonce, cipherText); | |||
| if (!signature.equals(msgSignature)) { | |||
| throw new RuntimeException("加密消息签名校验失败"); | |||
| } | |||
| // 解密 | |||
| String result = decrypt(cipherText); | |||
| return result; | |||
| } | |||
| /** | |||
| * 对密文进行解密. | |||
| * | |||
| * @param cipherText 需要解密的密文 | |||
| * @return 解密得到的明文 | |||
| */ | |||
| public String decrypt(String cipherText) { | |||
| byte[] original; | |||
| try { | |||
| // 设置解密模式为AES的CBC模式 | |||
| Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); | |||
| SecretKeySpec key_spec = new SecretKeySpec(this.aesKey, "AES"); | |||
| IvParameterSpec iv = new IvParameterSpec( | |||
| Arrays.copyOfRange(this.aesKey, 0, 16)); | |||
| cipher.init(Cipher.DECRYPT_MODE, key_spec, iv); | |||
| // 使用BASE64对密文进行解码 | |||
| byte[] encrypted = Base64.decodeBase64(cipherText); | |||
| // 解密 | |||
| original = cipher.doFinal(encrypted); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| String xmlContent, from_appid; | |||
| try { | |||
| // 去除补位字符 | |||
| byte[] bytes = PKCS7Encoder.decode(original); | |||
| // 分离16位随机字符串,网络字节序和AppId | |||
| byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20); | |||
| int xmlLength = bytesNetworkOrder2Number(networkOrder); | |||
| xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), | |||
| CHARSET); | |||
| from_appid = new String( | |||
| Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| // appid不相同的情况 | |||
| if (!from_appid.equals(this.appidOrCorpid)) { | |||
| throw new RuntimeException("AppID不正确"); | |||
| } | |||
| return xmlContent; | |||
| } | |||
| /** | |||
| * 将一个数字转换成生成4个字节的网络字节序bytes数组 | |||
| * | |||
| * @param number | |||
| */ | |||
| private static byte[] number2BytesInNetworkOrder(int number) { | |||
| byte[] orderBytes = new byte[4]; | |||
| orderBytes[3] = (byte) (number & 0xFF); | |||
| orderBytes[2] = (byte) (number >> 8 & 0xFF); | |||
| orderBytes[1] = (byte) (number >> 16 & 0xFF); | |||
| orderBytes[0] = (byte) (number >> 24 & 0xFF); | |||
| return orderBytes; | |||
| } | |||
| /** | |||
| * 4个字节的网络字节序bytes数组还原成一个数字 | |||
| * | |||
| * @param bytesInNetworkOrder | |||
| */ | |||
| private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) { | |||
| int sourceNumber = 0; | |||
| for (int i = 0; i < 4; i++) { | |||
| sourceNumber <<= 8; | |||
| sourceNumber |= bytesInNetworkOrder[i] & 0xff; | |||
| } | |||
| return sourceNumber; | |||
| } | |||
| /** | |||
| * 随机生成16位字符串 | |||
| */ | |||
| private static String genRandomStr() { | |||
| String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |||
| Random random = new Random(); | |||
| StringBuffer sb = new StringBuffer(); | |||
| for (int i = 0; i < 16; i++) { | |||
| int number = random.nextInt(base.length()); | |||
| sb.append(base.charAt(number)); | |||
| } | |||
| return sb.toString(); | |||
| } | |||
| /** | |||
| * 生成xml消息 | |||
| * | |||
| * @param encrypt 加密后的消息密文 | |||
| * @param signature 安全签名 | |||
| * @param timestamp 时间戳 | |||
| * @param nonce 随机字符串 | |||
| * @return 生成的xml字符串 | |||
| */ | |||
| private static String generateXml(String encrypt, String signature, | |||
| String timestamp, String nonce) { | |||
| String format = "<xml>\n" + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n" | |||
| + "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n" | |||
| + "<TimeStamp>%3$s</TimeStamp>\n" + "<Nonce><![CDATA[%4$s]]></Nonce>\n" | |||
| + "</xml>"; | |||
| return String.format(format, encrypt, signature, timestamp, nonce); | |||
| } | |||
| } | |||
| /** | |||
| * 对公众平台发送给公众账号的消息加解密示例代码. | |||
| * | |||
| * @copyright Copyright (c) 1998-2014 Tencent Inc. | |||
| * <p> | |||
| * 针对org.apache.commons.codec.binary.Base64, | |||
| * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) | |||
| * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi | |||
| */ | |||
| // ------------------------------------------------------------------------ | |||
| /** | |||
| * 针对org.apache.commons.codec.binary.Base64, | |||
| * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) | |||
| * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi | |||
| */ | |||
| package me.chanjar.weixin.common.util.crypto; | |||
| import java.io.StringReader; | |||
| import java.nio.charset.Charset; | |||
| import java.util.Arrays; | |||
| import java.util.Random; | |||
| import javax.crypto.Cipher; | |||
| import javax.crypto.spec.IvParameterSpec; | |||
| import javax.crypto.spec.SecretKeySpec; | |||
| import javax.xml.parsers.DocumentBuilder; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import org.apache.commons.codec.binary.Base64; | |||
| import org.w3c.dom.Document; | |||
| import org.w3c.dom.Element; | |||
| import org.xml.sax.InputSource; | |||
| public class WxCryptUtil { | |||
| private static final Base64 base64 = new Base64(); | |||
| private static final Charset CHARSET = Charset.forName("utf-8"); | |||
| private static final ThreadLocal<DocumentBuilder> builderLocal = new ThreadLocal<DocumentBuilder>() { | |||
| @Override | |||
| protected DocumentBuilder initialValue() { | |||
| try { | |||
| return DocumentBuilderFactory.newInstance().newDocumentBuilder(); | |||
| } catch (ParserConfigurationException exc) { | |||
| throw new IllegalArgumentException(exc); | |||
| } | |||
| } | |||
| }; | |||
| protected byte[] aesKey; | |||
| protected String token; | |||
| protected String appidOrCorpid; | |||
| public WxCryptUtil() { | |||
| super(); | |||
| } | |||
| /** | |||
| * 构造函数 | |||
| * | |||
| * @param token 公众平台上,开发者设置的token | |||
| * @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey | |||
| * @param appidOrCorpid 公众平台appid/corpid | |||
| */ | |||
| public WxCryptUtil(String token, String encodingAesKey, | |||
| String appidOrCorpid) { | |||
| this.token = token; | |||
| this.appidOrCorpid = appidOrCorpid; | |||
| this.aesKey = Base64.decodeBase64(encodingAesKey + "="); | |||
| } | |||
| static String extractEncryptPart(String xml) { | |||
| try { | |||
| DocumentBuilder db = builderLocal.get(); | |||
| Document document = db.parse(new InputSource(new StringReader(xml))); | |||
| Element root = document.getDocumentElement(); | |||
| return root.getElementsByTagName("Encrypt").item(0).getTextContent(); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| } | |||
| /** | |||
| * 将公众平台回复用户的消息加密打包. | |||
| * <ol> | |||
| * <li>对要发送的消息进行AES-CBC加密</li> | |||
| * <li>生成安全签名</li> | |||
| * <li>将消息密文和安全签名打包成xml格式</li> | |||
| * </ol> | |||
| * | |||
| * @param plainText 公众平台待回复用户的消息,xml格式的字符串 | |||
| * @return 加密后的可以直接回复用户的密文,包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串 | |||
| */ | |||
| public String encrypt(String plainText) { | |||
| // 加密 | |||
| String encryptedXml = encrypt(genRandomStr(), plainText); | |||
| // 生成安全签名 | |||
| String timeStamp = Long.toString(System.currentTimeMillis() / 1000l); | |||
| String nonce = genRandomStr(); | |||
| String signature = SHA1.gen(this.token, timeStamp, nonce, encryptedXml); | |||
| String result = generateXml(encryptedXml, signature, timeStamp, nonce); | |||
| return result; | |||
| } | |||
| /** | |||
| * 对明文进行加密. | |||
| * | |||
| * @param plainText 需要加密的明文 | |||
| * @return 加密后base64编码的字符串 | |||
| */ | |||
| protected String encrypt(String randomStr, String plainText) { | |||
| ByteGroup byteCollector = new ByteGroup(); | |||
| byte[] randomStringBytes = randomStr.getBytes(CHARSET); | |||
| byte[] plainTextBytes = plainText.getBytes(CHARSET); | |||
| byte[] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder( | |||
| plainTextBytes.length); | |||
| byte[] appIdBytes = this.appidOrCorpid.getBytes(CHARSET); | |||
| // randomStr + networkBytesOrder + text + appid | |||
| byteCollector.addBytes(randomStringBytes); | |||
| byteCollector.addBytes(bytesOfSizeInNetworkOrder); | |||
| byteCollector.addBytes(plainTextBytes); | |||
| byteCollector.addBytes(appIdBytes); | |||
| // ... + pad: 使用自定义的填充方式对明文进行补位填充 | |||
| byte[] padBytes = PKCS7Encoder.encode(byteCollector.size()); | |||
| byteCollector.addBytes(padBytes); | |||
| // 获得最终的字节流, 未加密 | |||
| byte[] unencrypted = byteCollector.toBytes(); | |||
| try { | |||
| // 设置加密模式为AES的CBC模式 | |||
| Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); | |||
| SecretKeySpec keySpec = new SecretKeySpec(this.aesKey, "AES"); | |||
| IvParameterSpec iv = new IvParameterSpec(this.aesKey, 0, 16); | |||
| cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); | |||
| // 加密 | |||
| byte[] encrypted = cipher.doFinal(unencrypted); | |||
| // 使用BASE64对加密后的字符串进行编码 | |||
| String base64Encrypted = base64.encodeToString(encrypted); | |||
| return base64Encrypted; | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| } | |||
| /** | |||
| * 检验消息的真实性,并且获取解密后的明文. | |||
| * <ol> | |||
| * <li>利用收到的密文生成安全签名,进行签名验证</li> | |||
| * <li>若验证通过,则提取xml中的加密消息</li> | |||
| * <li>对消息进行解密</li> | |||
| * </ol> | |||
| * | |||
| * @param msgSignature 签名串,对应URL参数的msg_signature | |||
| * @param timeStamp 时间戳,对应URL参数的timestamp | |||
| * @param nonce 随机串,对应URL参数的nonce | |||
| * @param encryptedXml 密文,对应POST请求的数据 | |||
| * @return 解密后的原文 | |||
| */ | |||
| public String decrypt(String msgSignature, String timeStamp, String nonce, | |||
| String encryptedXml) { | |||
| // 密钥,公众账号的app corpSecret | |||
| // 提取密文 | |||
| String cipherText = extractEncryptPart(encryptedXml); | |||
| // 验证安全签名 | |||
| String signature = SHA1.gen(this.token, timeStamp, nonce, cipherText); | |||
| if (!signature.equals(msgSignature)) { | |||
| throw new RuntimeException("加密消息签名校验失败"); | |||
| } | |||
| // 解密 | |||
| String result = decrypt(cipherText); | |||
| return result; | |||
| } | |||
| /** | |||
| * 对密文进行解密. | |||
| * | |||
| * @param cipherText 需要解密的密文 | |||
| * @return 解密得到的明文 | |||
| */ | |||
| public String decrypt(String cipherText) { | |||
| byte[] original; | |||
| try { | |||
| // 设置解密模式为AES的CBC模式 | |||
| Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); | |||
| SecretKeySpec key_spec = new SecretKeySpec(this.aesKey, "AES"); | |||
| IvParameterSpec iv = new IvParameterSpec( | |||
| Arrays.copyOfRange(this.aesKey, 0, 16)); | |||
| cipher.init(Cipher.DECRYPT_MODE, key_spec, iv); | |||
| // 使用BASE64对密文进行解码 | |||
| byte[] encrypted = Base64.decodeBase64(cipherText); | |||
| // 解密 | |||
| original = cipher.doFinal(encrypted); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| String xmlContent, from_appid; | |||
| try { | |||
| // 去除补位字符 | |||
| byte[] bytes = PKCS7Encoder.decode(original); | |||
| // 分离16位随机字符串,网络字节序和AppId | |||
| byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20); | |||
| int xmlLength = bytesNetworkOrder2Number(networkOrder); | |||
| xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), | |||
| CHARSET); | |||
| from_appid = new String( | |||
| Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET); | |||
| } catch (Exception e) { | |||
| throw new RuntimeException(e); | |||
| } | |||
| // appid不相同的情况 | |||
| if (!from_appid.equals(this.appidOrCorpid)) { | |||
| throw new RuntimeException("AppID不正确"); | |||
| } | |||
| return xmlContent; | |||
| } | |||
| /** | |||
| * 将一个数字转换成生成4个字节的网络字节序bytes数组 | |||
| * | |||
| * @param number | |||
| */ | |||
| private static byte[] number2BytesInNetworkOrder(int number) { | |||
| byte[] orderBytes = new byte[4]; | |||
| orderBytes[3] = (byte) (number & 0xFF); | |||
| orderBytes[2] = (byte) (number >> 8 & 0xFF); | |||
| orderBytes[1] = (byte) (number >> 16 & 0xFF); | |||
| orderBytes[0] = (byte) (number >> 24 & 0xFF); | |||
| return orderBytes; | |||
| } | |||
| /** | |||
| * 4个字节的网络字节序bytes数组还原成一个数字 | |||
| * | |||
| * @param bytesInNetworkOrder | |||
| */ | |||
| private static int bytesNetworkOrder2Number(byte[] bytesInNetworkOrder) { | |||
| int sourceNumber = 0; | |||
| for (int i = 0; i < 4; i++) { | |||
| sourceNumber <<= 8; | |||
| sourceNumber |= bytesInNetworkOrder[i] & 0xff; | |||
| } | |||
| return sourceNumber; | |||
| } | |||
| /** | |||
| * 随机生成16位字符串 | |||
| */ | |||
| private static String genRandomStr() { | |||
| String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |||
| Random random = new Random(); | |||
| StringBuffer sb = new StringBuffer(); | |||
| for (int i = 0; i < 16; i++) { | |||
| int number = random.nextInt(base.length()); | |||
| sb.append(base.charAt(number)); | |||
| } | |||
| return sb.toString(); | |||
| } | |||
| /** | |||
| * 生成xml消息 | |||
| * | |||
| * @param encrypt 加密后的消息密文 | |||
| * @param signature 安全签名 | |||
| * @param timestamp 时间戳 | |||
| * @param nonce 随机字符串 | |||
| * @return 生成的xml字符串 | |||
| */ | |||
| private static String generateXml(String encrypt, String signature, | |||
| String timestamp, String nonce) { | |||
| String format = "<xml>\n" + "<Encrypt><![CDATA[%1$s]]></Encrypt>\n" | |||
| + "<MsgSignature><![CDATA[%2$s]]></MsgSignature>\n" | |||
| + "<TimeStamp>%3$s</TimeStamp>\n" + "<Nonce><![CDATA[%4$s]]></Nonce>\n" | |||
| + "</xml>"; | |||
| return String.format(format, encrypt, signature, timestamp, nonce); | |||
| } | |||
| } | |||
| @@ -5,6 +5,7 @@ import org.apache.http.impl.client.CloseableHttpClient; | |||
| /** | |||
| * httpclient build interface | |||
| * @author kakotor | |||
| */ | |||
| public interface ApacheHttpClientBuilder { | |||
| @@ -1,6 +1,6 @@ | |||
| package me.chanjar.weixin.common.util.http; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.http.annotation.NotThreadSafe; | |||
| import org.apache.http.auth.AuthScope; | |||
| import org.apache.http.auth.UsernamePasswordCredentials; | |||
| @@ -20,15 +20,22 @@ import org.apache.http.impl.client.HttpClientBuilder; | |||
| import org.apache.http.impl.client.HttpClients; | |||
| import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |||
| import org.apache.http.protocol.HttpContext; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.io.IOException; | |||
| import java.util.concurrent.TimeUnit; | |||
| import java.util.concurrent.atomic.AtomicBoolean; | |||
| /** | |||
| * httpclient 连接管理器 | |||
| * | |||
| * @author kakotor | |||
| */ | |||
| @NotThreadSafe | |||
| public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | |||
| protected final Logger log = LoggerFactory.getLogger(DefaultApacheHttpClientBuilder.class); | |||
| private final AtomicBoolean prepared = new AtomicBoolean(false); | |||
| private int connectionRequestTimeout = 3000; | |||
| private int connectionTimeout = 5000; | |||
| private int soTimeout = 5000; | |||
| @@ -45,21 +52,16 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | |||
| }; | |||
| private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory(); | |||
| private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory(); | |||
| private String httpProxyHost; | |||
| private int httpProxyPort; | |||
| private String httpProxyUsername; | |||
| private String httpProxyPassword; | |||
| /** | |||
| * 闲置连接监控线程 | |||
| */ | |||
| private IdleConnectionMonitorThread idleConnectionMonitorThread; | |||
| private HttpClientBuilder httpClientBuilder; | |||
| private boolean prepared = false; | |||
| private DefaultApacheHttpClientBuilder() { | |||
| } | |||
| @@ -97,11 +99,98 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | |||
| return this; | |||
| } | |||
| /** | |||
| * 获取链接的超时时间设置,默认3000ms | |||
| * <p> | |||
| * 设置为零时不超时,一直等待. | |||
| * 设置为负数是使用系统默认设置(非上述的3000ms的默认值,而是httpclient的默认设置). | |||
| * </p> | |||
| * | |||
| * @param connectionRequestTimeout 获取链接的超时时间设置(单位毫秒),默认3000ms | |||
| */ | |||
| public void setConnectionRequestTimeout(int connectionRequestTimeout) { | |||
| this.connectionRequestTimeout = connectionRequestTimeout; | |||
| } | |||
| /** | |||
| * 建立链接的超时时间,默认为5000ms.由于是在链接池获取链接,此设置应该并不起什么作用 | |||
| * <p> | |||
| * 设置为零时不超时,一直等待. | |||
| * 设置为负数是使用系统默认设置(非上述的5000ms的默认值,而是httpclient的默认设置). | |||
| * </p> | |||
| * | |||
| * @param connectionTimeout 建立链接的超时时间设置(单位毫秒),默认5000ms | |||
| */ | |||
| public void setConnectionTimeout(int connectionTimeout) { | |||
| this.connectionTimeout = connectionTimeout; | |||
| } | |||
| /** | |||
| * 默认NIO的socket超时设置,默认5000ms. | |||
| * | |||
| * @param soTimeout 默认NIO的socket超时设置,默认5000ms. | |||
| * @see java.net.SocketOptions#SO_TIMEOUT | |||
| */ | |||
| public void setSoTimeout(int soTimeout) { | |||
| this.soTimeout = soTimeout; | |||
| } | |||
| /** | |||
| * 空闲链接的超时时间,默认60000ms. | |||
| * <p> | |||
| * 超时的链接将在下一次空闲链接检查是被销毁 | |||
| * </p> | |||
| * | |||
| * @param idleConnTimeout 空闲链接的超时时间,默认60000ms. | |||
| */ | |||
| public void setIdleConnTimeout(int idleConnTimeout) { | |||
| this.idleConnTimeout = idleConnTimeout; | |||
| } | |||
| /** | |||
| * 检查空间链接的间隔周期,默认60000ms. | |||
| * | |||
| * @param checkWaitTime 检查空间链接的间隔周期,默认60000ms. | |||
| */ | |||
| public void setCheckWaitTime(int checkWaitTime) { | |||
| this.checkWaitTime = checkWaitTime; | |||
| } | |||
| /** | |||
| * 每路的最大链接数,默认10 | |||
| * | |||
| * @param maxConnPerHost 每路的最大链接数,默认10 | |||
| */ | |||
| public void setMaxConnPerHost(int maxConnPerHost) { | |||
| this.maxConnPerHost = maxConnPerHost; | |||
| } | |||
| /** | |||
| * 最大总连接数,默认50 | |||
| * | |||
| * @param maxTotalConn 最大总连接数,默认50 | |||
| */ | |||
| public void setMaxTotalConn(int maxTotalConn) { | |||
| this.maxTotalConn = maxTotalConn; | |||
| } | |||
| /** | |||
| * 自定义httpclient的User Agent | |||
| * | |||
| * @param userAgent User Agent | |||
| */ | |||
| public void setUserAgent(String userAgent) { | |||
| this.userAgent = userAgent; | |||
| } | |||
| public IdleConnectionMonitorThread getIdleConnectionMonitorThread() { | |||
| return this.idleConnectionMonitorThread; | |||
| } | |||
| private void prepare() { | |||
| private synchronized void prepare() { | |||
| if(prepared.get()){ | |||
| return; | |||
| } | |||
| Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() | |||
| .register("http", this.plainConnectionSocketFactory) | |||
| .register("https", this.sslConnectionSocketFactory) | |||
| @@ -148,16 +237,14 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { | |||
| if (StringUtils.isNotBlank(this.userAgent)) { | |||
| this.httpClientBuilder.setUserAgent(this.userAgent); | |||
| } | |||
| prepared.set(true); | |||
| } | |||
| @Override | |||
| public CloseableHttpClient build() { | |||
| if (!this.prepared) { | |||
| if(!prepared.get()){ | |||
| prepare(); | |||
| this.prepared = true; | |||
| } | |||
| return this.httpClientBuilder.build(); | |||
| } | |||
| @@ -1,55 +0,0 @@ | |||
| package me.chanjar.weixin.common.util.http; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import jodd.http.net.SocketHttpConnectionProvider; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| /** | |||
| * 简单的GET请求执行器,请求的参数是String, 返回的结果也是String | |||
| * | |||
| * @author Daniel Qian | |||
| */ | |||
| public class JoddGetRequestExecutor implements RequestExecutor<String, String> { | |||
| @Override | |||
| public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| String queryParam) throws WxErrorException { | |||
| if (queryParam != null) { | |||
| if (uri.indexOf('?') == -1) { | |||
| uri += '?'; | |||
| } | |||
| uri += uri.endsWith("?") ? queryParam : '&' + queryParam; | |||
| } | |||
| SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); | |||
| if (httpProxy != null) { | |||
| ProxyInfo proxyInfoObj = new ProxyInfo( | |||
| ProxyInfo.ProxyType.HTTP, | |||
| httpProxy.getHostName(), | |||
| httpProxy.getPort(), "", ""); | |||
| provider.useProxy(proxyInfoObj); | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| request.method("GET"); | |||
| request.charset("UTF-8"); | |||
| HttpResponse response = request.open(provider).send(); | |||
| response.charset("UTF-8"); | |||
| String result = response.bodyText(); | |||
| WxError error = WxError.fromJson(result); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return result; | |||
| } | |||
| } | |||
| @@ -1,49 +0,0 @@ | |||
| package me.chanjar.weixin.common.util.http; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import jodd.http.HttpRequest; | |||
| import jodd.http.HttpResponse; | |||
| import jodd.http.ProxyInfo; | |||
| import jodd.http.net.SocketHttpConnectionProvider; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| /** | |||
| * 简单的POST请求执行器,请求的参数是String, 返回的结果也是String | |||
| * | |||
| * @author Edison Guo | |||
| */ | |||
| public class JoddPostRequestExecutor implements RequestExecutor<String, String> { | |||
| @Override | |||
| public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, | |||
| String postEntity) throws WxErrorException { | |||
| SocketHttpConnectionProvider provider = new SocketHttpConnectionProvider(); | |||
| if (httpProxy != null) { | |||
| ProxyInfo proxyInfoObj = new ProxyInfo( | |||
| ProxyInfo.ProxyType.HTTP, | |||
| httpProxy.getAddress().getHostAddress(), | |||
| httpProxy.getPort(), "", ""); | |||
| provider.useProxy(proxyInfoObj); | |||
| } | |||
| HttpRequest request = HttpRequest.get(uri); | |||
| request.method("POST"); | |||
| request.charset("UTF-8"); | |||
| request.bodyText(postEntity); | |||
| HttpResponse response = request.open(provider).send(); | |||
| response.charset("UTF-8"); | |||
| String result = response.bodyText(); | |||
| WxError error = WxError.fromJson(result); | |||
| if (error.getErrorCode() != 0) { | |||
| throw new WxErrorException(error); | |||
| } | |||
| return result; | |||
| } | |||
| } | |||
| @@ -2,8 +2,8 @@ package me.chanjar.weixin.common.util.http; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.http.Header; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| @@ -1,6 +1,7 @@ | |||
| package me.chanjar.weixin.common.util.http; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import java.io.UnsupportedEncodingException; | |||
| @@ -1,103 +1,103 @@ | |||
| package me.chanjar.weixin.common.util.crypto; | |||
| import org.testng.annotations.Test; | |||
| import org.w3c.dom.Document; | |||
| import org.w3c.dom.Element; | |||
| import org.w3c.dom.NodeList; | |||
| import org.xml.sax.InputSource; | |||
| import org.xml.sax.SAXException; | |||
| import javax.xml.parsers.DocumentBuilder; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import java.io.IOException; | |||
| import java.io.StringReader; | |||
| import static org.testng.Assert.assertEquals; | |||
| import static org.testng.Assert.fail; | |||
| @Test | |||
| public class WxCryptUtilTest { | |||
| String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; | |||
| String token = "pamtest"; | |||
| String timestamp = "1409304348"; | |||
| String nonce = "xxxxxx"; | |||
| String appId = "wxb11529c136998cb6"; | |||
| String randomStr = "aaaabbbbccccdddd"; | |||
| String xmlFormat = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>"; | |||
| String replyMsg = "我是中文abcd123"; | |||
| String afterAesEncrypt = "jn1L23DB+6ELqJ+6bruv21Y6MD7KeIfP82D6gU39rmkgczbWwt5+3bnyg5K55bgVtVzd832WzZGMhkP72vVOfg=="; | |||
| String replyMsg2 = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>"; | |||
| String afterAesEncrypt2 = "jn1L23DB+6ELqJ+6bruv23M2GmYfkv0xBh2h+XTBOKVKcgDFHle6gqcZ1cZrk3e1qjPQ1F4RsLWzQRG9udbKWesxlkupqcEcW7ZQweImX9+wLMa0GaUzpkycA8+IamDBxn5loLgZpnS7fVAbExOkK5DYHBmv5tptA9tklE/fTIILHR8HLXa5nQvFb3tYPKAlHF3rtTeayNf0QuM+UW/wM9enGIDIJHF7CLHiDNAYxr+r+OrJCmPQyTy8cVWlu9iSvOHPT/77bZqJucQHQ04sq7KZI27OcqpQNSto2OdHCoTccjggX5Z9Mma0nMJBU+jLKJ38YB1fBIz+vBzsYjrTmFQ44YfeEuZ+xRTQwr92vhA9OxchWVINGC50qE/6lmkwWTwGX9wtQpsJKhP+oS7rvTY8+VdzETdfakjkwQ5/Xka042OlUb1/slTwo4RscuQ+RdxSGvDahxAJ6+EAjLt9d8igHngxIbf6YyqqROxuxqIeIch3CssH/LqRs+iAcILvApYZckqmA7FNERspKA5f8GoJ9sv8xmGvZ9Yrf57cExWtnX8aCMMaBropU/1k+hKP5LVdzbWCG0hGwx/dQudYR/eXp3P0XxjlFiy+9DMlaFExWUZQDajPkdPrEeOwofJb"; | |||
| public void testNormal() throws ParserConfigurationException, SAXException, IOException { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| String encryptedXml = pc.encrypt(this.replyMsg); | |||
| System.out.println(encryptedXml); | |||
| DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |||
| DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |||
| Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml))); | |||
| Element root = document.getDocumentElement(); | |||
| String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent(); | |||
| System.out.println(cipherText); | |||
| String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent(); | |||
| System.out.println(msgSignature); | |||
| String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent(); | |||
| System.out.println(timestamp); | |||
| String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent(); | |||
| System.out.println(nonce); | |||
| String messageText = String.format(this.xmlFormat, cipherText); | |||
| System.out.println(messageText); | |||
| // 第三方收到企业号平台发送的消息 | |||
| String plainMessage = pc.decrypt(cipherText); | |||
| System.out.println(plainMessage); | |||
| assertEquals(plainMessage, this.replyMsg); | |||
| } | |||
| public void testAesEncrypt() { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| assertEquals(pc.encrypt(this.randomStr, this.replyMsg), this.afterAesEncrypt); | |||
| } | |||
| public void testAesEncrypt2() { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| assertEquals(pc.encrypt(this.randomStr, this.replyMsg2), this.afterAesEncrypt2); | |||
| } | |||
| public void testValidateSignatureError() throws ParserConfigurationException, SAXException, | |||
| IOException { | |||
| try { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| String afterEncrpt = pc.encrypt(this.replyMsg); | |||
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |||
| DocumentBuilder db = dbf.newDocumentBuilder(); | |||
| StringReader sr = new StringReader(afterEncrpt); | |||
| InputSource is = new InputSource(sr); | |||
| Document document = db.parse(is); | |||
| Element root = document.getDocumentElement(); | |||
| NodeList nodelist1 = root.getElementsByTagName("Encrypt"); | |||
| String encrypt = nodelist1.item(0).getTextContent(); | |||
| String fromXML = String.format(this.xmlFormat, encrypt); | |||
| pc.decrypt("12345", this.timestamp, this.nonce, fromXML); // 这里签名错误 | |||
| } catch (RuntimeException e) { | |||
| assertEquals(e.getMessage(), "加密消息签名校验失败"); | |||
| return; | |||
| } | |||
| fail("错误流程不抛出异常???"); | |||
| } | |||
| } | |||
| package me.chanjar.weixin.common.util.crypto; | |||
| import org.testng.annotations.Test; | |||
| import org.w3c.dom.Document; | |||
| import org.w3c.dom.Element; | |||
| import org.w3c.dom.NodeList; | |||
| import org.xml.sax.InputSource; | |||
| import org.xml.sax.SAXException; | |||
| import javax.xml.parsers.DocumentBuilder; | |||
| import javax.xml.parsers.DocumentBuilderFactory; | |||
| import javax.xml.parsers.ParserConfigurationException; | |||
| import java.io.IOException; | |||
| import java.io.StringReader; | |||
| import static org.testng.Assert.assertEquals; | |||
| import static org.testng.Assert.fail; | |||
| @Test | |||
| public class WxCryptUtilTest { | |||
| String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"; | |||
| String token = "pamtest"; | |||
| String timestamp = "1409304348"; | |||
| String nonce = "xxxxxx"; | |||
| String appId = "wxb11529c136998cb6"; | |||
| String randomStr = "aaaabbbbccccdddd"; | |||
| String xmlFormat = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>"; | |||
| String replyMsg = "我是中文abcd123"; | |||
| String afterAesEncrypt = "jn1L23DB+6ELqJ+6bruv21Y6MD7KeIfP82D6gU39rmkgczbWwt5+3bnyg5K55bgVtVzd832WzZGMhkP72vVOfg=="; | |||
| String replyMsg2 = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>"; | |||
| String afterAesEncrypt2 = "jn1L23DB+6ELqJ+6bruv23M2GmYfkv0xBh2h+XTBOKVKcgDFHle6gqcZ1cZrk3e1qjPQ1F4RsLWzQRG9udbKWesxlkupqcEcW7ZQweImX9+wLMa0GaUzpkycA8+IamDBxn5loLgZpnS7fVAbExOkK5DYHBmv5tptA9tklE/fTIILHR8HLXa5nQvFb3tYPKAlHF3rtTeayNf0QuM+UW/wM9enGIDIJHF7CLHiDNAYxr+r+OrJCmPQyTy8cVWlu9iSvOHPT/77bZqJucQHQ04sq7KZI27OcqpQNSto2OdHCoTccjggX5Z9Mma0nMJBU+jLKJ38YB1fBIz+vBzsYjrTmFQ44YfeEuZ+xRTQwr92vhA9OxchWVINGC50qE/6lmkwWTwGX9wtQpsJKhP+oS7rvTY8+VdzETdfakjkwQ5/Xka042OlUb1/slTwo4RscuQ+RdxSGvDahxAJ6+EAjLt9d8igHngxIbf6YyqqROxuxqIeIch3CssH/LqRs+iAcILvApYZckqmA7FNERspKA5f8GoJ9sv8xmGvZ9Yrf57cExWtnX8aCMMaBropU/1k+hKP5LVdzbWCG0hGwx/dQudYR/eXp3P0XxjlFiy+9DMlaFExWUZQDajPkdPrEeOwofJb"; | |||
| public void testNormal() throws ParserConfigurationException, SAXException, IOException { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| String encryptedXml = pc.encrypt(this.replyMsg); | |||
| System.out.println(encryptedXml); | |||
| DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |||
| DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |||
| Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml))); | |||
| Element root = document.getDocumentElement(); | |||
| String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent(); | |||
| System.out.println(cipherText); | |||
| String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent(); | |||
| System.out.println(msgSignature); | |||
| String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent(); | |||
| System.out.println(timestamp); | |||
| String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent(); | |||
| System.out.println(nonce); | |||
| String messageText = String.format(this.xmlFormat, cipherText); | |||
| System.out.println(messageText); | |||
| // 第三方收到企业号平台发送的消息 | |||
| String plainMessage = pc.decrypt(cipherText); | |||
| System.out.println(plainMessage); | |||
| assertEquals(plainMessage, this.replyMsg); | |||
| } | |||
| public void testAesEncrypt() { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| assertEquals(pc.encrypt(this.randomStr, this.replyMsg), this.afterAesEncrypt); | |||
| } | |||
| public void testAesEncrypt2() { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| assertEquals(pc.encrypt(this.randomStr, this.replyMsg2), this.afterAesEncrypt2); | |||
| } | |||
| public void testValidateSignatureError() throws ParserConfigurationException, SAXException, | |||
| IOException { | |||
| try { | |||
| WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId); | |||
| String afterEncrpt = pc.encrypt(this.replyMsg); | |||
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |||
| DocumentBuilder db = dbf.newDocumentBuilder(); | |||
| StringReader sr = new StringReader(afterEncrpt); | |||
| InputSource is = new InputSource(sr); | |||
| Document document = db.parse(is); | |||
| Element root = document.getDocumentElement(); | |||
| NodeList nodelist1 = root.getElementsByTagName("Encrypt"); | |||
| String encrypt = nodelist1.item(0).getTextContent(); | |||
| String fromXML = String.format(this.xmlFormat, encrypt); | |||
| pc.decrypt("12345", this.timestamp, this.nonce, fromXML); // 这里签名错误 | |||
| } catch (RuntimeException e) { | |||
| assertEquals(e.getMessage(), "加密消息签名校验失败"); | |||
| return; | |||
| } | |||
| fail("错误流程不抛出异常???"); | |||
| } | |||
| } | |||
| @@ -6,7 +6,7 @@ | |||
| <parent> | |||
| <groupId>com.github.binarywang</groupId> | |||
| <artifactId>weixin-java-parent</artifactId> | |||
| <version>2.2.0</version> | |||
| <version>2.3.0</version> | |||
| </parent> | |||
| <artifactId>weixin-java-cp</artifactId> | |||
| @@ -1,13 +1,11 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import java.io.File; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
| import java.io.File; | |||
| /** | |||
| * 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | |||
| * | |||
| @@ -203,7 +201,7 @@ public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| @Override | |||
| @@ -1,28 +1,7 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.List; | |||
| import java.util.UUID; | |||
| 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.HttpGet; | |||
| import org.apache.http.impl.client.BasicResponseHandler; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.google.gson.JsonArray; | |||
| import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.JsonPrimitive; | |||
| import com.google.gson.*; | |||
| import com.google.gson.reflect.TypeToken; | |||
| import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
| import me.chanjar.weixin.common.bean.menu.WxMenu; | |||
| @@ -33,23 +12,31 @@ import me.chanjar.weixin.common.session.StandardSessionManager; | |||
| import me.chanjar.weixin.common.session.WxSession; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.common.util.RandomUtils; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import me.chanjar.weixin.common.util.crypto.SHA1; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
| import me.chanjar.weixin.common.util.http.DefaultApacheHttpClientBuilder; | |||
| import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.URIUtil; | |||
| import me.chanjar.weixin.common.util.http.*; | |||
| import me.chanjar.weixin.common.util.json.GsonHelper; | |||
| import me.chanjar.weixin.cp.bean.WxCpDepart; | |||
| import me.chanjar.weixin.cp.bean.WxCpMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpTag; | |||
| import me.chanjar.weixin.cp.bean.WxCpUser; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| 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.HttpGet; | |||
| import org.apache.http.impl.client.BasicResponseHandler; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.util.List; | |||
| import java.util.UUID; | |||
| public class WxCpServiceImpl implements WxCpService { | |||
| @@ -185,10 +172,10 @@ public class WxCpServiceImpl implements WxCpService { | |||
| jsapiSignature.setNoncestr(noncestr); | |||
| jsapiSignature.setUrl(url); | |||
| jsapiSignature.setSignature(signature); | |||
| // Fixed bug | |||
| jsapiSignature.setAppid(this.configStorage.getCorpId()); | |||
| return jsapiSignature; | |||
| } | |||
| @@ -503,7 +490,7 @@ this.configStorage.getOauth2redirectUri(), | |||
| public String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException { | |||
| String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?" | |||
| + "code=" + code | |||
| + "&agendid=" + agentId; | |||
| + "&agentid=" + agentId; | |||
| String responseText = get(url, null); | |||
| JsonElement je = new JsonParser().parse(responseText); | |||
| JsonObject jo = je.getAsJsonObject(); | |||
| @@ -1,9 +1,9 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import java.io.Serializable; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| /** | |||
| * 微信部门 | |||
| * | |||
| @@ -11,9 +11,6 @@ import java.io.Serializable; | |||
| */ | |||
| public class WxCpDepart implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -5028321625140879571L; | |||
| private Integer id; | |||
| private String name; | |||
| @@ -1,12 +1,17 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.*; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.ImageBuilder; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.NewsBuilder; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder; | |||
| import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| /** | |||
| * 消息 | |||
| * | |||
| @@ -14,9 +19,6 @@ import java.util.List; | |||
| */ | |||
| public class WxCpMessage implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -2082278303476631708L; | |||
| private String toUser; | |||
| private String toParty; | |||
| @@ -1,17 +1,14 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import java.io.Serializable; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| /** | |||
| * Created by Daniel Qian | |||
| */ | |||
| public class WxCpTag implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -7243320279646928402L; | |||
| private String id; | |||
| @@ -1,11 +1,11 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||
| /** | |||
| * 微信用户信息 | |||
| * | |||
| @@ -13,9 +13,6 @@ import java.util.List; | |||
| */ | |||
| public class WxCpUser implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -5696099236344075582L; | |||
| private final List<Attr> extAttrs = new ArrayList<>(); | |||
| private String userId; | |||
| @@ -1,18 +1,20 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.apache.commons.io.IOUtils; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||
| import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||
| import me.chanjar.weixin.cp.util.xml.XStreamTransformer; | |||
| import org.apache.commons.io.IOUtils; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| @@ -27,16 +29,12 @@ import java.util.List; | |||
| */ | |||
| @XStreamAlias("xml") | |||
| public class WxCpXmlMessage implements Serializable { | |||
| private static final long serialVersionUID = -1042994982179476410L; | |||
| /////////////////////// | |||
| // 以下都是微信推送过来的消息的xml的element所对应的属性 | |||
| /////////////////////// | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -1042994982179476410L; | |||
| @XStreamAlias("AgentID") | |||
| private Integer agentId; | |||
| @@ -2,11 +2,13 @@ package me.chanjar.weixin.cp.bean; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.util.xml.XStreamMediaIdConverter; | |||
| @XStreamAlias("xml") | |||
| public class WxCpXmlOutImageMessage extends WxCpXmlOutMessage { | |||
| private static final long serialVersionUID = -1099446240667237313L; | |||
| @XStreamAlias("Image") | |||
| @XStreamConverter(value = XStreamMediaIdConverter.class) | |||
| @@ -8,8 +8,12 @@ import me.chanjar.weixin.cp.bean.outxmlbuilder.*; | |||
| import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||
| import me.chanjar.weixin.cp.util.xml.XStreamTransformer; | |||
| import java.io.Serializable; | |||
| @XStreamAlias("xml") | |||
| public abstract class WxCpXmlOutMessage { | |||
| public abstract class WxCpXmlOutMessage implements Serializable { | |||
| private static final long serialVersionUID = 1418629839964153110L; | |||
| @XStreamAlias("ToUserName") | |||
| @XStreamConverter(value = XStreamCDataConverter.class) | |||
| @@ -1,15 +1,17 @@ | |||
| package me.chanjar.weixin.cp.bean; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| @XStreamAlias("xml") | |||
| public class WxCpXmlOutNewsMessage extends WxCpXmlOutMessage { | |||
| private static final long serialVersionUID = -5796178637883178826L; | |||
| @XStreamAlias("Articles") | |||
| protected final List<Item> articles = new ArrayList<>(); | |||
| @@ -2,11 +2,13 @@ package me.chanjar.weixin.cp.bean; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| @XStreamAlias("xml") | |||
| public class WxCpXmlOutTextMessage extends WxCpXmlOutMessage { | |||
| private static final long serialVersionUID = 2569239617185930232L; | |||
| @XStreamAlias("Content") | |||
| @XStreamConverter(value = XStreamCDataConverter.class) | |||
| @@ -2,11 +2,13 @@ package me.chanjar.weixin.cp.bean; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| @XStreamAlias("xml") | |||
| public class WxCpXmlOutVideoMessage extends WxCpXmlOutMessage { | |||
| private static final long serialVersionUID = -8672761162722733622L; | |||
| @XStreamAlias("Video") | |||
| protected final Video video = new Video(); | |||
| @@ -2,11 +2,13 @@ package me.chanjar.weixin.cp.bean; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.util.xml.XStreamMediaIdConverter; | |||
| @XStreamAlias("xml") | |||
| public class WxCpXmlOutVoiceMessage extends WxCpXmlOutMessage { | |||
| private static final long serialVersionUID = -7947384031546099340L; | |||
| @XStreamAlias("Voice") | |||
| @XStreamConverter(value = XStreamMediaIdConverter.class) | |||
| @@ -10,8 +10,8 @@ package me.chanjar.weixin.cp.util.json; | |||
| import com.google.gson.*; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import me.chanjar.weixin.cp.bean.WxCpMessage; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import java.lang.reflect.Type; | |||
| @@ -1,14 +1,12 @@ | |||
| package me.chanjar.weixin.cp.api; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.testng.Assert; | |||
| import org.testng.annotations.Guice; | |||
| import org.testng.annotations.Test; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| /** | |||
| * 基础API测试 | |||
| * | |||
| @@ -19,12 +19,12 @@ public class WxCpXmlOutNewsMessageTest { | |||
| item.setUrl("url"); | |||
| m.addArticle(item); | |||
| m.addArticle(item); | |||
| String expected = "<xml>" | |||
| + "<ToUserName><![CDATA[toUser]]></ToUserName>" | |||
| + "<FromUserName><![CDATA[fromUser]]></FromUserName>" | |||
| + "<CreateTime>1122</CreateTime>" | |||
| + "<MsgType><![CDATA[news]]></MsgType>" | |||
| + " <ArticleCount>2</ArticleCount>" | |||
| + " <Articles>" | |||
| + " <item>" | |||
| + " <Title><![CDATA[title]]></Title>" | |||
| @@ -39,6 +39,7 @@ public class WxCpXmlOutNewsMessageTest { | |||
| + " <Url><![CDATA[url]]></Url>" | |||
| + " </item>" | |||
| + " </Articles>" | |||
| + " <ArticleCount>2</ArticleCount>" | |||
| + "</xml>"; | |||
| System.out.println(m.toXml()); | |||
| Assert.assertEquals(m.toXml().replaceAll("\\s", ""), expected.replaceAll("\\s", "")); | |||
| @@ -62,7 +63,6 @@ public class WxCpXmlOutNewsMessageTest { | |||
| + "<FromUserName><![CDATA[fromUser]]></FromUserName>" | |||
| + "<CreateTime>1122</CreateTime>" | |||
| + "<MsgType><![CDATA[news]]></MsgType>" | |||
| + " <ArticleCount>2</ArticleCount>" | |||
| + " <Articles>" | |||
| + " <item>" | |||
| + " <Title><![CDATA[title]]></Title>" | |||
| @@ -77,6 +77,7 @@ public class WxCpXmlOutNewsMessageTest { | |||
| + " <Url><![CDATA[url]]></Url>" | |||
| + " </item>" | |||
| + " </Articles>" | |||
| + " <ArticleCount>2</ArticleCount>" | |||
| + "</xml>"; | |||
| System.out.println(m.toXml()); | |||
| Assert.assertEquals( | |||
| @@ -1,18 +1,17 @@ | |||
| package me.chanjar.weixin.cp.demo; | |||
| import java.io.IOException; | |||
| import javax.servlet.http.HttpServlet; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import me.chanjar.weixin.common.util.StringUtils; | |||
| import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||
| import me.chanjar.weixin.cp.api.WxCpMessageRouter; | |||
| import me.chanjar.weixin.cp.api.WxCpService; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||
| import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||
| import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import javax.servlet.http.HttpServlet; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import java.io.IOException; | |||
| /** | |||
| * @author Daniel Qian | |||
| @@ -6,7 +6,7 @@ | |||
| <parent> | |||
| <groupId>com.github.binarywang</groupId> | |||
| <artifactId>weixin-java-parent</artifactId> | |||
| <version>2.2.0</version> | |||
| <version>2.3.0</version> | |||
| </parent> | |||
| <artifactId>weixin-java-mp</artifactId> | |||
| <name>WeiXin Java Tools - MP</name> | |||
| @@ -1,13 +1,10 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
| import javax.net.ssl.SSLContext; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import java.io.File; | |||
| /** | |||
| @@ -43,7 +40,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
| * 临时文件目录 | |||
| */ | |||
| protected volatile File tmpDirFile; | |||
| protected volatile SSLContext sslContext; | |||
| protected volatile ApacheHttpClientBuilder apacheHttpClientBuilder; | |||
| @@ -62,7 +59,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
| public synchronized void updateAccessToken(WxAccessToken accessToken) { | |||
| updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn()); | |||
| } | |||
| @Override | |||
| public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) { | |||
| this.accessToken = accessToken; | |||
| @@ -229,7 +226,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this,ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| @Override | |||
| @@ -263,7 +260,7 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { | |||
| public SSLContext getSSLContext() { | |||
| return this.sslContext; | |||
| } | |||
| public void setSSLContext(SSLContext context) { | |||
| this.sslContext = context; | |||
| } | |||
| @@ -1,17 +1,12 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import java.io.File; | |||
| import java.util.Date; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.bean.WxMpCustomMessage; | |||
| import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; | |||
| import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfMsgList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfOnlineList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.*; | |||
| import java.io.File; | |||
| import java.util.Date; | |||
| /** | |||
| * 客服接口 , | |||
| @@ -27,9 +22,10 @@ public interface WxMpKefuService { | |||
| * <pre> | |||
| * 发送客服消息 | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547&token=&lang=zh_CN">发送客服消息</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| */ | |||
| boolean customMessageSend(WxMpCustomMessage message) throws WxErrorException; | |||
| boolean sendKefuMessage(WxMpKefuMessage message) throws WxErrorException; | |||
| //*******************客服管理接口***********************// | |||
| @@ -2,11 +2,12 @@ package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.bean.*; | |||
| import me.chanjar.weixin.mp.bean.result.*; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.bean.material.*; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| /** | |||
| @@ -20,21 +21,58 @@ import java.io.InputStream; | |||
| public interface WxMpMaterialService { | |||
| /** | |||
| * <pre> | |||
| * 新增临时素材 | |||
| * | |||
| * @param mediaType | |||
| * @param file | |||
| * 公众号经常有需要用到一些临时性的多媒体素材的场景,例如在使用接口特别是发送消息时,对多媒体文件、多媒体消息的获取和调用等操作,是通过media_id来进行的。 | |||
| * 素材管理接口对所有认证的订阅号和服务号开放。通过本接口,公众号可以新增临时素材(即上传临时多媒体文件)。 | |||
| * 请注意: | |||
| * 1、对于临时素材,每个素材(media_id)会在开发者上传或粉丝发送到微信服务器3天后自动删除(所以用户发送给开发者的素材,若开发者需要,应尽快下载到本地),以节省服务器资源。 | |||
| * 2、media_id是可复用的。 | |||
| * 3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/amr格式 | |||
| * 4、需使用https调用本接口。 | |||
| * 本接口即为原“上传多媒体文件”接口。 | |||
| * 注意事项: | |||
| * 上传的临时多媒体文件有格式和大小限制,如下: | |||
| * 图片(image): 2M,支持PNG\JPEG\JPG\GIF格式 | |||
| * 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 | |||
| * 视频(video):10MB,支持MP4格式 | |||
| * 缩略图(thumb):64KB,支持JPG格式 | |||
| *媒体文件在后台保存时间为3天,即3天后media_id失效。 | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726&token=&lang=zh_CN">新增临时素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE | |||
| * </pre> | |||
| * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
| * @param file 文件对象 | |||
| * @throws WxErrorException | |||
| * @see #mediaUpload(String, String, InputStream) | |||
| */ | |||
| WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 新增临时素材 | |||
| * 本接口即为原“上传多媒体文件”接口。 | |||
| * | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738726&token=&lang=zh_CN">新增临时素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE | |||
| * </pre> | |||
| * | |||
| * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
| * @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
| * @param inputStream 输入流 | |||
| * @throws WxErrorException | |||
| * @see #mediaUpload(java.lang.String, java.io.File) | |||
| */ | |||
| WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取临时素材 | |||
| * 公众号可以使用本接口获取临时素材(即下载临时的多媒体文件)。请注意,视频文件不支持https下载,调用该接口需http协议。 | |||
| * 本接口即为原“下载多媒体文件”接口。 | |||
| * 根据微信文档,视频文件下载不了,会返回null | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki/9/677a85e3f3849af35de54bb5516c2521.html">获取临时素材</a> | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738727&token=&lang=zh_CN">获取临时素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID | |||
| * </pre> | |||
| * | |||
| * @param media_id | |||
| @@ -46,10 +84,12 @@ public interface WxMpMaterialService { | |||
| /** | |||
| * <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 | |||
| * 请注意,本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。 | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">新增永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param file | |||
| * @param file 上传的文件对象 | |||
| * @return WxMediaImgUploadResult 返回图片url | |||
| * @throws WxErrorException | |||
| */ | |||
| @@ -57,114 +97,123 @@ public interface WxMpMaterialService { | |||
| /** | |||
| * <pre> | |||
| * 新增临时素材 | |||
| * 本接口即为原“上传多媒体文件”接口。 | |||
| * | |||
| * 上传的多媒体文件有格式和大小限制,如下: | |||
| * 图片(image): 1M,支持JPG格式 | |||
| * 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 | |||
| * 视频(video):10MB,支持MP4格式 | |||
| * 缩略图(thumb):64KB,支持JPG格式 | |||
| * | |||
| * 详情请见: <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} | |||
| * @param inputStream 输入流 | |||
| * @throws WxErrorException | |||
| */ | |||
| WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException; | |||
| /** | |||
| * <pre> | |||
| * 上传非图文永久素材 | |||
| * | |||
| * 上传的多媒体文件有格式和大小限制,如下: | |||
| * 图片(image): 图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式 | |||
| * 语音(voice):语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式 | |||
| * 视频(video):在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON | |||
| * 缩略图(thumb):文档未说明 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html | |||
| * 新增非图文永久素材 | |||
| * 通过POST表单来调用接口,表单id为media,包含需要上传的素材内容,有filename、filelength、content-type等信息。请注意:图片素材将进入公众平台官网素材管理模块中的默认分组。 | |||
| * 新增永久视频素材需特别注意: | |||
| * 在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON,格式如下: | |||
| * { "title":VIDEO_TITLE, "introduction":INTRODUCTION } | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">新增永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE | |||
| * | |||
| * 除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材。 | |||
| 永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽)。 | |||
| 请注意: | |||
| 1、新增的永久素材也可以在公众平台官网素材管理模块中看到 | |||
| 2、永久素材的数量是有上限的,请谨慎新增。图文消息素材和图片素材的上限为5000,其他类型为1000 | |||
| 3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式 | |||
| 4、调用该接口需https协议 | |||
| * </pre> | |||
| * | |||
| * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
| * @param material 上传的素材, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterial} | |||
| * @param material 上传的素材, 请看{@link WxMpMaterial} | |||
| */ | |||
| WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 上传永久图文素材 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html | |||
| * 新增永久图文素材 | |||
| * | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">新增永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN | |||
| * | |||
| * 除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材。 | |||
| 永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽)。 | |||
| 请注意: | |||
| 1、新增的永久素材也可以在公众平台官网素材管理模块中看到 | |||
| 2、永久素材的数量是有上限的,请谨慎新增。图文消息素材和图片素材的上限为5000,其他类型为1000 | |||
| 3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持bmp/png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/wma/wav/amr格式 | |||
| 4、调用该接口需https协议 | |||
| * </pre> | |||
| * | |||
| * @param news 上传的图文消息, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialNews} | |||
| * @param news 上传的图文消息, 请看{@link WxMpMaterialNews} | |||
| */ | |||
| WxMpMaterialUploadResult materialNewsUpload(WxMpMaterialNews news) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 下载声音或者图片永久素材 | |||
| * 获取声音或者图片永久素材 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">获取永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param media_id 永久素材的id | |||
| * @param mediaId 永久素材的id | |||
| */ | |||
| InputStream materialImageOrVoiceDownload(String media_id) throws WxErrorException; | |||
| InputStream materialImageOrVoiceDownload(String mediaId) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取视频永久素材的信息和下载地址 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">获取永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param media_id 永久素材的id | |||
| * @param mediaId 永久素材的id | |||
| */ | |||
| WxMpMaterialVideoInfoResult materialVideoInfo(String media_id) throws WxErrorException; | |||
| WxMpMaterialVideoInfoResult materialVideoInfo(String mediaId) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取图文永久素材的信息 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738729&token=&lang=zh_CN">获取永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param media_id 永久素材的id | |||
| * @param mediaId 永久素材的id | |||
| */ | |||
| WxMpMaterialNews materialNewsInfo(String media_id) throws WxErrorException; | |||
| WxMpMaterialNews materialNewsInfo(String mediaId) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 更新图文永久素材 | |||
| * 修改永久图文素材 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738732&token=&lang=zh_CN">修改永久图文素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate} | |||
| * @param wxMpMaterialArticleUpdate 用来更新图文素材的bean, 请看{@link WxMpMaterialArticleUpdate} | |||
| */ | |||
| boolean materialNewsUpdate(WxMpMaterialArticleUpdate wxMpMaterialArticleUpdate) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 删除永久素材 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html | |||
| * 在新增了永久素材后,开发者可以根据本接口来删除不再需要的永久素材,节省空间。 | |||
| * 请注意: | |||
| * 1、请谨慎操作本接口,因为它可以删除公众号在公众平台官网素材管理模块中新建的图文消息、语音、视频等素材(但需要先通过获取素材列表来获知素材的media_id) | |||
| * 2、临时素材无法通过本接口删除 | |||
| * 3、调用该接口需https协议 | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738731&token=&lang=zh_CN">删除永久素材</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param media_id 永久素材的id | |||
| * @param mediaId 永久素材的id | |||
| */ | |||
| boolean materialDelete(String media_id) throws WxErrorException; | |||
| boolean materialDelete(String mediaId) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取各类素材总数 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html | |||
| * 开发者可以根据本接口来获取永久素材的列表,需要时也可保存到本地。 | |||
| * 请注意: | |||
| * 1.永久素材的总数,也会计算公众平台官网素材管理中的素材 | |||
| * 2.图片和图文消息素材(包括单图文和多图文)的总数上限为5000,其他素材的总数上限为1000 | |||
| * 3.调用该接口需https协议 | |||
| * | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738733&token=&lang=zh_CN">获取素材总数</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| */ | |||
| WxMpMaterialCountResult materialCount() throws WxErrorException; | |||
| @@ -173,7 +222,8 @@ public interface WxMpMaterialService { | |||
| * <pre> | |||
| * 分页获取图文素材列表 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738734&token=&lang=zh_CN">获取素材列表</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param offset 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 | |||
| @@ -185,7 +235,8 @@ public interface WxMpMaterialService { | |||
| * <pre> | |||
| * 分页获取其他媒体素材列表 | |||
| * | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html | |||
| * 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738734&token=&lang=zh_CN">获取素材列表</a> | |||
| * 接口url格式:https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param type 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | |||
| @@ -2,8 +2,8 @@ package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; | |||
| import java.util.Map; | |||
| @@ -2,7 +2,7 @@ package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; | |||
| import java.util.Map; | |||
| @@ -1,6 +1,6 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; | |||
| /** | |||
| * 消息匹配器,用在消息路由的时候 | |||
| @@ -1,230 +1,230 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| 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; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| 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.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.mp.bean.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | |||
| /** | |||
| * <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<>(); | |||
| 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规则 | |||
| */ | |||
| 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<>(); | |||
| // 收集匹配的规则 | |||
| for (final WxMpMessageRouterRule rule : this.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<>(); | |||
| for (final WxMpMessageRouterRule rule : matchRules) { | |||
| // 返回最后一个非异步的rule的执行结果 | |||
| if(rule.isAsync()) { | |||
| futures.add( | |||
| this.executorService.submit(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| rule.service(wxMessage, WxMpMessageRouter.this.wxMpService, WxMpMessageRouter.this.sessionManager, WxMpMessageRouter.this.exceptionHandler); | |||
| } | |||
| }) | |||
| ); | |||
| } else { | |||
| res = rule.service(wxMessage, this.wxMpService, this.sessionManager, this.exceptionHandler); | |||
| // 在同步操作结束,session访问结束 | |||
| this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUser()); | |||
| sessionEndAccess(wxMessage); | |||
| } | |||
| } | |||
| if (futures.size() > 0) { | |||
| this.executorService.submit(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| for (Future<?> future : futures) { | |||
| try { | |||
| future.get(); | |||
| WxMpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser()); | |||
| // 异步操作结束,session访问结束 | |||
| sessionEndAccess(wxMessage); | |||
| } catch (InterruptedException e) { | |||
| WxMpMessageRouter.this.log.error("Error happened when wait task finish", e); | |||
| } catch (ExecutionException e) { | |||
| WxMpMessageRouter.this.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.getFromUser()) | |||
| .append("-").append(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | |||
| .append("-").append(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | |||
| ; | |||
| } else { | |||
| messageId.append(wxMessage.getMsgId()); | |||
| } | |||
| return this.messageDuplicateChecker.isDuplicate(messageId.toString()); | |||
| } | |||
| /** | |||
| * 对session的访问结束 | |||
| * @param wxMessage | |||
| */ | |||
| protected void sessionEndAccess(WxMpXmlMessage wxMessage) { | |||
| InternalSession session = ((InternalSessionManager)this.sessionManager).findSession(wxMessage.getFromUser()); | |||
| if (session != null) { | |||
| session.endAccess(); | |||
| } | |||
| } | |||
| } | |||
| package me.chanjar.weixin.mp.api; | |||
| 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; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| 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.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.mp.bean.message.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; | |||
| /** | |||
| * <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<>(); | |||
| 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规则 | |||
| */ | |||
| 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<>(); | |||
| // 收集匹配的规则 | |||
| for (final WxMpMessageRouterRule rule : this.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<>(); | |||
| for (final WxMpMessageRouterRule rule : matchRules) { | |||
| // 返回最后一个非异步的rule的执行结果 | |||
| if(rule.isAsync()) { | |||
| futures.add( | |||
| this.executorService.submit(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| rule.service(wxMessage, WxMpMessageRouter.this.wxMpService, WxMpMessageRouter.this.sessionManager, WxMpMessageRouter.this.exceptionHandler); | |||
| } | |||
| }) | |||
| ); | |||
| } else { | |||
| res = rule.service(wxMessage, this.wxMpService, this.sessionManager, this.exceptionHandler); | |||
| // 在同步操作结束,session访问结束 | |||
| this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUser()); | |||
| sessionEndAccess(wxMessage); | |||
| } | |||
| } | |||
| if (futures.size() > 0) { | |||
| this.executorService.submit(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| for (Future<?> future : futures) { | |||
| try { | |||
| future.get(); | |||
| WxMpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser()); | |||
| // 异步操作结束,session访问结束 | |||
| sessionEndAccess(wxMessage); | |||
| } catch (InterruptedException e) { | |||
| WxMpMessageRouter.this.log.error("Error happened when wait task finish", e); | |||
| } catch (ExecutionException e) { | |||
| WxMpMessageRouter.this.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.getFromUser()) | |||
| .append("-").append(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) | |||
| .append("-").append(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) | |||
| ; | |||
| } else { | |||
| messageId.append(wxMessage.getMsgId()); | |||
| } | |||
| return this.messageDuplicateChecker.isDuplicate(messageId.toString()); | |||
| } | |||
| /** | |||
| * 对session的访问结束 | |||
| * @param wxMessage | |||
| */ | |||
| protected void sessionEndAccess(WxMpXmlMessage wxMessage) { | |||
| InternalSession session = ((InternalSessionManager)this.sessionManager).findSession(wxMessage.getFromUser()); | |||
| if (session != null) { | |||
| session.endAccess(); | |||
| } | |||
| } | |||
| } | |||
| @@ -3,8 +3,8 @@ package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; | |||
| import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| @@ -1,16 +1,16 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import java.util.Map; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayCallback; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPrepayIdResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxRedpackResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxSendRedpackRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxPayJsSDKCallback; | |||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayOrderCloseResult; | |||
| import me.chanjar.weixin.mp.bean.pay.request.WxEntPayRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayRefundRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.request.WxPaySendRedpackRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.request.WxPayUnifiedOrderRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.result.*; | |||
| import java.io.File; | |||
| import java.util.Map; | |||
| /** | |||
| * 微信支付相关接口 | |||
| @@ -19,53 +19,74 @@ import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderResult; | |||
| */ | |||
| public interface WxMpPayService { | |||
| /** | |||
| * <pre> | |||
| * 查询订单(详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2) | |||
| * 该接口提供所有微信支付订单的查询,商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。 | |||
| * 需要调用查询接口的情况: | |||
| ◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知; | |||
| ◆ 调用支付接口后,返回系统错误或未知交易状态情况; | |||
| ◆ 调用被扫支付API,返回USERPAYING的状态; | |||
| ◆ 调用关单或撤销接口API之前,需确认支付状态; | |||
| * 接口地址:https://api.mch.weixin.qq.com/pay/orderquery | |||
| * </pre> | |||
| * @param transactionId 微信支付分配的商户号 | |||
| * @param outTradeNo 商户系统内部的订单号,当没提供transaction_id时需要传这个。 | |||
| * @throws WxErrorException | |||
| */ | |||
| WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 关闭订单 | |||
| * 应用场景 | |||
| * 以下情况需要调用关单接口: | |||
| * 1. 商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付; | |||
| * 2. 系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口。 | |||
| * 注意:订单生成后不能马上调用关单接口,最短调用时间间隔为5分钟。 | |||
| * 接口地址:https://api.mch.weixin.qq.com/pay/closeorder | |||
| * 是否需要证书: 不需要。 | |||
| * </pre> | |||
| * @param outTradeNo 商户系统内部的订单号,当没提供transaction_id时需要传这个。 | |||
| * @throws WxErrorException | |||
| */ | |||
| WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorException; | |||
| /** | |||
| * 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) | |||
| * 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识" | |||
| * 接口地址:https://api.mch.weixin.qq.com/pay/unifiedorder | |||
| * @throws WxErrorException | |||
| * @throws WxErrorException | |||
| * @param request 请求对象 | |||
| * | |||
| */ | |||
| WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request) | |||
| throws WxErrorException; | |||
| WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxErrorException; | |||
| /** | |||
| * 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数 | |||
| * 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN | |||
| * | |||
| * @param request 请求对象 | |||
| */ | |||
| Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException; | |||
| Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException; | |||
| /** | |||
| * 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。 | |||
| * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2 | |||
| * @throws WxErrorException | |||
| * | |||
| * <pre> | |||
| * 微信支付-申请退款 | |||
| * 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 | |||
| * 接口链接:https://api.mch.weixin.qq.com/secapi/pay/refund | |||
| * </pre> | |||
| * @param request 请求对象 | |||
| * @param keyFile 证书文件对象 | |||
| * @return 退款操作结果 | |||
| */ | |||
| WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo) | |||
| throws WxErrorException; | |||
| WxPayRefundResult refund(WxPayRefundRequest request, File keyFile) throws WxErrorException; | |||
| /** | |||
| * 读取支付结果通知 | |||
| * 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 | |||
| * | |||
| */ | |||
| 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 退款操作结果 | |||
| */ | |||
| WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException; | |||
| WxPayJsSDKCallback getJSSDKCallbackData(String xmlData) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| @@ -78,12 +99,40 @@ public interface WxMpPayService { | |||
| /** | |||
| * 发送微信红包给个人用户 | |||
| * <pre> | |||
| * <pre> | |||
| * 文档详见: | |||
| * 发送普通红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3 | |||
| * 发送裂变红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4 | |||
| * </pre> | |||
| * @param request 请求对象 | |||
| * @param keyFile 证书文件对象 | |||
| */ | |||
| WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File keyFile) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 企业付款业务是基于微信支付商户平台的资金管理能力,为了协助商户方便地实现企业向个人付款,针对部分有开发能力的商户,提供通过API完成企业付款的功能。 | |||
| * 比如目前的保险行业向客户退保、给付、理赔。 | |||
| * 企业付款将使用商户的可用余额,需确保可用余额充足。查看可用余额、充值、提现请登录商户平台“资金管理”https://pay.weixin.qq.com/进行操作。 | |||
| * 注意:与商户微信支付收款资金并非同一账户,需要单独充值。 | |||
| * 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2 | |||
| * 接口链接:https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers | |||
| * </pre> | |||
| * @param request 请求对象 | |||
| * @param keyFile 证书文件对象 | |||
| */ | |||
| WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 查询企业付款API | |||
| * 用于商户的企业付款操作进行结果查询,返回付款操作详细结果。 | |||
| * 文档详见:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_3 | |||
| * 接口链接:https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo | |||
| * </pre> | |||
| * @param partnerTradeNo 商户订单号 | |||
| * @param keyFile 证书文件对象 | |||
| */ | |||
| WxRedpackResult sendRedpack(WxSendRedpackRequest request) throws WxErrorException; | |||
| WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) throws WxErrorException; | |||
| } | |||
| @@ -3,19 +3,9 @@ package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.mp.bean.WxMpIndustry; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassTagMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassNews; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassVideo; | |||
| import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; | |||
| import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
| import me.chanjar.weixin.mp.bean.*; | |||
| import me.chanjar.weixin.mp.bean.result.*; | |||
| import org.apache.http.HttpHost; | |||
| /** | |||
| * 微信API的Service | |||
| @@ -126,22 +116,26 @@ public interface WxMpService { | |||
| /** | |||
| * <pre> | |||
| * 长链接转短链接接口 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口 | |||
| * 群发消息预览接口 | |||
| * 开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版。为了满足第三方平台开发者的需求,在保留对openID预览能力的同时,增加了对指定微信号发送预览的能力,但该能力每日调用次数有限制(100次),请勿滥用。 | |||
| * 接口调用请求说明 | |||
| * http请求方式: POST | |||
| * https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN | |||
| * 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN | |||
| * </pre> | |||
| * | |||
| * @return wxMpMassSendResult | |||
| */ | |||
| String shortUrl(String long_url) throws WxErrorException; | |||
| WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | |||
| /** | |||
| * <pre> | |||
| * 发送模板消息 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=模板消息接口 | |||
| * 长链接转短链接接口 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=长链接转短链接接口 | |||
| * </pre> | |||
| * | |||
| * @return msgid | |||
| */ | |||
| String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException; | |||
| String shortUrl(String long_url) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| @@ -235,6 +229,11 @@ public interface WxMpService { | |||
| */ | |||
| <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException; | |||
| /** | |||
| * 获取代理对象 | |||
| */ | |||
| HttpHost getHttpProxy(); | |||
| /** | |||
| * 注入 {@link WxMpConfigStorage} 的实现 | |||
| */ | |||
| @@ -256,37 +255,6 @@ public interface WxMpService { | |||
| */ | |||
| void setMaxRetryTimes(int maxRetryTimes); | |||
| /** | |||
| * <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> | |||
| * | |||
| * @return wxMpMassSendResult | |||
| */ | |||
| WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | |||
| /** | |||
| * <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 JsonObject | |||
| */ | |||
| 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 | |||
| */ | |||
| WxMpIndustry getIndustry() throws WxErrorException; | |||
| /** | |||
| * 获取WxMpConfigStorage 对象 | |||
| * | |||
| @@ -370,4 +338,11 @@ public interface WxMpService { | |||
| * @return WxMpStoreService | |||
| */ | |||
| WxMpStoreService getStoreService(); | |||
| /** | |||
| * 返回模板消息相关接口方法的实现类对象,以方便调用其各种接口 | |||
| * | |||
| * @return WxMpTemplateMsgService | |||
| */ | |||
| WxMpTemplateMsgService getTemplateMsgService(); | |||
| } | |||
| @@ -9,7 +9,7 @@ import java.util.List; | |||
| /** | |||
| * 门店管理的相关接口代码 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016-09-23. | |||
| */ | |||
| public interface WxMpStoreService { | |||
| @@ -0,0 +1,87 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplate; | |||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry; | |||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| * 模板消息接口 | |||
| * http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * Created by Binary Wang on 2016-10-14. | |||
| * @author miller.lin | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * </pre> | |||
| */ | |||
| public interface WxMpTemplateMsgService { | |||
| /** | |||
| * <pre> | |||
| * 设置所属行业 | |||
| * 官方文档中暂未告知响应内容 | |||
| * 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * </pre> | |||
| * | |||
| * @return 是否成功 | |||
| */ | |||
| boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException; | |||
| /*** | |||
| * <pre> | |||
| * 获取设置的行业信息 | |||
| * 详情请见:http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * </pre> | |||
| * | |||
| * @return wxMpIndustry | |||
| */ | |||
| WxMpTemplateIndustry getIndustry() throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 发送模板消息 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * </pre> | |||
| * | |||
| * @return 消息Id | |||
| */ | |||
| String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获得模板ID | |||
| * 从行业模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * @param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式 | |||
| * @return templateId 模板Id | |||
| */ | |||
| String addTemplate(String shortTemplateId) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取模板列表 | |||
| * 获取已添加至帐号下所有模板列表,可在MP中查看模板列表信息,为方便第三方开发者,提供通过接口调用的方式来获取帐号下所有模板信息 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @return templateId 模板Id | |||
| */ | |||
| List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 删除模板 | |||
| * 删除模板可在MP中完成,为方便第三方开发者,提供通过接口调用的方式来删除某帐号下的模板 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN | |||
| * 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param templateId 模板Id | |||
| */ | |||
| boolean delPrivateTemplate(String templateId) throws WxErrorException; | |||
| } | |||
| @@ -1,12 +1,12 @@ | |||
| package me.chanjar.weixin.mp.api; | |||
| import java.util.List; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.bean.WxMpUserQuery; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUserList; | |||
| import java.util.List; | |||
| /** | |||
| * 用户管理相关操作接口 | |||
| * | |||
| @@ -16,8 +16,10 @@ public interface WxMpUserService { | |||
| /** | |||
| * <pre> | |||
| * 设置用户备注名接口 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=设置用户备注名接口 | |||
| * 设置用户备注名 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140838&token=&lang=zh_CN | |||
| * http请求方式: POST(请使用https协议) | |||
| * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param openid 用户openid | |||
| @@ -25,10 +27,24 @@ public interface WxMpUserService { | |||
| */ | |||
| void userUpdateRemark(String openid, String remark) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取用户基本信息(语言为默认的zh_CN 简体) | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN | |||
| * http请求方式: GET | |||
| * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN | |||
| * </pre> | |||
| * | |||
| * @param openid 用户openid | |||
| */ | |||
| WxMpUser userInfo(String openid) throws WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 获取用户基本信息 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN | |||
| * http请求方式: GET | |||
| * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN | |||
| * </pre> | |||
| * | |||
| * @param openid 用户openid | |||
| @@ -39,7 +55,10 @@ public interface WxMpUserService { | |||
| /** | |||
| * <pre> | |||
| * 获取用户基本信息列表 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=批量获取用户基本信息 | |||
| * 开发者可通过该接口来批量获取用户基本信息。最多支持一次拉取100条。 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN | |||
| * http请求方式: POST | |||
| * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param openids 用户openid列表 | |||
| @@ -49,7 +68,10 @@ public interface WxMpUserService { | |||
| /** | |||
| * <pre> | |||
| * 获取用户基本信息列表 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=批量获取用户基本信息 | |||
| * 开发者可通过该接口来批量获取用户基本信息。最多支持一次拉取100条。 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839&token=&lang=zh_CN | |||
| * http请求方式: POST | |||
| * 接口地址:https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @param userQuery 详细查询参数 | |||
| @@ -58,8 +80,11 @@ public interface WxMpUserService { | |||
| /** | |||
| * <pre> | |||
| * 获取关注者列表 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表 | |||
| * 获取用户列表 | |||
| * 公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID,可以通过多次拉取的方式来满足需求。 | |||
| * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140840&token=&lang=zh_CN | |||
| * http请求方式: GET(请使用https协议) | |||
| * 接口地址:https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID | |||
| * </pre> | |||
| * | |||
| * @param nextOpenid 可选,第一个拉取的OPENID,null为从头开始拉取 | |||
| @@ -9,7 +9,7 @@ import me.chanjar.weixin.mp.bean.tag.WxUserTag; | |||
| /** | |||
| * 用户标签管理相关接口 | |||
| * Created by Binary Wang on 2016/9/2. | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * | |||
| */ | |||
| public interface WxMpUserTagService { | |||
| @@ -94,8 +94,8 @@ public interface WxMpUserTagService { | |||
| * 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">用户标签管理</a> | |||
| * 接口url格式: https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=ACCESS_TOKEN | |||
| * </pre> | |||
| * | |||
| * @return 标签Id的列表 | |||
| */ | |||
| List<Integer> userTagList(String openid) throws WxErrorException; | |||
| List<Long> userTagList(String openid) throws WxErrorException; | |||
| } | |||
| @@ -1,21 +1,28 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import java.io.File; | |||
| import java.util.Date; | |||
| import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.google.gson.JsonObject; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.mp.api.WxMpKefuService; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.bean.WxMpCustomMessage; | |||
| import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest; | |||
| import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.*; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.io.File; | |||
| import java.util.Date; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfMsgList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfOnlineList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionGetResult; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionList; | |||
| import me.chanjar.weixin.mp.bean.kefu.result.WxMpKfSessionWaitCaseList; | |||
| /** | |||
| * | |||
| @@ -34,11 +41,11 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| } | |||
| @Override | |||
| public boolean customMessageSend(WxMpCustomMessage message) | |||
| public boolean sendKefuMessage(WxMpKefuMessage message) | |||
| throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; | |||
| String responseContent = this.wxMpService.post(url, message.toJson()); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| @@ -60,7 +67,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/kfaccount/add"; | |||
| String responseContent = this.wxMpService.post(url, request.toJson()); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| @@ -68,14 +75,14 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/kfaccount/update"; | |||
| String responseContent = this.wxMpService.post(url, request.toJson()); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/kfaccount/inviteworker"; | |||
| String responseContent = this.wxMpService.post(url, request.toJson()); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| @@ -84,14 +91,14 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount; | |||
| WxMediaUploadResult responseContent = this.wxMpService | |||
| .execute(new MediaUploadRequestExecutor(), url, imgFile); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| public boolean kfAccountDel(String kfAccount) throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount; | |||
| String responseContent = this.wxMpService.get(url, null); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| @@ -100,7 +107,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); | |||
| String url = API_URL_PREFIX + "/kfsession/create"; | |||
| String responseContent = this.wxMpService.post(url, request.toJson()); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| @@ -109,7 +116,7 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||
| WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); | |||
| String url = API_URL_PREFIX + "/kfsession/close"; | |||
| String responseContent = this.wxMpService.post(url, request.toJson()); | |||
| return true; | |||
| return responseContent != null; | |||
| } | |||
| @Override | |||
| @@ -10,10 +10,10 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | |||
| import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
| import me.chanjar.weixin.mp.api.WxMpMaterialService; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.bean.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate; | |||
| import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.bean.result.*; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterial; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate; | |||
| import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; | |||
| import me.chanjar.weixin.mp.bean.material.*; | |||
| import me.chanjar.weixin.mp.util.http.*; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| @@ -37,8 +37,13 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||
| } | |||
| @Override | |||
| public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { | |||
| return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); | |||
| public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException { | |||
| try { | |||
| return this.mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); | |||
| } catch (IOException e) { | |||
| e.printStackTrace(); | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build()); | |||
| } | |||
| } | |||
| @Override | |||
| @@ -1,37 +1,39 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import java.lang.reflect.Field; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Map.Entry; | |||
| import java.util.SortedMap; | |||
| import java.util.TreeMap; | |||
| import org.apache.commons.codec.digest.DigestUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.joor.Reflect; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.google.common.collect.Lists; | |||
| import com.google.common.collect.Maps; | |||
| import com.thoughtworks.xstream.XStream; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import me.chanjar.weixin.common.annotation.Required; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.BeanUtils; | |||
| import me.chanjar.weixin.common.util.xml.XStreamInitializer; | |||
| import me.chanjar.weixin.mp.api.WxMpPayService; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayCallback; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxMpPayResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxRedpackResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxSendRedpackRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderRequest; | |||
| import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderResult; | |||
| import me.chanjar.weixin.mp.bean.pay.WxPayJsSDKCallback; | |||
| import me.chanjar.weixin.mp.bean.pay.result.WxPayOrderCloseResult; | |||
| import me.chanjar.weixin.mp.bean.pay.request.*; | |||
| import me.chanjar.weixin.mp.bean.pay.result.*; | |||
| import org.apache.commons.codec.digest.DigestUtils; | |||
| import org.apache.commons.lang3.ArrayUtils; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.apache.http.Consts; | |||
| 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.conn.ssl.DefaultHostnameVerifier; | |||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
| import org.apache.http.entity.StringEntity; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.apache.http.impl.client.HttpClients; | |||
| import org.apache.http.ssl.SSLContexts; | |||
| import org.apache.http.util.EntityUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import javax.net.ssl.SSLContext; | |||
| import java.io.File; | |||
| import java.io.FileInputStream; | |||
| import java.io.IOException; | |||
| import java.security.KeyStore; | |||
| import java.util.*; | |||
| /** | |||
| * Created by Binary Wang on 2016/7/28. | |||
| @@ -40,9 +42,13 @@ import me.chanjar.weixin.mp.bean.pay.WxUnifiedOrderResult; | |||
| */ | |||
| public class WxMpPayServiceImpl implements WxMpPayService { | |||
| private static final List<String> TRADE_TYPES = Lists.newArrayList("JSAPI", | |||
| "NATIVE", "APP"); | |||
| private final Logger log = LoggerFactory.getLogger(WxMpPayServiceImpl.class); | |||
| protected final Logger log = LoggerFactory.getLogger(this.getClass()); | |||
| private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com"; | |||
| private static final String[] TRADE_TYPES = new String[]{"JSAPI","NATIVE", "APP"}; | |||
| private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS", | |||
| "REFUND_SOURCE_UNSETTLED_FUNDS"}; | |||
| private WxMpService wxMpService; | |||
| public WxMpPayServiceImpl(WxMpService wxMpService) { | |||
| @@ -50,98 +56,64 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
| } | |||
| @Override | |||
| public WxMpPayResult getJSSDKPayResult(String transactionId, | |||
| String outTradeNo) throws WxErrorException { | |||
| String nonce_str = System.currentTimeMillis() + ""; | |||
| SortedMap<String, String> packageParams = new TreeMap<>(); | |||
| packageParams.put("appid", | |||
| this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| packageParams.put("mch_id", | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| if (transactionId != null && !"".equals(transactionId.trim())) { | |||
| packageParams.put("transaction_id", transactionId); | |||
| } else if (outTradeNo != null && !"".equals(outTradeNo.trim())) { | |||
| packageParams.put("out_trade_no", outTradeNo); | |||
| } else { | |||
| throw new IllegalArgumentException( | |||
| "Either 'transactionId' or 'outTradeNo' must be given."); | |||
| } | |||
| public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile) | |||
| throws WxErrorException { | |||
| checkParameters(request); | |||
| packageParams.put("nonce_str", nonce_str); | |||
| packageParams.put("sign", this.createSign(packageParams, | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey())); | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxPayRefundRequest.class); | |||
| xstream.processAnnotations(WxPayRefundResult.class); | |||
| StringBuilder request = new StringBuilder("<xml>"); | |||
| for (Map.Entry<String, String> para : packageParams.entrySet()) { | |||
| request.append(String.format("<%s>%s</%s>", para.getKey(), | |||
| para.getValue(), para.getKey())); | |||
| } | |||
| request.append("</xml>"); | |||
| request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| String partnerId = this.wxMpService.getWxMpConfigStorage().getPartnerId(); | |||
| request.setMchId(partnerId); | |||
| request.setNonceStr( System.currentTimeMillis() + ""); | |||
| request.setOpUserId(partnerId); | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| String url = "https://api.mch.weixin.qq.com/pay/orderquery"; | |||
| String responseContent = this.wxMpService.post(url, request.toString()); | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.alias("xml", WxMpPayResult.class); | |||
| return (WxMpPayResult) xstream.fromXML(responseContent); | |||
| String url = PAY_BASE_URL + "/secapi/pay/refund"; | |||
| String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), partnerId); | |||
| WxPayRefundResult result = (WxPayRefundResult) xstream.fromXML(responseContent); | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| @Override | |||
| public WxMpPayCallback getJSSDKCallbackData(String xmlData) { | |||
| try { | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.alias("xml", WxMpPayCallback.class); | |||
| return (WxMpPayCallback) xstream.fromXML(xmlData); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| private void checkResult(WxPayBaseResult result) throws WxErrorException { | |||
| if (!"SUCCESS".equalsIgnoreCase(result.getReturnCode()) | |||
| || !"SUCCESS".equalsIgnoreCase(result.getResultCode())) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorCode(-1) | |||
| .setErrorMsg("返回代码:" + result.getReturnCode() + ", 返回信息: " | |||
| + result.getReturnMsg() + ", 结果代码: " + result.getResultCode() + ", 错误代码: " | |||
| + result.getErrCode() + ", 错误详情: " + result.getErrCodeDes()) | |||
| .build()); | |||
| } | |||
| return new WxMpPayCallback(); | |||
| } | |||
| @Override | |||
| public WxMpPayRefundResult refundPay(Map<String, String> parameters) | |||
| throws WxErrorException { | |||
| SortedMap<String, String> refundParams = new TreeMap<>(parameters); | |||
| refundParams.put("appid", | |||
| this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| refundParams.put("mch_id", | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| refundParams.put("nonce_str", System.currentTimeMillis() + ""); | |||
| refundParams.put("op_user_id", | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| String sign = this.createSign(refundParams, | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| refundParams.put("sign", sign); | |||
| private void checkParameters(WxPayRefundRequest request) throws WxErrorException { | |||
| BeanUtils.checkRequiredFields(request); | |||
| StringBuilder request = new StringBuilder("<xml>"); | |||
| for (Map.Entry<String, String> para : refundParams.entrySet()) { | |||
| request.append(String.format("<%s>%s</%s>", para.getKey(), | |||
| para.getValue(), para.getKey())); | |||
| if (StringUtils.isNotBlank(request.getRefundAccount())) { | |||
| if(!ArrayUtils.contains(REFUND_ACCOUNT, request.getRefundAccount())){ | |||
| throw new IllegalArgumentException("refund_account目前必须为" + Arrays.toString(REFUND_ACCOUNT) + "其中之一"); | |||
| } | |||
| } | |||
| request.append("</xml>"); | |||
| String url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; | |||
| String responseContent = this.wxMpService.post(url, request.toString()); | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxMpPayRefundResult.class); | |||
| WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream | |||
| .fromXML(responseContent); | |||
| if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode()) | |||
| || !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) { | |||
| WxError error = new WxError(); | |||
| error.setErrorCode(-1); | |||
| error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode() | |||
| + ";return_msg:" + wxMpPayRefundResult.getReturnMsg() | |||
| + ";result_code:" + wxMpPayRefundResult.getResultCode() + ";err_code" | |||
| + wxMpPayRefundResult.getErrCode() + ";err_code_des" | |||
| + wxMpPayRefundResult.getErrCodeDes()); | |||
| throw new WxErrorException(error); | |||
| if (StringUtils.isBlank(request.getOutTradeNo()) && StringUtils.isBlank(request.getTransactionId())) { | |||
| throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个"); | |||
| } | |||
| } | |||
| return wxMpPayRefundResult; | |||
| @Override | |||
| public WxPayJsSDKCallback getJSSDKCallbackData(String xmlData) throws WxErrorException { | |||
| try { | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.alias("xml", WxPayJsSDKCallback.class); | |||
| return (WxPayJsSDKCallback) xstream.fromXML(xmlData); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| throw new WxErrorException(WxError.newBuilder().setErrorMsg("发生异常" + e.getMessage()).build()); | |||
| } | |||
| } | |||
| @Override | |||
| @@ -152,59 +124,31 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
| } | |||
| @Override | |||
| public WxRedpackResult sendRedpack(WxSendRedpackRequest request) | |||
| public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File keyFile) | |||
| throws WxErrorException { | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxSendRedpackRequest.class); | |||
| xstream.processAnnotations(WxRedpackResult.class); | |||
| xstream.processAnnotations(WxPaySendRedpackRequest.class); | |||
| xstream.processAnnotations(WxPaySendRedpackResult.class); | |||
| request.setWxAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| String mchId = this.wxMpService.getWxMpConfigStorage().getPartnerId(); | |||
| request.setMchId(mchId); | |||
| request.setNonceStr(System.currentTimeMillis() + ""); | |||
| String sign = this.createSign(xmlBean2Map(request), | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; | |||
| String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack"; | |||
| if (request.getAmtType() != null) { | |||
| //裂变红包 | |||
| url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack"; | |||
| url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack"; | |||
| } | |||
| String responseContent = this.wxMpService.post(url, xstream.toXML(request)); | |||
| WxRedpackResult redpackResult = (WxRedpackResult) xstream | |||
| String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), mchId); | |||
| WxPaySendRedpackResult result = (WxPaySendRedpackResult) xstream | |||
| .fromXML(responseContent); | |||
| if ("FAIL".equals(redpackResult.getResultCode())) { | |||
| throw new WxErrorException(WxError.newBuilder() | |||
| .setErrorMsg( | |||
| redpackResult.getErrCode() + ":" + redpackResult.getErrCodeDes()) | |||
| .build()); | |||
| } | |||
| return redpackResult; | |||
| } | |||
| private Map<String, String> xmlBean2Map(Object bean) { | |||
| Map<String, String> result = Maps.newHashMap(); | |||
| for (Entry<String, Reflect> entry : Reflect.on(bean).fields().entrySet()) { | |||
| Reflect reflect = entry.getValue(); | |||
| if (reflect.get() == null) { | |||
| continue; | |||
| } | |||
| try { | |||
| Field field = bean.getClass().getDeclaredField(entry.getKey()); | |||
| if (field.isAnnotationPresent(XStreamAlias.class)) { | |||
| result.put(field.getAnnotation(XStreamAlias.class).value(), | |||
| reflect.get().toString()); | |||
| } | |||
| } catch (NoSuchFieldException | SecurityException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| @@ -232,87 +176,110 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
| } | |||
| @Override | |||
| public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request) | |||
| throws WxErrorException { | |||
| checkParameters(request); | |||
| public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException { | |||
| if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) || | |||
| (StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) { | |||
| throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一"); | |||
| } | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxUnifiedOrderRequest.class); | |||
| xstream.processAnnotations(WxUnifiedOrderResult.class); | |||
| xstream.processAnnotations(WxPayOrderQueryRequest.class); | |||
| xstream.processAnnotations(WxPayOrderQueryResult.class); | |||
| WxPayOrderQueryRequest request = new WxPayOrderQueryRequest(); | |||
| request.setOutTradeNo(StringUtils.trimToNull(outTradeNo)); | |||
| request.setTransactionId(StringUtils.trimToNull(transactionId)); | |||
| request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| request.setNonceStr(System.currentTimeMillis() + ""); | |||
| String sign = this.createSign(xmlBean2Map(request), | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; | |||
| String url = PAY_BASE_URL + "/pay/orderquery"; | |||
| String responseContent = this.wxMpService.post(url, xstream.toXML(request)); | |||
| WxUnifiedOrderResult result = (WxUnifiedOrderResult) xstream | |||
| .fromXML(responseContent); | |||
| if ("FAIL".equals(result.getResultCode())) { | |||
| throw new WxErrorException(WxError.newBuilder() | |||
| .setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes()) | |||
| .build()); | |||
| String responseContent = this.executeRequest(url, xstream.toXML(request)); | |||
| WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent); | |||
| result.composeCoupons(responseContent); | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| @Override | |||
| public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorException { | |||
| if (StringUtils.isBlank(outTradeNo)) { | |||
| throw new IllegalArgumentException("out_trade_no 不能为空"); | |||
| } | |||
| return result; | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxPayOrderCloseRequest.class); | |||
| xstream.processAnnotations(WxPayOrderCloseResult.class); | |||
| WxPayOrderCloseRequest request = new WxPayOrderCloseRequest(); | |||
| request.setOutTradeNo(StringUtils.trimToNull(outTradeNo)); | |||
| request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| request.setNonceStr(System.currentTimeMillis() + ""); | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| String url = PAY_BASE_URL + "/pay/closeorder"; | |||
| String responseContent = this.executeRequest(url, xstream.toXML(request)); | |||
| WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent); | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| private void checkParameters(WxUnifiedOrderRequest request) { | |||
| List<String> nullFields = Lists.newArrayList(); | |||
| for (Entry<String, Reflect> entry : Reflect.on(request).fields() | |||
| .entrySet()) { | |||
| Reflect reflect = entry.getValue(); | |||
| try { | |||
| Field field = request.getClass().getDeclaredField(entry.getKey()); | |||
| if (field.isAnnotationPresent(Required.class) | |||
| && reflect.get() == null) { | |||
| nullFields.add(entry.getKey()); | |||
| } | |||
| } catch (NoSuchFieldException | SecurityException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| @Override | |||
| public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) | |||
| throws WxErrorException { | |||
| checkParameters(request); | |||
| if (!nullFields.isEmpty()) { | |||
| throw new IllegalArgumentException("必填字段[" + nullFields + "]必须提供值"); | |||
| } | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxPayUnifiedOrderRequest.class); | |||
| xstream.processAnnotations(WxPayUnifiedOrderResult.class); | |||
| request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| request.setNonceStr(System.currentTimeMillis() + ""); | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), | |||
| this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| if (!TRADE_TYPES.contains(request.getTradeType())) { | |||
| throw new IllegalArgumentException( | |||
| "trade_type目前必须为" + TRADE_TYPES + "其中之一"); | |||
| String url = PAY_BASE_URL + "/pay/unifiedorder"; | |||
| String responseContent = this.executeRequest(url, xstream.toXML(request)); | |||
| WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream | |||
| .fromXML(responseContent); | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| private void checkParameters(WxPayUnifiedOrderRequest request) throws WxErrorException { | |||
| BeanUtils.checkRequiredFields(request); | |||
| if (! ArrayUtils.contains(TRADE_TYPES, request.getTradeType())) { | |||
| throw new IllegalArgumentException("trade_type目前必须为" + Arrays.toString(TRADE_TYPES) + "其中之一"); | |||
| } | |||
| if ("JSAPI".equals(request.getTradeType()) && request.getOpenid() == null) { | |||
| throw new IllegalArgumentException("当 trade_type是'JSAPI'时未指定openid"); | |||
| } | |||
| if ("NATIVE".equals(request.getTradeType()) | |||
| && request.getProductId() == null) { | |||
| if ("NATIVE".equals(request.getTradeType()) && request.getProductId() == null) { | |||
| throw new IllegalArgumentException("当 trade_type是'NATIVE'时未指定product_id"); | |||
| } | |||
| } | |||
| @Override | |||
| public Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxErrorException { | |||
| WxUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request); | |||
| if (!"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getReturnCode()) | |||
| || !"SUCCESS".equalsIgnoreCase(unifiedOrderResult.getResultCode())) { | |||
| throw new WxErrorException(WxError.newBuilder().setErrorCode(-1) | |||
| .setErrorMsg("return_code:" + unifiedOrderResult.getReturnCode() + ";return_msg:" | |||
| + unifiedOrderResult.getReturnMsg() + ";result_code:" + unifiedOrderResult.getResultCode() + ";err_code" | |||
| + unifiedOrderResult.getErrCode() + ";err_code_des" + unifiedOrderResult.getErrCodeDes()) | |||
| .build()); | |||
| } | |||
| public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxErrorException { | |||
| WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request); | |||
| String prepayId = unifiedOrderResult.getPrepayId(); | |||
| if (StringUtils.isBlank(prepayId)) { | |||
| throw new RuntimeException(String.format("Failed to get prepay id due to error code '%s'(%s).", | |||
| @@ -335,4 +302,101 @@ public class WxMpPayServiceImpl implements WxMpPayService { | |||
| return payInfo; | |||
| } | |||
| @Override | |||
| public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErrorException { | |||
| BeanUtils.checkRequiredFields(request); | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxEntPayRequest.class); | |||
| xstream.processAnnotations(WxEntPayResult.class); | |||
| request.setMchAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| request.setNonceStr(System.currentTimeMillis() + ""); | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers"; | |||
| String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId()); | |||
| WxEntPayResult result = (WxEntPayResult) xstream.fromXML(responseContent); | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| @Override | |||
| public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) throws WxErrorException { | |||
| XStream xstream = XStreamInitializer.getInstance(); | |||
| xstream.processAnnotations(WxEntPayQueryRequest.class); | |||
| xstream.processAnnotations(WxEntPayQueryResult.class); | |||
| WxEntPayQueryRequest request = new WxEntPayQueryRequest(); | |||
| request.setAppid(this.wxMpService.getWxMpConfigStorage().getAppId()); | |||
| request.setMchId(this.wxMpService.getWxMpConfigStorage().getPartnerId()); | |||
| request.setNonceStr(System.currentTimeMillis() + ""); | |||
| String sign = this.createSign(BeanUtils.xmlBean2Map(request), this.wxMpService.getWxMpConfigStorage().getPartnerKey()); | |||
| request.setSign(sign); | |||
| String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo"; | |||
| String responseContent = this.executeRequestWithKeyFile(url, keyFile, xstream.toXML(request), request.getMchId()); | |||
| WxEntPayQueryResult result = (WxEntPayQueryResult) xstream.fromXML(responseContent); | |||
| this.checkResult(result); | |||
| return result; | |||
| } | |||
| private String executeRequest( String url, String requestStr) throws WxErrorException { | |||
| HttpPost httpPost = new HttpPost(url); | |||
| if (this.wxMpService.getHttpProxy() != null) { | |||
| httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build()); | |||
| } | |||
| try (CloseableHttpClient httpclient = HttpClients.custom().build()) { | |||
| httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1"))); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8); | |||
| this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",url, requestStr, result); | |||
| return result; | |||
| } | |||
| } catch (IOException e) { | |||
| this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); | |||
| throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e); | |||
| }finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } | |||
| private String executeRequestWithKeyFile( String url, File keyFile, String requestStr, String mchId) throws WxErrorException { | |||
| try (FileInputStream inputStream = new FileInputStream(keyFile)) { | |||
| KeyStore keyStore = KeyStore.getInstance("PKCS12"); | |||
| keyStore.load(inputStream, mchId.toCharArray()); | |||
| SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build(); | |||
| SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, | |||
| new DefaultHostnameVerifier()); | |||
| HttpPost httpPost = new HttpPost(url); | |||
| if (this.wxMpService.getHttpProxy() != null) { | |||
| httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build()); | |||
| } | |||
| try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) { | |||
| httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1"))); | |||
| try (CloseableHttpResponse response = httpclient.execute(httpPost)) { | |||
| String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8); | |||
| this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",url, requestStr, result); | |||
| return result; | |||
| } | |||
| }finally { | |||
| httpPost.releaseConnection(); | |||
| } | |||
| } catch (Exception e) { | |||
| this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); | |||
| throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,23 +1,9 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import java.io.IOException; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.conn.ssl.DefaultHostnameVerifier; | |||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
| import org.apache.http.impl.client.BasicResponseHandler; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import com.google.gson.JsonArray; | |||
| import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| import me.chanjar.weixin.common.bean.WxJsapiSignature; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| @@ -26,44 +12,28 @@ import me.chanjar.weixin.common.session.StandardSessionManager; | |||
| import me.chanjar.weixin.common.session.WxSessionManager; | |||
| import me.chanjar.weixin.common.util.RandomUtils; | |||
| import me.chanjar.weixin.common.util.crypto.SHA1; | |||
| import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder; | |||
| import me.chanjar.weixin.common.util.http.DefaultApacheHttpClientBuilder; | |||
| import me.chanjar.weixin.common.util.http.RequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | |||
| import me.chanjar.weixin.common.util.http.URIUtil; | |||
| import me.chanjar.weixin.mp.api.WxMpCardService; | |||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | |||
| import me.chanjar.weixin.mp.api.WxMpDataCubeService; | |||
| import me.chanjar.weixin.mp.api.WxMpKefuService; | |||
| import me.chanjar.weixin.mp.api.WxMpMaterialService; | |||
| import me.chanjar.weixin.mp.api.WxMpMenuService; | |||
| import me.chanjar.weixin.mp.api.WxMpPayService; | |||
| import me.chanjar.weixin.mp.api.WxMpQrcodeService; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.api.WxMpStoreService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserBlacklistService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserTagService; | |||
| import me.chanjar.weixin.mp.bean.WxMpIndustry; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassNews; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassTagMessage; | |||
| import me.chanjar.weixin.mp.bean.WxMpMassVideo; | |||
| import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; | |||
| import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
| import me.chanjar.weixin.common.util.http.*; | |||
| import me.chanjar.weixin.mp.api.*; | |||
| import me.chanjar.weixin.mp.bean.*; | |||
| import me.chanjar.weixin.mp.bean.result.*; | |||
| import org.apache.http.HttpHost; | |||
| import org.apache.http.client.config.RequestConfig; | |||
| import org.apache.http.client.methods.CloseableHttpResponse; | |||
| import org.apache.http.client.methods.HttpGet; | |||
| import org.apache.http.conn.ssl.DefaultHostnameVerifier; | |||
| import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |||
| import org.apache.http.impl.client.BasicResponseHandler; | |||
| import org.apache.http.impl.client.CloseableHttpClient; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import java.io.IOException; | |||
| public class WxMpServiceImpl implements WxMpService { | |||
| private static final JsonParser JSON_PARSER = new JsonParser(); | |||
| protected final Logger log = LoggerFactory.getLogger(WxMpServiceImpl.class); | |||
| protected final Logger log = LoggerFactory.getLogger(this.getClass()); | |||
| /** | |||
| * 全局的是否正在刷新access token的锁 | |||
| @@ -99,6 +69,8 @@ public class WxMpServiceImpl implements WxMpService { | |||
| private WxMpUserBlacklistService blackListService = new WxMpUserBlacklistServiceImpl(this); | |||
| private WxMpTemplateMsgService templateMsgService = new WxMpTemplateMsgServiceImpl(this); | |||
| private CloseableHttpClient httpClient; | |||
| private HttpHost httpProxy; | |||
| @@ -251,35 +223,6 @@ 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 = this.post(url, templateMessage.toJson()); | |||
| final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||
| if (jsonObject.get("errcode").getAsInt() == 0){ | |||
| return jsonObject.get("msgid").getAsString(); | |||
| } | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| @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 this.post(url, wxMpIndustry.toJson()); | |||
| } | |||
| @Override | |||
| public WxMpIndustry getIndustry() throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry"; | |||
| String responseContent = this.get(url, null); | |||
| return WxMpIndustry.fromJson(responseContent); | |||
| } | |||
| @Override | |||
| public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/semantic/semproxy/search"; | |||
| @@ -477,10 +420,12 @@ public class WxMpServiceImpl implements WxMpService { | |||
| } | |||
| return null; | |||
| } catch (IOException e) { | |||
| this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage()); | |||
| throw new RuntimeException(e); | |||
| } | |||
| } | |||
| @Override | |||
| public HttpHost getHttpProxy() { | |||
| return this.httpProxy; | |||
| } | |||
| @@ -591,4 +536,9 @@ public class WxMpServiceImpl implements WxMpService { | |||
| return this.storeService; | |||
| } | |||
| @Override | |||
| public WxMpTemplateMsgService getTemplateMsgService() { | |||
| return this.templateMsgService; | |||
| } | |||
| } | |||
| @@ -1,23 +1,19 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import com.google.common.collect.Lists; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.reflect.TypeToken; | |||
| import me.chanjar.weixin.common.annotation.Required; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.BeanUtils; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.api.WxMpStoreService; | |||
| import me.chanjar.weixin.mp.bean.store.WxMpStoreBaseInfo; | |||
| import me.chanjar.weixin.mp.bean.store.WxMpStoreInfo; | |||
| import me.chanjar.weixin.mp.bean.store.WxMpStoreListResult; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import org.joor.Reflect; | |||
| import java.lang.reflect.Field; | |||
| import java.util.List; | |||
| import java.util.Map.Entry; | |||
| /** | |||
| * Created by Binary Wang on 2016/9/26. | |||
| @@ -35,7 +31,7 @@ public class WxMpStoreServiceImpl implements WxMpStoreService { | |||
| @Override | |||
| public void add(WxMpStoreBaseInfo request) throws WxErrorException { | |||
| checkParameters(request); | |||
| BeanUtils.checkRequiredFields(request); | |||
| String url = API_BASE_URL + "/addpoi"; | |||
| String response = this.wxMpService.post(url, request.toJson()); | |||
| @@ -71,28 +67,6 @@ public class WxMpStoreServiceImpl implements WxMpStoreService { | |||
| } | |||
| } | |||
| private void checkParameters(WxMpStoreBaseInfo request) { | |||
| List<String> nullFields = Lists.newArrayList(); | |||
| for (Entry<String, Reflect> entry : Reflect.on(request).fields() | |||
| .entrySet()) { | |||
| Reflect reflect = entry.getValue(); | |||
| try { | |||
| Field field = request.getClass().getDeclaredField(entry.getKey()); | |||
| if (field.isAnnotationPresent(Required.class) | |||
| && reflect.get() == null) { | |||
| nullFields.add(entry.getKey()); | |||
| } | |||
| } catch (NoSuchFieldException | SecurityException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| if (!nullFields.isEmpty()) { | |||
| throw new IllegalArgumentException("必填字段[" + nullFields + "]必须提供值"); | |||
| } | |||
| } | |||
| @Override | |||
| public WxMpStoreListResult list(int begin, int limit) | |||
| throws WxErrorException { | |||
| @@ -0,0 +1,95 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.api.WxMpTemplateMsgService; | |||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplate; | |||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry; | |||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| * Created by Binary Wang on 2016-10-14. | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * </pre> | |||
| */ | |||
| public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService { | |||
| public static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/template"; | |||
| private static final JsonParser JSON_PARSER = new JsonParser(); | |||
| private WxMpService wxMpService; | |||
| public WxMpTemplateMsgServiceImpl(WxMpService wxMpService) { | |||
| this.wxMpService = wxMpService; | |||
| } | |||
| @Override | |||
| public String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/message/template/send"; | |||
| String responseContent = this.wxMpService.post(url, templateMessage.toJson()); | |||
| final JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||
| if (jsonObject.get("errcode").getAsInt() == 0) { | |||
| return jsonObject.get("msgid").getAsString(); | |||
| } | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| @Override | |||
| public boolean setIndustry(WxMpTemplateIndustry wxMpIndustry) throws WxErrorException { | |||
| if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId() | |||
| || null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) { | |||
| throw new IllegalArgumentException("行业Id不能为空,请核实"); | |||
| } | |||
| String url = API_URL_PREFIX + "/api_set_industry"; | |||
| this.wxMpService.post(url, wxMpIndustry.toJson()); | |||
| return true; | |||
| } | |||
| @Override | |||
| public WxMpTemplateIndustry getIndustry() throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/get_industry"; | |||
| String responseContent = this.wxMpService.get(url, null); | |||
| return WxMpTemplateIndustry.fromJson(responseContent); | |||
| } | |||
| @Override | |||
| public String addTemplate(String shortTemplateId) throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/api_add_template"; | |||
| JsonObject jsonObject = new JsonObject(); | |||
| jsonObject.addProperty("template_id_short", shortTemplateId); | |||
| String responseContent = this.wxMpService.post(url, jsonObject.toString()); | |||
| final JsonObject result = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||
| if (result.get("errcode").getAsInt() == 0) { | |||
| return result.get("template_id").getAsString(); | |||
| } | |||
| throw new WxErrorException(WxError.fromJson(responseContent)); | |||
| } | |||
| @Override | |||
| public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/get_all_private_template"; | |||
| return WxMpTemplate.fromJson(this.wxMpService.get(url, null)); | |||
| } | |||
| @Override | |||
| public boolean delPrivateTemplate(String templateId) throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/del_private_template"; | |||
| JsonObject jsonObject = new JsonObject(); | |||
| jsonObject.addProperty("template_id", templateId); | |||
| String responseContent = this.wxMpService.post(url, jsonObject.toString()); | |||
| WxError error = WxError.fromJson(responseContent); | |||
| if (error.getErrorCode() == 0) { | |||
| return true; | |||
| } | |||
| throw new WxErrorException(error); | |||
| } | |||
| } | |||
| @@ -1,9 +1,6 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import java.util.List; | |||
| import com.google.gson.JsonObject; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| import me.chanjar.weixin.mp.api.WxMpUserService; | |||
| @@ -11,6 +8,8 @@ import me.chanjar.weixin.mp.bean.WxMpUserQuery; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||
| import me.chanjar.weixin.mp.bean.result.WxMpUserList; | |||
| import java.util.List; | |||
| /** | |||
| * Created by Binary Wang on 2016/7/21. | |||
| */ | |||
| @@ -31,6 +30,11 @@ public class WxMpUserServiceImpl implements WxMpUserService { | |||
| this.wxMpService.post(url, json.toString()); | |||
| } | |||
| @Override | |||
| public WxMpUser userInfo(String openid) throws WxErrorException { | |||
| return this.userInfo(openid, null); | |||
| } | |||
| @Override | |||
| public WxMpUser userInfo(String openid, String lang) throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/info"; | |||
| @@ -1,9 +1,14 @@ | |||
| package me.chanjar.weixin.mp.api.impl; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import com.google.gson.JsonArray; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.reflect.TypeToken; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.mp.api.WxMpService; | |||
| @@ -11,13 +16,10 @@ import me.chanjar.weixin.mp.api.WxMpUserTagService; | |||
| import me.chanjar.weixin.mp.bean.tag.WxTagListUser; | |||
| import me.chanjar.weixin.mp.bean.tag.WxUserTag; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import java.util.List; | |||
| /** | |||
| * | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/9/2. | |||
| */ | |||
| public class WxMpUserTagServiceImpl implements WxMpUserTagService { | |||
| @@ -102,7 +104,7 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { | |||
| @Override | |||
| public boolean batchTagging(Long tagId, String[] openids) | |||
| throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging"; | |||
| String url = API_URL_PREFIX + "/members/batchtagging"; | |||
| JsonObject json = new JsonObject(); | |||
| json.addProperty("tagid", tagId); | |||
| @@ -124,7 +126,7 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { | |||
| @Override | |||
| public boolean batchUntagging(Long tagId, String[] openids) | |||
| throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging"; | |||
| String url = API_URL_PREFIX + "/members/batchuntagging"; | |||
| JsonObject json = new JsonObject(); | |||
| json.addProperty("tagid", tagId); | |||
| @@ -144,8 +146,8 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { | |||
| } | |||
| @Override | |||
| public List<Integer> userTagList(String openid) throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/cgi-bin/tags/getidlist"; | |||
| public List<Long> userTagList(String openid) throws WxErrorException { | |||
| String url = API_URL_PREFIX + "/getidlist"; | |||
| JsonObject json = new JsonObject(); | |||
| json.addProperty("openid", openid); | |||
| @@ -154,7 +156,7 @@ public class WxMpUserTagServiceImpl implements WxMpUserTagService { | |||
| return WxMpGsonBuilder.create().fromJson( | |||
| new JsonParser().parse(responseContent).getAsJsonObject().get("tagid_list"), | |||
| new TypeToken<List<Integer>>() { | |||
| new TypeToken<List<Long>>() { | |||
| }.getType()); | |||
| } | |||
| } | |||
| @@ -1,38 +0,0 @@ | |||
| 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 this.id; | |||
| } | |||
| public void setId(String id) { | |||
| this.id = id; | |||
| } | |||
| public String getFirstClass() { | |||
| return this.firstClass; | |||
| } | |||
| public void setFirstClass(String firstClass) { | |||
| this.firstClass = firstClass; | |||
| } | |||
| public String getSecondClass() { | |||
| return this.secondClass; | |||
| } | |||
| public void setSecondClass(String secondClass) { | |||
| this.secondClass = secondClass; | |||
| } | |||
| } | |||
| @@ -1,6 +1,8 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| /** | |||
| * 微信卡券 | |||
| * | |||
| @@ -61,12 +63,6 @@ public class WxMpCard { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpCard{" + | |||
| "cardId='" + this.cardId + '\'' + | |||
| ", beginTime=" + this.beginTime + | |||
| ", endTime=" + this.endTime + | |||
| ", userCardStatus='" + this.userCardStatus + '\'' + | |||
| ", canConsume=" + this.canConsume + | |||
| '}'; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @@ -1,39 +0,0 @@ | |||
| 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 this.primaryIndustry; | |||
| } | |||
| public void setPrimaryIndustry(Industry primaryIndustry) { | |||
| this.primaryIndustry = primaryIndustry; | |||
| } | |||
| public Industry getSecondIndustry() { | |||
| return this.secondIndustry; | |||
| } | |||
| public void setSecondIndustry(Industry secondIndustry) { | |||
| this.secondIndustry = secondIndustry; | |||
| } | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.io.Serializable; | |||
| @@ -14,7 +15,7 @@ import java.util.List; | |||
| public class WxMpMassNews implements Serializable { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = 565937155013581016L; | |||
| private List<WxMpMassNewsArticle> articles = new ArrayList<>(); | |||
| @@ -137,14 +138,12 @@ public class WxMpMassNews implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMassNewsArticle [" + "thumbMediaId=" + this.thumbMediaId + ", author=" + this.author + ", title=" + this.title + | |||
| ", contentSourceUrl=" + this.contentSourceUrl + ", content=" + this.content + ", digest=" + this.digest + | |||
| ", showCoverPic=" + this.showCoverPic + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMassNews [" + "articles=" + this.articles + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @@ -8,9 +8,6 @@ import java.io.Serializable; | |||
| * @author miller | |||
| */ | |||
| public class WxMpMassPreviewMessage implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = 9095211638358424020L; | |||
| private String toWxUsername; | |||
| private String msgType; | |||
| @@ -1,16 +1,15 @@ | |||
| package me.chanjar.weixin.mp.bean.datacube; | |||
| import java.util.List; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import com.google.gson.reflect.TypeToken; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.util.List; | |||
| /** | |||
| * 图文分析数据接口返回结果对象 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/8/24. | |||
| */ | |||
| public class WxDataCubeArticleResult extends WxDataCubeBaseResult { | |||
| @@ -26,7 +25,9 @@ public class WxDataCubeArticleResult extends WxDataCubeBaseResult { | |||
| /** | |||
| * msgid | |||
| * 请注意:这里的msgid实际上是由msgid(图文消息id,这也就是群发接口调用后返回的msg_data_id)和index(消息次序索引)组成, 例如12003_3, 其中12003是msgid,即一次群发的消息的id; 3为index,假设该次群发的图文消息共5个文章(因为可能为多图文),3表示5个中的第3个 | |||
| * 请注意:这里的msgid实际上是由msgid(图文消息id,这也就是群发接口调用后返回的msg_data_id) | |||
| * 和index(消息次序索引)组成, 例如12003_3, 其中12003是msgid,即一次群发的消息的id; 3为index, | |||
| * 假设该次群发的图文消息共5个文章(因为可能为多图文),3表示5个中的第3个 | |||
| */ | |||
| @SerializedName("msgid") | |||
| private String msgId; | |||
| @@ -10,7 +10,7 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * 图文分析数据接口返回结果对象 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/8/24. | |||
| */ | |||
| public class WxDataCubeArticleTotal extends WxDataCubeBaseResult { | |||
| @@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName; | |||
| /** | |||
| * 获取图文群发总数据接口(getarticletotal)中的详细字段 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/8/24. | |||
| */ | |||
| public class WxDataCubeArticleTotalDetail { | |||
| @@ -1,18 +1,17 @@ | |||
| package me.chanjar.weixin.mp.bean.datacube; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| /** | |||
| * 统计接口的共用属性类 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/8/25. | |||
| */ | |||
| public class WxDataCubeBaseResult { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| /** | |||
| @@ -29,5 +28,5 @@ public class WxDataCubeBaseResult { | |||
| public void setRefDate(String refDate) { | |||
| this.refDate = refDate; | |||
| } | |||
| } | |||
| @@ -10,7 +10,7 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * 接口分析数据接口返回结果对象 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/8/30. | |||
| */ | |||
| public class WxDataCubeInterfaceResult extends WxDataCubeBaseResult { | |||
| @@ -98,5 +98,5 @@ public class WxDataCubeInterfaceResult extends WxDataCubeBaseResult { | |||
| new TypeToken<List<WxDataCubeInterfaceResult>>() { | |||
| }.getType()); | |||
| } | |||
| } | |||
| @@ -10,7 +10,7 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * 消息分析数据接口返回结果对象 | |||
| * @author binarywang(https://github.com/binarywang) | |||
| * @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a> | |||
| * Created by Binary Wang on 2016/8/29. | |||
| */ | |||
| public class WxDataCubeMsgResult extends WxDataCubeBaseResult { | |||
| @@ -1,17 +1,14 @@ | |||
| package me.chanjar.weixin.mp.bean.datacube; | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.reflect.TypeToken; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| * 累计用户数据接口的返回JSON数据包 | |||
| @@ -46,7 +43,7 @@ public class WxDataCubeUserCumulate implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static List<WxDataCubeUserCumulate> fromJson(String json) { | |||
| @@ -1,17 +1,14 @@ | |||
| package me.chanjar.weixin.mp.bean.datacube; | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.JsonParser; | |||
| import com.google.gson.reflect.TypeToken; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| * 用户增减数据接口的返回JSON数据包 | |||
| @@ -65,7 +62,7 @@ public class WxDataCubeUserSummary implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static List<WxDataCubeUserSummary> fromJson(String json) { | |||
| @@ -1,24 +1,25 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.kefu; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.ImageBuilder; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.MusicBuilder; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.NewsBuilder; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.TextBuilder; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.VideoBuilder; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.VoiceBuilder; | |||
| import me.chanjar.weixin.mp.bean.custombuilder.WxCardBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.ImageBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.MpNewsBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.MusicBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.NewsBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.TextBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.VideoBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.VoiceBuilder; | |||
| import me.chanjar.weixin.mp.builder.kefu.WxCardBuilder; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * 客服消息 | |||
| * @author chanjarster | |||
| * | |||
| * @author chanjarster | |||
| */ | |||
| public class WxMpCustomMessage implements Serializable { | |||
| public class WxMpKefuMessage implements Serializable { | |||
| private static final long serialVersionUID = -9196732086954365246L; | |||
| private String toUser; | |||
| @@ -32,18 +33,85 @@ public class WxMpCustomMessage implements Serializable { | |||
| private String hqMusicUrl; | |||
| private String kfAccount; | |||
| private String cardId; | |||
| private String mpNewsMediaId; | |||
| private List<WxArticle> articles = new ArrayList<>(); | |||
| /** | |||
| * 获得文本消息builder | |||
| */ | |||
| public static TextBuilder TEXT() { | |||
| return new TextBuilder(); | |||
| } | |||
| /** | |||
| * 获得图片消息builder | |||
| */ | |||
| public static ImageBuilder IMAGE() { | |||
| return new ImageBuilder(); | |||
| } | |||
| /** | |||
| * 获得语音消息builder | |||
| */ | |||
| public static VoiceBuilder VOICE() { | |||
| return new VoiceBuilder(); | |||
| } | |||
| /** | |||
| * 获得视频消息builder | |||
| */ | |||
| public static VideoBuilder VIDEO() { | |||
| return new VideoBuilder(); | |||
| } | |||
| /** | |||
| * 获得音乐消息builder | |||
| */ | |||
| public static MusicBuilder MUSIC() { | |||
| return new MusicBuilder(); | |||
| } | |||
| /** | |||
| * 获得图文消息(点击跳转到外链)builder | |||
| */ | |||
| public static NewsBuilder NEWS() { | |||
| return new NewsBuilder(); | |||
| } | |||
| /** | |||
| * 获得图文消息(点击跳转到图文消息页面)builder | |||
| */ | |||
| public static MpNewsBuilder MPNEWS() { | |||
| return new MpNewsBuilder(); | |||
| } | |||
| /** | |||
| * 获得卡券消息builder | |||
| */ | |||
| public static WxCardBuilder WXCARD() { | |||
| return new WxCardBuilder(); | |||
| } | |||
| public String getToUser() { | |||
| return this.toUser; | |||
| } | |||
| public void setToUser(String toUser) { | |||
| this.toUser = toUser; | |||
| } | |||
| public String getMsgType() { | |||
| return this.msgType; | |||
| } | |||
| public String getMpNewsMediaId() { | |||
| return this.mpNewsMediaId; | |||
| } | |||
| public void setMpNewsMediaId(String mpNewsMediaId) { | |||
| this.mpNewsMediaId = mpNewsMediaId; | |||
| } | |||
| /** | |||
| * <pre> | |||
| * 请使用 | |||
| @@ -53,52 +121,68 @@ public class WxMpCustomMessage implements Serializable { | |||
| * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MUSIC} | |||
| * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_VIDEO} | |||
| * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_NEWS} | |||
| * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_MPNEWS} | |||
| * {@link me.chanjar.weixin.common.api.WxConsts#CUSTOM_MSG_WXCARD} | |||
| * </pre> | |||
| * | |||
| * @param msgType | |||
| */ | |||
| public void setMsgType(String msgType) { | |||
| this.msgType = msgType; | |||
| } | |||
| public String getContent() { | |||
| return this.content; | |||
| } | |||
| public void setContent(String content) { | |||
| this.content = content; | |||
| } | |||
| public String getMediaId() { | |||
| return this.mediaId; | |||
| } | |||
| public void setMediaId(String mediaId) { | |||
| this.mediaId = mediaId; | |||
| } | |||
| public String getThumbMediaId() { | |||
| return this.thumbMediaId; | |||
| } | |||
| public void setThumbMediaId(String thumbMediaId) { | |||
| this.thumbMediaId = thumbMediaId; | |||
| } | |||
| public String getTitle() { | |||
| return this.title; | |||
| } | |||
| public void setTitle(String title) { | |||
| this.title = title; | |||
| } | |||
| public String getDescription() { | |||
| return this.description; | |||
| } | |||
| public void setDescription(String description) { | |||
| this.description = description; | |||
| } | |||
| public String getMusicUrl() { | |||
| return this.musicUrl; | |||
| } | |||
| public void setMusicUrl(String musicUrl) { | |||
| this.musicUrl = musicUrl; | |||
| } | |||
| public String getHqMusicUrl() { | |||
| return this.hqMusicUrl; | |||
| } | |||
| public void setHqMusicUrl(String hqMusicUrl) { | |||
| this.hqMusicUrl = hqMusicUrl; | |||
| } | |||
| @@ -114,6 +198,7 @@ public class WxMpCustomMessage implements Serializable { | |||
| public List<WxArticle> getArticles() { | |||
| return this.articles; | |||
| } | |||
| public void setArticles(List<WxArticle> articles) { | |||
| this.articles = articles; | |||
| } | |||
| @@ -122,8 +207,15 @@ public class WxMpCustomMessage implements Serializable { | |||
| return WxMpGsonBuilder.INSTANCE.create().toJson(this); | |||
| } | |||
| public static class WxArticle { | |||
| public String getKfAccount() { | |||
| return this.kfAccount; | |||
| } | |||
| public void setKfAccount(String kfAccount) { | |||
| this.kfAccount = kfAccount; | |||
| } | |||
| public static class WxArticle { | |||
| private String title; | |||
| private String description; | |||
| private String url; | |||
| @@ -132,85 +224,34 @@ public class WxMpCustomMessage implements Serializable { | |||
| public String getTitle() { | |||
| return this.title; | |||
| } | |||
| public void setTitle(String title) { | |||
| this.title = title; | |||
| } | |||
| public String getDescription() { | |||
| return this.description; | |||
| } | |||
| public void setDescription(String description) { | |||
| this.description = description; | |||
| } | |||
| public String getUrl() { | |||
| return this.url; | |||
| } | |||
| public void setUrl(String url) { | |||
| this.url = url; | |||
| } | |||
| public String getPicUrl() { | |||
| return this.picUrl; | |||
| } | |||
| public void setPicUrl(String picUrl) { | |||
| this.picUrl = picUrl; | |||
| } | |||
| } | |||
| /** | |||
| * 获得文本消息builder | |||
| */ | |||
| public static TextBuilder TEXT() { | |||
| return new TextBuilder(); | |||
| } | |||
| /** | |||
| * 获得图片消息builder | |||
| */ | |||
| public static ImageBuilder IMAGE() { | |||
| return new ImageBuilder(); | |||
| } | |||
| /** | |||
| * 获得语音消息builder | |||
| */ | |||
| public static VoiceBuilder VOICE() { | |||
| return new VoiceBuilder(); | |||
| } | |||
| /** | |||
| * 获得视频消息builder | |||
| */ | |||
| public static VideoBuilder VIDEO() { | |||
| return new VideoBuilder(); | |||
| } | |||
| /** | |||
| * 获得音乐消息builder | |||
| */ | |||
| public static MusicBuilder MUSIC() { | |||
| return new MusicBuilder(); | |||
| } | |||
| /** | |||
| * 获得图文消息builder | |||
| */ | |||
| public static NewsBuilder NEWS() { | |||
| return new NewsBuilder(); | |||
| } | |||
| /** | |||
| * 获得卡券消息builder | |||
| */ | |||
| public static WxCardBuilder WXCARD() { | |||
| return new WxCardBuilder(); | |||
| } | |||
| public String getKfAccount() { | |||
| return this.kfAccount; | |||
| } | |||
| public void setKfAccount(String kfAccount) { | |||
| this.kfAccount = kfAccount; | |||
| } | |||
| } | |||
| @@ -1,14 +1,11 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.request; | |||
| import java.io.Serializable; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.io.Serializable; | |||
| public class WxMpKfSessionRequest implements Serializable { | |||
| private static final long serialVersionUID = -5451863610674856927L; | |||
| @@ -17,7 +14,7 @@ public class WxMpKfSessionRequest implements Serializable { | |||
| */ | |||
| @SerializedName("kf_account") | |||
| private String kfAccount; | |||
| /** | |||
| * openid 客户openid | |||
| */ | |||
| @@ -31,9 +28,9 @@ public class WxMpKfSessionRequest implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String toJson() { | |||
| return WxMpGsonBuilder.INSTANCE.create().toJson(this); | |||
| } | |||
| @@ -1,12 +1,10 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import java.io.Serializable; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.Expose; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import java.io.Serializable; | |||
| /** | |||
| * 客服基本信息以及客服在线状态信息 | |||
| @@ -127,7 +125,7 @@ public class WxMpKfInfo implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String getWxAccount() { | |||
| @@ -1,15 +1,12 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.util.List; | |||
| /** | |||
| * | |||
| * | |||
| * @author Binary Wang | |||
| * | |||
| */ | |||
| @@ -19,7 +16,7 @@ public class WxMpKfList { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public List<WxMpKfInfo> getKfList() { | |||
| @@ -28,8 +25,8 @@ public class WxMpKfList { | |||
| public void setKfList(List<WxMpKfInfo> kfList) { | |||
| this.kfList = kfList; | |||
| } | |||
| } | |||
| public static WxMpKfList fromJson(String json) { | |||
| return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpKfList.class); | |||
| } | |||
| @@ -1,9 +1,8 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import java.util.List; | |||
| @@ -46,7 +45,7 @@ public class WxMpKfMsgList { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static WxMpKfMsgList fromJson(String responseContent) { | |||
| @@ -1,9 +1,7 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| /** | |||
| * Created by Binary Wang on 2016/7/18. | |||
| @@ -41,8 +39,9 @@ public class WxMpKfMsgRecord { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String getWorker() { | |||
| return this.worker; | |||
| } | |||
| @@ -1,15 +1,12 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.util.List; | |||
| /** | |||
| * | |||
| * | |||
| * @author Binary Wang | |||
| * | |||
| */ | |||
| @@ -19,9 +16,9 @@ public class WxMpKfOnlineList { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public List<WxMpKfInfo> getKfOnlineList() { | |||
| return this.kfOnlineList; | |||
| } | |||
| @@ -1,12 +1,10 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| /** | |||
| * | |||
| * | |||
| * @author Binary Wang | |||
| * | |||
| */ | |||
| @@ -39,9 +37,9 @@ public class WxMpKfSession { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String getKfAccount() { | |||
| return this.kfAccount; | |||
| } | |||
| @@ -1,14 +1,11 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| /** | |||
| * | |||
| * | |||
| * @author Binary Wang | |||
| * | |||
| */ | |||
| @@ -27,13 +24,13 @@ public class WxMpKfSessionGetResult { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static WxMpKfSessionGetResult fromJson(String json) { | |||
| return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpKfSessionGetResult.class); | |||
| } | |||
| public String getKfAccount() { | |||
| return this.kfAccount; | |||
| } | |||
| @@ -1,16 +1,13 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.util.List; | |||
| /** | |||
| * | |||
| * | |||
| * @author Binary Wang | |||
| * | |||
| */ | |||
| @@ -23,7 +20,7 @@ public class WxMpKfSessionList { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static WxMpKfSessionList fromJson(String json) { | |||
| @@ -1,16 +1,13 @@ | |||
| package me.chanjar.weixin.mp.bean.kefu.result; | |||
| import java.util.List; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.util.List; | |||
| /** | |||
| * | |||
| * | |||
| * @author Binary Wang | |||
| * | |||
| */ | |||
| @@ -29,7 +26,7 @@ public class WxMpKfSessionWaitCaseList { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static WxMpKfSessionWaitCaseList fromJson(String json) { | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| @@ -9,7 +9,7 @@ import java.io.Serializable; | |||
| */ | |||
| public class WxMediaImgUploadResult implements Serializable { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = 1996392453428768829L; | |||
| private String url; | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import java.io.File; | |||
| import java.util.HashMap; | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| @@ -7,7 +7,7 @@ import java.io.Serializable; | |||
| public class WxMpMaterialArticleUpdate implements Serializable { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -7611963949517780270L; | |||
| private String mediaId; | |||
| @@ -1,12 +1,10 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import java.io.Serializable; | |||
| public class WxMpMaterialCountResult implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -5568772662085874138L; | |||
| private int voiceCount; | |||
| private int videoCount; | |||
| @@ -47,8 +45,7 @@ public class WxMpMaterialCountResult implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMaterialCountResult [" + "voiceCount=" + this.voiceCount + ", videoCount=" + this.videoCount | |||
| + ", imageCount=" + this.imageCount + ", newsCount=" + this.newsCount + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @@ -1,14 +1,12 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| public class WxMpMaterialFileBatchGetResult implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -560388368297267884L; | |||
| private int totalCount; | |||
| private int itemCount; | |||
| @@ -40,7 +38,7 @@ public class WxMpMaterialFileBatchGetResult implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMaterialFileBatchGetResult [" + "totalCount=" + this.totalCount + ", itemCount=" + this.itemCount + ", items=" + this.items + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static class WxMaterialFileBatchGetNewsItem { | |||
| @@ -83,7 +81,7 @@ public class WxMpMaterialFileBatchGetResult implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMaterialFileBatchGetNewsItem [" + "mediaId=" + this.mediaId + ", updateTime=" + this.updateTime + ", name=" + this.name + ", url=" + this.url + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| import java.io.Serializable; | |||
| @@ -7,11 +8,8 @@ import java.util.ArrayList; | |||
| import java.util.List; | |||
| public class WxMpMaterialNews implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -3283203652013494976L; | |||
| private List<WxMpMaterialNewsArticle> articles = new ArrayList<>(); | |||
| public List<WxMpMaterialNewsArticle> getArticles() { | |||
| @@ -81,7 +79,6 @@ public class WxMpMaterialNews implements Serializable { | |||
| /** | |||
| * 点击图文消息跳转链接 | |||
| * @return | |||
| */ | |||
| private String url; | |||
| @@ -159,14 +156,12 @@ public class WxMpMaterialNews implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMassNewsArticle [" + "thumbMediaId=" + this.thumbMediaId + "thumbUrl=" + this.thumbUrl + ", author=" + this.author + ", title=" + this.title + | |||
| ", contentSourceUrl=" + this.contentSourceUrl + ", content=" + this.content + ", digest=" + this.digest + | |||
| ", showCoverPic=" + this.showCoverPic +", url=" + this.url + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMaterialNews [" + "articles=" + this.articles + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @@ -1,17 +1,14 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.mp.bean.WxMpMaterialNews; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| public class WxMpMaterialNewsBatchGetResult implements Serializable { | |||
| /** | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -1617952797921001666L; | |||
| private int totalCount; | |||
| private int itemCount; | |||
| private List<WxMaterialNewsBatchGetNewsItem> items; | |||
| @@ -42,7 +39,7 @@ public class WxMpMaterialNewsBatchGetResult implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMpMaterialNewsBatchGetResult [" + "totalCount=" + this.totalCount + ", itemCount=" + this.itemCount + ", items=" + this.items + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public static class WxMaterialNewsBatchGetNewsItem { | |||
| @@ -76,7 +73,7 @@ public class WxMpMaterialNewsBatchGetResult implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return "WxMaterialNewsBatchGetNewsItem [" + "mediaId=" + this.mediaId + ", updateTime=" + this.updateTime + ", content=" + this.content + "]"; | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| @@ -7,7 +7,7 @@ import java.io.Serializable; | |||
| public class WxMpMaterialUploadResult implements Serializable { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -128818731449449537L; | |||
| private String mediaId; | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean.result; | |||
| package me.chanjar.weixin.mp.bean.material; | |||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
| @@ -7,7 +7,7 @@ import java.io.Serializable; | |||
| public class WxMpMaterialVideoInfoResult implements Serializable { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = 1269131745333792202L; | |||
| private String title; | |||
| @@ -1,22 +1,19 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.apache.commons.io.IOUtils; | |||
| import org.apache.commons.lang3.builder.ToStringBuilder; | |||
| import org.apache.commons.lang3.builder.ToStringStyle; | |||
| package me.chanjar.weixin.mp.bean.message; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.util.ToStringUtils; | |||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | |||
| import me.chanjar.weixin.mp.util.crypto.WxMpCryptUtil; | |||
| import me.chanjar.weixin.mp.util.xml.XStreamTransformer; | |||
| import org.apache.commons.io.IOUtils; | |||
| import java.io.IOException; | |||
| import java.io.InputStream; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| /** | |||
| * <pre> | |||
| @@ -56,6 +53,9 @@ public class WxMpXmlMessage implements Serializable { | |||
| @XStreamConverter(value = XStreamCDataConverter.class) | |||
| private String content; | |||
| @XStreamAlias("MenuId") | |||
| private Long menuId; | |||
| @XStreamAlias("MsgId") | |||
| private Long msgId; | |||
| @@ -239,6 +239,54 @@ public class WxMpXmlMessage implements Serializable { | |||
| @XStreamAlias("msg") | |||
| private String msg; | |||
| /////////////////////////////////////// | |||
| // 微信认证事件推送 | |||
| /////////////////////////////////////// | |||
| /** | |||
| * ExpiredTime | |||
| * 资质认证成功/名称认证成功: 有效期 (整形),指的是时间戳,将于该时间戳认证过期 | |||
| * 年审通知: 有效期 (整形),指的是时间戳,将于该时间戳认证过期,需尽快年审 | |||
| * 认证过期失效通知: 有效期 (整形),指的是时间戳,表示已于该时间戳认证过期,需要重新发起微信认证 | |||
| */ | |||
| @XStreamAlias("ExpiredTime") | |||
| private Long expiredTime; | |||
| /** | |||
| * FailTime | |||
| * 失败发生时间 (整形),时间戳 | |||
| */ | |||
| @XStreamAlias("FailTime") | |||
| private Long failTime; | |||
| /** | |||
| * FailReason | |||
| * 认证失败的原因 | |||
| */ | |||
| @XStreamAlias("FailReason") | |||
| private String failReason; | |||
| public Long getExpiredTime() { | |||
| return this.expiredTime; | |||
| } | |||
| public void setExpiredTime(Long expiredTime) { | |||
| this.expiredTime = expiredTime; | |||
| } | |||
| public Long getFailTime() { | |||
| return this.failTime; | |||
| } | |||
| public void setFailTime(Long failTime) { | |||
| this.failTime = failTime; | |||
| } | |||
| public String getFailReason() { | |||
| return this.failReason; | |||
| } | |||
| public void setFailReason(String failReason) { | |||
| this.failReason = failReason; | |||
| } | |||
| public String getStoreUniqId() { | |||
| return this.storeUniqId; | |||
| } | |||
| @@ -639,6 +687,14 @@ public class WxMpXmlMessage implements Serializable { | |||
| this.sendLocationInfo = sendLocationInfo; | |||
| } | |||
| public Long getMenuId() { | |||
| return this.menuId; | |||
| } | |||
| public void setMenuId(Long menuId) { | |||
| this.menuId = menuId; | |||
| } | |||
| public String getKfAccount() { | |||
| return this.kfAccount; | |||
| } | |||
| @@ -667,7 +723,7 @@ public class WxMpXmlMessage implements Serializable { | |||
| public static class ScanCodeInfo { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| @XStreamAlias("ScanType") | |||
| @@ -707,7 +763,7 @@ public class WxMpXmlMessage implements Serializable { | |||
| public static class SendPicsInfo { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| @XStreamAlias("Count") | |||
| @@ -732,8 +788,7 @@ public class WxMpXmlMessage implements Serializable { | |||
| public static class Item { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, | |||
| ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| @XStreamAlias("PicMd5Sum") | |||
| @@ -775,7 +830,7 @@ public class WxMpXmlMessage implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| public String getLocationX() { | |||
| @@ -821,6 +876,6 @@ public class WxMpXmlMessage implements Serializable { | |||
| @Override | |||
| public String toString() { | |||
| return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE); | |||
| return ToStringUtils.toSimpleString(this); | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.message; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| @@ -9,7 +9,7 @@ import me.chanjar.weixin.common.util.xml.XStreamMediaIdConverter; | |||
| public class WxMpXmlOutImageMessage extends WxMpXmlOutMessage { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -2684778597067990723L; | |||
| @XStreamAlias("Image") | |||
| @@ -1,10 +1,10 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.message; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | |||
| import me.chanjar.weixin.mp.bean.outxmlbuilder.*; | |||
| import me.chanjar.weixin.mp.builder.outxml.*; | |||
| import me.chanjar.weixin.mp.util.crypto.WxMpCryptUtil; | |||
| import me.chanjar.weixin.mp.util.xml.XStreamTransformer; | |||
| @@ -18,14 +18,14 @@ public abstract class WxMpXmlOutMessage implements Serializable { | |||
| @XStreamAlias("ToUserName") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| protected String toUserName; | |||
| @XStreamAlias("FromUserName") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| protected String fromUserName; | |||
| @XStreamAlias("CreateTime") | |||
| protected Long createTime; | |||
| @XStreamAlias("MsgType") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| protected String msgType; | |||
| @@ -61,7 +61,7 @@ public abstract class WxMpXmlOutMessage implements Serializable { | |||
| public void setMsgType(String msgType) { | |||
| this.msgType = msgType; | |||
| } | |||
| public String toXml() { | |||
| return XStreamTransformer.toXml((Class<WxMpXmlOutMessage>) this.getClass(), this); | |||
| } | |||
| @@ -95,28 +95,28 @@ public abstract class WxMpXmlOutMessage implements Serializable { | |||
| public static VoiceBuilder VOICE() { | |||
| return new VoiceBuilder(); | |||
| } | |||
| /** | |||
| * 获得视频消息builder | |||
| */ | |||
| public static VideoBuilder VIDEO() { | |||
| return new VideoBuilder(); | |||
| } | |||
| /** | |||
| * 获得音乐消息builder | |||
| */ | |||
| public static MusicBuilder MUSIC() { | |||
| return new MusicBuilder(); | |||
| } | |||
| /** | |||
| * 获得图文消息builder | |||
| */ | |||
| public static NewsBuilder NEWS() { | |||
| return new NewsBuilder(); | |||
| } | |||
| /** | |||
| * 获得客服消息builder | |||
| */ | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.message; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| @@ -9,7 +9,7 @@ import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; | |||
| public class WxMpXmlOutMusicMessage extends WxMpXmlOutMessage { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -4159937804975448945L; | |||
| @XStreamAlias("Music") | |||
| @@ -34,7 +34,7 @@ public class WxMpXmlOutMusicMessage extends WxMpXmlOutMessage { | |||
| public void setDescription(String description) { | |||
| this.music.setDescription(description); | |||
| } | |||
| public String getThumbMediaId() { | |||
| return this.music.getThumbMediaId(); | |||
| } | |||
| @@ -58,10 +58,10 @@ public class WxMpXmlOutMusicMessage extends WxMpXmlOutMessage { | |||
| public void setHqMusicUrl(String hqMusicUrl) { | |||
| this.music.setHqMusicUrl(hqMusicUrl); | |||
| } | |||
| @XStreamAlias("Music") | |||
| public static class Music { | |||
| @XStreamAlias("Title") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String title; | |||
| @@ -73,15 +73,15 @@ public class WxMpXmlOutMusicMessage extends WxMpXmlOutMessage { | |||
| @XStreamAlias("ThumbMediaId") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String thumbMediaId; | |||
| @XStreamAlias("MusicUrl") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String musicUrl; | |||
| @XStreamAlias("HQMusicUrl") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String hqMusicUrl; | |||
| public String getTitle() { | |||
| return this.title; | |||
| } | |||
| @@ -121,7 +121,7 @@ public class WxMpXmlOutMusicMessage extends WxMpXmlOutMessage { | |||
| public void setHqMusicUrl(String hqMusicUrl) { | |||
| this.hqMusicUrl = hqMusicUrl; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package me.chanjar.weixin.mp.bean; | |||
| package me.chanjar.weixin.mp.bean.message; | |||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | |||
| import com.thoughtworks.xstream.annotations.XStreamConverter; | |||
| @@ -12,16 +12,16 @@ import java.util.List; | |||
| public class WxMpXmlOutNewsMessage extends WxMpXmlOutMessage { | |||
| /** | |||
| * | |||
| * | |||
| */ | |||
| private static final long serialVersionUID = -4604402850905714772L; | |||
| @XStreamAlias("ArticleCount") | |||
| protected int articleCount; | |||
| @XStreamAlias("Articles") | |||
| protected final List<Item> articles = new ArrayList<>(); | |||
| public WxMpXmlOutNewsMessage() { | |||
| this.msgType = WxConsts.XML_MSG_NEWS; | |||
| } | |||
| @@ -34,15 +34,15 @@ public class WxMpXmlOutNewsMessage extends WxMpXmlOutMessage { | |||
| this.articles.add(item); | |||
| this.articleCount = this.articles.size(); | |||
| } | |||
| public List<Item> getArticles() { | |||
| return this.articles; | |||
| } | |||
| @XStreamAlias("item") | |||
| public static class Item { | |||
| @XStreamAlias("Title") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String Title; | |||
| @@ -54,11 +54,11 @@ public class WxMpXmlOutNewsMessage extends WxMpXmlOutMessage { | |||
| @XStreamAlias("PicUrl") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String PicUrl; | |||
| @XStreamAlias("Url") | |||
| @XStreamConverter(value=XStreamCDataConverter.class) | |||
| private String Url; | |||
| public String getTitle() { | |||
| return this.Title; | |||
| } | |||