@@ -4,10 +4,11 @@ | |||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||
<modelVersion>4.0.0</modelVersion> | <modelVersion>4.0.0</modelVersion> | ||||
<groupId>me.chanjar</groupId> | <groupId>me.chanjar</groupId> | ||||
<artifactId>weixin-java-enterprise-tools</artifactId> | |||||
<artifactId>weixin-java-parent</artifactId> | |||||
<version>1.0.3-SNAPSHOT</version> | <version>1.0.3-SNAPSHOT</version> | ||||
<name>WeiXin Java Enterprise Tools</name> | |||||
<description>微信企业号Java SDK</description> | |||||
<packaging>pom</packaging> | |||||
<name>WeiXin Java Tools - Parent</name> | |||||
<description>微信公众号、企业号上级POM</description> | |||||
<url>https://github.com/chanjarster/weixin-java-tools</url> | <url>https://github.com/chanjarster/weixin-java-tools</url> | ||||
<licenses> | <licenses> | ||||
@@ -30,19 +31,20 @@ | |||||
<url>https://github.com/chanjarster/weixin-java-tools</url> | <url>https://github.com/chanjarster/weixin-java-tools</url> | ||||
</scm> | </scm> | ||||
<modules> | |||||
<module>weixin-java-common</module> | |||||
<module>weixin-java-cp</module> | |||||
<module>weixin-java-mp</module> | |||||
</modules> | |||||
<properties> | <properties> | ||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||
<downloadJavadocs>true</downloadJavadocs> | <downloadJavadocs>true</downloadJavadocs> | ||||
<downloadSources>true</downloadSources> | <downloadSources>true</downloadSources> | ||||
<httpclient.version>4.3.5</httpclient.version> | <httpclient.version>4.3.5</httpclient.version> | ||||
</properties> | </properties> | ||||
<dependencies> | <dependencies> | ||||
<dependency> | |||||
<groupId>junit</groupId> | |||||
<artifactId>junit</artifactId> | |||||
<version>4.11</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | <dependency> | ||||
<groupId>org.apache.httpcomponents</groupId> | <groupId>org.apache.httpcomponents</groupId> | ||||
<artifactId>fluent-hc</artifactId> | <artifactId>fluent-hc</artifactId> | ||||
@@ -63,34 +65,11 @@ | |||||
<artifactId>jaxb-impl</artifactId> | <artifactId>jaxb-impl</artifactId> | ||||
<version>2.2.7</version> | <version>2.2.7</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.apache.oltu.oauth2</groupId> | |||||
<artifactId>org.apache.oltu.oauth2.client</artifactId> | |||||
<version>1.0.0</version> | |||||
</dependency> | |||||
<dependency> | <dependency> | ||||
<groupId>com.google.code.gson</groupId> | <groupId>com.google.code.gson</groupId> | ||||
<artifactId>gson</artifactId> | <artifactId>gson</artifactId> | ||||
<version>2.2.2</version> | <version>2.2.2</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.testng</groupId> | |||||
<artifactId>testng</artifactId> | |||||
<version>6.8.7</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.mockito</groupId> | |||||
<artifactId>mockito-all</artifactId> | |||||
<version>1.9.5</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.inject</groupId> | |||||
<artifactId>guice</artifactId> | |||||
<version>3.0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | <dependency> | ||||
<groupId>org.apache.commons</groupId> | <groupId>org.apache.commons</groupId> | ||||
<artifactId>commons-lang3</artifactId> | <artifactId>commons-lang3</artifactId> | ||||
@@ -106,20 +85,49 @@ | |||||
<artifactId>commons-io</artifactId> | <artifactId>commons-io</artifactId> | ||||
<version>2.4</version> | <version>2.4</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-server</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-servlet</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
<dependencyManagement> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>junit</groupId> | |||||
<artifactId>junit</artifactId> | |||||
<version>4.11</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.inject</groupId> | |||||
<artifactId>guice</artifactId> | |||||
<version>3.0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.testng</groupId> | |||||
<artifactId>testng</artifactId> | |||||
<version>6.8.7</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.mockito</groupId> | |||||
<artifactId>mockito-all</artifactId> | |||||
<version>1.9.5</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-server</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-servlet</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | |||||
</dependencyManagement> | |||||
<distributionManagement> | <distributionManagement> | ||||
<snapshotRepository> | <snapshotRepository> | ||||
<id>ossrh</id> | <id>ossrh</id> | ||||
@@ -186,17 +194,17 @@ | |||||
</profiles> | </profiles> | ||||
<build> | <build> | ||||
<pluginManagement> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.apache.maven.plugins</groupId> | |||||
<artifactId>maven-surefire-plugin</artifactId> | |||||
<version>2.17</version> | |||||
</plugin> | |||||
</plugins> | |||||
</pluginManagement> | |||||
<plugins> | <plugins> | ||||
<plugin> | |||||
<groupId>org.apache.maven.plugins</groupId> | |||||
<artifactId>maven-surefire-plugin</artifactId> | |||||
<version>2.17</version> | |||||
<configuration> | |||||
<suiteXmlFiles> | |||||
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> | |||||
</suiteXmlFiles> | |||||
</configuration> | |||||
</plugin> | |||||
<plugin> | <plugin> | ||||
<groupId>org.sonatype.plugins</groupId> | <groupId>org.sonatype.plugins</groupId> | ||||
<artifactId>nexus-staging-maven-plugin</artifactId> | <artifactId>nexus-staging-maven-plugin</artifactId> |
@@ -0,0 +1,68 @@ | |||||
<?xml version="1.0"?> | |||||
<project | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<parent> | |||||
<groupId>me.chanjar</groupId> | |||||
<artifactId>weixin-java-parent</artifactId> | |||||
<version>1.0.3-SNAPSHOT</version> | |||||
</parent> | |||||
<artifactId>weixin-java-common</artifactId> | |||||
<name>WeiXin Java Tools - Common</name> | |||||
<description>微信公众号、企业号Java SDK Common</description> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>junit</groupId> | |||||
<artifactId>junit</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.testng</groupId> | |||||
<artifactId>testng</artifactId> | |||||
<version>6.8.7</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.mockito</groupId> | |||||
<artifactId>mockito-all</artifactId> | |||||
<version>1.9.5</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.inject</groupId> | |||||
<artifactId>guice</artifactId> | |||||
<version>3.0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-server</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-servlet</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.apache.maven.plugins</groupId> | |||||
<artifactId>maven-surefire-plugin</artifactId> | |||||
<configuration> | |||||
<suiteXmlFiles> | |||||
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> | |||||
</suiteXmlFiles> | |||||
</configuration> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -1,12 +1,12 @@ | |||||
package me.chanjar.weixin.mp.bean; | |||||
package me.chanjar.weixin.common.bean; | |||||
import me.chanjar.weixin.mp.util.json.WxGsonBuilder; | |||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
public class WxAccessToken { | public class WxAccessToken { | ||||
private String accessToken; | private String accessToken; | ||||
private int expiresIn; | |||||
private int expiresIn = -1; | |||||
public String getAccessToken() { | public String getAccessToken() { | ||||
return accessToken; | return accessToken; |
@@ -1,15 +1,15 @@ | |||||
package me.chanjar.weixin.mp.bean; | |||||
package me.chanjar.weixin.common.bean; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.mp.util.json.WxGsonBuilder; | |||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
/** | /** | ||||
* 公众号菜单 | |||||
* @author chanjarster | |||||
* 企业号菜单 | |||||
* @author Daniel Qian | |||||
* | * | ||||
*/ | */ | ||||
public class WxMenu { | public class WxMenu { |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.common.bean.result; | package me.chanjar.weixin.common.bean.result; | ||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
/** | /** | ||||
* 微信错误码说明 | * 微信错误码说明 | ||||
@@ -32,9 +32,16 @@ public class WxError { | |||||
this.errorMsg = errorMsg; | this.errorMsg = errorMsg; | ||||
} | } | ||||
public String getJson() { | |||||
return json; | |||||
} | |||||
public void setJson(String json) { | |||||
this.json = json; | |||||
} | |||||
public static WxError fromJson(String json) { | public static WxError fromJson(String json) { | ||||
WxError error = WxCpGsonBuilder.create().fromJson(json, WxError.class); | |||||
error.json = json; | |||||
WxError error = WxGsonBuilder.create().fromJson(json, WxError.class); | |||||
return error; | return error; | ||||
} | } | ||||
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.mp.bean.result; | |||||
package me.chanjar.weixin.common.bean.result; | |||||
import me.chanjar.weixin.mp.util.json.WxGsonBuilder; | |||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
public class WxMediaUploadResult { | public class WxMediaUploadResult { | ||||
@@ -33,10 +33,6 @@ public class WxMediaUploadResult { | |||||
this.createdAt = createdAt; | this.createdAt = createdAt; | ||||
} | } | ||||
public static WxMediaUploadResult fromJson(String json) { | |||||
return WxGsonBuilder.create().fromJson(json, WxMediaUploadResult.class); | |||||
} | |||||
public String getThumbMediaId() { | public String getThumbMediaId() { | ||||
return thumbMediaId; | return thumbMediaId; | ||||
} | } | ||||
@@ -45,6 +41,10 @@ public class WxMediaUploadResult { | |||||
this.thumbMediaId = thumbMediaId; | this.thumbMediaId = thumbMediaId; | ||||
} | } | ||||
public static WxMediaUploadResult fromJson(String json) { | |||||
return WxGsonBuilder.create().fromJson(json, WxMediaUploadResult.class); | |||||
} | |||||
@Override | @Override | ||||
public String toString() { | public String toString() { | ||||
return "WxUploadResult [type=" + type + ", media_id=" + mediaId + ", thumb_media_id=" + thumbMediaId | return "WxUploadResult [type=" + type + ", media_id=" + mediaId + ", thumb_media_id=" + thumbMediaId |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.exception; | |||||
package me.chanjar.weixin.common.exception; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
@@ -1,11 +1,11 @@ | |||||
package me.chanjar.weixin.enterprise.util.crypto; | |||||
package me.chanjar.weixin.common.util.crypto; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
class ByteGroup { | |||||
public class ByteGroup { | |||||
ArrayList<Byte> byteContainer = new ArrayList<Byte>(); | ArrayList<Byte> byteContainer = new ArrayList<Byte>(); | ||||
byte[] toBytes() { | |||||
public byte[] toBytes() { | |||||
byte[] bytes = new byte[byteContainer.size()]; | byte[] bytes = new byte[byteContainer.size()]; | ||||
for (int i = 0; i < byteContainer.size(); i++) { | for (int i = 0; i < byteContainer.size(); i++) { | ||||
bytes[i] = byteContainer.get(i); | bytes[i] = byteContainer.get(i); | ||||
@@ -13,14 +13,14 @@ class ByteGroup { | |||||
return bytes; | return bytes; | ||||
} | } | ||||
ByteGroup addBytes(byte[] bytes) { | |||||
public ByteGroup addBytes(byte[] bytes) { | |||||
for (byte b : bytes) { | for (byte b : bytes) { | ||||
byteContainer.add(b); | byteContainer.add(b); | ||||
} | } | ||||
return this; | return this; | ||||
} | } | ||||
int size() { | |||||
public int size() { | |||||
return byteContainer.size(); | return byteContainer.size(); | ||||
} | } | ||||
} | } |
@@ -6,7 +6,7 @@ | |||||
// ------------------------------------------------------------------------ | // ------------------------------------------------------------------------ | ||||
package me.chanjar.weixin.mp.util.crypto; | |||||
package me.chanjar.weixin.common.util.crypto; | |||||
import java.nio.charset.Charset; | import java.nio.charset.Charset; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
@@ -14,7 +14,7 @@ import java.util.Arrays; | |||||
/** | /** | ||||
* 提供基于PKCS7算法的加解 | * 提供基于PKCS7算法的加解 | ||||
*/ | */ | ||||
class PKCS7Encoder { | |||||
public class PKCS7Encoder { | |||||
private static final Charset CHARSET = Charset.forName("utf-8"); | private static final Charset CHARSET = Charset.forName("utf-8"); | ||||
private static final int BLOCK_SIZE = 32; | private static final int BLOCK_SIZE = 32; | ||||
@@ -25,7 +25,7 @@ class PKCS7Encoder { | |||||
* @param count 需要进行填充补位操作的明文字节个数 | * @param count 需要进行填充补位操作的明文字节个数 | ||||
* @return 补齐用的字节数组 | * @return 补齐用的字节数组 | ||||
*/ | */ | ||||
static byte[] encode(int count) { | |||||
public static byte[] encode(int count) { | |||||
// 计算需要填充的位数 | // 计算需要填充的位数 | ||||
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE); | int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE); | ||||
if (amountToPad == 0) { | if (amountToPad == 0) { | ||||
@@ -46,7 +46,7 @@ class PKCS7Encoder { | |||||
* @param decrypted 解密后的明文 | * @param decrypted 解密后的明文 | ||||
* @return 删除补位字符后的明文 | * @return 删除补位字符后的明文 | ||||
*/ | */ | ||||
static byte[] decode(byte[] decrypted) { | |||||
public static byte[] decode(byte[] decrypted) { | |||||
int pad = (int) decrypted[decrypted.length - 1]; | int pad = (int) decrypted[decrypted.length - 1]; | ||||
if (pad < 1 || pad > 32) { | if (pad < 1 || pad > 32) { | ||||
pad = 0; | pad = 0; | ||||
@@ -60,7 +60,7 @@ class PKCS7Encoder { | |||||
* @param a 需要转化的数字 | * @param a 需要转化的数字 | ||||
* @return 转化得到的字符 | * @return 转化得到的字符 | ||||
*/ | */ | ||||
static char chr(int a) { | |||||
public static char chr(int a) { | |||||
byte target = (byte) (a & 0xFF); | byte target = (byte) (a & 0xFF); | ||||
return (char) target; | return (char) target; | ||||
} | } |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.util.crypto; | |||||
package me.chanjar.weixin.common.util.crypto; | |||||
import java.security.MessageDigest; | import java.security.MessageDigest; | ||||
import java.security.NoSuchAlgorithmException; | import java.security.NoSuchAlgorithmException; |
@@ -11,13 +11,12 @@ | |||||
* 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) | * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本) | ||||
* 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi | * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi | ||||
*/ | */ | ||||
package me.chanjar.weixin.enterprise.util.crypto; | |||||
package me.chanjar.weixin.common.util.crypto; | |||||
import java.io.StringReader; | |||||
import java.nio.charset.Charset; | |||||
import java.security.NoSuchAlgorithmException; | |||||
import java.util.Arrays; | |||||
import java.util.Random; | |||||
import org.apache.commons.codec.binary.Base64; | |||||
import org.w3c.dom.Document; | |||||
import org.w3c.dom.Element; | |||||
import org.xml.sax.InputSource; | |||||
import javax.crypto.Cipher; | import javax.crypto.Cipher; | ||||
import javax.crypto.spec.IvParameterSpec; | import javax.crypto.spec.IvParameterSpec; | ||||
@@ -25,12 +24,11 @@ import javax.crypto.spec.SecretKeySpec; | |||||
import javax.xml.parsers.DocumentBuilder; | import javax.xml.parsers.DocumentBuilder; | ||||
import javax.xml.parsers.DocumentBuilderFactory; | import javax.xml.parsers.DocumentBuilderFactory; | ||||
import javax.xml.parsers.ParserConfigurationException; | import javax.xml.parsers.ParserConfigurationException; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConfigStorage; | |||||
import org.apache.commons.codec.binary.Base64; | |||||
import org.w3c.dom.Document; | |||||
import org.w3c.dom.Element; | |||||
import org.xml.sax.InputSource; | |||||
import java.io.StringReader; | |||||
import java.nio.charset.Charset; | |||||
import java.security.NoSuchAlgorithmException; | |||||
import java.util.Arrays; | |||||
import java.util.Random; | |||||
public class WxCryptUtil { | public class WxCryptUtil { | ||||
@@ -48,41 +46,24 @@ public class WxCryptUtil { | |||||
} | } | ||||
}; | }; | ||||
private byte[] aesKey; | |||||
private String token; | |||||
private String corpId; | |||||
/** | |||||
* 构造函数 | |||||
* | |||||
* @param wxCpConfigStorage | |||||
*/ | |||||
public WxCryptUtil(WxCpConfigStorage wxCpConfigStorage) { | |||||
/* | |||||
* @param token 公众平台上,开发者设置的token | |||||
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey | |||||
* @param corpId 公众平台appid | |||||
*/ | |||||
String encodingAesKey = wxCpConfigStorage.getAesKey(); | |||||
String token = wxCpConfigStorage.getToken(); | |||||
String corpId = wxCpConfigStorage.getCorpId(); | |||||
this.token = token; | |||||
this.corpId = corpId; | |||||
this.aesKey = Base64.decodeBase64(encodingAesKey + "="); | |||||
protected byte[] aesKey; | |||||
protected String token; | |||||
protected String appidOrCorpid; | |||||
public WxCryptUtil() { | |||||
super(); | |||||
} | } | ||||
/** | /** | ||||
* 构造函数 | * 构造函数 | ||||
* | * | ||||
* @param token 公众平台上,开发者设置的token | |||||
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey | |||||
* @param corpId 公众平台appid | |||||
* @param token 公众平台上,开发者设置的token | |||||
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey | |||||
* @param appidOrCorpid 公众平台appid/corpid | |||||
*/ | */ | ||||
public WxCryptUtil(String token, String encodingAesKey, String corpId) { | |||||
public WxCryptUtil(String token, String encodingAesKey, String appidOrCorpid) { | |||||
this.token = token; | this.token = token; | ||||
this.corpId = corpId; | |||||
this.appidOrCorpid = appidOrCorpid; | |||||
this.aesKey = Base64.decodeBase64(encodingAesKey + "="); | this.aesKey = Base64.decodeBase64(encodingAesKey + "="); | ||||
} | } | ||||
@@ -125,7 +106,7 @@ public class WxCryptUtil { | |||||
byte[] randomStringBytes = randomStr.getBytes(CHARSET); | byte[] randomStringBytes = randomStr.getBytes(CHARSET); | ||||
byte[] plainTextBytes = plainText.getBytes(CHARSET); | byte[] plainTextBytes = plainText.getBytes(CHARSET); | ||||
byte[] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder(plainTextBytes.length); | byte[] bytesOfSizeInNetworkOrder = number2BytesInNetworkOrder(plainTextBytes.length); | ||||
byte[] appIdBytes = corpId.getBytes(CHARSET); | |||||
byte[] appIdBytes = appidOrCorpid.getBytes(CHARSET); | |||||
// randomStr + networkBytesOrder + text + appid | // randomStr + networkBytesOrder + text + appid | ||||
byteCollector.addBytes(randomStringBytes); | byteCollector.addBytes(randomStringBytes); | ||||
@@ -235,7 +216,7 @@ public class WxCryptUtil { | |||||
} | } | ||||
// appid不相同的情况 | // appid不相同的情况 | ||||
if (!from_appid.equals(corpId)) { | |||||
if (!from_appid.equals(appidOrCorpid)) { | |||||
throw new RuntimeException("AppID不正确"); | throw new RuntimeException("AppID不正确"); | ||||
} | } | ||||
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.mp.util.fs; | |||||
package me.chanjar.weixin.common.util.fs; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.FileOutputStream; | import java.io.FileOutputStream; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.mp.util.http; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; |
@@ -1,11 +1,11 @@ | |||||
package me.chanjar.weixin.enterprise.util.http; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.regex.Matcher; | |||||
import java.util.regex.Pattern; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | |||||
import me.chanjar.weixin.common.util.fs.FileUtils; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler; | |||||
import me.chanjar.weixin.common.util.http.RequestExecutor; | |||||
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.apache.http.Header; | import org.apache.http.Header; | ||||
import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||
@@ -13,9 +13,11 @@ import org.apache.http.client.methods.CloseableHttpResponse; | |||||
import org.apache.http.client.methods.HttpGet; | import org.apache.http.client.methods.HttpGet; | ||||
import org.apache.http.entity.ContentType; | import org.apache.http.entity.ContentType; | ||||
import me.chanjar.weixin.common.bean.result.WxError; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.util.FileUtils; | |||||
import java.io.File; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.util.regex.Matcher; | |||||
import java.util.regex.Pattern; | |||||
/** | /** | ||||
* 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File | * 下载媒体文件请求执行器,请求的参数是String, 返回的结果是File |
@@ -1,8 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.util.http; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import org.apache.http.HttpEntity; | import org.apache.http.HttpEntity; | ||||
import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||
import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; | ||||
@@ -11,8 +12,7 @@ import org.apache.http.entity.ContentType; | |||||
import org.apache.http.entity.mime.MultipartEntityBuilder; | import org.apache.http.entity.mime.MultipartEntityBuilder; | ||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
/** | /** | ||||
* 上传媒体文件请求执行器,请求的参数是File, 返回的结果是String | * 上传媒体文件请求执行器,请求的参数是File, 返回的结果是String |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.util.http; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
@@ -6,7 +6,7 @@ import org.apache.http.client.ClientProtocolException; | |||||
import org.apache.http.impl.client.CloseableHttpClient; | import org.apache.http.impl.client.CloseableHttpClient; | ||||
import org.apache.http.impl.client.HttpClients; | import org.apache.http.impl.client.HttpClients; | ||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
/** | /** | ||||
* http请求执行器 | * http请求执行器 |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.util.http; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
@@ -7,7 +7,7 @@ import org.apache.http.client.methods.CloseableHttpResponse; | |||||
import org.apache.http.client.methods.HttpGet; | import org.apache.http.client.methods.HttpGet; | ||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
/** | /** | ||||
* 简单的GET请求执行器,请求的参数是String, 返回的结果也是String | * 简单的GET请求执行器,请求的参数是String, 返回的结果也是String |
@@ -1,9 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.util.http; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import org.apache.http.Consts; | import org.apache.http.Consts; | ||||
import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||
import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.util.http; | |||||
package me.chanjar.weixin.common.util.http; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
@@ -6,7 +6,7 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.common.util; | |||||
package me.chanjar.weixin.common.util.json; | |||||
import com.google.gson.JsonElement; | import com.google.gson.JsonElement; |
@@ -6,10 +6,10 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.mp.util.json; | |||||
package me.chanjar.weixin.common.util.json; | |||||
import com.google.gson.*; | import com.google.gson.*; | ||||
import me.chanjar.weixin.mp.bean.WxAccessToken; | |||||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
@@ -6,10 +6,9 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.enterprise.util.json; | |||||
package me.chanjar.weixin.common.util.json; | |||||
import com.google.gson.*; | import com.google.gson.*; | ||||
import me.chanjar.weixin.common.util.GsonHelper; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
@@ -31,6 +30,7 @@ public class WxErrorAdapter implements JsonDeserializer<WxError> { | |||||
if (wxErrorJsonObject.get("errmsg") != null && !wxErrorJsonObject.get("errmsg").isJsonNull()) { | if (wxErrorJsonObject.get("errmsg") != null && !wxErrorJsonObject.get("errmsg").isJsonNull()) { | ||||
wxError.setErrorMsg(GsonHelper.getAsString(wxErrorJsonObject.get("errmsg"))); | wxError.setErrorMsg(GsonHelper.getAsString(wxErrorJsonObject.get("errmsg"))); | ||||
} | } | ||||
wxError.setJson(json.toString()); | |||||
return wxError; | return wxError; | ||||
} | } | ||||
@@ -0,0 +1,26 @@ | |||||
package me.chanjar.weixin.common.util.json; | |||||
import com.google.gson.Gson; | |||||
import com.google.gson.GsonBuilder; | |||||
import me.chanjar.weixin.common.bean.WxMenu; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | |||||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
public class WxGsonBuilder { | |||||
public static final GsonBuilder INSTANCE = new GsonBuilder(); | |||||
static { | |||||
INSTANCE.disableHtmlEscaping(); | |||||
INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxAccessTokenAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxMenu.class, new WxMenuGsonAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxMediaUploadResultAdapter()); | |||||
} | |||||
public static Gson create() { | |||||
return INSTANCE.create(); | |||||
} | |||||
} |
@@ -6,10 +6,10 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.mp.util.json; | |||||
package me.chanjar.weixin.common.util.json; | |||||
import com.google.gson.*; | import com.google.gson.*; | ||||
import me.chanjar.weixin.mp.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
@@ -6,11 +6,11 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.mp.util.json; | |||||
package me.chanjar.weixin.common.util.json; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
import me.chanjar.weixin.mp.bean.WxMenu; | |||||
import me.chanjar.weixin.common.bean.WxMenu; | |||||
import com.google.gson.JsonArray; | import com.google.gson.JsonArray; | ||||
import com.google.gson.JsonDeserializationContext; | import com.google.gson.JsonDeserializationContext; | ||||
@@ -23,7 +23,7 @@ import com.google.gson.JsonSerializer; | |||||
/** | /** | ||||
* | * | ||||
* @author qianjia | |||||
* @author Daniel Qian | |||||
* | * | ||||
*/ | */ | ||||
public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializer<WxMenu> { | public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializer<WxMenu> { |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.common.util; | |||||
package me.chanjar.weixin.common.util.xml; | |||||
import javax.xml.bind.annotation.adapters.XmlAdapter; | import javax.xml.bind.annotation.adapters.XmlAdapter; | ||||
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.common.util; | |||||
package me.chanjar.weixin.common.util.xml; | |||||
import javax.xml.bind.annotation.adapters.XmlAdapter; | import javax.xml.bind.annotation.adapters.XmlAdapter; | ||||
@@ -1,6 +1,5 @@ | |||||
package me.chanjar.weixin.common.bean; | package me.chanjar.weixin.common.bean; | ||||
import me.chanjar.weixin.common.bean.result.WxAccessToken; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
@@ -1,10 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.common.bean; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.common.bean.result.WxError; | |||||
@Test | @Test | ||||
public class WxErrorTest { | public class WxErrorTest { | ||||
@@ -1,12 +1,10 @@ | |||||
package me.chanjar.weixin.mp.bean; | |||||
package me.chanjar.weixin.common.bean; | |||||
import me.chanjar.weixin.mp.bean.WxMenu; | |||||
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.DataProvider; | import org.testng.annotations.DataProvider; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.mp.bean.WxMenu.WxMenuButton; | |||||
@Test | @Test | ||||
public class WxMenuTest { | public class WxMenuTest { | ||||
@@ -1,6 +1,5 @@ | |||||
package me.chanjar.weixin.enterprise.util.crypto; | |||||
package me.chanjar.weixin.common.util.crypto; | |||||
import me.chanjar.weixin.enterprise.util.crypto.WxCryptUtil; | |||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import org.w3c.dom.Document; | import org.w3c.dom.Document; | ||||
import org.w3c.dom.Element; | import org.w3c.dom.Element; | ||||
@@ -14,7 +13,8 @@ import javax.xml.parsers.ParserConfigurationException; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.StringReader; | import java.io.StringReader; | ||||
import static org.testng.Assert.*; | |||||
import static org.testng.Assert.assertEquals; | |||||
import static org.testng.Assert.fail; | |||||
@Test | @Test | ||||
public class WxCryptUtilTest { | public class WxCryptUtilTest { |
@@ -0,0 +1,12 @@ | |||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > | |||||
<suite name="Weixin-java-tool-suite" verbose="1"> | |||||
<test name="Bean_Test"> | |||||
<classes> | |||||
<class name="me.chanjar.weixin.common.bean.WxAccessTokenTest" /> | |||||
<class name="me.chanjar.weixin.common.bean.WxErrorTest" /> | |||||
<class name="me.chanjar.weixin.common.bean.WxMenuTest" /> | |||||
<class name="me.chanjar.weixin.common.util.crypto.WxCryptUtilTest" /> | |||||
</classes> | |||||
</test> | |||||
</suite> |
@@ -0,0 +1,46 @@ | |||||
weixin-java-cp | |||||
=========== | |||||
微信企业号Java SDK开发工具集,本项目主要分为两大块:微信消息路由器、微信Java API | |||||
详细文档请看 [wiki](https://github.com/chanjarster/weixin-java-tools/wiki) | |||||
# Quickstart | |||||
在你的maven项目中添加: | |||||
```xml | |||||
<dependency> | |||||
<groupId>me.chanjar</groupId> | |||||
<artifactId>weixin-java-cp-tools</artifactId> | |||||
<version>1.0.3</version> | |||||
</dependency> | |||||
``` | |||||
如果要使用``*-SNAPSHOT``版,则需要在你的``pom.xml``中添加这段: | |||||
```xml | |||||
<repositories> | |||||
<repository> | |||||
<snapshots /> | |||||
<id>sonatype snapshots</id> | |||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url> | |||||
</repository> | |||||
</repositories> | |||||
``` | |||||
## Hello World | |||||
```java | |||||
WxCpInMemoryConfigStorage config = new WxCpInMemoryConfigStorage(); | |||||
config.getCorpId(...); // 设置微信企业号的appid | |||||
config.getCorpSecret(...); // 设置微信企业号的app corpSecret | |||||
config.setAgentId(...); // 设置微信企业号应用ID | |||||
config.setToken(...); // 设置微信企业号应用的token | |||||
config.setAesKey(...); // 设置微信企业号应用的EncodingAESKey | |||||
WxCpServiceImpl wxCpService = new WxCpServiceImpl(); | |||||
wxCpService.setWxCpConfigStorage(config); | |||||
String userId = ...; | |||||
WxCpMessage message = WxCpMessage.TEXT().agentId(...).toUser(userId).content("Hello World").build(); | |||||
wxService.messageSend(message); | |||||
``` |
@@ -0,0 +1,73 @@ | |||||
<?xml version="1.0"?> | |||||
<project | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<parent> | |||||
<groupId>me.chanjar</groupId> | |||||
<artifactId>weixin-java-parent</artifactId> | |||||
<version>1.0.3-SNAPSHOT</version> | |||||
</parent> | |||||
<artifactId>weixin-java-cp</artifactId> | |||||
<name>WeiXin Java Tools - CP</name> | |||||
<description>微信企业号Java SDK</description> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>me.chanjar</groupId> | |||||
<artifactId>weixin-java-common</artifactId> | |||||
<version>${project.version}</version> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>junit</groupId> | |||||
<artifactId>junit</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.testng</groupId> | |||||
<artifactId>testng</artifactId> | |||||
<version>6.8.7</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.mockito</groupId> | |||||
<artifactId>mockito-all</artifactId> | |||||
<version>1.9.5</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.inject</groupId> | |||||
<artifactId>guice</artifactId> | |||||
<version>3.0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-server</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.eclipse.jetty</groupId> | |||||
<artifactId>jetty-servlet</artifactId> | |||||
<version>9.3.0.M0</version> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.apache.maven.plugins</groupId> | |||||
<artifactId>maven-surefire-plugin</artifactId> | |||||
<configuration> | |||||
<suiteXmlFiles> | |||||
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> | |||||
</suiteXmlFiles> | |||||
</configuration> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import me.chanjar.weixin.common.bean.result.WxAccessToken; | |||||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||||
/** | /** | ||||
* 微信客户端配置存储 | * 微信客户端配置存储 |
@@ -1,7 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
package me.chanjar.weixin.cp.api; | |||||
public class WxCpConsts { | public class WxCpConsts { | ||||
@@ -1,13 +1,13 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import me.chanjar.weixin.common.bean.result.WxAccessToken; | |||||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||||
/** | /** | ||||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | * 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化 | ||||
* @author Daniel Qian | * @author Daniel Qian | ||||
* | * | ||||
*/ | */ | ||||
public class WxInMemoryCpConfigStorage implements WxCpConfigStorage { | |||||
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage { | |||||
protected String corpId; | protected String corpId; | ||||
protected String corpSecret; | protected String corpSecret; | ||||
@@ -86,7 +86,7 @@ public class WxInMemoryCpConfigStorage implements WxCpConfigStorage { | |||||
@Override | @Override | ||||
public String toString() { | public String toString() { | ||||
return "WxInMemoryCpConfigStorage{" + | return "WxInMemoryCpConfigStorage{" + | ||||
"corpId='" + corpId + '\'' + | |||||
"appidOrCorpid='" + corpId + '\'' + | |||||
", corpSecret='" + corpSecret + '\'' + | ", corpSecret='" + corpSecret + '\'' + | ||||
", token='" + token + '\'' + | ", token='" + token + '\'' + | ||||
", accessToken='" + accessToken + '\'' + | ", accessToken='" + accessToken + '\'' + |
@@ -1,9 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.util.Map; | import java.util.Map; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||||
/** | /** | ||||
* 处理微信推送消息的处理器接口 | * 处理微信推送消息的处理器接口 |
@@ -1,8 +1,8 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.util.Map; | import java.util.Map; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||||
/** | /** | ||||
* 微信消息拦截器,可以用来做验证 | * 微信消息拦截器,可以用来做验证 |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.HashMap; | import java.util.HashMap; | ||||
@@ -8,20 +8,20 @@ import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | import java.util.concurrent.Executors; | ||||
import java.util.regex.Pattern; | import java.util.regex.Pattern; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | * 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 | ||||
* | |||||
* | |||||
* 说明: | * 说明: | ||||
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | * 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 | ||||
* 2. 默认情况下消息只会被处理一次,除非使用 {@link Rule#next()} | * 2. 默认情况下消息只会被处理一次,除非使用 {@link Rule#next()} | ||||
* 3. 规则的结束必须用{@link Rule#end()}或者{@link Rule#next()},否则不会生效 | * 3. 规则的结束必须用{@link Rule#end()}或者{@link Rule#next()},否则不会生效 | ||||
* | |||||
* | |||||
* 使用方法: | * 使用方法: | ||||
* WxMessageRouter router = new WxMessageRouter(); | |||||
* WxCpMessageRouter router = new WxCpMessageRouter(); | |||||
* router | * router | ||||
* .rule() | * .rule() | ||||
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | * .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") | ||||
@@ -31,20 +31,20 @@ import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; | |||||
* // 另外一个匹配规则 | * // 另外一个匹配规则 | ||||
* .end() | * .end() | ||||
* ; | * ; | ||||
* | |||||
* | |||||
* // 将WxXmlMessage交给消息路由器 | * // 将WxXmlMessage交给消息路由器 | ||||
* router.route(message); | * router.route(message); | ||||
* | |||||
* | |||||
* </pre> | * </pre> | ||||
* @author Daniel Qian | * @author Daniel Qian | ||||
* | * | ||||
*/ | */ | ||||
public class WxCpMessageRouter { | public class WxCpMessageRouter { | ||||
private final List<Rule> rules = new ArrayList<Rule>(); | private final List<Rule> rules = new ArrayList<Rule>(); | ||||
private final ExecutorService es = Executors.newCachedThreadPool(); | private final ExecutorService es = Executors.newCachedThreadPool(); | ||||
/** | /** | ||||
* 开始一个新的Route规则 | * 开始一个新的Route规则 | ||||
* @return | * @return | ||||
@@ -65,11 +65,11 @@ public class WxCpMessageRouter { | |||||
matchRules.add(rule); | matchRules.add(rule); | ||||
} | } | ||||
} | } | ||||
if (matchRules.size() == 0) { | if (matchRules.size() == 0) { | ||||
return null; | return null; | ||||
} | } | ||||
if (matchRules.get(0).async) { | if (matchRules.get(0).async) { | ||||
// 只要第一个是异步的,那就异步执行 | // 只要第一个是异步的,那就异步执行 | ||||
// 在另一个线程里执行 | // 在另一个线程里执行 | ||||
@@ -85,7 +85,7 @@ public class WxCpMessageRouter { | |||||
}); | }); | ||||
return null; | return null; | ||||
} | } | ||||
WxCpXmlOutMessage res = null; | WxCpXmlOutMessage res = null; | ||||
for (final Rule rule : matchRules) { | for (final Rule rule : matchRules) { | ||||
// 返回最后一个匹配规则的结果 | // 返回最后一个匹配规则的结果 | ||||
@@ -96,33 +96,35 @@ public class WxCpMessageRouter { | |||||
} | } | ||||
return res; | return res; | ||||
} | } | ||||
public static class Rule { | public static class Rule { | ||||
private final WxCpMessageRouter routerBuilder; | private final WxCpMessageRouter routerBuilder; | ||||
private boolean async = true; | private boolean async = true; | ||||
private String msgType; | private String msgType; | ||||
private String event; | private String event; | ||||
private String eventKey; | private String eventKey; | ||||
private String content; | private String content; | ||||
private String rContent; | private String rContent; | ||||
private boolean reEnter = false; | private boolean reEnter = false; | ||||
private Integer agentId; | |||||
private List<WxCpMessageHandler> handlers = new ArrayList<WxCpMessageHandler>(); | private List<WxCpMessageHandler> handlers = new ArrayList<WxCpMessageHandler>(); | ||||
private List<WxCpMessageInterceptor> interceptors = new ArrayList<WxCpMessageInterceptor>(); | private List<WxCpMessageInterceptor> interceptors = new ArrayList<WxCpMessageInterceptor>(); | ||||
protected Rule(WxCpMessageRouter routerBuilder) { | protected Rule(WxCpMessageRouter routerBuilder) { | ||||
this.routerBuilder = routerBuilder; | this.routerBuilder = routerBuilder; | ||||
} | } | ||||
/** | /** | ||||
* 设置是否异步执行,默认是true | * 设置是否异步执行,默认是true | ||||
* @param async | * @param async | ||||
@@ -132,7 +134,17 @@ public class WxCpMessageRouter { | |||||
this.async = async; | this.async = async; | ||||
return this; | return this; | ||||
} | } | ||||
/** | |||||
* 如果agentId匹配 | |||||
* @param agentId | |||||
* @return | |||||
*/ | |||||
public Rule agentId(Integer agentId) { | |||||
this.agentId = agentId; | |||||
return this; | |||||
} | |||||
/** | /** | ||||
* 如果msgType等于某值 | * 如果msgType等于某值 | ||||
* @param msgType | * @param msgType | ||||
@@ -142,7 +154,7 @@ public class WxCpMessageRouter { | |||||
this.msgType = msgType; | this.msgType = msgType; | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 如果event等于某值 | * 如果event等于某值 | ||||
* @param event | * @param event | ||||
@@ -152,7 +164,7 @@ public class WxCpMessageRouter { | |||||
this.event = event; | this.event = event; | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 如果eventKey等于某值 | * 如果eventKey等于某值 | ||||
* @param eventKey | * @param eventKey | ||||
@@ -162,7 +174,7 @@ public class WxCpMessageRouter { | |||||
this.eventKey = eventKey; | this.eventKey = eventKey; | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 如果content等于某值 | * 如果content等于某值 | ||||
* @param content | * @param content | ||||
@@ -172,7 +184,7 @@ public class WxCpMessageRouter { | |||||
this.content = content; | this.content = content; | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 如果content匹配该正则表达式 | * 如果content匹配该正则表达式 | ||||
* @param regex | * @param regex | ||||
@@ -182,7 +194,7 @@ public class WxCpMessageRouter { | |||||
this.rContent = regex; | this.rContent = regex; | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 设置微信消息拦截器 | * 设置微信消息拦截器 | ||||
* @param interceptor | * @param interceptor | ||||
@@ -191,7 +203,7 @@ public class WxCpMessageRouter { | |||||
public Rule interceptor(WxCpMessageInterceptor interceptor) { | public Rule interceptor(WxCpMessageInterceptor interceptor) { | ||||
return interceptor(interceptor, (WxCpMessageInterceptor[]) null); | return interceptor(interceptor, (WxCpMessageInterceptor[]) null); | ||||
} | } | ||||
/** | /** | ||||
* 设置微信消息拦截器 | * 设置微信消息拦截器 | ||||
* @param interceptor | * @param interceptor | ||||
@@ -207,7 +219,7 @@ public class WxCpMessageRouter { | |||||
} | } | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 设置微信消息处理器 | * 设置微信消息处理器 | ||||
* @param handler | * @param handler | ||||
@@ -216,7 +228,7 @@ public class WxCpMessageRouter { | |||||
public Rule handler(WxCpMessageHandler handler) { | public Rule handler(WxCpMessageHandler handler) { | ||||
return handler(handler, (WxCpMessageHandler[]) null); | return handler(handler, (WxCpMessageHandler[]) null); | ||||
} | } | ||||
/** | /** | ||||
* 设置微信消息处理器 | * 设置微信消息处理器 | ||||
* @param handler | * @param handler | ||||
@@ -232,7 +244,7 @@ public class WxCpMessageRouter { | |||||
} | } | ||||
return this; | return this; | ||||
} | } | ||||
/** | /** | ||||
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则 | * 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则 | ||||
* @return | * @return | ||||
@@ -241,7 +253,7 @@ public class WxCpMessageRouter { | |||||
this.routerBuilder.rules.add(this); | this.routerBuilder.rules.add(this); | ||||
return this.routerBuilder; | return this.routerBuilder; | ||||
} | } | ||||
/** | /** | ||||
* 规则结束,但是消息还会进入其他规则 | * 规则结束,但是消息还会进入其他规则 | ||||
* @return | * @return | ||||
@@ -250,9 +262,11 @@ public class WxCpMessageRouter { | |||||
this.reEnter = true; | this.reEnter = true; | ||||
return end(); | return end(); | ||||
} | } | ||||
protected boolean test(WxCpXmlMessage wxMessage) { | protected boolean test(WxCpXmlMessage wxMessage) { | ||||
return | |||||
return | |||||
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId())) | |||||
&& | |||||
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType())) | (this.msgType == null || this.msgType.equals(wxMessage.getMsgType())) | ||||
&& | && | ||||
(this.event == null || this.event.equals(wxMessage.getEvent())) | (this.event == null || this.event.equals(wxMessage.getEvent())) | ||||
@@ -264,7 +278,7 @@ public class WxCpMessageRouter { | |||||
(this.rContent == null || Pattern.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim())) | (this.rContent == null || Pattern.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim())) | ||||
; | ; | ||||
} | } | ||||
/** | /** | ||||
* 处理微信推送过来的消息 | * 处理微信推送过来的消息 | ||||
* @param wxMessage | * @param wxMessage | ||||
@@ -278,7 +292,7 @@ public class WxCpMessageRouter { | |||||
return null; | return null; | ||||
} | } | ||||
} | } | ||||
// 交给handler处理 | // 交给handler处理 | ||||
WxCpXmlOutMessage res = null; | WxCpXmlOutMessage res = null; | ||||
for (WxCpMessageHandler handler : this.handlers) { | for (WxCpMessageHandler handler : this.handlers) { | ||||
@@ -287,7 +301,7 @@ public class WxCpMessageRouter { | |||||
} | } | ||||
return res; | return res; | ||||
} | } | ||||
} | } | ||||
} | } |
@@ -1,15 +1,16 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.enterprise.bean.*; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpDepart; | |||||
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpUser; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.bean.WxMenu; | |||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.cp.bean.*; | |||||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
/** | /** | ||||
* 微信API的Service | * 微信API的Service | ||||
@@ -49,7 +50,7 @@ public interface WxCpService { | |||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token | * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token | ||||
* </pre> | * </pre> | ||||
* | * | ||||
* @throws me.chanjar.weixin.enterprise.exception.WxErrorException | |||||
* @throws me.chanjar.weixin.common.exception.WxErrorException | |||||
*/ | */ | ||||
public void accessTokenRefresh() throws WxErrorException; | public void accessTokenRefresh() throws WxErrorException; | ||||
@@ -113,7 +114,7 @@ public interface WxCpService { | |||||
* @param menu | * @param menu | ||||
* @throws WxErrorException | * @throws WxErrorException | ||||
*/ | */ | ||||
public void menuCreate(WxCpMenu menu) throws WxErrorException; | |||||
public void menuCreate(WxMenu menu) throws WxErrorException; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
@@ -134,7 +135,7 @@ public interface WxCpService { | |||||
* @return | * @return | ||||
* @throws WxErrorException | * @throws WxErrorException | ||||
*/ | */ | ||||
public WxCpMenu menuGet() throws WxErrorException; | |||||
public WxMenu menuGet() throws WxErrorException; | |||||
/** | /** | ||||
* <pre> | * <pre> |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
@@ -11,12 +11,14 @@ import java.util.concurrent.atomic.AtomicBoolean; | |||||
import com.google.gson.JsonArray; | import com.google.gson.JsonArray; | ||||
import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||
import com.google.gson.JsonPrimitive; | import com.google.gson.JsonPrimitive; | ||||
import me.chanjar.weixin.common.bean.result.WxAccessToken; | |||||
import me.chanjar.weixin.common.util.GsonHelper; | |||||
import me.chanjar.weixin.enterprise.bean.*; | |||||
import me.chanjar.weixin.enterprise.util.http.SimpleGetRequestExecutor; | |||||
import me.chanjar.weixin.enterprise.util.crypto.SHA1; | |||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
import me.chanjar.weixin.common.bean.WxAccessToken; | |||||
import me.chanjar.weixin.common.bean.WxMenu; | |||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||||
import me.chanjar.weixin.cp.bean.*; | |||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | |||||
import me.chanjar.weixin.common.util.crypto.SHA1; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.apache.http.client.ClientProtocolException; | import org.apache.http.client.ClientProtocolException; | ||||
import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; | ||||
@@ -25,16 +27,15 @@ import org.apache.http.impl.client.BasicResponseHandler; | |||||
import org.apache.http.impl.client.CloseableHttpClient; | import org.apache.http.impl.client.CloseableHttpClient; | ||||
import org.apache.http.impl.client.HttpClients; | import org.apache.http.impl.client.HttpClients; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpDepart; | |||||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpUser; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.util.FileUtils; | |||||
import me.chanjar.weixin.enterprise.util.http.MediaDownloadRequestExecutor; | |||||
import me.chanjar.weixin.enterprise.util.http.MediaUploadRequestExecutor; | |||||
import me.chanjar.weixin.enterprise.util.http.RequestExecutor; | |||||
import me.chanjar.weixin.enterprise.util.http.SimplePostRequestExecutor; | |||||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.util.fs.FileUtils; | |||||
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.SimplePostRequestExecutor; | |||||
import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||
import com.google.gson.internal.Streams; | import com.google.gson.internal.Streams; | ||||
@@ -110,7 +111,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||
execute(new SimplePostRequestExecutor(), url, message.toJson()); | execute(new SimplePostRequestExecutor(), url, message.toJson()); | ||||
} | } | ||||
public void menuCreate(WxCpMenu menu) throws WxErrorException { | |||||
public void menuCreate(WxMenu menu) throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" + wxCpConfigStorage.getAgentId(); | String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" + wxCpConfigStorage.getAgentId(); | ||||
execute(new SimplePostRequestExecutor(), url, menu.toJson()); | execute(new SimplePostRequestExecutor(), url, menu.toJson()); | ||||
} | } | ||||
@@ -120,11 +121,11 @@ public class WxCpServiceImpl implements WxCpService { | |||||
execute(new SimpleGetRequestExecutor(), url, null); | execute(new SimpleGetRequestExecutor(), url, null); | ||||
} | } | ||||
public WxCpMenu menuGet() throws WxErrorException { | |||||
public WxMenu menuGet() throws WxErrorException { | |||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + wxCpConfigStorage.getAgentId(); | String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + wxCpConfigStorage.getAgentId(); | ||||
try { | try { | ||||
String resultContent = execute(new SimpleGetRequestExecutor(), url, null); | String resultContent = execute(new SimpleGetRequestExecutor(), url, null); | ||||
return WxCpMenu.fromJson(resultContent); | |||||
return WxMenu.fromJson(resultContent); | |||||
} catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
// 46003 不存在的菜单数据 | // 46003 不存在的菜单数据 | ||||
if (e.getError().getErrorCode() == 46003) { | if (e.getError().getErrorCode() == 46003) { |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
/** | /** | ||||
* 微信部门 | * 微信部门 |
@@ -1,10 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.enterprise.bean.messagebuilder.*; | |||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
import me.chanjar.weixin.cp.bean.messagebuilder.*; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
/** | /** | ||||
* 消息 | * 消息 | ||||
@@ -65,12 +65,12 @@ public class WxCpMessage { | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 请使用 | * 请使用 | ||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#CUSTOM_MSG_TEXT} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#CUSTOM_MSG_IMAGE} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#CUSTOM_MSG_VOICE} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#CUSTOM_MSG_MUSIC} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#CUSTOM_MSG_VIDEO} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#CUSTOM_MSG_NEWS} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_TEXT} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_IMAGE} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_VOICE} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_MUSIC} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_VIDEO} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#CUSTOM_MSG_NEWS} | |||||
* </pre> | * </pre> | ||||
* @param msgType | * @param msgType | ||||
*/ | */ |
@@ -1,13 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import com.google.gson.JsonElement; | |||||
import com.google.gson.JsonObject; | |||||
import com.google.gson.internal.Streams; | |||||
import com.google.gson.stream.JsonReader; | |||||
import me.chanjar.weixin.common.util.GsonHelper; | |||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
import java.io.StringReader; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
/** | /** | ||||
* Created by Daniel Qian | * Created by Daniel Qian |
@@ -1,8 +1,7 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
import java.sql.PseudoColumnUsage; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -1,9 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConfigStorage; | |||||
import me.chanjar.weixin.common.util.AdapterCDATA; | |||||
import me.chanjar.weixin.common.util.XmlTransformer; | |||||
import me.chanjar.weixin.enterprise.util.crypto.WxCryptUtil; | |||||
import me.chanjar.weixin.common.util.xml.AdapterCDATA; | |||||
import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||||
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||||
import me.chanjar.weixin.cp.util.xml.XmlTransformer; | |||||
import org.apache.commons.io.IOUtils; | import org.apache.commons.io.IOUtils; | ||||
import javax.xml.bind.JAXBException; | import javax.xml.bind.JAXBException; | ||||
@@ -190,13 +190,13 @@ public class WxCpXmlMessage { | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 当接受用户消息时,可能会获得以下值: | * 当接受用户消息时,可能会获得以下值: | ||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_TEXT} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_IMAGE} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_VOICE} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_VIDEO} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_LOCATION} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_LINK} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_EVENT} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_TEXT} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_IMAGE} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VOICE} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VIDEO} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_LOCATION} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_LINK} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_EVENT} | |||||
* </pre> | * </pre> | ||||
* | * | ||||
* @return | * @return | ||||
@@ -208,12 +208,11 @@ public class WxCpXmlMessage { | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 当发送消息的时候使用: | * 当发送消息的时候使用: | ||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_TEXT} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_IMAGE} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_VOICE} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_VIDEO} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_NEWS} | |||||
* {@link me.chanjar.weixin.enterprise.api.WxCpConsts#XML_MSG_MUSIC} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_TEXT} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_IMAGE} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VOICE} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_VIDEO} | |||||
* {@link me.chanjar.weixin.cp.api.WxCpConsts#XML_MSG_NEWS} | |||||
* </pre> | * </pre> | ||||
* | * | ||||
* @param msgType | * @param msgType | ||||
@@ -420,7 +419,7 @@ public class WxCpXmlMessage { | |||||
String encryptedXml, | String encryptedXml, | ||||
WxCpConfigStorage wxCpConfigStorage, | WxCpConfigStorage wxCpConfigStorage, | ||||
String timestamp, String nonce, String msgSignature) { | String timestamp, String nonce, String msgSignature) { | ||||
WxCryptUtil cryptUtil = new WxCryptUtil(wxCpConfigStorage); | |||||
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage); | |||||
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml); | String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml); | ||||
return fromXml(plainText); | return fromXml(plainText); | ||||
} | } |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||||
import javax.xml.bind.annotation.XmlAccessorType; | import javax.xml.bind.annotation.XmlAccessorType; | ||||
@@ -6,8 +6,8 @@ import javax.xml.bind.annotation.XmlElement; | |||||
import javax.xml.bind.annotation.XmlRootElement; | import javax.xml.bind.annotation.XmlRootElement; | ||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.MediaIdMarshaller; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.xml.MediaIdMarshaller; | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) |
@@ -1,4 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import me.chanjar.weixin.common.util.xml.AdapterCDATA; | |||||
import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||||
import me.chanjar.weixin.cp.bean.outxmlbuilder.*; | |||||
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||||
import me.chanjar.weixin.cp.util.xml.XmlTransformer; | |||||
import javax.xml.bind.JAXBException; | import javax.xml.bind.JAXBException; | ||||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||||
@@ -7,16 +13,6 @@ import javax.xml.bind.annotation.XmlElement; | |||||
import javax.xml.bind.annotation.XmlRootElement; | import javax.xml.bind.annotation.XmlRootElement; | ||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConfigStorage; | |||||
import me.chanjar.weixin.enterprise.bean.outxmlbuilder.ImageBuilder; | |||||
import me.chanjar.weixin.enterprise.bean.outxmlbuilder.NewsBuilder; | |||||
import me.chanjar.weixin.enterprise.bean.outxmlbuilder.TextBuilder; | |||||
import me.chanjar.weixin.enterprise.bean.outxmlbuilder.VideoBuilder; | |||||
import me.chanjar.weixin.enterprise.bean.outxmlbuilder.VoiceBuilder; | |||||
import me.chanjar.weixin.enterprise.util.crypto.WxCryptUtil; | |||||
import me.chanjar.weixin.common.util.AdapterCDATA; | |||||
import me.chanjar.weixin.common.util.XmlTransformer; | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) | ||||
public class WxCpXmlOutMessage { | public class WxCpXmlOutMessage { | ||||
@@ -82,7 +78,7 @@ public class WxCpXmlOutMessage { | |||||
*/ | */ | ||||
public String toEncryptedXml(WxCpConfigStorage wxCpConfigStorage) { | public String toEncryptedXml(WxCpConfigStorage wxCpConfigStorage) { | ||||
String plainXml = toXml(); | String plainXml = toXml(); | ||||
WxCryptUtil pc = new WxCryptUtil(wxCpConfigStorage); | |||||
WxCpCryptUtil pc = new WxCpCryptUtil(wxCpConfigStorage); | |||||
return pc.encrypt(plainXml); | return pc.encrypt(plainXml); | ||||
} | } | ||||
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -10,8 +10,8 @@ import javax.xml.bind.annotation.XmlElementWrapper; | |||||
import javax.xml.bind.annotation.XmlRootElement; | import javax.xml.bind.annotation.XmlRootElement; | ||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.AdapterCDATA; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.xml.AdapterCDATA; | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||||
import javax.xml.bind.annotation.XmlAccessorType; | import javax.xml.bind.annotation.XmlAccessorType; | ||||
@@ -6,8 +6,8 @@ import javax.xml.bind.annotation.XmlElement; | |||||
import javax.xml.bind.annotation.XmlRootElement; | import javax.xml.bind.annotation.XmlRootElement; | ||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.AdapterCDATA; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.xml.AdapterCDATA; | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||||
import javax.xml.bind.annotation.XmlAccessorType; | import javax.xml.bind.annotation.XmlAccessorType; | ||||
@@ -6,8 +6,8 @@ import javax.xml.bind.annotation.XmlElement; | |||||
import javax.xml.bind.annotation.XmlRootElement; | import javax.xml.bind.annotation.XmlRootElement; | ||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.AdapterCDATA; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.xml.AdapterCDATA; | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||||
import javax.xml.bind.annotation.XmlAccessorType; | import javax.xml.bind.annotation.XmlAccessorType; | ||||
@@ -6,8 +6,8 @@ import javax.xml.bind.annotation.XmlElement; | |||||
import javax.xml.bind.annotation.XmlRootElement; | import javax.xml.bind.annotation.XmlRootElement; | ||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.MediaIdMarshaller; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.common.util.xml.MediaIdMarshaller; | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
public class BaseBuilder<T> { | public class BaseBuilder<T> { | ||||
protected String msgType; | protected String msgType; |
@@ -1,7 +1,7 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
/** | /** | ||||
* 获得消息builder | * 获得消息builder |
@@ -1,7 +1,7 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
/** | /** | ||||
* 获得消息builder | * 获得消息builder |
@@ -1,10 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
/** | /** | ||||
* 图文消息builder | * 图文消息builder |
@@ -1,7 +1,7 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
/** | /** | ||||
* 文本消息builder | * 文本消息builder |
@@ -1,7 +1,7 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
/** | /** | ||||
* 视频消息builder | * 视频消息builder |
@@ -1,7 +1,7 @@ | |||||
package me.chanjar.weixin.enterprise.bean.messagebuilder; | |||||
package me.chanjar.weixin.cp.bean.messagebuilder; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
/** | /** | ||||
* 语音消息builder | * 语音消息builder |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean.outxmlbuilder; | |||||
package me.chanjar.weixin.cp.bean.outxmlbuilder; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||||
public abstract class BaseBuilder<BuilderType, ValueType> { | public abstract class BaseBuilder<BuilderType, ValueType> { | ||||
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean.outxmlbuilder; | |||||
package me.chanjar.weixin.cp.bean.outxmlbuilder; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutImageMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutImageMessage; | |||||
/** | /** | ||||
* 图片消息builder | * 图片消息builder |
@@ -1,10 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.bean.outxmlbuilder; | |||||
package me.chanjar.weixin.cp.bean.outxmlbuilder; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMewsMessage; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMewsMessage.Item; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMewsMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMewsMessage.Item; | |||||
/** | /** | ||||
* 图文消息builder | * 图文消息builder |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean.outxmlbuilder; | |||||
package me.chanjar.weixin.cp.bean.outxmlbuilder; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutTextMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage; | |||||
/** | /** | ||||
* 文本消息builder | * 文本消息builder |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean.outxmlbuilder; | |||||
package me.chanjar.weixin.cp.bean.outxmlbuilder; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutVideoMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutVideoMessage; | |||||
/** | /** | ||||
* 视频消息builder | * 视频消息builder |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean.outxmlbuilder; | |||||
package me.chanjar.weixin.cp.bean.outxmlbuilder; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutVoiceMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutVoiceMessage; | |||||
/** | /** | ||||
* 语音消息builder | * 语音消息builder |
@@ -0,0 +1,43 @@ | |||||
/** | |||||
* 对公众平台发送给公众账号的消息加解密示例代码. | |||||
* | |||||
* @copyright Copyright (c) 1998-2014 Tencent Inc. | |||||
*/ | |||||
// ------------------------------------------------------------------------ | |||||
/** | |||||
* 针对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.cp.util.crypto; | |||||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil; | |||||
import me.chanjar.weixin.cp.api.WxCpConfigStorage; | |||||
import org.apache.commons.codec.binary.Base64; | |||||
public class WxCpCryptUtil extends WxCryptUtil { | |||||
/** | |||||
* 构造函数 | |||||
* | |||||
* @param wxCpConfigStorage | |||||
*/ | |||||
public WxCpCryptUtil(WxCpConfigStorage wxCpConfigStorage) { | |||||
/* | |||||
* @param token 公众平台上,开发者设置的token | |||||
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey | |||||
* @param appidOrCorpid 公众平台appid | |||||
*/ | |||||
String encodingAesKey = wxCpConfigStorage.getAesKey(); | |||||
String token = wxCpConfigStorage.getToken(); | |||||
String corpId = wxCpConfigStorage.getCorpId(); | |||||
this.token = token; | |||||
this.appidOrCorpid = corpId; | |||||
this.aesKey = Base64.decodeBase64(encodingAesKey + "="); | |||||
} | |||||
} |
@@ -6,12 +6,12 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.enterprise.util.json; | |||||
package me.chanjar.weixin.cp.util.json; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
import me.chanjar.weixin.common.util.GsonHelper; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpDepart; | |||||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||||
import com.google.gson.JsonDeserializationContext; | import com.google.gson.JsonDeserializationContext; | ||||
import com.google.gson.JsonDeserializer; | import com.google.gson.JsonDeserializer; |
@@ -1,11 +1,12 @@ | |||||
package me.chanjar.weixin.enterprise.util.json; | |||||
package me.chanjar.weixin.cp.util.json; | |||||
import com.google.gson.Gson; | import com.google.gson.Gson; | ||||
import com.google.gson.GsonBuilder; | import com.google.gson.GsonBuilder; | ||||
import me.chanjar.weixin.common.bean.result.WxAccessToken; | |||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.common.bean.result.WxError; | import me.chanjar.weixin.common.bean.result.WxError; | ||||
import me.chanjar.weixin.enterprise.bean.*; | |||||
import me.chanjar.weixin.enterprise.bean.result.*; | |||||
import me.chanjar.weixin.common.util.json.WxMediaUploadResultAdapter; | |||||
import me.chanjar.weixin.common.util.json.WxErrorAdapter; | |||||
import me.chanjar.weixin.cp.bean.*; | |||||
public class WxCpGsonBuilder { | public class WxCpGsonBuilder { | ||||
@@ -14,12 +15,9 @@ public class WxCpGsonBuilder { | |||||
static { | static { | ||||
INSTANCE.disableHtmlEscaping(); | INSTANCE.disableHtmlEscaping(); | ||||
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter()); | INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxCpMenu.class, new WxCpMenuGsonAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter()); | INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter()); | INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxCpAccessTokenAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); | INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); | ||||
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxCpMediaUploadResultAdapter()); | |||||
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter()); | INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter()); | ||||
} | } | ||||
@@ -6,12 +6,12 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.enterprise.util.json; | |||||
package me.chanjar.weixin.cp.util.json; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
import com.google.gson.JsonArray; | import com.google.gson.JsonArray; | ||||
import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||
@@ -30,11 +30,11 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> { | |||||
public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializationContext context) { | public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializationContext context) { | ||||
JsonObject messageJson = new JsonObject(); | JsonObject messageJson = new JsonObject(); | ||||
messageJson.addProperty("agentid", message.getAgentId()); | messageJson.addProperty("agentid", message.getAgentId()); | ||||
messageJson.addProperty("msgtype", message.getMsgType()); | |||||
if (StringUtils.isNotBlank(message.getToUser())) { | if (StringUtils.isNotBlank(message.getToUser())) { | ||||
messageJson.addProperty("touser", message.getToUser()); | messageJson.addProperty("touser", message.getToUser()); | ||||
} | } | ||||
messageJson.addProperty("msgtype", message.getMsgType()); | |||||
if (StringUtils.isNotBlank(message.getToParty())) { | if (StringUtils.isNotBlank(message.getToParty())) { | ||||
messageJson.addProperty("toparty", message.getToUser()); | messageJson.addProperty("toparty", message.getToUser()); | ||||
} | } |
@@ -6,16 +6,12 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.enterprise.util.json; | |||||
package me.chanjar.weixin.cp.util.json; | |||||
import com.google.gson.*; | import com.google.gson.*; | ||||
import com.google.gson.internal.Streams; | |||||
import com.google.gson.stream.JsonReader; | |||||
import me.chanjar.weixin.common.util.GsonHelper; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpDepart; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpTag; | |||||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||||
import me.chanjar.weixin.cp.bean.WxCpTag; | |||||
import java.io.StringReader; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
/** | /** |
@@ -6,13 +6,13 @@ | |||||
* arose from modification of the original source, or other redistribution of this source | * arose from modification of the original source, or other redistribution of this source | ||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||
*/ | */ | ||||
package me.chanjar.weixin.enterprise.util.json; | |||||
package me.chanjar.weixin.cp.util.json; | |||||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||
import com.google.gson.*; | import com.google.gson.*; | ||||
import me.chanjar.weixin.common.util.GsonHelper; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpUser; | |||||
import me.chanjar.weixin.common.util.json.GsonHelper; | |||||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||||
/** | /** | ||||
* @author Daniel Qian | * @author Daniel Qian |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.common.util; | |||||
package me.chanjar.weixin.cp.util.xml; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
@@ -8,7 +8,7 @@ import java.io.Writer; | |||||
import javax.xml.bind.*; | import javax.xml.bind.*; | ||||
import me.chanjar.weixin.enterprise.bean.*; | |||||
import me.chanjar.weixin.cp.bean.*; | |||||
import org.xml.sax.InputSource; | import org.xml.sax.InputSource; | ||||
import com.sun.xml.bind.marshaller.CharacterEscapeHandler; | import com.sun.xml.bind.marshaller.CharacterEscapeHandler; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
@@ -19,7 +19,7 @@ public class ApiTestModule implements Module { | |||||
public void configure(Binder binder) { | public void configure(Binder binder) { | ||||
try { | try { | ||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | ||||
WxXmlCpConfigStorage config = fromXml(WxXmlCpConfigStorage.class, is1); | |||||
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, is1); | |||||
WxCpServiceImpl wxService = new WxCpServiceImpl(); | WxCpServiceImpl wxService = new WxCpServiceImpl(); | ||||
wxService.setWxCpConfigStorage(config); | wxService.setWxCpConfigStorage(config); | ||||
@@ -40,7 +40,7 @@ public class ApiTestModule implements Module { | |||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) | ||||
public static class WxXmlCpConfigStorage extends WxInMemoryCpConfigStorage { | |||||
public static class WxXmlCpInMemoryConfigStorage extends WxCpInMemoryConfigStorage { | |||||
protected String userId; | protected String userId; | ||||
@@ -1,11 +1,11 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
@@ -1,13 +1,13 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpDepart; | |||||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
@@ -1,17 +1,17 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.DataProvider; | import org.testng.annotations.DataProvider; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
@@ -1,10 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | |||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
@@ -21,7 +21,7 @@ public class WxCpMessageAPITest { | |||||
protected WxCpServiceImpl wxService; | protected WxCpServiceImpl wxService; | ||||
public void testSendCustomMessage() throws WxErrorException { | public void testSendCustomMessage() throws WxErrorException { | ||||
ApiTestModule.WxXmlCpConfigStorage configStorage = (ApiTestModule.WxXmlCpConfigStorage) wxService.wxCpConfigStorage; | |||||
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) wxService.wxCpConfigStorage; | |||||
WxCpMessage message1 = new WxCpMessage(); | WxCpMessage message1 = new WxCpMessage(); | ||||
message1.setAgentId(configStorage.getAgentId()); | message1.setAgentId(configStorage.getAgentId()); | ||||
message1.setMsgType(WxCpConsts.CUSTOM_MSG_TEXT); | message1.setMsgType(WxCpConsts.CUSTOM_MSG_TEXT); |
@@ -1,9 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import java.util.Map; | import java.util.Map; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.DataProvider; | import org.testng.annotations.DataProvider; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,8 +1,8 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpTag; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpUser; | |||||
import me.chanjar.weixin.cp.bean.WxCpTag; | |||||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
@@ -41,7 +41,7 @@ public class WxCpTagAPITest { | |||||
@Test(dependsOnMethods = "testTagGet") | @Test(dependsOnMethods = "testTagGet") | ||||
public void testTagAddUsers() throws Exception { | public void testTagAddUsers() throws Exception { | ||||
List<String> userIds = new ArrayList<String>(); | List<String> userIds = new ArrayList<String>(); | ||||
userIds.add(((ApiTestModule.WxXmlCpConfigStorage)configStorage).getUserId()); | |||||
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage)configStorage).getUserId()); | |||||
wxService.tagAddUsers(tagId, userIds); | wxService.tagAddUsers(tagId, userIds); | ||||
} | } | ||||
@@ -54,7 +54,7 @@ public class WxCpTagAPITest { | |||||
@Test(dependsOnMethods = "testTagGetUsers") | @Test(dependsOnMethods = "testTagGetUsers") | ||||
public void testTagRemoveUsers() throws Exception { | public void testTagRemoveUsers() throws Exception { | ||||
List<String> userIds = new ArrayList<String>(); | List<String> userIds = new ArrayList<String>(); | ||||
userIds.add(((ApiTestModule.WxXmlCpConfigStorage)configStorage).getUserId()); | |||||
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage)configStorage).getUserId()); | |||||
wxService.tagRemoveUsers(tagId, userIds); | wxService.tagRemoveUsers(tagId, userIds); | ||||
} | } | ||||
@@ -1,9 +1,9 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpDepart; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpUser; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.cp.bean.WxCpDepart; | |||||
import me.chanjar.weixin.cp.bean.WxCpUser; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,8 +1,8 @@ | |||||
package me.chanjar.weixin.enterprise.api; | |||||
package me.chanjar.weixin.cp.api; | |||||
import javax.xml.bind.JAXBException; | import javax.xml.bind.JAXBException; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpMenu; | |||||
import me.chanjar.weixin.common.bean.WxMenu; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.DataProvider; | import org.testng.annotations.DataProvider; | ||||
import org.testng.annotations.Guice; | import org.testng.annotations.Guice; | ||||
@@ -10,8 +10,8 @@ import org.testng.annotations.Test; | |||||
import com.google.inject.Inject; | import com.google.inject.Inject; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpMenu.WxMenuButton; | |||||
import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton; | |||||
import me.chanjar.weixin.common.exception.WxErrorException; | |||||
/** | /** | ||||
* 测试菜单 | * 测试菜单 | ||||
@@ -20,14 +20,14 @@ import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||
*/ | */ | ||||
@Test(groups="menuAPI", dependsOnGroups="baseAPI") | @Test(groups="menuAPI", dependsOnGroups="baseAPI") | ||||
@Guice(modules = ApiTestModule.class) | @Guice(modules = ApiTestModule.class) | ||||
public class WxCpMenuAPITest { | |||||
public class WxMenuAPITest { | |||||
@Inject | @Inject | ||||
protected WxCpServiceImpl wxService; | protected WxCpServiceImpl wxService; | ||||
@Test(dataProvider = "menu") | @Test(dataProvider = "menu") | ||||
public void testCreateMenu(WxCpMenu wxCpMenu) throws WxErrorException { | |||||
wxService.menuCreate(wxCpMenu); | |||||
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException { | |||||
wxService.menuCreate(wxMenu); | |||||
} | } | ||||
@Test(dependsOnMethods = { "testCreateMenu"}) | @Test(dependsOnMethods = { "testCreateMenu"}) | ||||
@@ -42,7 +42,7 @@ public class WxCpMenuAPITest { | |||||
@DataProvider(name="menu") | @DataProvider(name="menu") | ||||
public Object[][] getMenu() throws JAXBException { | public Object[][] getMenu() throws JAXBException { | ||||
WxCpMenu menu = new WxCpMenu(); | |||||
WxMenu menu = new WxMenu(); | |||||
WxMenuButton button1 = new WxMenuButton(); | WxMenuButton button1 = new WxMenuButton(); | ||||
button1.setType(WxCpConsts.BUTTON_CLICK); | button1.setType(WxCpConsts.BUTTON_CLICK); | ||||
button1.setName("今日歌曲"); | button1.setName("今日歌曲"); |
@@ -1,10 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
import me.chanjar.weixin.enterprise.bean.WxCpMessage.WxArticle; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage.WxArticle; | |||||
@Test | @Test | ||||
public class WxCpMessageTest { | public class WxCpMessageTest { |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import me.chanjar.weixin.enterprise.api.WxCpConsts; | |||||
import me.chanjar.weixin.cp.api.WxCpConsts; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; | ||||
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.bean; | |||||
package me.chanjar.weixin.cp.bean; | |||||
import org.testng.Assert; | import org.testng.Assert; | ||||
import org.testng.annotations.Test; | import org.testng.annotations.Test; |
@@ -1,6 +1,6 @@ | |||||
package me.chanjar.weixin.enterprise.demo; | |||||
package me.chanjar.weixin.cp.demo; | |||||
import me.chanjar.weixin.enterprise.api.WxInMemoryCpConfigStorage; | |||||
import me.chanjar.weixin.cp.api.WxCpInMemoryConfigStorage; | |||||
import org.xml.sax.InputSource; | import org.xml.sax.InputSource; | ||||
import javax.xml.bind.JAXBContext; | import javax.xml.bind.JAXBContext; | ||||
@@ -16,20 +16,20 @@ import java.io.InputStream; | |||||
*/ | */ | ||||
@XmlRootElement(name = "xml") | @XmlRootElement(name = "xml") | ||||
@XmlAccessorType(XmlAccessType.FIELD) | @XmlAccessorType(XmlAccessType.FIELD) | ||||
class WxTestCpConfigStorage extends WxInMemoryCpConfigStorage { | |||||
class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage { | |||||
@Override | @Override | ||||
public String toString() { | public String toString() { | ||||
return "SimpleWxConfigProvider [corpId=" + corpId + ", corpSecret=" + corpSecret + ", accessToken=" + accessToken | |||||
return "SimpleWxConfigProvider [appidOrCorpid=" + corpId + ", corpSecret=" + corpSecret + ", accessToken=" + accessToken | |||||
+ ", expiresIn=" + expiresIn + ", token=" + token + ", aesKey=" + aesKey + "]"; | + ", expiresIn=" + expiresIn + ", token=" + token + ", aesKey=" + aesKey + "]"; | ||||
} | } | ||||
public static WxTestCpConfigStorage fromXml(InputStream is) throws JAXBException { | |||||
Unmarshaller um = JAXBContext.newInstance(WxTestCpConfigStorage.class).createUnmarshaller(); | |||||
public static WxCpDemoInMemoryConfigStorage fromXml(InputStream is) throws JAXBException { | |||||
Unmarshaller um = JAXBContext.newInstance(WxCpDemoInMemoryConfigStorage.class).createUnmarshaller(); | |||||
InputSource inputSource = new InputSource(is); | InputSource inputSource = new InputSource(is); | ||||
inputSource.setEncoding("utf-8"); | inputSource.setEncoding("utf-8"); | ||||
return (WxTestCpConfigStorage) um.unmarshal(inputSource); | |||||
return (WxCpDemoInMemoryConfigStorage) um.unmarshal(inputSource); | |||||
} | } | ||||
} | } |
@@ -1,4 +1,4 @@ | |||||
package me.chanjar.weixin.enterprise.demo; | |||||
package me.chanjar.weixin.cp.demo; | |||||
import org.eclipse.jetty.server.Server; | import org.eclipse.jetty.server.Server; | ||||
import org.eclipse.jetty.servlet.ServletHandler; | import org.eclipse.jetty.servlet.ServletHandler; | ||||
@@ -6,14 +6,14 @@ import org.eclipse.jetty.servlet.ServletHandler; | |||||
/** | /** | ||||
* @author Daniel Qian | * @author Daniel Qian | ||||
*/ | */ | ||||
public class WxCpTestServer { | |||||
public class WxCpDemoServer { | |||||
public static void main(String[] args) throws Exception { | public static void main(String[] args) throws Exception { | ||||
Server server = new Server(8080); | Server server = new Server(8080); | ||||
ServletHandler handler = new ServletHandler(); | ServletHandler handler = new ServletHandler(); | ||||
server.setHandler(handler); | server.setHandler(handler); | ||||
handler.addServletWithMapping(WxCpTestServlet.class, "/*"); | |||||
handler.addServletWithMapping(WxCpDemoServlet.class, "/*"); | |||||
server.start(); | server.start(); | ||||
server.join(); | server.join(); | ||||
} | } |
@@ -1,10 +1,10 @@ | |||||
package me.chanjar.weixin.enterprise.demo; | |||||
package me.chanjar.weixin.cp.demo; | |||||
import me.chanjar.weixin.enterprise.api.*; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; | |||||
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutTextMessage; | |||||
import me.chanjar.weixin.enterprise.util.crypto.WxCryptUtil; | |||||
import me.chanjar.weixin.cp.api.*; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; | |||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage; | |||||
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import javax.servlet.ServletException; | import javax.servlet.ServletException; | ||||
@@ -19,7 +19,7 @@ import java.util.Map; | |||||
/** | /** | ||||
* @author Daniel Qian | * @author Daniel Qian | ||||
*/ | */ | ||||
public class WxCpTestServlet extends HttpServlet { | |||||
public class WxCpDemoServlet extends HttpServlet { | |||||
protected WxCpService wxCpService; | protected WxCpService wxCpService; | ||||
protected WxCpConfigStorage wxCpConfigStorage; | protected WxCpConfigStorage wxCpConfigStorage; | ||||
@@ -30,7 +30,7 @@ public class WxCpTestServlet extends HttpServlet { | |||||
super.init(); | super.init(); | ||||
try { | try { | ||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); | ||||
WxTestCpConfigStorage config = WxTestCpConfigStorage.fromXml(is1); | |||||
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(is1); | |||||
wxCpConfigStorage = config; | wxCpConfigStorage = config; | ||||
wxCpService = new WxCpServiceImpl(); | wxCpService = new WxCpServiceImpl(); | ||||
@@ -77,7 +77,7 @@ public class WxCpTestServlet extends HttpServlet { | |||||
response.getWriter().println("非法请求"); | response.getWriter().println("非法请求"); | ||||
return; | return; | ||||
} | } | ||||
WxCryptUtil cryptUtil = new WxCryptUtil(wxCpConfigStorage); | |||||
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage); | |||||
String plainText = cryptUtil.decrypt(echostr); | String plainText = cryptUtil.decrypt(echostr); | ||||
// 说明是一个仅仅用来验证的请求,回显echostr | // 说明是一个仅仅用来验证的请求,回显echostr | ||||
response.getWriter().println(plainText); | response.getWriter().println(plainText); |
@@ -0,0 +1,28 @@ | |||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > | |||||
<suite name="Weixin-java-tool-suite" verbose="1"> | |||||
<test name="API_Test"> | |||||
<classes> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpBaseAPITest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpMessageAPITest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxMenuAPITest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpDepartAPITest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpMediaAPITest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpMessageRouterTest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpTagAPITest" /> | |||||
<class name="me.chanjar.weixin.cp.api.WxCpUserAPITest" /> | |||||
</classes> | |||||
</test> | |||||
<test name="Bean_Test"> | |||||
<classes> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpMessageTest" /> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpXmlMessageTest" /> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpXmlOutImageMessageTest" /> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpXmlOutNewsMessageTest" /> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpXmlOutVideoMessageTest" /> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpXmlOutVoiceMessageTest" /> | |||||
<class name="me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessageTest" /> | |||||
</classes> | |||||
</test> | |||||
</suite> |
@@ -1,56 +0,0 @@ | |||||
weixin-java-tools | |||||
=========== | |||||
微信java开发工具集,本项目主要分为两大块:微信消息路由器、微信Java API | |||||
特性列表: | |||||
1. 不基于Servlet、和其他MVC框架,仅作为工具使用,提供更多的灵活性 | |||||
2. 详尽的单元测试代码,可以拿来当example用 | |||||
3. 详尽的javadoc | |||||
4. access token过期自动刷新的功能 | |||||
5. 微信服务端繁忙自动重试的功能 | |||||
6. 提供微信错误信息的异常处理机制 | |||||
详细文档请看 [wiki](https://github.com/chanjarster/weixin-java-tools/wiki) | |||||
# Quickstart | |||||
在你的maven项目中添加: | |||||
```xml | |||||
<dependency> | |||||
<groupId>me.chanjar</groupId> | |||||
<artifactId>weixin-java-tools</artifactId> | |||||
<version>1.0.2</version> | |||||
</dependency> | |||||
``` | |||||
如果要使用``*-SNAPSHOT``版,则需要在你的``pom.xml``中添加这段: | |||||
```xml | |||||
<repositories> | |||||
<repository> | |||||
<snapshots /> | |||||
<id>sonatype snapshots</id> | |||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url> | |||||
</repository> | |||||
</repositories> | |||||
``` | |||||
## Hello World | |||||
```java | |||||
WxConfigStorage config = new WxInMemoryConfigStorage(); | |||||
config.setAppId(...); // 设置微信企业号的appid | |||||
config.setSecret(...); // 设置微信企业号的app corpSecret | |||||
config.setToken(...); // 设置微信企业号的token | |||||
WxServiceImpl wxService = new WxServiceImpl(); | |||||
wxService.setWxConfigStorage(config); | |||||
// 用户的openid在下面地址获得 | |||||
// https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=用户管理&form=获取关注者列表接口%20/user/get | |||||
String userId = ...; | |||||
WxCustomMessage message = WxCustomMessage.TEXT().toUser(userId).content("Hello World").build(); | |||||
wxService.customMessageSend(message); | |||||
``` |
@@ -1,31 +0,0 @@ | |||||
package me.chanjar.weixin.common.bean.result; | |||||
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||
public class WxAccessToken { | |||||
private String accessToken; | |||||
private int expiresIn = -1; | |||||
public String getAccessToken() { | |||||
return accessToken; | |||||
} | |||||
public void setAccessToken(String accessToken) { | |||||
this.accessToken = accessToken; | |||||
} | |||||
public int getExpiresIn() { | |||||
return expiresIn; | |||||
} | |||||
public void setExpiresIn(int expiresIn) { | |||||
this.expiresIn = expiresIn; | |||||
} | |||||
public static WxAccessToken fromJson(String json) { | |||||
return WxCpGsonBuilder.create().fromJson(json, WxAccessToken.class); | |||||
} | |||||
} |