0
收藏
评论

微信公众号根据openid获取微信用户获取头像和昵称等信息

微信公众号根据openid获取微信用户获取头像和昵称等信息

1、微信用户数据表

2、controller文件

@RestController
@RequestMapping("/api/wechatuserinfo")
public class WechatController {
    @Autowired
    WechatService wechatService;
    @Autowired
    CustomerInfoService customerInfoService;
     @Autowired
     WechatUserInfoService wechatUserInfoService;
     @Autowired
    AddressInfoService addressInfoService;
    @RequestMapping("wechat")
    @Transactional
    public R getOpenId(String code) {
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
                WECHAT_APP_ID +
                "&secret=" +
                WECHAT_APP_SECRET +
                "&code=" +
                code +
                "&grant_type=authorization_code";
        String urlResult = HttpUtil.get(url);
        JSONObject jsonUrl = JSONUtil.parseObj(urlResult);
        String openid = MapUtil.getStr(jsonUrl, "openid");
        String accessToken = MapUtil.getStr(jsonUrl, "access_token");
        WechatUserInfoEntity userInfo =wechatService.getSNSUserInfo(accessToken, openid);
        WechatUserInfoEntity wechatUserInfoEntity = wechatUserInfoService.queryByOpenId(userInfo.getOpenId());
        if (wechatUserInfoEntity != null) {
            return R.ok(wechatUserInfoEntity);
        } else {
 
            CustomerInfoEntity customerInfoEntity = new CustomerInfoEntity();
            customerInfoEntity.setName(Base64.encode(userInfo.getNickname()));
            customerInfoEntity.setPhoto(userInfo.getHeadImage());
            customerInfoEntity.setCreateTime(new Date());
            customerInfoEntity.setUpdateTime(new Date());
            customerInfoService.save(customerInfoEntity);
            userInfo.setNickname(Base64.encode(userInfo.getNickname()));
            userInfo.setUserId(customerInfoEntity.getId());
            userInfo.setCreateTime(new Date());
            userInfo.setUpdateTime(new Date());
            wechatUserInfoService.save(userInfo);
            AddressInfoEntity addressInfoEntity = new AddressInfoEntity();
            addressInfoEntity.setUserId(userInfo.getUserId());
            addressInfoEntity.setArea("");
            addressInfoService.save(addressInfoEntity);
            customerInfoEntity.setAddressId(addressInfoEntity.getId());
            customerInfoService.updateById(customerInfoEntity);
            userInfo.setNickname(Base64.decodeStr(userInfo.getNickname()));
            return R.ok(userInfo);
        }
    }

3、service代码

/**
 * 通过网页授权获取用户信息
 */
@Service
public class WechatService {
    @SuppressWarnings({"deprecation", "unchecked"})
    public  WechatUserInfoEntity getSNSUserInfo(String accessToken, String openId) {
        WechatUserInfoEntity snsUserInfo = null;
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
        // 通过网页授权获取用户信息
        String urlResult = HttpUtil.get(requestUrl);
        JSONObject jsonUrl = JSONUtil.parseObj(urlResult);
        if (null != jsonUrl) {
            snsUserInfo = new WechatUserInfoEntity();
            // 用户的标识
            snsUserInfo.setOpenId(MapUtil.getStr(jsonUrl, "openid"));
            // 昵称
            snsUserInfo.setNickname(MapUtil.getStr(jsonUrl, "nickname"));
            // 用户头像
            snsUserInfo.setHeadImage(MapUtil.getStr(jsonUrl, "headimgurl"));
        }
        return snsUserInfo;
    }
}

4、常量代码

public interface WechatConsts {
// String WECHAT_APP_ID="wx0afb876c6fc70e1e";
// String WECHAT_APP_SECRET="1e7db4c466a2846006836c0007991d49";
// String TEMPLATE_ID="cLrzp2ThnYNhKUySQADT8Yqizyd9Cdm7qUzKgOFSoBs";
 
 String WECHAT_APP_ID="wx9648a4f1ddb1abfa";
 String WECHAT_APP_SECRET="5db2c92690351b04f01a06913abdf186";
 String TEMPLATE_ID="cLrzp2ThnYNhKUySQADT8Yqizyd9Cdm7qUzKgOFSoBs";
}

代码片段:http://github.crmeb.net/u/LXT

最后一次编辑于  08-28  (未经腾讯允许,不得转载)
复制链接赞 0收藏投诉评论

2 个评论

  • 杭州弧途科技有限公司
    杭州弧途科技有限公司
    08-28

    更换头像后 头像链接失效 怎么办呢。是否有上传到cdn上?通过openId和cdn链接绑定么?

    08-28
    赞同
    回复
  • 老张
    老张
    08-28

    公众号还有这样的接口?

    小程序可以吗?

    08-28
    赞同
    回复