前言:目前小程序推出了自己的识别码,小程序码,这个圆形的码看起来比二维码好看。本文总结微信小程序的获取小程序码和二维码并生成二维码图片的接口开发。主要内容摘抄自微信小程序的API文档,java接口开发是自己总结开发。

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

一、简介

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

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

二、获取小程序码

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

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

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

接口地址:https://api.weixin.qq.com/wxa/getwxacode"external nofollow" target="_blank" href="https://mp.weixin.qq.com">https://mp.weixin.qq.com ,就可以在网站的“设置”-“开发者设置”中,查看到微信小程序的 AppID 了,注意不可直接使用服务号或订阅号的 AppID 。

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

获取微信小程序的 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请求测试。

微信小程序实现获取小程序码和二维码java接口开发
请求测试结果返回一个小程序码图片,与微信公众平台生成二维码不同,小程序码直接返回文件流,不是微信公众平台的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"+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 带参数无限个数小程序码接口

适用于需要的码数量极多,或仅临时使用的业务场景

接口地址:https://api.weixin.qq.com/wxa/getwxacodeunlimit"height: 217px; width: 664px"> 参数 类型 默认值 说明 scene String 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;="htmlcode">

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

(2)请求接口测试

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

(3)java接口开发

 public Map getminiqrQr(String sceneStr, String accessToken) {
  RestTemplate rest = new RestTemplate();
  InputStream inputStream = null;
  OutputStream outputStream = null;
  try {
   String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit"+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 获取小程序二维码

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

接口地址:https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode"htmlcode">

{"path": "pages/index", "width": 430}

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

(2)请求接口测试

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

(3)java接口开发

 public Map getminiqrQr(String accessToken) {
  RestTemplate rest = new RestTemplate();
  InputStream inputStream = null;
  OutputStream outputStream = null;
  try {
   String url = "https://api.weixin.qq.com/wxaapp/createwxaqrcode"+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 参数仅对小程序码生效。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。