xiangpei
2025-05-22 e868be6c913a6a99e1491c088d052a5f58e252f9
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
package cn.lili.modules.wechat.util;
 
import cn.lili.common.enums.ClientTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.message.entity.dos.ShortLink;
import cn.lili.modules.message.service.ShortLinkService;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 微信API交互token
 *
 * @author Chopper
 * @version v1.0
 * 2020-12-10 19:25
 */
@Slf4j
@Component
public class WechatMpCodeUtil {
 
    private static String UN_LIMIT_API = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
    private static String CREATE_QR_CODE = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
 
    @Autowired
    private WechatAccessTokenUtil wechatAccessTokenUtil;
    @Autowired
    private ShortLinkService shortLinkService;
 
    /**
     * 生成分享二维码
     *
     * @param path 路径
     * @return
     */
    public String createQrCode(String path) {
        try {
            String accessToken = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.WECHAT_MP);
            Map<String, String> params = new HashMap<>(2);
            params.put("path", path);
            params.put("width", "280");
 
            //======================================================================//
            //执行URL Post调用
            //======================================================================//
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(CREATE_QR_CODE + accessToken);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            //必须是json模式的 post
            String body = JSON.toJSONString(params);
            StringEntity entity = new StringEntity(body);
            entity.setContentType("image/png");
            httpPost.setEntity(entity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            //======================================================================//
            //处理HTTP返回结果
            //======================================================================//
            InputStream contentStream = httpEntity.getContent();
            byte[] bytes = toByteArray(contentStream);
            contentStream.read(bytes);
            //返回内容
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            log.error("生成二维码错误:", e);
            throw new ServiceException(ResultCode.WECHAT_QRCODE_ERROR);
        }
 
    }
 
    /**
     * 生成分享二维码
     *
     * @param page
     * @param scene
     * @return
     */
    public String createCode(String page, String scene) {
        try {
 
            //短链接存储
            ShortLink shortLink = new ShortLink();
            shortLink.setOriginalParams(scene);
            List<ShortLink> shortLinks = shortLinkService.queryShortLinks(shortLink);
            if (shortLinks.size() > 0) {
                shortLink = shortLinks.get(0);
            } else {
                shortLink.setOriginalParams(scene);
                shortLinkService.save(shortLink);
                shortLink = shortLinkService.queryShortLinks(shortLink).get(0);
            }
            String accessToken = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.WECHAT_MP);
            Map<String, Object> params = new HashMap<>(4);
            params.put("page", page);
            params.put("scene", shortLink.getId());
            params.put("width", "280");
 
            //======================================================================//
            //执行URL Post调用
            //======================================================================//
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(UN_LIMIT_API + accessToken);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            //必须是json模式的 post
            String body = JSON.toJSONString(params);
            StringEntity entity = new StringEntity(body);
            entity.setContentType("image/png");
            httpPost.setEntity(entity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            //======================================================================//
            //处理HTTP返回结果
            //======================================================================//
            InputStream contentStream = httpEntity.getContent();
            byte[] bytes = toByteArray(contentStream);
            contentStream.read(bytes);
            //返回内容
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            log.error("生成二维码错误:", e);
            throw new ServiceException(ResultCode.WECHAT_QRCODE_ERROR);
        }
 
    }
 
    /**
     * @param input
     * @return
     * @throws IOException
     */
    @SneakyThrows
    public static byte[] toByteArray(InputStream input) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }
 
    /**
     * @param input
     * @param output
     * @return
     * @throws IOException
     */
    public static int copy(InputStream input, OutputStream output) throws IOException {
        long count = copyLarge(input, output);
        if (count > 2147483647L) {
            return -1;
        }
        return (int) count;
    }
 
    /**
     * @param input
     * @param output
     * @return
     * @throws IOException
     */
    public static long copyLarge(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[4096];
        long count = 0L;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
 
 
}