标签:class ppi ret ram ring else turn code exp
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
public class WxUserInfo {
private static Logger log = LoggerFactory.getLogger(WxUserInfo.class);
private static ResourceBundle wx = ResourceBundle.getBundle("wx");// 读取配置文件
private static final String TOKEN = wx.getString("Token");
private static final String APPID = wx.getString("appID");
private static final String APPSECRET = wx.getString("appsecret");
private static final String ACCESS_TOKEN_URL = wx.getString("ACCESS_TOKEN_URL");
private static final String WEIXIN_USERINFO_URL = wx.getString("WEIXIN_USERINFO_URL");
/**
* 获取微信用户信息
* (需scope为 snsapi_userinfo)
* @param code
* @return
*/
public static String getWxUserInfo(String code){
log.info("=== 根据code < "+code+" > 获取 用户唯一标识 openid ===");
String wxUserInfo ="{}";
if("".equals(code) || code == null) {
log.info("=== code 不存在 ===");
return wxUserInfo;
}else{
String accessTokenUrl = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("SECRET", APPSECRET).replace("CODE", code);
String accessTokenString = SendQuest.sendGet(accessTokenUrl);
JSONObject accessTokenJson = JSONObject.parseObject(accessTokenString);//{"errcode":40029,"errmsg":"invalid code"}
if (!accessTokenJson.containsKey("errcode")) {
log.info("=== 根据code < "+code+" > 获取 用户唯一标识 openid success===");
// int expires_in = accessTokenJson.getInteger("expires_in");//错误状态码
String access_token = accessTokenJson.getString("access_token");
String openid = accessTokenJson.getString("openid");
log.info("=== 获取 access_token < "+access_token+" > openid < " +openid+" > 成功===");
String weixinUserInfoUrl = WEIXIN_USERINFO_URL.replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
String weixinUserInfoString = SendQuest.sendGet(weixinUserInfoUrl);
JSONObject weixinUserInfoJson = JSONObject.parseObject(weixinUserInfoString);//{"errcode":40029,"errmsg":"invalid code"}
if (!weixinUserInfoJson.containsKey("errcode")) {
log.info("=== 获取 wxUserInfo 成功===");
log.info(weixinUserInfoString);
// String nickname = accessTokenJson.getString("nickname");
return weixinUserInfoString;
}else{
log.info("=== 获取 WxuserInfo 失败===");
log.info(weixinUserInfoString);
return weixinUserInfoString;
}
}else{
log.info("=== 获取 access_token < > openid < > 失败===");
log.info(accessTokenString);
return accessTokenString;
}
}
}
}
标签:class ppi ret ram ring else turn code exp
原文地址:https://www.cnblogs.com/gzl180110/p/11584651.html