package com.taoke.autopay.utils;
|
|
import com.google.gson.Gson;
|
import com.taoke.autopay.dto.WXAppInfoDto;
|
import lombok.Data;
|
import net.sf.json.JSONObject;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.yeshi.utils.HttpUtil;
|
import org.yeshi.utils.entity.wx.WXAPPInfo;
|
|
import java.net.URLEncoder;
|
|
/**
|
* @author hxh
|
* @title: WxApiUtil
|
* @description: 微信帮助类
|
* @date 2024/6/28 17:27
|
*/
|
public class WxApiUtil {
|
|
private static Logger wxLogger = LoggerFactory.getLogger("wxLogger");
|
|
public class WXAccessTokenInfo {
|
private String access_token;
|
private int expires_in;
|
private String refresh_token;
|
private String openid;
|
private String scope;
|
private int is_snapshotuser;
|
private String unionid;
|
|
public String getAccess_token() {
|
return access_token;
|
}
|
|
public void setAccess_token(String access_token) {
|
this.access_token = access_token;
|
}
|
|
public int getExpires_in() {
|
return expires_in;
|
}
|
|
public void setExpires_in(int expires_in) {
|
this.expires_in = expires_in;
|
}
|
|
public String getRefresh_token() {
|
return refresh_token;
|
}
|
|
public void setRefresh_token(String refresh_token) {
|
this.refresh_token = refresh_token;
|
}
|
|
public String getOpenid() {
|
return openid;
|
}
|
|
public void setOpenid(String openid) {
|
this.openid = openid;
|
}
|
|
public String getScope() {
|
return scope;
|
}
|
|
public void setScope(String scope) {
|
this.scope = scope;
|
}
|
|
public int getIs_snapshotuser() {
|
return is_snapshotuser;
|
}
|
|
public void setIs_snapshotuser(int is_snapshotuser) {
|
this.is_snapshotuser = is_snapshotuser;
|
}
|
|
public String getUnionid() {
|
return unionid;
|
}
|
|
public void setUnionid(String unionid) {
|
this.unionid = unionid;
|
}
|
}
|
|
@Data
|
public static class WXUserInfo{
|
private String openid;// 用户的唯一标识
|
private String nickname ;// 用户昵称
|
private Integer sex;// 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
|
private String province;// 用户个人资料填写的省份
|
private String city ;// 普通用户个人资料填写的城市
|
private String country;// 国家,如中国为CN
|
private String headimgurl;// 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
|
private String unionid;// 只有在用户将公众号绑定到微信开放平台账号后,才会出现该字段。
|
|
}
|
|
|
public static WXAccessTokenInfo getAcessTokenInfo(String code, WXAppInfoDto app) throws Exception {
|
String tokenUrl = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", app.getAppId(), app.getAppSecret(), code);
|
String result = HttpUtil.get(tokenUrl);
|
System.out.println(result);
|
wxLogger.info(result);
|
JSONObject root = JSONObject.fromObject(result);
|
if (root.optInt("errcode", 0) != 0) {
|
throw new Exception(root.optString("errmsg"));
|
}
|
return JsonUtil.getSimpleGson().fromJson(result, WXAccessTokenInfo.class);
|
}
|
|
public static WXUserInfo getUserInfo(String accessToken,String openid) throws Exception {
|
String tokenUrl = String.format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", URLEncoder.encode(accessToken,"UTF-8"),openid);
|
String result = HttpUtil.get(tokenUrl);
|
result = new String(result.getBytes("ISO-8859-1"),"UTF-8");
|
System.out.println(result);
|
wxLogger.info(result);
|
JSONObject root = JSONObject.fromObject(result);
|
if (root.optInt("errcode", 0) != 0) {
|
throw new Exception(root.optString("errmsg"));
|
}
|
return JsonUtil.getSimpleGson().fromJson(result, WXUserInfo.class);
|
}
|
|
public static void main(String[] args) throws Exception {
|
|
getAcessTokenInfo("061KT5ll2vgWHd4VyIll2UVQZV0KT5lP", new WXAppInfoDto("wx6217429129959b05", "14ae1808a271111954c509d8cb06df92"));
|
}
|
|
}
|