15 Commits

Author SHA1 Message Date
Daniel Qian
669552e503 upgrade to 1.1.4 2015-04-16 14:15:43 +08:00
Daniel Qian
d29fdd1427 Merge pull request #122 from SimonDolph/develop
添加可自定义oauth2 redirectURI的方法
2015-04-10 12:57:12 +08:00
Daniel Qian
0fb143600b issue #123 WxCpServiceImpl#tagGet 返回数据中没有tagname 2015-04-07 11:39:23 +08:00
SimonDolph
37c8ebac2c 添加可自定义oauth2 redirectURI的方法 2015-04-01 17:14:37 +08:00
Daniel Qian
a5d05d3458 issue #113 企业号oauth2getUserInfo报errcode=40029, errmsg=invalid code 2015-03-24 16:49:48 +08:00
Daniel Qian
7efdd448d1 fix: issue #112 2015-03-23 16:25:45 +08:00
Daniel Qian
82016c02bf version prompt 2015-03-23 11:20:40 +08:00
Daniel Qian
dfd2ade23f prompt to stable version 2015-03-23 11:09:23 +08:00
Daniel Qian
0c66505e45 #111 修复从文件系统反序列化WxMenu时,在windows平台下会存在编码问题 2015-03-20 17:40:15 +08:00
Daniel Qian
8254caf9ed #111 2015-03-20 15:05:26 +08:00
Daniel Qian
b885d2a1a9 #111 的unit test code 2015-03-20 14:59:59 +08:00
Daniel Qian
913f49e470 删除没有用的测试用例 2015-03-20 14:24:50 +08:00
Daniel Qian
8a7192813d fix README 2015-02-26 15:12:03 +08:00
Daniel Qian
0ae7a75f56 issue #109 企业号几个方法添加agentId参数 2015-02-26 15:05:06 +08:00
Daniel Qian
b648c55444 upgrade to 1.1.3-SNAPSHOT 2015-02-25 15:36:19 +08:00
14 changed files with 222 additions and 90 deletions

View File

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

View File

@@ -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.4</version>
<packaging>pom</packaging>
<name>WeiXin Java Tools - Parent</name>
<description>微信公众号、企业号上级POM</description>

View File

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

View File

@@ -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 +
'}';
}
}
}

View File

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

View File

@@ -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;
/**
* 移除标签成员
*

View File

@@ -7,6 +7,7 @@ import com.google.gson.JsonPrimitive;
import com.google.gson.internal.Streams;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.sun.media.sound.SoftTuning;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxMenu;
@@ -46,6 +47,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
@@ -180,18 +182,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 +447,14 @@ public class WxCpServiceImpl implements WxCpService {
@Override
public String[] oauth2getUserInfo(String code) throws WxErrorException {
return oauth2getUserInfo(wxCpConfigStorage.getAgentId(), code);
}
@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();
@@ -603,4 +628,12 @@ public class WxCpServiceImpl implements WxCpService {
this.sessionManager = sessionManager;
}
public static void main(String[] args) {
Float a = 3.1f;
System.out.println(3.1d);
System.out.println(new BigDecimal(3.1d));
System.out.println(new BigDecimal(a));
System.out.println(a.toString());
System.out.println(a.doubleValue());
}
}

View File

@@ -34,7 +34,7 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
messageJson.addProperty("toparty", message.getToParty());
}
if (StringUtils.isNotBlank(message.getToTag())) {
messageJson.addProperty("totag", message.getToUser());
messageJson.addProperty("totag", message.getToTag());
}
if (WxConsts.CUSTOM_MSG_TEXT.equals(message.getMsgType())) {
JsonObject text = new JsonObject();

View File

@@ -19,17 +19,17 @@ import java.lang.reflect.Type;
*/
public class WxCpTagGsonAdapter implements JsonSerializer<WxCpTag>, JsonDeserializer<WxCpTag> {
public JsonElement serialize(WxCpTag group, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WxCpTag tag, Type typeOfSrc, JsonSerializationContext context) {
JsonObject o = new JsonObject();
o.addProperty("tagid", group.getId());
o.addProperty("tagname", group.getName());
o.addProperty("tagid", tag.getId());
o.addProperty("tagname", tag.getName());
return o;
}
public WxCpTag deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
return new WxCpTag(GsonHelper.getString(jsonObject, "tagid"), GsonHelper.getString(jsonObject, "name"));
return new WxCpTag(GsonHelper.getString(jsonObject, "tagid"), GsonHelper.getString(jsonObject, "tagname"));
}
}

View File

@@ -6,7 +6,7 @@
<parent>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>1.1.2</version>
<version>1.1.4</version>
</parent>
<artifactId>weixin-java-mp</artifactId>
<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

@@ -387,6 +387,18 @@ public interface WxMpService {
*/
public String oauth2buildAuthorizationUrl(String scope, String state);
/**
* <pre>
* 构造oauth2授权的url连接
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
* </pre>
* @param redirectURI
* 用户授权完成后的重定向链接无需urlencode, 方法内会进行encode
* @param scope
* @param state
* @return code
*/
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state);
/**
* <pre>
* 用code换取oauth2的access token

View File

@@ -363,9 +363,14 @@ public class WxMpServiceImpl implements WxMpService {
@Override
public String oauth2buildAuthorizationUrl(String scope, String state) {
return this.oauth2buildAuthorizationUrl(wxMpConfigStorage.getOauth2redirectUri(), scope, state);
}
@Override
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" ;
url += "appid=" + wxMpConfigStorage.getAppId();
url += "&redirect_uri=" + URIUtil.encodeURIComponent(wxMpConfigStorage.getOauth2redirectUri());
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectURI);
url += "&response_type=code";
url += "&scope=" + scope;
if (state != null) {

View File

@@ -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());