Java⽣成⼩程序⼆维码(可以指定⼩程序页⾯与动态参数)⼀、准备⼯作
1.
2. ⼩程序的唯⼀标识(appid)
3. ⼩程序的密钥(secret)
⼆、获取access_token
打开,在参数列表中输⼊⼩程序的appid和secret,点击检查问题,如果appid和secret正确,则可以返回正确的access_token结果(图中下⽅的红框)
三、⽣成⼩程序⼆维码
⽣成⼩程序⼆维码官⽅⽂档
⼀共有三种⽣成⼆维码的⽅式,可以根据使⽤场景去选择,这⾥我使⽤的是第三种⽣成⽅式 Unlimited
获取⼩程序⼆维码,适⽤于需要的码数量较少的业务场景。通过该接⼝⽣成的⼩程序码,永久有效,有数量限制,详见获取⼆维码。
POST api.weixin.qq/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
<
获取⼩程序码,适⽤于需要的码数量较少的业务场景。通过该接⼝⽣成的⼩程序码,永久有效,有数量限制,详见获取⼆维码。
POST api.weixin.qq/wxa/getwxacode?access_token=ACCESS_TOKEN
获取⼩程序码,适⽤于需要的码数量极多的业务场景。通过该接⼝⽣成的⼩程序码,永久有效,数量暂⽆限制。更多⽤法详见获取⼆维码。
POST api.weixin.qq/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
使⽤Unlimited⽣成⼩程序⼆维码
获取⼩程序码,适⽤于需要的码数量极多的业务场景。通过该接⼝⽣成的⼩程序码,永久有效,数量暂⽆限制。更多⽤法详见获取⼆维码。
POST api.weixin.qq/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*/入门的java游戏小程序
4public static String postToken(String appId, String appKey) throws Exception {
5
6        String requestUrl = "api.weixin.qq/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 OutputStream());
20        out.writeBytes("");
21        out.flush();
22        out.close();
23
24// 建⽴实际的连接
25        t();
26// 定义 BufferedReader输⼊流来读取URL的响应
27        BufferedReader in;
28if (ains("nlp"))
29            in = new BufferedReader(new InputStream(), "GBK"));
30else
31            in = new BufferedReader(new InputStream(), "UTF-8"));
32        StringBuilder result = new StringBuilder();
33        String getLine;
34while ((getLine = in.readLine()) != null) {
35            result.append(getLine);
36        }
37        in.close();
38        JSONObject jsonObject = JSONObject.String());
String("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*/
13public static void generateQrCode(String filePath, String page, String scene, String accessToken) {
14
15try {
16
17//调⽤接⼝⽣成⼆维码
18            URL url = new URL("api.weixin.qq/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 OutputStream());
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.String());
39// flush输出流的缓冲
40            printWriter.flush();
41
42//开始获取数据
43            BufferedInputStream bis = new InputStream());
44            OutputStream os = new FileOutputStream(new File(filePath));
45int len;
46byte[] arr = new byte[1024];
47while ((len = ad(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    }
测试类
1public 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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。