package com.taoke.autopay.utils;
|
|
import com.google.gson.Gson;
|
import com.taoke.autopay.dto.WXAppInfoDto;
|
import net.sf.json.JSONObject;
|
import org.yeshi.utils.HttpUtil;
|
import org.yeshi.utils.entity.wx.WXAPPInfo;
|
|
/**
|
* @author hxh
|
* @title: WxApiUtil
|
* @description: 微信帮助类
|
* @date 2024/6/28 17:27
|
*/
|
public class WxApiUtil {
|
|
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;
|
}
|
}
|
|
|
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);
|
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 void main(String[] args) throws Exception {
|
|
getAcessTokenInfo("061KT5ll2vgWHd4VyIll2UVQZV0KT5lP", new WXAppInfoDto("wx6217429129959b05", "14ae1808a271111954c509d8cb06df92"));
|
}
|
|
}
|