微信小程序生成二维码,并且根据自定义场景码切换数据

 public void createQRCode(String id, HttpServletResponse response) throws Exception {
        // 获取accessToken
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId
                + "&secret=" + appSecret;
        String responseString = restTemplate.getForObject(url, String.class);
        String accessToken = (responseString != null ? responseString.split(":") : new String[0])[1].split("\"")[1];
        // 获取路径
        String jumpPath = "pages/index/index";
        // 生成小程序二维码
        String qrcodeUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
        Map<String, Object> params = new HashMap<>();
        params.put("page", jumpPath);
        params.put("scene", id);
        params.put("width", 430);
        params.put("is_hyaline", true);// 是否去掉背景
        params.put("auto_color", false);
        params.put("line_color", new HashMap<String, Object>(){{put("r",0);put("g",0);put("b",0);}});
        restTemplate.postForEntity(qrcodeUrl, params, String.class);
        byte[] bytes = restTemplate.postForObject(qrcodeUrl, params, byte[].class);
        // 将二维码输出到输出流中
        response.setContentType("image/png");
        OutputStream outputStream = response.getOutputStream();
        assert bytes != null;
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }
参数名参数值解释
pagepages/index/index小程序的首页跳转地址,注意:前面不能加"/"
sceneid自定义场景码(就是和前端约定好的自定义参数,在小程序中不做任何处理,扫码以后会传给前端。比如我的是传的某商品的主键,扫码以后小程序端获取到id,会根据主键去查询商品展示)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值