use of com.guhanjie.weixin.model.UserInfo in project weixin-boot by guhanjie.
the class MessageKit method handleSubscribeEvent.
private static String handleSubscribeEvent(Map<String, String> msgMap) throws IOException {
LOGGER.info("starting to handle subscribe event....");
String openId = msgMap.get("FromUserName");
// 自动添加关注用户
try {
UserService userService = SpringContextUtil.getBean(UserService.class);
User user = userService.getUserByOpenId(openId);
if (user == null) {
user = new User();
user.setOpenId(openId);
UserInfo userInfo = UserKit.getUserInfo(openId);
user.setUnionid(userInfo.getUnionid());
user.setName(userInfo.getNickname());
user.setNickname(userInfo.getNickname());
user.setSex(userInfo.getSex());
user.setLanguage(userInfo.getLanguage());
user.setCountry(userInfo.getCountry());
user.setProvince(userInfo.getProvince());
user.setCity(userInfo.getCity());
if (StringUtils.isNumeric(userInfo.getSubscribe_time())) {
user.setSubscribeTime(new Date(Long.parseLong(userInfo.getSubscribe_time())));
}
userService.addUser(user);
}
} catch (Exception e) {
LOGGER.error("error happened in add user info, openId[{}].", openId, e);
}
Map<String, String> map = new HashMap<String, String>();
map.put("ToUserName", msgMap.get("FromUserName"));
map.put("FromUserName", msgMap.get("ToUserName"));
map.put("CreateTime", new Date().getTime() + "");
map.put("MsgType", "text");
map.put("Content", "您好,欢迎关注!");
return XmlUtil.map2xmlstr(map);
}
use of com.guhanjie.weixin.model.UserInfo in project weixin-boot by guhanjie.
the class TestWeixinModel method testUserInfo.
@Test
public void testUserInfo() {
String response = // "}";
"{" + " \"subscribe\": 0, " + " \"openid\": \"otvxTs_JZ6SEiP0imdhpi50fuSZg\", " + " \"unionid\": \"oR5GjjjrbqBZbrnPwwmSxFukE41U\"" + "}";
UserInfo user = JSON.parseObject(response, UserInfo.class);
// System.out.println(Arrays.asList(user.getTagid_list()).toString());
System.out.println(user.getOpenid());
assertNotNull(user);
}
use of com.guhanjie.weixin.model.UserInfo in project weixin-boot by guhanjie.
the class UserKit method getUserInfoByOauth2.
public static UserInfo getUserInfoByOauth2(final String openid, final String accsstoken) {
LOGGER.info("Starting to get user[{}] info by oauth2.0...", openid);
final UserInfo user = new UserInfo();
try {
String url = WeixinConstants.OAUTH2_GET_USER_INFO;
url = url.replaceAll("OPENID", openid);
url = url.replaceAll("ACCESS_TOKEN", accsstoken);
WeixinHttpUtil.sendGet(url, new WeixinHttpCallback() {
@Override
public void process(String json) {
UserInfo ui = JSONObject.parseObject(json, UserInfo.class);
if (ui != null && ui.getOpenid() != null) {
try {
PropertyUtils.copyProperties(user, ui);
LOGGER.info("Success to get user info:[{}] by oauth2.0.", json);
} catch (Exception e) {
LOGGER.error("error in coping user properties");
}
} else {
LOGGER.error("Failed to get user[{}] info by oauth2.0.", openid);
}
}
});
} catch (Exception e) {
LOGGER.error("Failed to get user[{}] info by oauth2.0.", openid);
}
return user;
}
use of com.guhanjie.weixin.model.UserInfo in project weixin-boot by guhanjie.
the class UserKit method getUserInfo.
public static UserInfo getUserInfo(final String openid) {
LOGGER.info("Starting to get user[{}] info...", openid);
final UserInfo user = new UserInfo();
try {
String url = WeixinConstants.API_USER_INFO;
url = url.replaceAll("OPENID", openid);
WeixinHttpUtil.sendGet(url, new WeixinHttpCallback() {
@Override
public void process(String json) {
UserInfo ui = JSONObject.parseObject(json, UserInfo.class);
if (ui != null && ui.getOpenid() != null) {
try {
PropertyUtils.copyProperties(user, ui);
LOGGER.info("Success to get user info:[{}].", json);
} catch (Exception e) {
LOGGER.error("error in coping user properties");
}
} else {
LOGGER.error("Failed to get user[{}] info.", openid);
}
}
});
} catch (Exception e) {
LOGGER.error("Failed to get user[{}] info.", openid);
}
return user;
}
use of com.guhanjie.weixin.model.UserInfo in project weixin-boot by guhanjie.
the class TestWeixinModel method testUserInfoByOauth2.
@Test
public void testUserInfoByOauth2() {
String response = "{\"openid\":\"OPENID\", " + " \"nickname\": \"NICKNAME\", " + " \"sex\":\"1\", " + " \"province\":\"PROVINCE\", " + " \"city\":\"CITY\", " + " \"country\":\"COUNTRY\", " + " \"headimgurl\": \"http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ" + "4eMsv84eavHiaiceqxibJxCfHe/46\", " + "\"privilege\":[ \"PRIVILEGE1\", \"PRIVILEGE2\" ], " + " \"unionid\": \"o6_bmasdasdsad6_2sgVt7hMZOPfL\" " + "} ";
UserInfo user = JSON.parseObject(response, UserInfo.class);
System.out.println(user.getNickname());
assertNotNull(user);
}
Aggregations