微信小程序创建二维码

1.获取小程序的accessToken 信息

2.调用接口获取小程序码或小程序二维码

接口A: 适用于需要的码数量较少的业务场景接口地址:

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

获取 access_token 详见官方文档

POST 参数说明

参数类型默认值说明
pathString 不能为空,最大长度 128 字节
widthInt430二维码的宽度
auto_colorBoolfalse自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
line_colorObject{"r":"0","g":"0","b":"0"}auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"},十进制表示
is_hyalineBoolfalse是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码

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

public void getwxacode(String authorizer_appid,String access_token) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    File file = null;
    try {
        
        String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+access_token;
        JSONObject param = new JSONObject();
        param.put("path","pages/index/index");
        param.put("width",430); //二维码的宽度
        param.put("auto_color",false);  //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
        param.put("is_hyaline",false);  //是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码
        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);
        logger.info(">>>>>> 调用生成微信小程序码URL接口入参:"+param.toString()+"  <<<<<< ");
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        HttpEntity requestEntity = new HttpEntity(param, headers);
        ResponseEntity<byte[]> entity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,byte[].class);
        logger.info(">>>>>> 调用小程序生成微信永久小程序码URL接口回参: " + entity);
        byte[] result = entity.getBody();
        inputStream = new ByteArrayInputStream(result);
        File filePath = new File("/soft/TEMP");
        if (!filePath.mkdir())
            filePath.mkdirs();

        file = new File(filePath+File.separator+authorizer_appid+".jpeg");
        if (!file.exists()) {
            file.createNewFile();
        }
        outputStream = new FileOutputStream(file);
        inputStream = new ByteArrayInputStream(result);
        int content = 0;
        byte[] buffer = new byte[1024 * 8];
        while  ((content = inputStream.read(buffer,0,1024)) != -1) {
            outputStream.write(buffer, 0, content);
        }
        outputStream.flush();
    }catch (Exception e){
        logger.error("调用小程序生成微信永久小程序码URL接口异常: "+e.getMessage());
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

接口B:适用于需要的码数量极多的业务场景

接口地址:

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

获取 access_token 详见官方文档

POST 参数说明

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

注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode

public void getwxacodeunlimit(String authorizer_appid,String scene,String access_token) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    File file = null;
    try {
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token;
        JSONObject param = new JSONObject();
        param.put("scene",scene);   //页面可接受参数
        param.put("page","pages/index/index");  //必须是已经发布的小程序存在的页面(否则报错),如果不填写这个字段,默认跳主页面
        param.put("width",430); //二维码的宽度
        param.put("auto_color",false);  //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
        param.put("is_hyaline",false);  //是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码
        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);
        logger.info(">>>>>> 调用生成微信小程序码URL接口入参:"+param.toString()+"  <<<<<< ");
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        HttpEntity requestEntity = new HttpEntity(param, headers);
        JSONObject return_json = restTemplate.postForEntity(url,requestEntity,JSONObject.class).getBody();
        logger.info(">>>>>> 返回JSON数据: " + return_json);
        ResponseEntity<byte[]> entity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,byte[].class);
        logger.info(">>>>>> 调用小程序生成微信永久小程序码URL接口回参: " + entity.getBody());
        byte[] result = entity.getBody();
        inputStream = new ByteArrayInputStream(result);
        File filePath = new File("/soft/TEMP");
        if (!filePath.mkdir())
            filePath.mkdirs();

        file = new File(filePath+File.separator+authorizer_appid+".jpeg");
        if (!file.exists()) {
            file.createNewFile();
        }
        outputStream = new FileOutputStream(file);
        inputStream = new ByteArrayInputStream(result);
        int content = 0;
        byte[] buffer = new byte[1024 * 8];
        while ((content = inputStream.read(buffer,0,1024)) != -1) {
            outputStream.write(buffer, 0, content);
        }
        outputStream.flush();
    }catch (Exception e){
        logger.error("调用小程序生成微信永久小程序码URL接口异常: "+e.getMessage());
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

获取小程序二维码

接口C:适用于需要的码数量较少的业务场景

接口地址:

https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

获取 access_token 详见官方文档

POST 参数说明

参数类型默认值说明
pathString 不能为空,最大长度 128 字节
widthInt430二维码的宽度

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

public void createwxaqrcode(String authorizer_appid,Sring access_token) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    File file = null;
    try {
       
        String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+authorizer_access_token;
        JSONObject param = new JSONObject();
        param.put("path","pages/index/index");  //必须是已经发布的小程序存在的页面(否则报错),如果不填写这个字段,默认跳主页面
        logger.info(">>>>>> 调用生成微信小程序码URL接口入参:"+param.toString()+"  <<<<<< ");
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        HttpEntity requestEntity = new HttpEntity(param, headers);
        ResponseEntity<byte[]> entity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,byte[].class);
        logger.info(">>>>>> 调用小程序生成微信永久小程序码URL接口回参: " + entity);
        byte[] result = entity.getBody();
        inputStream = new ByteArrayInputStream(result);
        File filePath = new File("/soft/TEMP");
        if (!filePath.mkdir())
            filePath.mkdirs();

        file = new File(filePath+File.separator+authorizer_appid+".jpeg");
        if (!file.exists()) {
            file.createNewFile();
        }
        outputStream = new FileOutputStream(file);
        inputStream = new ByteArrayInputStream(result);
        int content = 0;
        byte[] buffer = new byte[1024 * 8];
        while ((content = inputStream.read(buffer,0,1024)) != -1) {
            outputStream.write(buffer, 0, content);
        }
        outputStream.flush();
    }catch (Exception e){
        logger.error("调用小程序生成微信永久小程序码URL接口异常: "+e.getMessage());
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Bug & Tip

  1. tip:通过该接口,仅能生成已发布的小程序的二维码。
  2. tip:可以在开发者工具预览时生成开发版的带参二维码。
  3. tip:接口A加上接口C,总共生成的码数量限制为100,000,请谨慎调用。
  4. tip: POST 参数需要转成 json 字符串,不支持 form 表单提交。
  5. tip: auto_color line_color 参数仅对小程序码生效。

错误码

45009:B接口调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。45029:A接口和C接口生成码个数总和到达最大个数限制。41030:B接口所传page页面不存在,或者小程序没有发布,请注意B接口没有path参数,传path参数虽然可以生成小程序码,但是只能跳主页面。




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值