Merge branch 'develop'

Conflicts:
	README.md
This commit is contained in:
Daniel Qian 2015-03-23 11:07:58 +08:00
commit eccbf08809
10 changed files with 189 additions and 84 deletions

View File

@ -17,7 +17,7 @@ weixin-java-tools
<dependency> <dependency>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-mp</artifactId> <artifactId>weixin-java-mp</artifactId>
<version>1.1.2</version> <version>1.1.3</version>
</dependency> </dependency>
``` ```
@ -27,7 +27,7 @@ weixin-java-tools
<dependency> <dependency>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-cp</artifactId> <artifactId>weixin-java-cp</artifactId>
<version>1.1.2</version> <version>1.1.3</version>
</dependency> </dependency>
``` ```

View File

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId> <artifactId>weixin-java-parent</artifactId>
<version>1.1.2</version> <version>1.1.3-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name> <name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description> <description>微信公众号、企业号上级POM</description>

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId> <artifactId>weixin-java-parent</artifactId>
<version>1.1.2</version> <version>1.1.3-SNAPSHOT</version>
</parent> </parent>
<artifactId>weixin-java-common</artifactId> <artifactId>weixin-java-common</artifactId>

View File

@ -3,10 +3,12 @@ package me.chanjar.weixin.common.bean;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Serializable; import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.codec.Charsets;
/** /**
* 企业号菜单 * 企业号菜单
@ -29,12 +31,31 @@ public class WxMenu implements Serializable {
return WxGsonBuilder.create().toJson(this); return WxGsonBuilder.create().toJson(this);
} }
/**
* 要用 http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html 格式来反序列化
* 相比 http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html 的格式外层多套了一个menu
* @param json
* @return
*/
public static WxMenu fromJson(String json) { public static WxMenu fromJson(String json) {
return WxGsonBuilder.create().fromJson(json, WxMenu.class); return WxGsonBuilder.create().fromJson(json, WxMenu.class);
} }
/**
* 要用 http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html 格式来反序列化
* 相比 http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html 的格式外层多套了一个menu
* @param is
* @return
*/
public static WxMenu fromJson(InputStream is) { public static WxMenu fromJson(InputStream is) {
return WxGsonBuilder.create().fromJson(new InputStreamReader(is), WxMenu.class); return WxGsonBuilder.create().fromJson(new InputStreamReader(is, Charsets.UTF_8), WxMenu.class);
}
@Override
public String toString() {
return "WxMenu{" +
"buttons=" + buttons +
'}';
} }
public static class WxMenuButton { public static class WxMenuButton {
@ -86,6 +107,16 @@ public class WxMenu implements Serializable {
this.subButtons = subButtons; this.subButtons = subButtons;
} }
@Override
public String toString() {
return "WxMenuButton{" +
"type='" + type + '\'' +
", name='" + name + '\'' +
", key='" + key + '\'' +
", url='" + url + '\'' +
", subButtons=" + subButtons +
'}';
}
} }
} }

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId> <artifactId>weixin-java-parent</artifactId>
<version>1.1.2</version> <version>1.1.3-SNAPSHOT</version>
</parent> </parent>
<artifactId>weixin-java-cp</artifactId> <artifactId>weixin-java-cp</artifactId>

View File

@ -155,34 +155,87 @@ public interface WxCpService {
* <pre> * <pre>
* 自定义菜单创建接口 * 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口 * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre> * </pre>
* @see #menuCreate(String, me.chanjar.weixin.common.bean.WxMenu)
* *
* @param menu * @param menu
* @throws WxErrorException * @throws WxErrorException
*/ */
void menuCreate(WxMenu menu) throws WxErrorException; void menuCreate(WxMenu menu) throws WxErrorException;
/**
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #menuCreate(me.chanjar.weixin.common.bean.WxMenu)
*
* @param agentId 企业号应用的id
* @param menu
* @throws WxErrorException
*/
void menuCreate(String agentId, WxMenu menu) throws WxErrorException;
/** /**
* <pre> * <pre>
* 自定义菜单删除接口 * 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口 * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre> * </pre>
* @see #menuDelete(String)
* *
* @throws WxErrorException * @throws WxErrorException
*/ */
void menuDelete() throws WxErrorException; void menuDelete() throws WxErrorException;
/**
* <pre>
* 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #menuDelete()
*
* @param agentId 企业号应用的id
* @throws WxErrorException
*/
void menuDelete(String agentId) throws WxErrorException;
/** /**
* <pre> * <pre>
* 自定义菜单查询接口 * 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口 * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre> * </pre>
* @see #menuGet(String)
* *
* @return * @return
* @throws WxErrorException * @throws WxErrorException
*/ */
WxMenu menuGet() throws WxErrorException; WxMenu menuGet() throws WxErrorException;
/**
* <pre>
* 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #menuGet()
*
* @param agentId 企业号应用的id
* @return
* @throws WxErrorException
*/
WxMenu menuGet(String agentId) throws WxErrorException;
/** /**
* <pre> * <pre>
* 部门管理接口 - 创建部门 * 部门管理接口 - 创建部门
@ -364,12 +417,33 @@ public interface WxCpService {
* 用oauth2获取用户信息 * 用oauth2获取用户信息
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息 * http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名所以无法测试因此这个方法很可能是坏的 * 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名所以无法测试因此这个方法很可能是坏的
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre> * </pre>
* @see #oauth2getUserInfo(String, String)
*
* @param code * @param code
* @return [userid, deviceid] * @return [userid, deviceid]
*/ */
String[] oauth2getUserInfo(String code) throws WxErrorException; String[] oauth2getUserInfo(String code) throws WxErrorException;
/**
* <pre>
* 用oauth2获取用户信息
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名所以无法测试因此这个方法很可能是坏的
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId需要开发人员自己给出
* </pre>
* @see #oauth2getUserInfo(String)
*
* @param agentId 企业号应用的id
* @param code
* @return [userid, deviceid]
*/
String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException;
/** /**
* 移除标签成员 * 移除标签成员
* *

View File

@ -180,18 +180,36 @@ public class WxCpServiceImpl implements WxCpService {
post(url, message.toJson()); post(url, message.toJson());
} }
@Override
public void menuCreate(WxMenu menu) throws WxErrorException { public void menuCreate(WxMenu menu) throws WxErrorException {
menuCreate(wxCpConfigStorage.getAgentId(), menu);
}
@Override
public void menuCreate(String agentId, 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();
post(url, menu.toJson()); post(url, menu.toJson());
} }
@Override
public void menuDelete() throws WxErrorException { public void menuDelete() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=" + wxCpConfigStorage.getAgentId(); menuDelete(wxCpConfigStorage.getAgentId());
}
@Override
public void menuDelete(String agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=" + agentId;
get(url, null); get(url, null);
} }
@Override
public WxMenu menuGet() throws WxErrorException { public WxMenu menuGet() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + wxCpConfigStorage.getAgentId(); return menuGet(wxCpConfigStorage.getAgentId());
}
@Override
public WxMenu menuGet(String agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + agentId;
try { try {
String resultContent = get(url, null); String resultContent = get(url, null);
return WxMenu.fromJson(resultContent); return WxMenu.fromJson(resultContent);
@ -427,9 +445,14 @@ public class WxCpServiceImpl implements WxCpService {
@Override @Override
public String[] oauth2getUserInfo(String code) throws WxErrorException { public String[] oauth2getUserInfo(String code) throws WxErrorException {
return oauth2getUserInfo(code, wxCpConfigStorage.getAgentId());
}
@Override
public String[] oauth2getUserInfo(String agentId, String code) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?" String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
+ "code=" + code + "code=" + code
+ "&agendid=" + wxCpConfigStorage.getAgentId(); + "&agendid=" + agentId;
String responseText = get(url, null); String responseText = get(url, null);
JsonElement je = Streams.parse(new JsonReader(new StringReader(responseText))); JsonElement je = Streams.parse(new JsonReader(new StringReader(responseText)));
JsonObject jo = je.getAsJsonObject(); JsonObject jo = je.getAsJsonObject();

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>me.chanjar</groupId> <groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId> <artifactId>weixin-java-parent</artifactId>
<version>1.1.2</version> <version>1.1.3-SNAPSHOT</version>
</parent> </parent>
<artifactId>weixin-java-mp</artifactId> <artifactId>weixin-java-mp</artifactId>
<name>WeiXin Java Tools - MP</name> <name>WeiXin Java Tools - MP</name>

View File

@ -1,68 +0,0 @@
/**
* Created by qianjia on 15/1/25.
*/
public class TestNonAtomicLongAssignment {
private static final long HI = 1l << 32;
private static final long LO = 1l;
private static final long TEST_NUMBER = HI | LO;
private static long assignee = 0l;
public static void main(String[] args) {
Thread writer = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
assignee = TEST_NUMBER;
}
}
});
writer.setDaemon(true);
Thread reader = new Thread(new Runnable() {
@Override
public void run() {
long i = 0;
while (true) {
i++;
long test = assignee;
if (test != TEST_NUMBER) {
System.out.print(i + " times:" + toBin(test));
break;
}
}
}
});
// Thread worker = new Thread(new Runnable() {
// @Override
// public void run() {
// double d = 89009808877238948224343435452333323113131313133434434341212323232424243434335354232390490189190420928348910913094983.323334401928d;
// while(true) {
// Math.cbrt(d);
// d = d - 1l;
// }
// }
// });
// worker.setDaemon(true);
// worker.start();
writer.start();
reader.start();
}
public static String toBin(long n) {
StringBuilder sb = new StringBuilder(Long.toBinaryString(n));
int padding = 64 - sb.length();
while (padding > 0) {
sb.insert(0, '0');
padding--;
}
return sb.toString();
}
}

View File

@ -24,9 +24,54 @@ public class WxMpMenuAPITest {
@Test(dataProvider = "menu") @Test(dataProvider = "menu")
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException { public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
System.out.println(wxMenu.toJson());
wxService.menuCreate(wxMenu); wxService.menuCreate(wxMenu);
} }
@Test
public void testCreateMenu2() throws WxErrorException {
String a = "{\n"
+ " \"menu\": {\n"
+ " \"button\": [\n"
+ " {\n"
+ " \"type\": \"click\",\n"
+ " \"name\": \"今日歌曲\",\n"
+ " \"key\": \"V1001_TODAY_MUSIC\"\n"
+ " },\n"
+ " {\n"
+ " \"type\": \"click\",\n"
+ " \"name\": \"歌手简介\",\n"
+ " \"key\": \"V1001_TODAY_SINGER\"\n"
+ " },\n"
+ " {\n"
+ " \"name\": \"菜单\",\n"
+ " \"sub_button\": [\n"
+ " {\n"
+ " \"type\": \"view\",\n"
+ " \"name\": \"搜索\",\n"
+ " \"url\": \"http://www.soso.com/\"\n"
+ " },\n"
+ " {\n"
+ " \"type\": \"view\",\n"
+ " \"name\": \"视频\",\n"
+ " \"url\": \"http://v.qq.com/\"\n"
+ " },\n"
+ " {\n"
+ " \"type\": \"click\",\n"
+ " \"name\": \"赞一下我们\",\n"
+ " \"key\": \"V1001_GOOD\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "}";
WxMenu menu = WxMenu.fromJson(a);
System.out.println(menu.toJson());
wxService.menuCreate(menu);
}
@Test(dependsOnMethods = { "testCreateMenu"}) @Test(dependsOnMethods = { "testCreateMenu"})
public void testGetMenu() throws WxErrorException { public void testGetMenu() throws WxErrorException {
Assert.assertNotNull(wxService.menuGet()); Assert.assertNotNull(wxService.menuGet());