600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Java 生成微信小程序二维码(可以指定小程序页面 与 动态参数)

Java 生成微信小程序二维码(可以指定小程序页面 与 动态参数)

时间:2019-09-23 08:11:00

相关推荐

Java 生成微信小程序二维码(可以指定小程序页面 与 动态参数)

一、准备工作

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

二、获取access_token

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

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

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

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

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

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

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

获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 更多用法详见 获取二维码。

POST https://api./wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

说明

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

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

获取接口调用凭证

1/**2* 接口调用凭证 access_token3*/4public static String postToken(String appId, String appKey) throws Exception {5 6 String requestUrl = "https://api./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 else31 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 filePath5* 本地生成二维码路径6* @param page7* 当前小程序相对页面 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面8* @param scene9* 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)10* @param accessToken11* 接口调用凭证12*/13public static void generateQrCode(String filePath, String page, String scene, String accessToken) {14 15 try {16 17 //调用微信接口生成二维码18 URL url = new URL("https://api./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而不是path33 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}

测试类

1public static void main(String[] args) throws Exception {2 3 //获取接口调用凭证access_token4 String appId = "小程序id";//小程序id5 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}

注意

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

参考地址:

/daipianpian/p/9239452.html

/u/10004/675647748968305.htm

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。