@@ -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 { | |||
* <pre> | |||
* 设置用户备注名接口 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=设置用户备注名接口 | |||
* | |||
* </pre> | |||
* @param openid 用户openid | |||
* @param remark 备注名 | |||
@@ -246,6 +246,18 @@ public interface WxService { | |||
*/ | |||
public void userUpdateRemark(String openid, String remark) throws WxErrorException; | |||
/** | |||
* <pre> | |||
* 获取用户基本信息 | |||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息 | |||
* </pre> | |||
* @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 | |||
@@ -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 | |||
@@ -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); | |||
} | |||
} |
@@ -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() { | |||
@@ -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<WxUser> { | |||
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; | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |