微信公众号获取用户信息工具类

Posted zhengyuanyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信公众号获取用户信息工具类相关的知识,希望对你有一定的参考价值。

package com.hfcx.cfms.base.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hfcx.cfms.manage.entity.WxUser;

import net.sf.json.JSONObject;

public class WeixinUtil {
    
    private static String proxyHost = null;
    private static Integer proxyPort = null;
    
    public static final String APPID = "";
    public static final String SECRET = "";

    public static final String token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APPID+"&secret="+SECRET+"&grant_type=authorization_code";
    
    public static JSONObject httpsRequest(String code) throws Exception{
        String requestURL = token_url +"&code="+code;
        //第一次请求 获取access_token 和 openid
        String openid = doGet(requestURL);
        JSONObject jo = JSONObject.fromObject(openid);
        return jo;
    }
    
    private static String doGet(String url) throws Exception {
        URL localURL = new URL(url);
        URLConnection connection = openConnection(localURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        if (httpURLConnection.getResponseCode() >= 300) {
            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
        }
        try {
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
            reader = new BufferedReader(inputStreamReader);
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
        return resultBuffer.toString();
    }
    private static URLConnection openConnection(URL localURL) throws IOException {
        URLConnection connection;
        if (proxyHost != null && proxyPort != null) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            connection = localURL.openConnection(proxy);
        } else {
            connection = localURL.openConnection();
        }
        return connection;
    }
    
    public static WxUser getUserInfo(JSONObject jsonObject){
        WxUser wxUser = new WxUser();
        wxUser.setOpenId(jsonObject.getString("openid"));
        wxUser.setSex(jsonObject.getInt("sex"));
        wxUser.setCity(jsonObject.getString("city"));
        wxUser.setLanguage(jsonObject.getString("language"));
        //过滤emoji
        wxUser.setNickname(filterEmoji(jsonObject.getString("nickname")));
        wxUser.setHeadImgUrl(jsonObject.getString("headimgurl"));
        wxUser.setProvince(jsonObject.getString("province"));
        wxUser.setCountry(jsonObject.getString("country"));
        
        return wxUser;
    }
    
    public static String filterEmoji(String source) {
        if (source == null) {
            return source;
        }
        Pattern emoji = Pattern.compile("[ud83cudc00-ud83cudfff]|[ud83dudc00-ud83dudfff]|[u2600-u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (emojiMatcher.find()) {
            source = emojiMatcher.replaceAll("*");
            return source;
        }
        return source;
    }
}

 其中用户信息实体类

package com.hfcx.cfms.manage.entity;

public class WxUser {
    // 用户的标识
    private String openId;
    // 昵称
    private String nickname;
    // 用户的性别(1是男性,2是女性,0是未知)
    private int sex;
    // 用户所在国家
    private String country;
    // 用户所在省份
    private String province;
    // 用户所在城市
    private String city;
    // 用户的语言,简体中文为zh_CN
    private String language;
    // 用户头像
    private String headImgUrl;

    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 int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getHeadImgUrl() {
        return headImgUrl;
    }

    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }
}

 

以上是关于微信公众号获取用户信息工具类的主要内容,如果未能解决你的问题,请参考以下文章

微信网页授权认证获取用户的详细信息,实现自动登陆-微信公众号开发干货

微信公众号开发之网页授权认证获取用户的详细信息,实现自动登陆

微信公众号基础02_获取accessToken和用户信息

微信公众号开发之网页授权认证获取用户的详细信息,实现自动登陆

微信公众号网页授权登录获取用户基本信息

微信公众平台接口如何获取微信用户详细信息