【小程序】生成小程序码

2020-09-30  本文已影响0人  7ColorLotus

小程序码通过后台接口获取,扫描小程序码可以跳转到小程序对应的页面所有生成的小程序码永久有效,可放心使用。

public class MiniAppUtil {
    
    /**
     * 获取小程序码地址,适用于需要的码数量极多的业务场景 通过该接口生成的小程序码,永久有效,数量暂无限制。
     */
    public static final String GET_WXACODE_UNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";

    /**
     * 获取小程序码,适用于需要的码数量较少的业务场景。 通过该接口生成的小程序码,永久有效,有数量限制
     */
    public static final String GET_WXACODE_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s";
    

    /**
     * 获取无限制小程序码
     * @param sceneStr
     * @param page
     * @param filePath
     * @param uploadDomainUrl
     * @return 生成成功的小程序码http访问路径
     */
    public static String getWxacodeUnlimit(String sceneStr, String page, String filePath, String uploadDomainUrl, String accessToken)throws Exception{
        return getWxacode(sceneStr, page,filePath, uploadDomainUrl, true, accessToken);
    }

    /**
     * 获取有限制小程序码
     * @param page
     * @param filePath
     * @param uploadDomainUrl
     * @return 生成成功的小程序码http访问路径
     */
    public static String getWxacodeLimit(String page, String filePath, String uploadDomainUrl, String accessToken)throws Exception{
        return getWxacode(null,page, filePath, uploadDomainUrl, false, accessToken);
    }

    /**
     * 从微信上获取小程序码
     * @param sceneStr 场景值
     * @param page 扫码后跳转到的小程序页面, 可选 不填默认小程序首页
     * @param filePath 小程序码图片存放路径
     * @param uploadDomainUrl http访问目录
     * @param isUnlimit 是否有数量限制的小程序码,如果是没有限制的,需要传场景值 sceneStr
     * @return String
     * @throws Exception
     */
    private static String getWxacode(String sceneStr, String page, String filePath, String uploadDomainUrl, boolean isUnlimit, String accessToken) throws Exception{
        try {
            String fileName = System.currentTimeMillis() + ".jpg";
            Map<String, Object> params = new HashMap<>();
            params.put("width", 430);//二维码宽度
            String url = String.format(GET_WXACODE_URL, accessToken);
            if(isUnlimit){
                if(StringUtil.isNotEmpty(page)) {
                    params.put("page", page);//要跳转的页面,正式发布后才能传值
                }
                params.put("scene", sceneStr);//sceneStr值长度有限制,最大32个可见字符
                url = String.format(GET_WXACODE_UNLIMIT_URL, accessToken);
            }else {
                params.put("path", page);
            }

            CloseableHttpClient httpClient = HttpClientBuilder.create().build();

            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            String body = JSON.toJSONString(params);
            StringEntity entity;

            entity = new StringEntity(body);
            entity.setContentType("image/png");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            if(!response.getEntity().isStreaming()){ //如果小程序码一直异常时,可以放开if判断,输出异常错误信息
                log.error("getWxacode exception:{}", EntityUtils.toString(response.getEntity(), "UTF-8"));
                return null;
            }

            File targetFile = new File(filePath);
            if(!targetFile.exists()){
                targetFile.mkdirs();
            }
            try(InputStream inputStream = response.getEntity().getContent();
                FileOutputStream out = new FileOutputStream(filePath + fileName)) {
                byte[] buffer = new byte[8192];
                int bytesRead = 0;
                while((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
                out.flush();
            }

            return uploadDomainUrl + fileName;
        } catch (Exception e) {
            log.error("getWxacode exception:", e);
            return null;
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读