| | |
| | | @Override |
| | | public FebsResponse generateQrCode(WxGenerateQrCodeDto wxGenerateQrCodeDto) { |
| | | String base64 = null; |
| | | try { |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY); |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("scene", wxGenerateQrCodeDto.getScene()); |
| | | params.put("page", wxGenerateQrCodeDto.getPage()); |
| | | params.put("width", 430); |
| | | ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, params, byte[].class); |
| | | if (responseEntity.getStatusCode() == HttpStatus.OK) { |
| | | byte[] body = responseEntity.getBody(); |
| | | InputStream inputStream = new ByteArrayInputStream(body); |
| | | // 将获取流转为base64格式 |
| | | byte[] data = null; |
| | | ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
| | | byte[] buff = new byte[100]; |
| | | int rc = 0; |
| | | while ((rc = inputStream.read(buff, 0, 100)) > 0) { |
| | | swapStream.write(buff, 0, rc); |
| | | } |
| | | data = swapStream.toByteArray(); |
| | | base64 = Base64.byteArrayToBase64(data); |
| | | inputStream.close(); |
| | | swapStream.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | throw new ApiException("生成二维码失败"); |
| | | } |
| | | return new FebsResponse().success().data(base64); |
| | | // try { |
| | | // RestTemplate restTemplate = new RestTemplate(); |
| | | // String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY); |
| | | // Map<String, Object> params = new HashMap<>(); |
| | | // params.put("scene", wxGenerateQrCodeDto.getScene()); |
| | | // params.put("page", wxGenerateQrCodeDto.getPage()); |
| | | // params.put("width", 430); |
| | | // ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, params, byte[].class); |
| | | // log.info(String.valueOf(JSONUtil.parse(responseEntity))); |
| | | // if (responseEntity.getStatusCode() == HttpStatus.OK) { |
| | | // byte[] body = responseEntity.getBody(); |
| | | // InputStream inputStream = new ByteArrayInputStream(body); |
| | | // // 将获取流转为base64格式 |
| | | // byte[] data = null; |
| | | // ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
| | | // byte[] buff = new byte[100]; |
| | | // int rc = 0; |
| | | // while ((rc = inputStream.read(buff, 0, 100)) > 0) { |
| | | // swapStream.write(buff, 0, rc); |
| | | // } |
| | | // data = swapStream.toByteArray(); |
| | | // base64 = Base64.byteArrayToBase64(data); |
| | | // inputStream.close(); |
| | | // swapStream.close(); |
| | | // |
| | | // return new FebsResponse().success().data(body); |
| | | // } |
| | | // } catch (IOException e) { |
| | | // throw new ApiException("生成二维码失败"); |
| | | // } |
| | | return new FebsResponse().success(); |
| | | } |
| | | |
| | | /** |