微信小程序——服务端获取小程序二维码 永久有效 数量无限制

因为现在做的小程序,想要分享小程序中的页面给微信好友,那就可以使用二维码,很方便。

而且通过后台接口可以获取小程序任意页面的小程序码

扫描该小程序码可以直接进入小程序对应的页面,所有生成的小程序码永久有效

请求地址

POST方法
https://api.weixin.qq.com/wxa/getwxacodeunlimit?
access_token=ACCESS_TOKEN

后台代码

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class QRCodeHelp {

    //生成活动二维码
    public  void  createActQRCode()
    {
        String access_token = "你获取到的access_token" ;
        String path = "pages/index/huodong/huodong";//你小程序中页面的路径
        RestTemplate rest = new RestTemplate();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?" +
                    "access_token="+access_token;
            Map<String,Object> param = new HashMap<>();
            param.put("scene", 参数值);
            param.put("page", path);
            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);
            param.put("is_hyaline",false);
            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]); 
            byte[] result = entity.getBody();
            inputStream = new ByteArrayInputStream(result);
            //我将图片存储到了服务器中的ActQRImage文件夹下
            File file = new File("/root/application/ActQRImage/1.png");
            if (!file.exists()){
                file.createNewFile();
            }
            outputStream = new FileOutputStream(file);
            outputStream.write(result); 
            outputStream.flush();
        } catch (Exception e) {
            System.out.println("调用小程序生成微信永久小程序码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();
                }
            }
        }
    }
}

scene参数存放与获取: 

后台生成了二维码之后,前端小程序也要进行操作来获取到scene中存放的参数。

1.如果只需要传一个参数 可以直接scene=参数值  例如:scene=123

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

2.如果两个及两个以上的参数  可以是scene = "id/"+id+"*userId/"+userId;  例如:scene=id/1*userId/2

    这里自定义了scene的参数格式,以 / 代替 = ,以 * 代替 & 。

    因为如果用正常的格式(例如:id=1&userId=2)会导致参数识别一部分,因为小程序在识别二维码页面参数时,拿到的参数列表是这样的scene=id=1&userId=2,这样就会导致只能识别id,id后的等号后面的参数全都无效了。所以这里用特      殊字符代替常规字符,然后获取之后再用js去解析,从而拿到参数列表。 

onLoad:function getOptions(options){
    // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    var scene = decodeURIComponent(options.scene); 
    var obj = {};
    for (var i = 0; i < scene.split('*').length;i++){
      var arr = scene.split('*')[i].split('/');
      obj[arr[0]] = arr[1];
    }
    return obj
} 

 

请求参数

属性类型默认值必填说明
access_tokenstring 

接口调用凭证(获取方式见另一博客获取access_token

scenestring 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
pagestring主页必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
widthnumber430二维码的宽度,单位 px,最小 280px,最大 1280px
auto_colorbooleanfalse自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
line_colorObject{"r":0,"g":0,"b":0}auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
is_hyalinebooleanfalse是否需要透明底色,为 true 时,生成透明底色的小程序

返回值说明

如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。

返回值

Buffer

返回的图片 Buffer

异常返回Object

JSON

属性类型说明
errcodenumber错误码
errmsgstring错误信息

errcode 的合法值

说明 
45009调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。 
41030所传page页面不存在,或者小程序没有发布 

注意

  • POST 参数需要转成 JSON 字符串,不支持 form 表单提交。
  • 接口只能生成已发布的小程序的二维码
  • 调用分钟频率受限(5000次/分钟),如需大量小程序码,建议预生成
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值