创建微信小程序二维码有两个接口需要 一个是获取tocken的接口 一个是生成二维码的接口
获取tocken接口
public static String getAccessToken() throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=*********&secret=*********";
url.trim();
CloseableHttpClient aDefault = HttpClients.createDefault();
HttpGet httpGet=new HttpGet(url);
CloseableHttpResponse execute = aDefault.execute(httpGet);
String toString = EntityUtils.toString(execute.getEntity());
System.out.println(toString);
Map map = JSONObject.parseObject(toString, Map.class);
System.out.println(map);
return map.get("access_token").toString();
}
接下来就是获取二维码接口 微信小程序提供了三种 其余两种是数量或时间有限制我们使用第三种数量和时间没有限制的接口
@GetMapping("/WXEwm")
@ResponseBody
public String WXEwm(HttpServletRequest request , HttpServletResponse response) {
try
{
String accessToken = GetOpenIdUtil.getAccessToken();
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
JSONObject paramJson = new JSONObject();
String decode = URLDecoder.decode("a=132;b=132;", "UTF-8");
paramJson.put("scene", decode);
paramJson.put("page", "packtwo/pages/details/index");
paramJson.put("width", 430);
paramJson.put("auto_color", true);
printWriter.write(paramJson.toString());
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
Date date = new Date();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
long time = timestamp.getTime();
response.addHeader("Content-Disposition", "inline;filename=\"" + new String("aaa".getBytes("gb2312"), "ISO8859-1")+"\"");
BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1)
{
os.write(arr, 0, len);
os.flush();
}
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return "ok";
}
测试postman swagger
|