微信小程序分享二維碼獲取

二維碼獲取開發(fā)者文檔

以下是獲取二維碼代碼:

 public String getWxaCodeUnLimit(String scene, String page) {

        byte[] byteCode = null;
        try {
            byteCode = getByteCode(scene, page);
            String s = new String(byteCode);
            // 報異常信息 重試一次
            if (s.contains("errcode")) {
                LOGGER.error(s);
                byteCode = getByteCode(scene, page);
            }
        }catch (Exception e){
            throw new OpBusinessException(ExceptionCodeEnum.GET_WXACODEUNLIMIT_ERROR.getCode(), ExceptionCodeEnum.GET_WXACODEUNLIMIT_ERROR.getMsg());
        }
        String encode = Base64.getEncoder().encodeToString(byteCode);
        return encode;

    }


    private byte[] getByteCode(String scene, String page) {
        String accessToken = getAccessToken();
        LOGGER.info("accessToken:" + accessToken);
        StringBuilder stringBuilder = new StringBuilder(seed2);
        stringBuilder.append(accessToken);
        LOGGER.info("stringBuilder:" + stringBuilder);
        Map<String, Object> params = new HashMap<>();
        params.put("scene", scene);
        params.put("page", page);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(stringBuilder.toString());
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
        String body = JSON.toJSONString(params);
        StringEntity entity;
        InputStream content = null;
        byte[] bytes = null;
        try {
            entity = new StringEntity(body);
            entity.setContentType("image/png");

            httpPost.setEntity(entity);
            HttpResponse response;

            response = httpClient.execute(httpPost);
            content = response.getEntity().getContent();
            bytes = IOUtils.toByteArray(content);
        } catch (Exception e) {
            LOGGER.error("getWxaCodeUnLimit", e);
        } finally {
            try {
                if (content != null) {
                    content.close();
                }

            } catch (IOException ex) {
                LOGGER.error("getWxaCodeUnLimit IO 流關(guān)閉異常", ex);

            }
            return bytes;
        }

    }

獲取accesstoken 工具,accessToken只要在有效時間內(nèi)不重復獲取都是有效的,所以用了Redis做了緩存
accesstoken 獲取微信開發(fā)者文檔

 public String getAccessToken() {
        String cacheKey = GlobalCacheManageEnum.SMALL_PRODURES_ACCESS_TOKEN_CACHE.getCacheKey();
        String accessToken = redisService.get(cacheKey);
        if (StringUtils.isEmpty(accessToken)) {
            StringBuilder stringBuilder = new StringBuilder(seed);
            stringBuilder.append(appid).append("&secret=").append(appSecret);
            String token = HttpClientUtil.doGet(stringBuilder.toString());
            JSONObject jsonObject = JSONObject.parseObject(token);
            String errcode = jsonObject.getString("errcode");
            if (StringUtils.isNotEmpty(errcode) && !errcode.equals("0")) {
                LOGGER.error(jsonObject.getString("errmsg"));
                throw new OpBusinessException(ExceptionCodeEnum.SMALL_PRODURES_ACCESS_TOKEN_ERROR.getCode(), ExceptionCodeEnum.SMALL_PRODURES_ACCESS_TOKEN_ERROR.getMsg());
            }
            accessToken = jsonObject.getString("access_token");
            Object expiresIn = jsonObject.get("expires_in");
            long expiresTime = Long.valueOf(expiresIn.toString()) ;
            redisService.set(cacheKey, accessToken, expiresTime);
        }
        return accessToken;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容