admin
2019-02-19 9192375a211cff2f91d1f65f932f97612d8dbcdb
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.fanli.facade.user.util.wx;
 
import org.fanli.facade.user.dto.wx.WeiXinUser;
import org.fanli.facade.user.dto.wx.WeiXinUserParam;
import org.json.JSONException;
import org.json.JSONObject;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.base.Constant;
import com.yeshi.fanli.base.log.LogHelper;
 
public class WXLoginUtil {
 
    public static String WEXIN_APPID = "wx43617e2ed82c5b28";
    public static String WEXIN_SECRET = "f8a65908fb4681bdd181de70f6ab9a3b";
 
    static String APPID = "wx5c0d167c6e3ad726";
    static String SECRET = "0c79d5869bb0f2d7c13e43f9a18f440d";
 
    static {
        WEXIN_APPID = Constant.systemCommonConfig.getWxGZAppId();
        WEXIN_SECRET = Constant.systemCommonConfig.getWxGZAppSecret();
        APPID = Constant.systemCommonConfig.getWxOpenAppId();
        SECRET = Constant.systemCommonConfig.getWxOpenAppSecret();
    }
 
    // 通过code获取access_token openId
    private static String weiXinUrl1 = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
 
    public static String weiXinUrl2 = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
 
    public static WeiXinUserParam getWxParam(String code) {
        String wxUrl = String.format(weiXinUrl1, APPID, SECRET, code);
        String result = HttpUtil.get(wxUrl);
        WeiXinUserParam weiXinUserParam = getWeiXinUserParam(result);
        return weiXinUserParam;
    }
 
    public static WeiXinUserParam getWxWYParam(String code) {
        String wxUrl = String.format(weiXinUrl1, WEXIN_APPID, WEXIN_SECRET, code);
        String result = HttpUtil.get(wxUrl);
        WeiXinUserParam weiXinUserParam = getWeiXinUserParam(result);
        return weiXinUserParam;
    }
 
    public static WeiXinUser getWeiXinUser(WeiXinUserParam param) {
        String result = HttpUtil.getAsString(String.format(weiXinUrl2, param.getAccessToken(), param.getOpenId()),
                "ISO-8859-1", "UTF-8");
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String nickname = jsonObject.optString("nickname");
        String province = jsonObject.optString("province");
        String city = jsonObject.optString("city");
        String country = jsonObject.optString("country");
        String headimgurl = jsonObject.optString("headimgurl");
        String unionid = jsonObject.optString("unionid");
        int sex = jsonObject.optInt("sex");
        WeiXinUser weiXinUser = new WeiXinUser();
        weiXinUser.setCity(city);
        weiXinUser.setCountry(country);
        weiXinUser.setHeadimgurl(headimgurl);
        weiXinUser.setNickname(nickname);
        weiXinUser.setOpenid(param.getOpenId());
        weiXinUser.setSex(sex);
        weiXinUser.setUnionid(unionid);
        weiXinUser.setProvince(province);
        return weiXinUser;
    }
 
    private static WeiXinUser getWeiXinUser(String code) {
        WeiXinUserParam param = getWxParam(code);
        if (param == null) {
            return null;
        }
        WeiXinUser weiXinUser = getWeiXinUser(param);
        if (weiXinUser != null && StringUtil.isNullOrEmpty(weiXinUser.getUnionid())
                && !StringUtil.isNullOrEmpty(param.getUnionId())) {
            weiXinUser.setUnionid(param.getUnionId());
        }
        return weiXinUser;
    }
 
    /**
     * 通过code获取用户信息(伴随头像缓存)
     * 
     * @param code
     * @return
     */
    public static WeiXinUser getWeiXinUserWithSavePortrait(String code) {
        WeiXinUser user = getWeiXinUser(code);
        if (user != null) {
            if (!StringUtil.isNullOrEmpty(user.getHeadimgurl())) {
 
                FileUploadResult result = COSManager.getInstance().uploadFile(
                        HttpUtil.getAsInputStream(user.getHeadimgurl()),
                        String.format("/portrait/wx/%s_%s.jpg", user.getUnionid(), System.currentTimeMillis() + ""));
                if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
                    user.setHeadimgurl(result.getUrl());
                    // COS删除其余头像
                    // String prefix = String.format("/portrait/wx/%s_",
                    // user.getUnionid());
                    // ObjectListing list =
                    // COSManager.getInstance().getObjectList(prefix, null, 30);
                    // if (list != null && list.getObjectSummaries() != null)
                    // for (COSObjectSummary object : list.getObjectSummaries())
                    // {
                    // if (!result.getUrl().contains(object.getKey())) {
                    // COSManager.getInstance().deleteFile(object.getKey());
                    // }
                    // }
                }
            }
        }
        return user;
    }
 
    public static WeiXinUser getWeiXinWYUser(String code) {
        WeiXinUserParam param = getWxWYParam(code);
        if (param == null) {
            return null;
        }
        return getWeiXinUser(param);
    }
 
    private static WeiXinUserParam getWeiXinUserParam(String result) {
        LogHelper.test(result);
        WeiXinUserParam weiXinUserParam = new WeiXinUserParam();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String openId = jsonObject.optString("openid");
        if ("".equals(openId)) {
            return null;
        }
        String accessToken = jsonObject.optString("access_token");
        String refreshToken = jsonObject.optString("refresh_token");
        String scope = jsonObject.optString("scope");
        int expires = jsonObject.optInt("expires_in");
        String unionId = jsonObject.optString("unionid");
 
        weiXinUserParam.setRefreshToken(refreshToken);
        weiXinUserParam.setOpenId(openId);
        weiXinUserParam.setExpires(expires);
        weiXinUserParam.setAccessToken(accessToken);
        weiXinUserParam.setScope(scope);
        weiXinUserParam.setUnionId(unionId);
 
        return weiXinUserParam;
    }
 
    /**
     * 初始化公众号自定义菜单
     */
    public static void initGZMenu() {
        JSONObject root = new JSONObject();
 
        // WXUtil.createMenu(accessToken, root);
    }
 
}