@@ -17,7 +17,7 @@ weixin-java-tools | |||
<dependency> | |||
<groupId>me.chanjar</groupId> | |||
<artifactId>weixin-java-mp</artifactId> | |||
<version>1.1.2</version> | |||
<version>1.1.3</version> | |||
</dependency> | |||
``` | |||
@@ -27,7 +27,7 @@ weixin-java-tools | |||
<dependency> | |||
<groupId>me.chanjar</groupId> | |||
<artifactId>weixin-java-cp</artifactId> | |||
<version>1.1.2</version> | |||
<version>1.1.3</version> | |||
</dependency> | |||
``` | |||
@@ -5,7 +5,7 @@ | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>me.chanjar</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.1.2</version> | |||
<version>1.1.3-SNAPSHOT</version> | |||
<packaging>pom</packaging> | |||
<name>WeiXin Java Tools - Parent</name> | |||
<description>微信公众号、企业号上级POM</description> | |||
@@ -6,7 +6,7 @@ | |||
<parent> | |||
<groupId>me.chanjar</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.1.2</version> | |||
<version>1.1.3-SNAPSHOT</version> | |||
</parent> | |||
<artifactId>weixin-java-common</artifactId> | |||
@@ -3,10 +3,12 @@ package me.chanjar.weixin.common.bean; | |||
import java.io.InputStream; | |||
import java.io.InputStreamReader; | |||
import java.io.Serializable; | |||
import java.nio.charset.Charset; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||
import org.apache.commons.codec.Charsets; | |||
/** | |||
* 企业号菜单 | |||
@@ -28,15 +30,34 @@ public class WxMenu implements Serializable { | |||
public String toJson() { | |||
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) { | |||
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) { | |||
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 { | |||
private String type; | |||
@@ -85,7 +106,17 @@ public class WxMenu implements Serializable { | |||
public void setSubButtons(List<WxMenuButton> subButtons) { | |||
this.subButtons = subButtons; | |||
} | |||
@Override | |||
public String toString() { | |||
return "WxMenuButton{" + | |||
"type='" + type + '\'' + | |||
", name='" + name + '\'' + | |||
", key='" + key + '\'' + | |||
", url='" + url + '\'' + | |||
", subButtons=" + subButtons + | |||
'}'; | |||
} | |||
} | |||
} |
@@ -6,7 +6,7 @@ | |||
<parent> | |||
<groupId>me.chanjar</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.1.2</version> | |||
<version>1.1.3-SNAPSHOT</version> | |||
</parent> | |||
<artifactId>weixin-java-cp</artifactId> | |||
@@ -155,34 +155,87 @@ public interface WxCpService { | |||
* <pre> | |||
* 自定义菜单创建接口 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口 | |||
* | |||
* 注意: 这个方法使用WxCpConfigStorage里的agentId | |||
* </pre> | |||
* @see #menuCreate(String, me.chanjar.weixin.common.bean.WxMenu) | |||
* | |||
* @param 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> | |||
* 自定义菜单删除接口 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口 | |||
* | |||
* 注意: 这个方法使用WxCpConfigStorage里的agentId | |||
* </pre> | |||
* @see #menuDelete(String) | |||
* | |||
* @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> | |||
* 自定义菜单查询接口 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口 | |||
* | |||
* 注意: 这个方法使用WxCpConfigStorage里的agentId | |||
* </pre> | |||
* @see #menuGet(String) | |||
* | |||
* @return | |||
* @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> | |||
* 部门管理接口 - 创建部门 | |||
@@ -364,12 +417,33 @@ public interface WxCpService { | |||
* 用oauth2获取用户信息 | |||
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息 | |||
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名,所以无法测试,因此这个方法很可能是坏的。 | |||
* | |||
* 注意: 这个方法使用WxCpConfigStorage里的agentId | |||
* </pre> | |||
* @see #oauth2getUserInfo(String, String) | |||
* | |||
* @param code | |||
* @return [userid, deviceid] | |||
*/ | |||
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; | |||
/** | |||
* 移除标签成员 | |||
* | |||
@@ -180,18 +180,36 @@ public class WxCpServiceImpl implements WxCpService { | |||
post(url, message.toJson()); | |||
} | |||
@Override | |||
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(); | |||
post(url, menu.toJson()); | |||
} | |||
@Override | |||
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); | |||
} | |||
@Override | |||
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 { | |||
String resultContent = get(url, null); | |||
return WxMenu.fromJson(resultContent); | |||
@@ -427,9 +445,14 @@ public class WxCpServiceImpl implements WxCpService { | |||
@Override | |||
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?" | |||
+ "code=" + code | |||
+ "&agendid=" + wxCpConfigStorage.getAgentId(); | |||
+ "code=" + code | |||
+ "&agendid=" + agentId; | |||
String responseText = get(url, null); | |||
JsonElement je = Streams.parse(new JsonReader(new StringReader(responseText))); | |||
JsonObject jo = je.getAsJsonObject(); | |||
@@ -6,7 +6,7 @@ | |||
<parent> | |||
<groupId>me.chanjar</groupId> | |||
<artifactId>weixin-java-parent</artifactId> | |||
<version>1.1.2</version> | |||
<version>1.1.3-SNAPSHOT</version> | |||
</parent> | |||
<artifactId>weixin-java-mp</artifactId> | |||
<name>WeiXin Java Tools - MP</name> | |||
@@ -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(); | |||
} | |||
} |
@@ -24,9 +24,54 @@ public class WxMpMenuAPITest { | |||
@Test(dataProvider = "menu") | |||
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException { | |||
System.out.println(wxMenu.toJson()); | |||
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"}) | |||
public void testGetMenu() throws WxErrorException { | |||
Assert.assertNotNull(wxService.menuGet()); | |||