Search in sources :

Example 1 with UserInfo

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);
}
Also used : User(com.guhanjie.model.User) UserService(com.guhanjie.service.UserService) HashMap(java.util.HashMap) UserInfo(com.guhanjie.weixin.model.UserInfo) Date(java.util.Date) IOException(java.io.IOException)

Example 2 with UserInfo

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);
}
Also used : UserInfo(com.guhanjie.weixin.model.UserInfo) Test(org.junit.Test)

Example 3 with UserInfo

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;
}
Also used : UserInfo(com.guhanjie.weixin.model.UserInfo) WeixinHttpCallback(com.guhanjie.weixin.WeixinHttpUtil.WeixinHttpCallback)

Example 4 with UserInfo

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;
}
Also used : UserInfo(com.guhanjie.weixin.model.UserInfo) WeixinHttpCallback(com.guhanjie.weixin.WeixinHttpUtil.WeixinHttpCallback)

Example 5 with UserInfo

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);
}
Also used : UserInfo(com.guhanjie.weixin.model.UserInfo) Test(org.junit.Test)

Aggregations

UserInfo (com.guhanjie.weixin.model.UserInfo)6 WeixinHttpCallback (com.guhanjie.weixin.WeixinHttpUtil.WeixinHttpCallback)3 User (com.guhanjie.model.User)2 IOException (java.io.IOException)2 Date (java.util.Date)2 Test (org.junit.Test)2 UserService (com.guhanjie.service.UserService)1 AccessToken (com.guhanjie.weixin.model.AccessToken)1 HashMap (java.util.HashMap)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1