Administrator
2025-04-23 595b7935a30e84fba1bc3561d05f9d19d3e32e1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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"));
    }
 
}