/**
*
* @Title: getWeChatSmallProgramCode
* @Description: 获取微信小程序码
* @date 2018年6月26日下午3:35:25
*/
private BufferedImage getWeChatSmallProgramCode(String UserIdOfEncryption){
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
BufferedImage bufferedImage = null;
try {
String accessToken = “你的access_token”;
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
Map<String, Object> param = new HashMap<String, Object>();
param.put("width", 160);
param.put("auto_color", false);
Map<String, Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
param.put("page", "小程序页面");
param.put("scene", "参数");
param.put("is_hyaline", true);
logger.info("调用生成微信URL接口传参:" + JSON.toJSONString(param));
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(JSON.toJSONString(param), headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
byte[] result = entity.getBody();
inputStream = new ByteArrayInputStream(result);
bufferedImage = ImageIO.read(inputStream);
} catch (Exception e) {
logger.info(e.getMessage(),e);
}
return bufferedImage;
}
使用的是接口B:适用于需要的码数量极多的业务场景:page参数仔细阅读官方文档可发现,该参数的小程序页面必须是已发布的小程序页面,开发版和体验版传此参数会报错:
无效的页面({
"errcode": 41030,
"errmsg": "invalid page hint: [X7Y8ya02774528]"
})