From 4c8c42475eedcfa33b5f3443cb84891c7fe1cdc5 Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Tue, 26 Aug 2014 12:37:38 +0800 Subject: [PATCH] =?UTF-8?q?issue=20#3=20=E8=8E=B7=E5=8F=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjarster/weixin/api/WxService.java | 14 ++- .../chanjarster/weixin/api/WxServiceImpl.java | 9 ++ .../weixin/bean/result/WxUser.java | 95 +++++++++++++++++++ .../weixin/util/json/WxGsonBuilder.java | 3 +- .../weixin/util/json/WxUserGsonAdapter.java | 52 ++++++++++ .../chanjarster/weixin/api/WxUserAPITest.java | 11 ++- 6 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 src/main/java/chanjarster/weixin/bean/result/WxUser.java create mode 100644 src/main/java/chanjarster/weixin/util/json/WxUserGsonAdapter.java diff --git a/src/main/java/chanjarster/weixin/api/WxService.java b/src/main/java/chanjarster/weixin/api/WxService.java index b71289872..c4900afe1 100644 --- a/src/main/java/chanjarster/weixin/api/WxService.java +++ b/src/main/java/chanjarster/weixin/api/WxService.java @@ -15,6 +15,7 @@ import chanjarster.weixin.bean.WxMenu; import chanjarster.weixin.bean.result.WxMassSendResult; import chanjarster.weixin.bean.result.WxMassUploadResult; import chanjarster.weixin.bean.result.WxMediaUploadResult; +import chanjarster.weixin.bean.result.WxUser; import chanjarster.weixin.exception.WxErrorException; /** @@ -238,7 +239,6 @@ public interface WxService { *
    * 设置用户备注名接口
    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=设置用户备注名接口
-   * 
    * 
* @param openid 用户openid * @param remark 备注名 @@ -246,6 +246,18 @@ public interface WxService { */ public void userUpdateRemark(String openid, String remark) throws WxErrorException; + /** + *
+   * 获取用户基本信息
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息
+   * 
+ * @param openid 用户openid + * @param lang 语言,zh_CN 简体(默认),zh_TW 繁体,en 英语 + * @return + * @throws WxErrorException + */ + public WxUser userInfo(String openid, String lang) throws WxErrorException; + /** * 注入 {@link WxConfigStorage} 的实现 * @param wxConfigProvider diff --git a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java index 8c462cc3e..e1d2abb0c 100644 --- a/src/main/java/chanjarster/weixin/api/WxServiceImpl.java +++ b/src/main/java/chanjarster/weixin/api/WxServiceImpl.java @@ -31,6 +31,7 @@ import chanjarster.weixin.bean.result.WxError; import chanjarster.weixin.bean.result.WxMassSendResult; import chanjarster.weixin.bean.result.WxMassUploadResult; import chanjarster.weixin.bean.result.WxMediaUploadResult; +import chanjarster.weixin.bean.result.WxUser; import chanjarster.weixin.exception.WxErrorException; import chanjarster.weixin.util.fs.FileUtil; import chanjarster.weixin.util.http.MediaDownloadRequestExecutor; @@ -41,6 +42,7 @@ import chanjarster.weixin.util.http.SimplePostRequestExecutor; import chanjarster.weixin.util.json.GsonHelper; import chanjarster.weixin.util.json.WxGsonBuilder; +import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.internal.Streams; @@ -248,6 +250,13 @@ public class WxServiceImpl implements WxService { execute(new SimplePostRequestExecutor(), url, json.toString()); } + public WxUser userInfo(String openid, String lang) throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/user/info"; + lang = lang == null ? "zh_CN" : lang; + String responseContent = execute(new SimpleGetRequestExecutor(), url, "openid=" + openid + "&lang=" + lang); + ;return WxUser.fromJson(responseContent); + } + /** * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 * @param executor diff --git a/src/main/java/chanjarster/weixin/bean/result/WxUser.java b/src/main/java/chanjarster/weixin/bean/result/WxUser.java new file mode 100644 index 000000000..5dfb786eb --- /dev/null +++ b/src/main/java/chanjarster/weixin/bean/result/WxUser.java @@ -0,0 +1,95 @@ +package chanjarster.weixin.bean.result; + +import chanjarster.weixin.util.json.WxGsonBuilder; + +/** + * 微信用户信息 + * @author chanjarster + * + */ +public class WxUser { + + protected boolean subscribe; + protected String openid; + protected String nickname; + protected String sex; + protected String language; + protected String city; + protected String province; + protected String country; + protected String headimgurl; + protected long subscribe_time; + protected String unionid; + + public boolean isSubscribe() { + return subscribe; + } + public void setSubscribe(boolean subscribe) { + this.subscribe = subscribe; + } + public String getOpenid() { + return openid; + } + public void setOpenid(String openid) { + this.openid = openid; + } + public String getNickname() { + return nickname; + } + public void setNickname(String nickname) { + this.nickname = nickname; + } + public String getSex() { + return sex; + } + public void setSex(String sex) { + this.sex = sex; + } + public String getLanguage() { + return language; + } + public void setLanguage(String language) { + this.language = language; + } + public String getCity() { + return city; + } + public void setCity(String city) { + this.city = city; + } + public String getProvince() { + return province; + } + public void setProvince(String province) { + this.province = province; + } + public String getCountry() { + return country; + } + public void setCountry(String country) { + this.country = country; + } + public String getHeadimgurl() { + return headimgurl; + } + public void setHeadimgurl(String headimgurl) { + this.headimgurl = headimgurl; + } + public long getSubscribe_time() { + return subscribe_time; + } + public void setSubscribe_time(long subscribe_time) { + this.subscribe_time = subscribe_time; + } + public String getUnionid() { + return unionid; + } + public void setUnionid(String unionid) { + this.unionid = unionid; + } + + public static WxUser fromJson(String json) { + return WxGsonBuilder.INSTANCE.create().fromJson(json, WxUser.class); + } + +} diff --git a/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java b/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java index a30ca67e6..a46823fd0 100644 --- a/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java +++ b/src/main/java/chanjarster/weixin/util/json/WxGsonBuilder.java @@ -6,6 +6,7 @@ import chanjarster.weixin.bean.WxMassGroupMessage; import chanjarster.weixin.bean.WxMassNews; import chanjarster.weixin.bean.WxMassOpenIdsMessage; import chanjarster.weixin.bean.WxMenu; +import chanjarster.weixin.bean.result.WxUser; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -22,7 +23,7 @@ public class WxGsonBuilder { INSTANCE.registerTypeAdapter(WxMassGroupMessage.class, new WxMassMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxMassOpenIdsMessage.class, new WxMassOpenIdsMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxGroup.class, new WxGroupGsonAdapter()); - + INSTANCE.registerTypeAdapter(WxUser.class, new WxUserGsonAdapter()); } public static Gson create() { diff --git a/src/main/java/chanjarster/weixin/util/json/WxUserGsonAdapter.java b/src/main/java/chanjarster/weixin/util/json/WxUserGsonAdapter.java new file mode 100644 index 000000000..609ff6152 --- /dev/null +++ b/src/main/java/chanjarster/weixin/util/json/WxUserGsonAdapter.java @@ -0,0 +1,52 @@ +/* + * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. + * + * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended + * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction + * 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. + */ +package chanjarster.weixin.util.json; + +import java.lang.reflect.Type; + +import chanjarster.weixin.bean.result.WxUser; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +/** + * + * @author qianjia + * + */ +public class WxUserGsonAdapter implements JsonDeserializer { + + public WxUser deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject o = json.getAsJsonObject(); + WxUser wxUser = new WxUser(); + wxUser.setSubscribe(new Integer(0).equals(GsonHelper.getInteger(o, "subscribe")) ? false : true); + wxUser.setCity(GsonHelper.getString(o, "city")); + wxUser.setCountry(GsonHelper.getString(o, "country")); + wxUser.setHeadimgurl(GsonHelper.getString(o, "headimgurl")); + wxUser.setLanguage(GsonHelper.getString(o, "language")); + wxUser.setNickname(GsonHelper.getString(o, "nickname")); + wxUser.setOpenid(GsonHelper.getString(o, "openid")); + wxUser.setProvince(GsonHelper.getString(o, "province")); + wxUser.setSubscribe_time(GsonHelper.getLong(o, "subscribe_time")); + wxUser.setUnionid(GsonHelper.getString(o, "unionid")); + Integer sex = GsonHelper.getInteger(o, "sex"); + if(new Integer(1).equals(sex)) { + wxUser.setSex("男"); + } else if (new Integer(2).equals(sex)) { + wxUser.setSex("女"); + } else { + wxUser.setSex("未知"); + } + return wxUser; + } + +} \ No newline at end of file diff --git a/src/test/java/chanjarster/weixin/api/WxUserAPITest.java b/src/test/java/chanjarster/weixin/api/WxUserAPITest.java index 4875055ff..c57335663 100644 --- a/src/test/java/chanjarster/weixin/api/WxUserAPITest.java +++ b/src/test/java/chanjarster/weixin/api/WxUserAPITest.java @@ -1,10 +1,13 @@ package chanjarster.weixin.api; +import org.testng.Assert; import org.testng.annotations.Guice; import org.testng.annotations.Test; import chanjarster.weixin.api.ApiTestModule.WxXmlConfigStorage; +import chanjarster.weixin.bean.result.WxUser; import chanjarster.weixin.exception.WxErrorException; +import chanjarster.weixin.util.json.WxGsonBuilder; import com.google.inject.Inject; @@ -20,10 +23,16 @@ public class WxUserAPITest { @Inject protected WxServiceImpl wxService; - @Test public void testUserUpdateRemark() throws WxErrorException { WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; wxService.userUpdateRemark(configProvider.getOpenId(), "测试备注名"); } + public void testUserInfo() throws WxErrorException { + WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage; + WxUser user = wxService.userInfo(configProvider.getOpenId(), null); + Assert.assertNotNull(user); + System.out.println(WxGsonBuilder.INSTANCE.create().toJson(user)); + } + }