| 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.getWxAccessToken() ; | 
|         //生成图片上传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.getWxAccessToken() ; | 
|         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(),"生成图片失败"); | 
|     } | 
|   | 
| } |