xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
package com.nuvole.util;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.nuvole.common.domain.wx.PushMsg;
import com.nuvole.constants.ServiceConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Description: 外网接口请求工具类(微信)
 * @Author: liujun
 * @Date: 2019-07-25 11:59
 **/
@Slf4j
public class WxCallUtil {
 
    /**
     * 获取临时登录凭证code
     *
     * @param jsCode
     * @return
     */
    public static String getOpenidSessionKey(String jsCode) {
        String url = ServiceConstants.SERVICE_JSCODE2SESSION_URL;
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("jsCode", jsCode);
        return HttpUtil.get(url, paramMap);
    }
 
    /**
     * 获取AccessToken
     *
     * @return
     */
    public static String getAccessToken() {
        String url = ServiceConstants.SERVICE_TOKEN_URL;
        String result = HttpUtil.get(url);
        log.info("getAccessToken 返回值: {}", result);
        return result;
    }
 
    /**
     * 获取 jsapi_ticket
     *
     * @return
     */
    public static String getJsapTicket(String accessToken) {
        String url = ServiceConstants.SERVICE_JSAPITICKET_URL;
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("accessToken", accessToken);
        return HttpUtil.get(url, paramMap);
    }
 
    /**
     * 微信公众号获取access_token
     *
     * @param code
     * @return
     */
    public static String getWechatAccessToken(String code) {
        String url = ServiceConstants.SERVICE_ACCESS_TOKEN_URL;
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("code", code);
        return HttpUtil.get(url, paramMap);
    }
 
    /**
     * 微信公众号获取用户信息
     *
     * @param accessToken
     * @param openId
     * @return
     */
    public static String getWechatUserInfo(String accessToken, String openId) {
        String url = ServiceConstants.SERVICE_USERINFO_URL;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("accessToken", accessToken);
        paramMap.put("openId", openId);
        return HttpUtil.get(url, paramMap);
    }
 
    /**
     * 获取小程序码(前提是已发布)
     *
     * @param accessToken
     * @param scene
     * @param width
     * @param page
     * @return
     */
    public static byte[] getQRcode(String accessToken, String scene, int width, String page) {
        byte[] result = null;
        String url = ServiceConstants.SERVICE_WXACODEUNLIMIT_URL;
        if (false) {
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(url);
            HttpMethodParams paramMap = new HttpMethodParams();
            paramMap.setParameter("accessToken", accessToken);
            paramMap.setParameter("scene", scene);
            paramMap.setParameter("width", width);
            paramMap.setParameter("page", page);
            method.setParams(paramMap);
            try {
                int statusCode = client.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK) {
                    log.info("Method failed: " + method.getStatusLine());
                } else {
                    // 返回响应消息
                    result = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
                }
                method.releaseConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            RestTemplate rest = new RestTemplate();
            try {
                Map<String, Object> param = new HashMap<>();
                url = url + "?accessToken=" + accessToken + "&scene=" + scene + "&width=" + width + "&page=" + page;
                MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
                HttpEntity requestEntity = new HttpEntity(param, headers);
                ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class,
                        new Object[0]);
                result = entity.getBody();
                /*BASE64Encoder encoder = new BASE64Encoder();
                String imageStr = encoder.encode(result);
                imageStr = HtmlUtils.htmlEscape(imageStr);
                BASE64Decoder decoder = new BASE64Decoder();
                result = decoder.decodeBuffer(imageStr);*/
            } catch (Exception e) {
                log.error("调用小程序生成微信永久小程序码URL接口异常", e);
            }
        }
        return result;
    }
 
    /**
     * 微信推送消息
     *
     * @param openId
     * @param formId
     * @param templateId
     * @param page
     * @param items
     */
    public static void push(String openId, String formId, String templateId, String page, List<String> items) {
        String url = ServiceConstants.SERVICE_TEMPLATE_SEND_URL;
        Map<String, Object> paramMap = new HashMap<>(5);
        paramMap.put("openId", openId);
        paramMap.put("formId", formId);
        paramMap.put("templateId", templateId);
        paramMap.put("page", page);
        paramMap.put("items", CommonUtil.listToString(items));
        HttpUtil.get(url, paramMap);
    }
 
 
    /* *
     * 微信小程序订阅消息推送
     *
     * @author lc
     * @date 2020/1/2 14:28
     */
    public static void pushSend(PushMsg pushMsg, String accessToken) {
        String url = ServiceConstants.SERVICE_SUBSCRIBER_SEND_URL;
        Map<String, Object> paramMap = BeanUtil.beanToMap(pushMsg);
        paramMap.put("accessToken", accessToken);
        HttpUtil.get(url, paramMap);
    }
}