Skip to content

Commit

Permalink
增加微信JSAPI签名高级接口
Browse files Browse the repository at this point in the history
  • Loading branch information
elkan1788 committed Feb 3, 2015
1 parent 7157fec commit 15bec48
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 69 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@
<dependency>
<groupId>org.elkan1788.osc</groupId>
<artifactId>mpsdk4j</artifactId>
<version>1.a.19</version>
<version>1.a.20</version>
</dependency>
```

或者自己编译jar包。
```
mvn clean package
mvn clean package -Dmaven.test.skip=true
```

<a name="示例代码"></a>
Expand Down
63 changes: 45 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.elkan1788.osc</groupId>
<artifactId>mpsdk4j</artifactId>
<version>1.a.19</version>
<version>1.a.20</version>
<packaging>jar</packaging>
<name>微信公众平台JAVA开发SDK</name>
<description>
Expand Down Expand Up @@ -133,23 +133,50 @@
<build>
<finalName>mpsdk4j-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
59 changes: 30 additions & 29 deletions src/main/java/com/qq/weixin/mp/aes/SHA1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Formatter;

/**
* SHA1算法计算公众平台的消息签名接口
Expand All @@ -11,40 +12,40 @@
*/
public class SHA1 {

/**
/**
* 用SHA1算法生成安全签名
*
* @param params [token, timestamp, nonce, encrypt]
* @return 安全签名
* @throws com.qq.weixin.mp.aes.AesException
*/
public static String calculate(String... params) throws AesException {
try {
public static String calculate(String... params) throws AesException {
try {
String[] array = params;
StringBuffer sb = new StringBuffer();
// 字符串排序
Arrays.sort(array);
int len = params.length;
for (int i = 0; i < len; i++) {
sb.append(array[i]);
}
String str = sb.toString();
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
// 字符串排序
Arrays.sort(array);
int len = params.length;
for (int i = 0; i < len; i++) {
sb.append(array[i]);
}

StringBuffer hexstr = new StringBuffer();
String shaHex = "";
for (int i = 0; i < digest.length; i++) {
shaHex = Integer.toHexString(digest[i] & 0xFF);
if (shaHex.length() < 2) {
hexstr.append(0);
}
hexstr.append(shaHex);
}
return hexstr.toString();
} catch (Exception e) {
throw new AesException(AesException.ComputeSignatureError);
}
}
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.reset();
md.update(new String(sb).getBytes("UTF-8"));

// HEX输出
byte[] hash = md.digest();
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
String hex = formatter.toString();
formatter.close();
return hex;
} catch (Exception e) {
throw new AesException(AesException.ComputeSignatureError);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/elkan1788/osc/weixin/mp/MPSDK4J.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static int majorVersion() {
}

public static int minorVersion() {
return 17;
return 20;
}

public static String releaseLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public interface WxApiUrl {
/**
* 发送模板消息地址
*/
public static final String TEMPLATE_MESSAGE_API = WX_API + "/message/template/send?access_token=%1$";
public static final String TEMPLATE_MESSAGE_API = WX_API + "/message/template/send?access_token=%1$s";
/**
* 上传图文素材地址
*/
Expand All @@ -97,4 +97,8 @@ public interface WxApiUrl {
* 获取服务组件token地址
*/
public static final String COMPONENT_TOKEN_API = WX_API + "/component/api_component_token";
/**
* 获取JSTICKET地址
*/
public static final String JSAPI_TICKET_URL = WX_API + "/ticket/getticket?type=jsapi&access_token=";
}
38 changes: 36 additions & 2 deletions src/main/java/org/elkan1788/osc/weixin/mp/core/WxApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;
import java.util.List;
import java.util.Map;

/**
* 微信公众平台开发者API接口设计
Expand All @@ -26,16 +27,22 @@ public interface WxApi {

/**
* 创建ACCESS_TOKEN
*
* @return 高级API需要的access_token
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
*/
String getAccessToken() throws WxRespException;

/**
* 刷新ACCESS_TOKE
* 刷新过期的ACCESS_TOKE
*
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
*/
void refreshAccessToken() throws WxRespException;

/**
* 获取微信服务器IP列表
*
* @return IP地址集合
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
*/
Expand All @@ -62,38 +69,44 @@ public interface WxApi {

/**
* 获取微信菜单
*
* @return 微信菜单集合
*/
List<Menu> getMenu() throws WxRespException;

/**
* 创建微信菜单
*
* @param menus 微信菜单
* @return true或false
*/
boolean createMenu(Menu... menus) throws WxRespException;

/**
* 删除自定义菜单
*
* @return true或false
*/
boolean deleteMenu() throws WxRespException;

/**
* 创建分组,成功返回分组ID,否则抛出异常
* 创建分组
*
* @param name 分组名称
* @return 分组ID
*/
int creatGroup(String name) throws WxRespException;

/**
* 获取所有分组
*
* @return Groups集合
*/
List<Group> getGroups() throws WxRespException;

/**
* 重命名分组
*
* @param id 分组ID
* @param name 新的分组名称
* @return true或false
Expand All @@ -102,13 +115,15 @@ public interface WxApi {

/**
* 获取用户分组ID
*
* @param openId 用户ID
* @return 分组ID
*/
int getGroupId(String openId) throws WxRespException;

/**
* 移动用户分组
*
* @param openId 用户ID
* @param groupId 新分组ID
* @return true或false
Expand All @@ -117,13 +132,15 @@ public interface WxApi {

/**
* 获取关注用户列表
*
* @param nextOpenId 拉取列表的后一个用户的OPENID
* @return 关注用户ID列表
*/
FollowList getFollowerList(String nextOpenId) throws WxRespException;

/**
* 获取关注者的信息
*
* @param openId 用户ID
* @param lang 使用语言
* @return 关注者的基本信息
Expand Down Expand Up @@ -174,6 +191,7 @@ public interface WxApi {

/**
* 群发消息[分组或指定用户]
*
* @param msg 消息输出实体[groupId或toUsers, content, msgType, mediaId]
* @return 消息ID
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
Expand All @@ -191,4 +209,20 @@ public interface WxApi {
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
*/
boolean delSendAll(String msgId) throws WxRespException;

/**
* 获取JSAPI签名
*
* @param url 使用jsapi页面地址
* @return 集合[url, ticket, nonce, timestamp, sign]
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
*/
Map<String, Object> getJsAPISign(String url) throws WxRespException;

/**
* 刷新过期的JSAPI TICKET
*
* @throws org.elkan1788.osc.weixin.mp.exception.WxRespException
*/
void refreshJsAPITicket() throws WxRespException;
}
Loading

0 comments on commit 15bec48

Please sign in to comment.