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

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

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

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

请求地址

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();
                }
            }
        }
    }
}

请求参数

属性 类型 默认值 必填 说明
access_token string  

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

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

返回值说明

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

返回值

Buffer

返回的图片 Buffer

异常返回Object

JSON

属性 类型 说明
errcode number 错误码
errmsg string 错误信息

errcode 的合法值

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

注意

  • POST 参数需要转成 JSON 字符串,不支持 form 表单提交。
  • 接口只能生成已发布的小程序的二维码
  • 调用分钟频率受限(5000次/分钟),如需大量小程序码,建议预生成
发布了183 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/SEVENY_/article/details/104600457