微信小程序如何获取小程序码和二维码接口实现长尾关键词优化?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2833个文字,预计阅读时间需要12分钟。
前言:近期小程序推出了自己的识别码,小程序码,这个圆形的码看起来比二维码更美观。本文总结了对小程序识别码和二维码的获取方法,并介绍了生成二维码图片的接口开发。
本文主要内容包括:
1.小程序识别码和二维码的获取方法
2.生成二维码图片的接口开发
一、小程序识别码和二维码的获取方法
1.小程序识别码获取:在小程序后台,选择设置>开发设置,在小程序码栏中,点击生成小程序码即可获取小程序识别码。
2.二维码获取:使用微信扫一扫功能,扫描小程序识别码或二维码图片,即可打开小程序。
二、生成二维码图片的接口开发
1.使用微信小程序提供的API:wx.createQRCode
2.使用第三方库:如uni-app的uQRCode插件
以下是一个使用uni-app的uQRCode插件生成二维码图片的示例代码:
javascript
// 引入uQRCode插件import uQRCode from 'uQRCode/uQRCode.vue';export default { components: { uQRCode }, data() { return { qrCodeSrc: '' }; }, mounted() { this.generateQRCode(); }, methods: { generateQRCode() { const qrCodeData={ text: 'https://www.example.com', // 需要生成二维码的链接 width: 300, // 二维码宽度 height: 300, // 二维码高度 margin: 10, // 二维码边距 background_color: '#FFFFFF', // 背景颜色 foreground_color: '#000000', // 前景颜色 image: '/static/logo.png' // 二维码中间的图片 }; this.qrCodeSrc=uQRCode.qrCode(qrCodeData); } }};
通过以上代码,可以生成一个包含指定链接、尺寸、边距、背景颜色、前景颜色和中间图片的二维码图片。
前言:目前小程序推出了自己的识别码,小程序码,这个圆形的码看起来比二维码好看。本文总结微信小程序的获取小程序码和二维码并生成二维码图片的接口开发。主要内容摘抄自微信小程序的API文档,java接口开发是自己总结开发。
微信小程序API文档:获取二维码
一、简介
通过后台接口可以获取小程序任意页面的二维码,扫描该二维码可以直接进入小程序对应的页面。目前微信支持两种二维码,小程序码(左),小程序二维码(右),如下所示:
二、获取小程序码
目前有两个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口。
1 不带参数有限个数小程序码接口
适用于需要的码数量较少的业务场景
接口地址:api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
注:获取accesstoken的方法跟微信公众获取accesstoken方法一致,不过小程序获取accesstoken需要小程序的appid和appsercet。登录 mp.weixin.qq.com ,就可以在网站的“设置”-“开发者设置”中,查看到微信小程序的 AppID 了,注意不可直接使用服务号或订阅号的 AppID 。
获取微信小程序的 AppID文章地址:小程序简易教程
(1)POST 参数说明
参数
类型
默认值
说明
注意:通过该接口生成的小程序码,永久有效,但数量有效,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。
(2)请求接口测试
使用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; }
说明:accessToken的获取方法就不多说,因为小程序二维码很坑爹的返回文件流,导致我们必须对流进行处理转换成图片保存到本地,这样还有一个严重的后果就是无法将二维码保存到数据库中,每次想获取二维码必须请求接口,此接口最多生成不超过100000个,请大家谨慎使用。
2 带参数无限个数小程序码接口
适用于需要的码数量极多,或仅临时使用的业务场景
接口地址:api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
获取accessToken的方法跟接口1一致。
(1)POST 参数说明
参数
类型
默认值
说明
注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode。同时需要注意,此接口的page参数中不能带任何参数,参数都在scene 参数中处理,切记!!!
// 这是首页的 js Page({ onLoad: function(options) { // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene var scene = decodeURIComponent(options.scene) } })
(2)请求接口测试
(3)java接口开发
public Map getminiqrQr(String sceneStr, String accessToken) { RestTemplate rest = new RestTemplate(); InputStream inputStream = null; OutputStream outputStream = null; try { String url = "api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken; Map<String,Object> param = new HashMap<>(); param.put("scene", sceneStr); 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; }
3 获取小程序二维码
适用于需要的码数量较少的业务场景
接口地址:api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
(1)POST 参数说明
参数
类型
默认值
说明
注意:通过该接口生成的小程序二维码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。
示例:
{"path": "pages/index?query=1", "width": 430}
注:pages/index 需要在 app.json 的 pages 中定义
(2)请求接口测试
(3)java接口开发
public Map getminiqrQr(String accessToken) { RestTemplate rest = new RestTemplate(); InputStream inputStream = null; OutputStream outputStream = null; try { String url = "api.weixin.qq.com/wxaapp/createwxaqrcode?access_token="+accessToken; Map<String,Object> param = new HashMap<>(); param.put("page", "pages/index/index"); param.put("width", 430); 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; }
三、说明
1:通过该接口,仅能生成已发布的小程序的二维码。
2:可以在开发者工具预览时生成开发版的带参二维码。
3:接口1加上接口2,总共生成的码数量限制为100,000,请谨慎调用。
4 : POST 参数需要转成 json 字符串,不支持 form 表单提交。
5 : auto_color line_color 参数仅对小程序码生效。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计2833个文字,预计阅读时间需要12分钟。
前言:近期小程序推出了自己的识别码,小程序码,这个圆形的码看起来比二维码更美观。本文总结了对小程序识别码和二维码的获取方法,并介绍了生成二维码图片的接口开发。
本文主要内容包括:
1.小程序识别码和二维码的获取方法
2.生成二维码图片的接口开发
一、小程序识别码和二维码的获取方法
1.小程序识别码获取:在小程序后台,选择设置>开发设置,在小程序码栏中,点击生成小程序码即可获取小程序识别码。
2.二维码获取:使用微信扫一扫功能,扫描小程序识别码或二维码图片,即可打开小程序。
二、生成二维码图片的接口开发
1.使用微信小程序提供的API:wx.createQRCode
2.使用第三方库:如uni-app的uQRCode插件
以下是一个使用uni-app的uQRCode插件生成二维码图片的示例代码:
javascript
// 引入uQRCode插件import uQRCode from 'uQRCode/uQRCode.vue';export default { components: { uQRCode }, data() { return { qrCodeSrc: '' }; }, mounted() { this.generateQRCode(); }, methods: { generateQRCode() { const qrCodeData={ text: 'https://www.example.com', // 需要生成二维码的链接 width: 300, // 二维码宽度 height: 300, // 二维码高度 margin: 10, // 二维码边距 background_color: '#FFFFFF', // 背景颜色 foreground_color: '#000000', // 前景颜色 image: '/static/logo.png' // 二维码中间的图片 }; this.qrCodeSrc=uQRCode.qrCode(qrCodeData); } }};
通过以上代码,可以生成一个包含指定链接、尺寸、边距、背景颜色、前景颜色和中间图片的二维码图片。
前言:目前小程序推出了自己的识别码,小程序码,这个圆形的码看起来比二维码好看。本文总结微信小程序的获取小程序码和二维码并生成二维码图片的接口开发。主要内容摘抄自微信小程序的API文档,java接口开发是自己总结开发。
微信小程序API文档:获取二维码
一、简介
通过后台接口可以获取小程序任意页面的二维码,扫描该二维码可以直接进入小程序对应的页面。目前微信支持两种二维码,小程序码(左),小程序二维码(右),如下所示:
二、获取小程序码
目前有两个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口。
1 不带参数有限个数小程序码接口
适用于需要的码数量较少的业务场景
接口地址:api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
注:获取accesstoken的方法跟微信公众获取accesstoken方法一致,不过小程序获取accesstoken需要小程序的appid和appsercet。登录 mp.weixin.qq.com ,就可以在网站的“设置”-“开发者设置”中,查看到微信小程序的 AppID 了,注意不可直接使用服务号或订阅号的 AppID 。
获取微信小程序的 AppID文章地址:小程序简易教程
(1)POST 参数说明
参数
类型
默认值
说明
注意:通过该接口生成的小程序码,永久有效,但数量有效,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。
(2)请求接口测试
使用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; }
说明:accessToken的获取方法就不多说,因为小程序二维码很坑爹的返回文件流,导致我们必须对流进行处理转换成图片保存到本地,这样还有一个严重的后果就是无法将二维码保存到数据库中,每次想获取二维码必须请求接口,此接口最多生成不超过100000个,请大家谨慎使用。
2 带参数无限个数小程序码接口
适用于需要的码数量极多,或仅临时使用的业务场景
接口地址:api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
获取accessToken的方法跟接口1一致。
(1)POST 参数说明
参数
类型
默认值
说明
注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode。同时需要注意,此接口的page参数中不能带任何参数,参数都在scene 参数中处理,切记!!!
// 这是首页的 js Page({ onLoad: function(options) { // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene var scene = decodeURIComponent(options.scene) } })
(2)请求接口测试
(3)java接口开发
public Map getminiqrQr(String sceneStr, String accessToken) { RestTemplate rest = new RestTemplate(); InputStream inputStream = null; OutputStream outputStream = null; try { String url = "api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken; Map<String,Object> param = new HashMap<>(); param.put("scene", sceneStr); 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; }
3 获取小程序二维码
适用于需要的码数量较少的业务场景
接口地址:api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
(1)POST 参数说明
参数
类型
默认值
说明
注意:通过该接口生成的小程序二维码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。
示例:
{"path": "pages/index?query=1", "width": 430}
注:pages/index 需要在 app.json 的 pages 中定义
(2)请求接口测试
(3)java接口开发
public Map getminiqrQr(String accessToken) { RestTemplate rest = new RestTemplate(); InputStream inputStream = null; OutputStream outputStream = null; try { String url = "api.weixin.qq.com/wxaapp/createwxaqrcode?access_token="+accessToken; Map<String,Object> param = new HashMap<>(); param.put("page", "pages/index/index"); param.put("width", 430); 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; }
三、说明
1:通过该接口,仅能生成已发布的小程序的二维码。
2:可以在开发者工具预览时生成开发版的带参二维码。
3:接口1加上接口2,总共生成的码数量限制为100,000,请谨慎调用。
4 : POST 参数需要转成 json 字符串,不支持 form 表单提交。
5 : auto_color line_color 参数仅对小程序码生效。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

