微信小程序生成太阳码官方链接
注:access_token需要调用微信的接口,获取token,否则无法获取二维码
{
"page":"pages/home/index", // 页面 page
"scene":"a=1", // 二维码携带参数(最大32个可见字符)
"is_hyaline":true //是否需要透明底色,为 true 时,生成透明底色的小程序
}
更多参数说明点击此处
Java代码获取base64
@RestController
@RequestMapping("wxa")
public class UrlUnlimitedController {
@Autowired
private TokenService tokenService;
@RequestMapping("/get-unlimited")
public Result getUnlimited(@RequestParam("path") String path,@RequestParam(value = "scene") String scene) {
// 获取接口凭证token,
String url = String.format(getunlimitedUrl, tokenService.getAccessToken());
JSONObject param = new JSONObject();
// 给相关参数赋值
param.put("page", path);
param.put("scene", scene);
param.put("is_hyaline", true);
// 通过RestTemplateUtils调用接口
HttpEntity requestEntity = new HttpEntity(param);
byte[] bytes = RestTemplateUtils.postData(url, requestEntity, byte[].class);
// 判断调用是否成功
if (bytes.length < 500) {
JSONObject jsonObject = JSONObject.parseObject(new String(bytes));
logger.error(String.format("获取获取小程序码unlimited失败,入参:%s,出参:%s",
param.toJSONString(), jsonObject.toJSONString()));
throw new WrappedRuntimeException(-1, jsonObject.getString("errmsg"));
}
// 若成功将bytes转成base64并返回给前端
Base64.Encoder encoder = Base64.getEncoder();
String encode = encoder.encodeToString(bytes);// 转码
return Result.successData("data:image/png;base64," + encode);
}
}
apipost测试:
将base64放到在线生成图片工具生成
另一种实现方法
@RestController
@RequestMapping("wxa")
public class UrlUnlimitedController {
@Autowired
private TokenService tokenService;
@RequestMapping("/get-unlimited")
public Result generateScheme(@RequestParam("path") String path,
@RequestParam(value = "scene") String scene
) throws IOException {
try {
// 获取接口凭证token,
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + tokenService.getAccessToken());
// 通过http调用接口
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// 提交模式
httpURLConnection.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", "a=1234567890");
paramJson.put("page", "pages/home/index");
paramJson.put("is_hyaline", true);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
// 通过IOUtils将流转成bytes
byte[] bytes = IOUtils.toByteArray(bis);
// 判断调用是否成功
if (bytes.length < 500) {
JSONObject jsonObject = JSONObject.parseObject(new String(bytes));
System.out.println(jsonObject.toJSONString());
}
// 若成功将bytes转成base64并返回给前端
Base64.Encoder encoder = Base64.getEncoder();
String encode = encoder.encodeToString(bytes);
return Result.successData("data:image/png;base64," + encode);
} catch (Exception e) {
e.printStackTrace();
}
return Result.success();
}
}
重要提示:
bytes 转base64不要使用 BASE64Encoder encoder = new BASE64Encoder();否则生成的base64会带有很多r导致转图片失败
BASE64Encoder encoder = new BASE64Encoder(); String encode = encoder.encode(bytes);



