zoukankan      html  css  js  c++  java
  • 微信公众号获取用户信息工具类

    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;
        }
    }
  • 相关阅读:
    tomcat启动问题
    索引优化
    itext转PDF(二)
    itext转pdf(一)
    定时器quartz配置文件
    hibernate自动去别名问题
    win10安装mysql 8.0.19 (解压方式)
    把数据按列拆分为数据表
    程序记时
    find的应用
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/10682617.html
Copyright © 2011-2022 走看看