获取带参数的微信小程序二维码

第一步:准备工作:准备小程序的APPID和SECRET

  可登陆微信公众平台查看APPID和secret

第二部:获取token

  微信提供了获取token的接口,访问此接口同时传入APPID和secret作为参数,就能获取token值。

  接口地址为:tokenURL = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET

  Java代码:

 1 public static String getAccessToken() throws UnsupportedEncodingException, IOException{
 2         String tokenURL = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET
 3         JSONObject jsonObject = null;
 4         URL conn_url =  new URL(tokenURL );
 5         HttpURLConnection conn = (HttpsURLConnection)conn_url.openConnection();
 6         conn.setRequestMethod("GET");
 7         conn.setReadTimeout(5000);
 8         conn.setConnectTimeout(5000);
 9         conn.connect();
10         //output获取access_token是不会用到
11        
12         //正常返回代码为200
13         if(conn.getResponseCode()==200){
14             InputStream stream = conn.getInputStream();
15             InputStreamReader inputStreamReader = new InputStreamReader(stream, "utf-8");
16             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
17             String str = null;
18             StringBuffer buffer = new StringBuffer();
19             while ((str = bufferedReader.readLine()) != null) {
20                 buffer.append(str);
21             }
22             bufferedReader.close();
23             inputStreamReader.close();
24           stream.close();
25           conn.disconnect();
26             jsonObject = JSONObject.fromObject(buffer.toString());
27         }
28         
29         String access_token = jsonObject.getString("access_token");
30         return access_token;
31     }

第三部:获取参数图片

  微信官方提供了获取微信小程序二维码的接口,访问此接口,同时把第二步获取的token值作为参数传入,同时还需传入二维码内需要带的参数,和图片的一些设置参数,便可获取二维码图片的输入流。

  接口地址:

  https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token

  基本的设置参数:

        "scene", param     --二维码内需要带的参数param
        "path", "pages/ShouYe/ShouYe/ShouYe"   --设置扫面二维码进入的页面
        "width", width          --设置二维码图片的大小   如果不设置,默认大小是430
        "is_hyaline", true
        "auto_color", true

  生成二维码代码:

  

 1 public static String getQRImg(String param,String size, HttpServletRequest request) {
 2         
 3         
 4         String p=request.getSession().getServletContext().getRealPath("/");
 5         String codeUrl = p+"image/codeImg/"+System.currentTimeMillis()+param + "Code.png";
 6         String twoCodeUrl="/image/codeImg/"+System.currentTimeMillis()+param + "Code.png";
 7          try
 8          {
 9              String access_token = token;
10              URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
11              HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
12              httpURLConnection.setRequestMethod("POST");// 提交模式
13              // conn.setConnectTimeout(10000);//连接超时 单位毫秒
14              // conn.setReadTimeout(2000);//读取超时 单位毫秒
15              // 发送POST请求必须设置如下两行
16              httpURLConnection.setDoOutput(true);
17              httpURLConnection.setDoInput(true);
18              // 获取URLConnection对象对应的输出流
19              PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
20              // 发送请求参数
21              JSONObject paramJson = new JSONObject();
22              String width;
23              if(size !=null && !size.trim().equals("")){
24                  if(size.trim().matches("^[1-9][0-9]*$")){
25                      width = size.trim();
26                  }else
27                  {
28                      width = "430";
29                  }
30              }else{
31                  width = "430";
32              }
33              paramJson.put("scene", param);
34              paramJson.put("path", "pages/ShouYe/ShouYe/ShouYe");
35              paramJson.put("width", width);
36              paramJson.put("is_hyaline", true);
37              paramJson.put("auto_color", true);
38              /**
39               * line_color生效
40               * paramJson.put("auto_color", false);
41               * JSONObject lineColor = new JSONObject();
42               * lineColor.put("r", 0);
43               * lineColor.put("g", 0);
44               * lineColor.put("b", 0);
45               * paramJson.put("line_color", lineColor);
46               * */
47  
48              printWriter.write(paramJson.toString());
49              // flush输出流的缓冲
50              printWriter.flush();
51              //开始获取数据
52              BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
53              OutputStream os = new FileOutputStream(new File(codeUrl));
54              int len;
55              byte[] arr = new byte[1024];
56              while ((len = bis.read(arr)) != -1)
57              {
58                  os.write(arr, 0, len);
59                  os.flush();
60              }
61              os.close();
62          }
63          catch (Exception e)
64          {
65              e.printStackTrace();
66          }
67         return twoCodeUrl;
68     }

猜你喜欢

转载自www.cnblogs.com/yigedingzi/p/9970861.html