为了账号安全,请及时绑定邮箱和手机立即绑定

获取微信小程序二维码

标签:
Java 小程序

1,首先获取access_token

    @Override
    public String getAudioMinAppToken() {
        String accessToken;
        try {
            String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxx&secret=xxxxxxxx";
            String data = HttpClientUtil.doGet(requestUrl);
            JSONObject json = JSONObject.parseObject(data);
            accessToken = String.valueOf(json.get("access_token"));
        } catch (Exception e) {
            logger.error("生成access_token异常", e);
            throw new CommonException(CommonErrorCode.UNKNOWN_ERROR, "获取access_token失败");
        }
        return accessToken;
    }

2,获取小程序二维码

    @Override
    public Map<String, String> createWxQRCode(String scene, String page, Integer width, Boolean autoColor, Object lineColor, Boolean isHyaline) {
        Map<String, String> map = new HashMap<>(16);
        String token;
        try {
            String tokenKey = RedisKeyCenter.getAudioMinAppToken();
            if (redisService.get(tokenKey) != null && redisService.isKeyExist(tokenKey)) {
                token = redisService.get(tokenKey);
            } else {
                token = getAudioMinAppToken();
                redisService.set(tokenKey, token, 60 * 60 * 2, TimeUnit.SECONDS);
            }
            if (scene == null) {
                scene = "n=1234";
            }
            if (page == null) {
                page = "pages/index";
            }
            if (isHyaline == null) {
                isHyaline = false;
            }
            Map<String, Object> params = new HashMap<>(16);
            params.put("scene", scene);
            params.put("page", page);
            params.put("width", width);
            params.put("auto_color", autoColor);
            params.put("line_color", lineColor);
            params.put("is_hyaline", isHyaline);

            String url = ApplicationConstant.MINAPP_GET_QR_CODE_URL.replace("ACCESS_TOKEN",token);
            logger.info("request rul is :" ,url);
            InputStream inputStream = HttpClientUtil.jsonPostToStream(url,params);
            String result = StreamUtils.getBase64FromInputStream(inputStream);
            String pic = "data:image/jpeg;base64," + result;
            map.put("pic", pic);
        } catch (Exception e) {
            logger.error("生成小程序二维码出错", e);
            throw new CommonException(CommonErrorCode.UNKNOWN_ERROR, "生成小程序二维码出错");
        }
        return map;
    }
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消