获取小程序二维码

 配置

Hutool参考文档

        <!--Hutool工具-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.9</version>
        </dependency>

5c13ba2082914541aeec20872a0f9cfb.png

1.获取微信accessToken

@GetMapping("/accessToken")
    @ApiOperation("获取微信accessToken")
    public R<Object> accessToken() {
        RestTemplate restTemplate = new RestTemplate();
        String api = url + "/cgi-bin/token?appid={appid}&secret={appSecret}&grant_type=client_credential";
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("appSecret", appSecret);
        Object obj = restTemplate.getForObject(api, Object.class, map);//此处三个参数分别是请求地址、请求体以及返回参数类型
        System.out.println(map);
        return R.success(obj);
    }

2.获取二维码

 @PostMapping("/qrCode")
    @ApiOperation("获取微信二维码")
    public R<String> cenerateTheQrCode(@RequestBody Wxma wxma){
        System.out.println(wxma.getScene());
        System.out.println(wxma.getPage());
        System.out.println(wxma.getAccessToken());
       Map<String,Object> param = new HashMap<String,Object>();
        param.put("page",wxma.getPage());
        //deviceId和I都只是参数,可以自己按需要定义,用&分隔
        param.put("scene",wxma.getScene());
        System.out.println(param);
        System.out.println(wxma.getAccessToken());
        //参数必须是Json格式
        String json = JSON.toJSONString( param );
        byte[] bytes= HttpRequest.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+wxma.getAccessToken())
                .header(HTTP.CONTENT_TYPE, "application/json").body(json).execute().bodyBytes();
        String encoded = "data:image/jpg;base64," + Base64.encodeBase64String(bytes).replaceAll("[\\s*\t\n\r]", "");
        return R.success(encoded);
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值