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