jiangping
2023-09-13 5c82263c61825c37514b2329ee3ffa425e62a900
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
package com.doumee.service.business;
 
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.HttpsUtil;
import com.doumee.core.wx.AccessToken;
import com.doumee.core.wx.SendWxMessage;
import com.doumee.core.wx.WxPayProperties;
import com.github.binarywang.utils.qrcode.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Hashtable;
 
import javax.imageio.ImageIO;
 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Rk
 * @create 2023/5/8 18:44
 */
@Slf4j
@Service
public class UtilService {
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
 
 
    public String generate(@RequestParam Integer bookingsId, @RequestParam Integer userId) {
        String accessToken =  systemDictDataBiz.queryCodeById(106);
        //生成图片上传OSS
        Map<String,Object> body = new HashMap<>();
        // 场景码,与前端约定,最终是需要前端解析
        //body.put("scene", "bookingsId="+bookingsId+"&userId="+userId);
        body.put("scene", bookingsId+"x"+userId);
        // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
        body.put("env_version", "develop");
        body.put("page", "packagesMine/meetingDetails/meetingDetails");
        // 透明,根据你的场景自行设置body参数
        body.put("is_hyaline", true);
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, JSONObject.toJSONString(body));
        Request request = new Request.Builder().url("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken)
                .method("POST", requestBody).build();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
                return "data:image/png;base64," + this.inputStream2Base64(inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"生成图片失败");
    }
 
    public  String inputStream2Base64(InputStream is) throws Exception {
        byte[] data = null;
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = is.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new Exception("输入流关闭异常");
                }
            }
        }
        return Base64.getEncoder().encodeToString(data);
    }
 
    public InputStream generateImgStream(Integer bookingsId, Integer userId) {
        String accessToken =  systemDictDataBiz.queryCodeById(106);
        log.info("微信小程序-> accessToken:{}",accessToken);
        //生成图片上传OSS
        Map<String,Object> body = new HashMap<>();
        // 场景码,与前端约定,最终是需要前端解析
        body.put("scene", bookingsId+"x"+userId);
        // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
        body.put("env_version", "release");
        body.put("page", "packagesMine/meetingDetails/meetingDetails");
        // 透明,根据你的场景自行设置body参数
        body.put("is_hyaline", true);
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, JSONObject.toJSONString(body));
        Request request = new Request.Builder().url("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken)
                .method("POST", requestBody).build();
        try {
            Response response = client.newCall(request).execute();
            log.info("微信小程序-> 生成小程序二维码:{}",JSONObject.toJSONString(response));
            if (response.isSuccessful()) {
                InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
                return inputStream;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"生成图片失败");
    }
 
}