前提需要:
- 微信的access_token
- 跳转页面:pages/index/index
代码实现:
public String getminiqrQr(String sceneStr) {
String accessToken = "你自己的access_token";
Integer qrcodeId = (int) (1 + Math.random() * (1000 - 1 + 1));
String urlQrCode = "";
String urls = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;
Map<String, Object> param = new HashMap<>();
param.put("path", "pages/index/index?orgId=" + sceneStr);
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);
logger.info("调用生成微信URL接口传参:" + param);
logger.info("调用生成微信URL接口:" + urls);
try {
URL url = new URL(urls);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
printWriter.write(JSON.toJSONString(param));
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
OutputStream os = new FileOutputStream(new File("二维码生成地址"));
int len;
byte[] arr = new byte[2048];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return "二维码生成地址";
}