shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.ishop.mobile.support;
 
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iplatform.base.PlatformRuntimeException;
import com.iplatform.base.WechatConstants;
import com.ishop.mobile.util.WechatUtils;
import com.ishop.model.vo.WechatMiniAuthorizeVo;
import com.ishop.model.wechat.WeChatAuthorizeLoginUserInfoVo;
import com.ishop.model.wechat.WeChatOauthToken;
import com.walker.infrastructure.arguments.ArgumentsManager;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
 
import java.text.MessageFormat;
import java.util.HashMap;
 
public class WechatEngine {
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 微信小程序获取授权信息
     * @param code
     * @return
     * @date 2023-09-14
     */
    public WechatMiniAuthorizeVo getMiniAuthCode(String code){
        if(StringUtils.isEmpty(this.wechatMiniAppId)){
            throw new PlatformRuntimeException("wechatMiniAppId未设置");
        }
        if(StringUtils.isEmpty(this.wechatMiniSecret)){
            throw new PlatformRuntimeException("wechatMiniSecret未设置");
        }
        String url = MessageFormat.format(WechatConstants.WECHAT_MINI_SNS_AUTH_CODE2SESSION_URL
                , this.wechatMiniAppId, this.wechatMiniSecret, code);
        String entity = restTemplate.getForObject(url, String.class, new HashMap<>(2));
        if(StringUtils.isEmpty(entity)){
            throw new PlatformRuntimeException("微信平台接口异常,没任何数据返回!" + WechatConstants.WECHAT_MINI_SNS_AUTH_CODE2SESSION_URL);
        }
 
        WechatMiniAuthorizeVo wechatMiniAuthorizeVo = null;
        ObjectNode objectNode = WechatUtils.acquireObjectNode(entity);
 
        if (objectNode.has("errcode") && !objectNode.get("errcode").asText().equals("0")) {
            if (objectNode.has("errmsg")) {
                // 保存到微信异常表
//                    wxExceptionDispose(data, StrUtil.format("微信获取accessToken异常,{}端", type));
                throw new PlatformRuntimeException("微信接口调用失败:" + objectNode.get("errcode") + objectNode.get("errmsg"));
            }
        }
        try{
            wechatMiniAuthorizeVo = JsonUtils.jsonStringToObject(entity, WechatMiniAuthorizeVo.class);
            logger.info("调用一次微信远程接口获取'jscode2session'={}", wechatMiniAuthorizeVo);
            return wechatMiniAuthorizeVo;
 
        } catch (Exception ex){
            throw new RuntimeException("json字符串转对象错误:" + objectNode, ex);
        }
    }
 
    /**
     * 获取开放平台用户信息
     * @param accessToken 调用凭证
     * @param openid      普通用户的标识,对当前开发者帐号唯一
     *                    公众号使用
     * @return 开放平台用户信息对象
     * @date 2023-07-27
     */
    public WeChatAuthorizeLoginUserInfoVo getSnsUserInfo(String accessToken, String openid) {
        String url = MessageFormat.format(WechatConstants.WECHAT_SNS_USERINFO_URL, accessToken, openid, "zh_CN");
//        ObjectNode objectNode = RestTemplateUtils.getData(url, this.restTemplate);
        String entity = restTemplate.getForObject(url, String.class, new HashMap<>(2));
        if(StringUtils.isEmpty(entity)){
            throw new PlatformRuntimeException("微信平台接口异常,没任何数据返回!");
        }
 
        WeChatAuthorizeLoginUserInfoVo loginUserInfoVo = null;
        ObjectNode objectNode = WechatUtils.acquireObjectNode(entity);
//        try {
//            objectNode = JsonUtils.jsonStringToObjectNode(entity);
//        } catch (Exception e) {
//            throw new PlatformRuntimeException("string转ObjectNode错误:" + e.getMessage(), e);
//        }
 
        if (objectNode.has("errcode") && !objectNode.get("errcode").asText().equals("0")) {
            if (objectNode.has("errmsg")) {
                // 保存到微信异常表
//                    wxExceptionDispose(data, StrUtil.format("微信获取accessToken异常,{}端", type));
                throw new PlatformRuntimeException("微信接口调用失败:" + objectNode.get("errcode") + objectNode.get("errmsg"));
            }
        }
        try{
            loginUserInfoVo = JsonUtils.jsonStringToObject(entity, WeChatAuthorizeLoginUserInfoVo.class);
            logger.info("调用一次微信远程接口获取'sns_userInfo'={}", loginUserInfoVo);
            return loginUserInfoVo;
 
        } catch (Exception ex){
            throw new RuntimeException("json字符串转对象错误:" + objectNode, ex);
        }
    }
 
    /**
     * 获取开放平台access_token
     * @param code
     * @return
     * @date 2023-07-27
     */
    public WeChatOauthToken getOauth2AccessToken(String code) {
        if(StringUtils.isEmpty(this.wechatPublicAppId)){
            throw new PlatformRuntimeException("wechatPublicAppId未设置");
        }
        if(StringUtils.isEmpty(this.wechatPublicSecret)){
            throw new PlatformRuntimeException("wechatAppSecret未设置");
        }
        String url = WechatUtils.getWechatOauthAccessTokenUrl(this.wechatPublicAppId, this.wechatPublicSecret, code);
        if(logger.isDebugEnabled()){
            logger.debug(url);
        }
//        ObjectNode objectNode = RestTemplateUtils.getData(url, this.restTemplate);
        String entity = restTemplate.getForObject(url, String.class, new HashMap<>(2));
        if(StringUtils.isEmpty(entity)){
            throw new PlatformRuntimeException("微信平台接口异常,没任何数据返回!");
        }
 
        WeChatOauthToken weChatOauthToken = null;
        ObjectNode objectNode = WechatUtils.acquireObjectNode(entity);
//        try {
//            objectNode = JsonUtils.jsonStringToObjectNode(entity);
//        } catch (Exception e) {
//            throw new PlatformRuntimeException("string转ObjectNode错误:" + e.getMessage(), e);
//        }
        if (objectNode.has("errcode") && !objectNode.get("errcode").asText().equals("0")) {
            if (objectNode.has("errmsg")) {
                // 保存到微信异常表
//                    wxExceptionDispose(data, StrUtil.format("微信获取accessToken异常,{}端", type));
                throw new PlatformRuntimeException("微信接口调用失败:" + objectNode.get("errcode") + objectNode.get("errmsg"));
            }
        }
 
        try{
            weChatOauthToken = JsonUtils.jsonStringToObject(entity, WeChatOauthToken.class);
            logger.info("调用一次微信远程接口获取'access_token'=", weChatOauthToken);
            return weChatOauthToken;
 
        } catch (Exception ex){
            throw new PlatformRuntimeException("json字符串转对象错误:" + objectNode, ex);
        }
    }
 
//    private ObjectNode acquireObjectNode(String entity){
//        ObjectNode objectNode = null;
//        try {
//            objectNode = JsonUtils.jsonStringToObjectNode(entity);
//        } catch (Exception e) {
//            throw new PlatformRuntimeException("string转ObjectNode错误:" + e.getMessage(), e);
//        }
//        return objectNode;
//    }
 
    public void setArgumentsManager(ArgumentsManager argumentsManager) {
        this.argumentsManager = argumentsManager;
    }
 
    public void setWechatPublicSecret(String wechatPublicSecret) {
        this.wechatPublicSecret = wechatPublicSecret;
    }
 
    public void setWechatPublicAppId(String wechatPublicAppId) {
        this.wechatPublicAppId = wechatPublicAppId;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
 
    /**
     * 返回微信小程序appId
     * @return
     * @date 2023-09-14
     */
    public String getWechatMiniSecret() {
        return wechatMiniSecret;
    }
 
    /**
     * 返回微信小程序对应密钥
     * @return
     * @date 2023-09-14
     */
    public void setWechatMiniSecret(String wechatMiniSecret) {
        this.wechatMiniSecret = wechatMiniSecret;
    }
 
    public String getWechatMiniAppId() {
        return wechatMiniAppId;
    }
 
    public void setWechatMiniAppId(String wechatMiniAppId) {
        this.wechatMiniAppId = wechatMiniAppId;
    }
 
    private String wechatMiniSecret;
    private String wechatMiniAppId;
    private RestTemplate restTemplate;
    private String wechatPublicSecret;     // 公众号密钥
    private String wechatPublicAppId;   // 公众号appid
    private ArgumentsManager argumentsManager;
}