IT-study

一、准备工作

  1. 微信公众平台接口调试工具
  2. 小程序的唯一标识(appid)
  3. 小程序的密钥(secret)

二、获取access_token

打开微信公众平台接口调试工具,在参数列表中输入小程序的appid和secret,点击检查问题,如果appid和secret正确,则可以返回正确的access_token结果(图中下方的红框)

获取access_token

 

三、生成微信小程序二维码

生成小程序二维码官方文档

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

一共有三种生成二维码的方式,可以根据使用场景去选择,这里我使用的是第三种生成方式 wxacode.getUnlimited

wxacode.createQRCode
获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。
POST https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN


wxacode.get 
获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。
POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN


wxacode.getUnlimited     
获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 更多用法详见 获取二维码。
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

 

使用wxacode.getUnlimited生成小程序二维码 

获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 更多用法详见 获取二维码。
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

  

说明

通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene字段的值,再做处理逻辑。
使用如下代码可以获取到二维码中的 scene 字段的值。
调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode

 

// 这是首页的 js
Page({
  onLoad: function(options) {
    // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    var scene = decodeURIComponent(options.scene)
  }
})

 

获取接口调用凭证

 1     /**
 2      * 接口调用凭证 access_token
 3      */
 4     public static String postToken(String appId, String appKey) throws Exception {
 5 
 6         String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appKey;
 7         URL url = new URL(requestUrl);
 8         // 打开和URL之间的连接
 9         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
10         connection.setRequestMethod("POST");
11         // 设置通用的请求属性
12         connection.setRequestProperty("Content-Type", "application/json");
13         connection.setRequestProperty("Connection", "Keep-Alive");
14         connection.setUseCaches(false);
15         connection.setDoOutput(true);
16         connection.setDoInput(true);
17 
18         // 得到请求的输出流对象
19         DataOutputStream out = new DataOutputStream(connection.getOutputStream());
20         out.writeBytes("");
21         out.flush();
22         out.close();
23 
24         // 建立实际的连接
25         connection.connect();
26         // 定义 BufferedReader输入流来读取URL的响应
27         BufferedReader in;
28         if (requestUrl.contains("nlp"))
29             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
30         else
31             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
32         StringBuilder result = new StringBuilder();
33         String getLine;
34         while ((getLine = in.readLine()) != null) {
35             result.append(getLine);
36         }
37         in.close();
38         JSONObject jsonObject = JSONObject.parseObject(result.toString());
39         return jsonObject.getString("access_token");
40     }

 

调用微信接口生成微信小程序二维码

 1     /**
 2      * 生成微信小程序二维码
 3      *
 4      * @param filePath
 5      *         本地生成二维码路径
 6      * @param page
 7      *         当前小程序相对页面 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
 8      * @param scene
 9      *         最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&\'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
10      * @param accessToken
11      *         接口调用凭证
12      */
13     public static void generateQrCode(String filePath, String page, String scene, String accessToken) {
14 
15         try {
16 
17             //调用微信接口生成二维码
18             URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
19             HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
20             httpURLConnection.setRequestMethod("POST");// 提交模式
21             // conn.setConnectTimeout(10000);//连接超时 单位毫秒
22             // conn.setReadTimeout(2000);//读取超时 单位毫秒
23             // 发送POST请求必须设置如下两行
24             httpURLConnection.setDoOutput(true);
25             httpURLConnection.setDoInput(true);
26             // 获取URLConnection对象对应的输出流
27             PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
28             // 发送请求参数
29             JSONObject paramJson = new JSONObject();
30             //这就是你二维码里携带的参数 String型  名称不可变
31             paramJson.put("scene", scene);
32             //注意该接口传入的是page而不是path
33             paramJson.put("page", page);
34             //这是设置扫描二维码后跳转的页面
35             paramJson.put("width", 200);
36             paramJson.put("is_hyaline", true);
37             paramJson.put("auto_color", true);
38             printWriter.write(paramJson.toString());
39             // flush输出流的缓冲
40             printWriter.flush();
41 
42             //开始获取数据
43             BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
44             OutputStream os = new FileOutputStream(new File(filePath));
45             int len;
46             byte[] arr = new byte[1024];
47             while ((len = bis.read(arr)) != -1) {
48                 os.write(arr, 0, len);
49                 os.flush();
50             }
51             os.close();
52         } catch (Exception e) {
53             e.printStackTrace();
54         }
55 
56         System.out.println("打开地址查看生成的二维码:" + filePath);
57 
58     }

 

测试类

 1     public static void main(String[] args) throws Exception {
 2 
 3         //获取接口调用凭证access_token
 4         String appId = "小程序id";//小程序id
 5         String appKey = "小程序密钥";//小程序密钥
 6         String token = postToken(appId, appKey);
 7 
 8         //生成二维码
 9         generateQrCode("E:\\tools\\qrCode\\test.png", "pages/index/index", "aa=108&bb=2&cc=3", token);
10 
11     }

 

注意

1      1.获取小程序appId 与appKey
2      2.生成小程序二维码页面参数传入的是page而不是path,其他的接口是path。
3      page后面不允许加参数,参数需要通过scene传入。而小程序也需要通过scene获取参数。
4      3.生成小程序二维码可将二维码写入本地,也可上传至服务器。自行选择
5      

 


参考地址:
https://www.cnblogs.com/daipianpian/p/9239452.html
http://www.what21.com/u/10004/6756200547748968305.htm

生成小程序二维码官方文档
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html

 

分类:

技术点:

相关文章: