小程序官方文档链接:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
需要注意base64转换需要拼接:data:image/jpg;base64,
public String getWxCode(String accessToken) {
String PROGRAM_CODE_URL=“https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s”;
try {
String url=String.format(PROGRAM_CODE_URL, accessToken);
RestTemplate rest = new RestTemplate();
Map<String,Object> param = new HashMap<>();
// 限制32位
param.put("scene", “123456”));
// 必须发布的页面,非必填
param.put("page", "pages/index/index");
param.put("width", 430);
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);
log.info("调用生成微信URL接口传参:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
log.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
byte[] result = entity.getBody();
log.info(Base64.encodeBase64String(result));
res=Base64.encodeBase64String(result);
if(StringUtils.isNotBlank(res)) {
String encoded = "data:image/jpg;base64," + res.replaceAll("[\\s*\t\n\r]", "");
return encoded;
}
} catch (Exception e) {
log.error("调用小程序生成二维码失败:{}", e);
throw new BussnessException("调用小程序生成二维码失败");
}
return res;
}
|