Search in sources :

Example 1 with UserService

use of com.guhanjie.service.UserService 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 UserService

use of com.guhanjie.service.UserService in project weixin-boot by guhanjie.

the class MessageKit method handleLocationEvent.

private static String handleLocationEvent(Map<String, String> msgMap) throws IOException {
    LOGGER.info("starting to handle location event....");
    String openId = msgMap.get("FromUserName");
    String lat = msgMap.get("Latitude");
    String lng = msgMap.get("Longitude");
    // 获取用户上报的地址微信,并绑定到内存中
    User user = new User();
    user.setOpenId(openId);
    Position p = new Position();
    p.setGeoLat(lat);
    p.setGeoLng(lng);
    user.setPosition(p);
    UserService userService = SpringContextUtil.getBean(UserService.class);
    userService.updateToCache(user);
    return "";
}
Also used : User(com.guhanjie.model.User) Position(com.guhanjie.model.Position) UserService(com.guhanjie.service.UserService)

Aggregations

User (com.guhanjie.model.User)2 UserService (com.guhanjie.service.UserService)2 Position (com.guhanjie.model.Position)1 UserInfo (com.guhanjie.weixin.model.UserInfo)1 IOException (java.io.IOException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1