wx第三方登录工具类

package com.chwl.medical.core.face.util.oAuth;

import com.chwl.medical.core.face.util.load.GetPropUtil;
import com.chwl.medical.core.util.StringUtils;
import com.chwl.medical.utils.http.HttpConnectionUtils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.httpclient.NameValuePair;

import java.io.IOException;


/**
 * 第三方微信登录util类
 */
public class WxOAuthUtil {
    private static final Logger logger = LoggerFactory.getLogger(WxOAuthUtil.class);
    private static final String APPID = GetPropUtil.getStringValueByKey("appid");
    private static final String SECRET = GetPropUtil.getStringValueByKey("secret");
    private static final Integer EXPIRES_IN = GetPropUtil.getIntValueByKey("expires_in");

    public WxOAuthUtil() {

    }

    /**
     * 获取access_token
     *
     * @param code 用户换取access_token的code
     * @return
     */
    public JSONObject getAccessToken(String code) throws Exception {
        JSONObject result = new JSONObject();
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
        //访问链接携带的参数
        NameValuePair[] nameValuePairs = new NameValuePair[4];
        nameValuePairs[0] = new NameValuePair("appid", this.APPID);
        nameValuePairs[1] = new NameValuePair("secret", this.SECRET);

        if (StringUtils.isNotEmpty(code)) {
            nameValuePairs[2] = new NameValuePair("code", code);
        }
        String grant_type = "authorization_code";
        nameValuePairs[3] = new NameValuePair("grant_type", grant_type);
        String json = HttpConnectionUtils.getData(url, nameValuePairs);
        result = JSONObject.fromObject(json);
        return result;
    }


    /**
     * 刷新access_token
     *
     * @param refresh_token 通过access_token获取到的refresh_token参数
     * @return
     */
    public JSONObject refreshToken(String refresh_token) throws Exception {
        JSONObject result = new JSONObject();
        String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
        //访问链接携带的参数
        NameValuePair[] nameValuePairs = new NameValuePair[3];
        nameValuePairs[0] = new NameValuePair("appid", this.APPID);
        String grant_type = "refresh_token";
        nameValuePairs[1] = new NameValuePair("grant_type", grant_type);

        if (StringUtils.isNotEmpty(refresh_token)) {
            nameValuePairs[2] = new NameValuePair("refresh_token", refresh_token);
        }
        String json = HttpConnectionUtils.getData(url, nameValuePairs);
        result = JSONObject.fromObject(json);
        return result;
    }


    /**
     * 验证access_token
     *
     * @param access_token 需要验证的access_token
     * @param openid       授权用户唯一标识
     * @return
     */
    public JSONObject verifyToken(String access_token, String openid) throws Exception {
        JSONObject result = new JSONObject();
        String url = "https://api.weixin.qq.com/sns/auth";
        //访问链接携带的参数
        NameValuePair[] nameValuePairs = new NameValuePair[2];

        if (StringUtils.isNotEmpty(openid)) {
            nameValuePairs[0] = new NameValuePair("openid", openid);
        }

        if (StringUtils.isNotEmpty(access_token)) {
            nameValuePairs[1] = new NameValuePair("access_token", access_token);
        }
        String json = HttpConnectionUtils.getData(url, nameValuePairs);
        result = JSONObject.fromObject(json);
        return result;
    }


    /**
     * 获取用户信息
     *
     * @param access_token 调用凭证
     * @param openid       授权用户唯一标识
     * @return
     */
    public JSONObject getUserInfo(String access_token, String openid) throws Exception {
        JSONObject result = new JSONObject();
        String url = "https://api.weixin.qq.com/sns/userinfo";
        //访问链接携带的参数
        NameValuePair[] nameValuePairs = new NameValuePair[3];

        if (StringUtils.isNotEmpty(access_token)) {
            nameValuePairs[0] = new NameValuePair("access_token", access_token);
        }

        if (StringUtils.isNotEmpty(openid)) {
            nameValuePairs[1] = new NameValuePair("openid", openid);
        }
        String lang = "zh_CN";
        nameValuePairs[2] = new NameValuePair("lang", lang);
        String json = HttpConnectionUtils.getData(url, nameValuePairs);
        result = JSONObject.fromObject(json);
        return result;
    }


}

 

posted @ 2018-03-16 10:48  夏文杰  阅读(114)  评论(0)    收藏  举报