博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取带参数的微信小程序二维码
阅读量:4503 次
发布时间:2019-06-08

本文共 5055 字,大约阅读时间需要 16 分钟。

第一步:准备工作:准备小程序的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         //正常返回代码为20013         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                  }else27                  {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     }

 

转载于:https://www.cnblogs.com/yigedingzi/p/9970861.html

你可能感兴趣的文章
Oracle数据库连接时“The Network Adapter could not establish the connection”“网络适配器无法建立连接问题”...
查看>>
awk用法
查看>>
关于近似装箱问题的思考。
查看>>
Java父类对象调用子类实体:方法重写与动态调用
查看>>
Prime 算法的简述
查看>>
开发工具 快捷键
查看>>
开发过程存在的问题
查看>>
如何写错误日志
查看>>
PTA 1067 Sort with Swap(0, i) (25 分)(思维)
查看>>
define和typedef的区别
查看>>
c/c++ 继承与多态 容器与继承3
查看>>
WPF 控件
查看>>
计算机硬件基础以及编程的含义
查看>>
[UE4]Reliable,可靠性
查看>>
django异常:当设置DEBUG = False 时导致500错误的解决方案
查看>>
Git 怎样是自己fork的项目与原作者同步
查看>>
ftp如何使用命令上传文件
查看>>
「C语言」文件的概念与简单数据流的读写函数
查看>>
exp_euler两种形式int,void(扩展欧几里得算法)----可求最大公约数,二元一次方程的解...
查看>>
找到当前视图的父视图控制器
查看>>