欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 微信小程序--java接口开发获取小程序码和二维码

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:505

HTML5影视传媒文化公司类网

2020-05-12   浏览:502

微信小程序--java接口开发获取小程序码和二维码

发布时间:2020-12-25  

微信小程序API文档:获取二维码

一、简介

通过后台接口可以获取小程序任意页面的二维码,扫描该二维码可以直接进入小程序对应的页面。目前微信支持两种二维码,小程序码(左),小程序二维码(右),如下所示: 

这里写图片描述

二、获取小程序码

目前有两个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口。

1 不带参数有限个数小程序码接口

适用于需要的码数量较少的业务场景

接口地址:

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

注:获取accesstoken的方法跟微信公众获取accesstoken方法一致,不过小程序获取accesstoken需要小程序的appid和appsercet。登录 https://mp.weixin.qq.com ,就可以在网站的“设置”-“开发者设置”中,查看到微信小程序的 AppID 了,注意不可直接使用服务号或订阅号的 AppID 。 

这里写图片描述

  获取微信小程序的 AppID文章地址:小程序简易教程

(1)POST 参数说明 参数 类型 默认值 说明
path   String       不能为空,最大长度 128 字节  
width   Int   430   二维码的宽度  
auto_color   Bool   false   自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调  
line_color   Object   {“r”:”0”,”g”:”0”,”b”:”0”}   auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {“r”:”xxx”,”g”:”xxx”,”b”:”xxx”}  

注意:通过该接口生成的小程序码,永久有效,但数量有效,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。

(2)请求接口测试

使用http请求插件postman或者RESTClient请求测试。 

这里写图片描述

  请求测试结果返回一个小程序码图片,与微信公众平台生成二维码不同,小程序码直接返回文件流,不是微信公众平台的url和ticket。

(3)java接口开发

注:此接口是基于Spring RestTemplate进行http请求,进行http请求有很多方法和工具类,可自行百度或参考下面的参考文章。接口只是提供一个解决方法的思路。

public Map getminiqrQr(String accessToken) { RestTemplate rest = new RestTemplate(); InputStream inputStream = null; OutputStream outputStream = null; try { String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken; Map<String,Object> param = new HashMap<>(); param.put("page", "pages/index/index"); param.put("width", 430); param.put("auto_color", false); Map<String,Object> line_color = new HashMap<>(); line_color.put("r", 0); line_color.put("g", 0); line_color.put("b", 0); param.put("line_color", line_color); LOG.info("调用生成微信URL接口传参:" + param); MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); HttpEntity requestEntity = new HttpEntity(param, headers); ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); LOG.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody()); byte[] result = entity.getBody(); LOG.info(Base64.encodeBase64String(result)); inputStream = new ByteArrayInputStream(result); File file = new File("C:/Users/wangqiulin/Desktop/1.png"); if (!file.exists()){ file.createNewFile(); } outputStream = new FileOutputStream(file); int len = 0; byte[] buf = new byte[1024]; while ((len = inputStream.read(buf, 0, 1024)) != -1) { outputStream.write(buf, 0, len); } outputStream.flush(); } catch (Exception e) { LOG.error("调用小程序生成微信永久小程序码URL接口异常",e); } finally { if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }

注:pages/index 需要在 app.json 的 pages 中定义

(2)请求接口测试