nidapeng
2024-04-23 5072f33b5c54142f00991d0dafc9ea75af696b81
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
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 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 java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
 * 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(),"生成图片失败");
    }
 
}