| | |
| | | package com.ruoyi.account.util.weChat; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.http.Header; |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.hutool.http.HttpUtil; |
| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.http.HttpHeaders; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.File; |
| | | import java.io.InputStream; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | |
| | | |
| | | |
| | | // /** |
| | | // * 获取小程序二维码 |
| | | // * @param page 跳转页 例如 pages/index/index |
| | | // * @param scene 参数 a=1&b=2 |
| | | // */ |
| | | // public InputStream getwxacodeunlimit(String page, String scene){ |
| | | // try { |
| | | // String token = getWxAppletsAccessToken(); |
| | | // if(StringUtils.isEmpty(token)){ |
| | | // System.err.println("获取接口调用凭证失败"); |
| | | // } |
| | | // String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token; |
| | | // Map<String, Object> param = new HashMap<>(); |
| | | // param.put("scene", scene); |
| | | // param.put("page", page); |
| | | // HttpHeaders httpHeaders = new HttpHeaders(); |
| | | // MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8"); |
| | | // httpHeaders.setContentType(type); |
| | | // HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(param, httpHeaders); |
| | | // ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, new Object[0]); |
| | | // String body1 = exchange.getBody(); |
| | | //// System.err.println(body1); |
| | | // ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); |
| | | // byte[] body = entity.getBody(); |
| | | //// System.err.println(Base64.encodeBase64String(body)); |
| | | // return new ByteArrayInputStream(body); |
| | | // }catch (Exception e){ |
| | | // e.printStackTrace(); |
| | | // } |
| | | // return null; |
| | | // } |
| | | /** |
| | | * 获取小程序二维码 |
| | | * @param page 跳转页 例如 pages/index/index |
| | | * @param scene 参数 a=1&b=2 |
| | | */ |
| | | public InputStream getwxacodeunlimit(String page, String scene, EnvVersion env_version){ |
| | | try { |
| | | String token = getWxAppletsAccessToken(); |
| | | if(StringUtils.isEmpty(token)){ |
| | | System.err.println("获取接口调用凭证失败"); |
| | | } |
| | | String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token; |
| | | Map<String, Object> param = new HashMap<>(); |
| | | param.put("scene", scene); |
| | | param.put("page", page); |
| | | param.put("env_version", env_version.getVersion()); |
| | | |
| | | HttpRequest post = HttpUtil.createPost(url); |
| | | post.header(Header.CONTENT_TYPE, "application/json;charset=UTF-8"); |
| | | post.body(JSON.toJSONString(param)); |
| | | HttpResponse execute = post.execute(); |
| | | byte[] bytes = execute.bodyBytes(); |
| | | String body1 = execute.body(); |
| | | System.err.println(body1); |
| | | System.err.println(Base64.encodeBase64String(bytes)); |
| | | return new ByteArrayInputStream(bytes); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取微信小程序二维码 |
| | | * @param page |
| | | * @param scene |
| | | * @param filePath |
| | | * @return |
| | | */ |
| | | public String getwxacodeunlimit(String page, String scene, EnvVersion env_version, String filePath){ |
| | | InputStream getwxacodeunlimit = getwxacodeunlimit(page, scene, env_version); |
| | | File file = FileUtil.writeFromStream(getwxacodeunlimit, new File(filePath)); |
| | | return file.getPath(); |
| | | } |
| | | } |