微信生成小程序码getwxacodeunlimit的Buffer怎么处理

微信官方文档(巨坑)返回的说是一个“Buffer”,究竟特么的是个啥?

写过后端的都知道,就是一个 byte[] ,即 字节数组。


以下上我此次需求相关的一些代码,放丢失。
细节也不组织了,亲测成功。

accessToken

@Component
@Slf4j
public class AccessTokenUtil {

    @Value("${terminal.miniPro.attendance.settings.appId}")
    private String attendanceMiniProAppId;

    @Value("${terminal.miniPro.attendance.settings.secretKey}")
    private String attendanceMiniProAppSecretKey;

    @Value("${terminal.miniPro.getAccessToken.request.url}")
    private String attendanceMiniProGetAccessTokenUrl;

    /**
     * 官网提供的获取accessToken的办法
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
     * @return
     */
    public AccessTokenRes getAccessToken(){

        String realUrl = String.format(attendanceMiniProGetAccessTokenUrl,attendanceMiniProAppId,attendanceMiniProAppSecretKey);

        String accessTokenRes = HttpHelper.get(realUrl, "");

        AccessTokenRes accessTokenResDO = JsonUtil.json2Object(accessTokenRes, AccessTokenRes.class);

        return accessTokenResDO;
    }
}

生成小程序码、将byte[]上传到aliyun OSS、将byte[]转为base64之类

@Component
@Slf4j
public class QrCodeUtil {

    /**
     * 小程序码路径根目录
     */
    @Value("${data.service.miniPro.qrcode.path}")
    private String miniProQrCodePath;

    @Value("${terminal.miniPro.qrcode.unlimited.request.url}")
    private String attendanceMiniProGetUnlimitedQrCodeUrl;


    @Autowired
    private XtOSSTool xtOSSTool;

    /**
     * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
     *
     * @param getQrCodeReq
     * @param accessToken
     */
    public QrCodeRes getUnlimitedQrCode(GetQrCodeReq getQrCodeReq, String accessToken) throws Exception {

        //临时文件地址
        File tempFile = null;

        OutputStream output = null;

        QrCodeRes result = new QrCodeRes();

        byte[] byteArr = getQrCodeStream(getQrCodeReq, accessToken);

        //估计byteArr.lenth 大于200的时候才是成功了;失败时候的信息较短
        if (byteArr.length < 200) {
            log.info("获取小程序码失败,getQrCodeReq = {}", getQrCodeReq);
            ExceptionUtil.throwBizException(ErrorCode.PARAM_ERR);
        }

        try {

            String filePath = miniProQrCodePath + "/" + UUIDUtil.getUUID() + ".png";
            tempFile = new File(filePath);

            output = new FileOutputStream(tempFile);
            output.write(byteArr);

            String ossFileName = xtOSSTool.SimpleUpload(tempFile);

            if (null == ossFileName || ossFileName.isEmpty()) {
                log.info("获取小程序码oss上传失败,getQrCodeReq = {}", getQrCodeReq);
                ExceptionUtil.throwBizException(ErrorCode.PARAM_ERR);
            }

            log.info("获取小程序码成功,ossFileName = {}", ossFileName);
            result.setSuccess(true);
            result.setFilePath(ossFileName);
            return result;

        } catch (Exception e) {

            throw e;

        } finally {

            if (null != output) {
                output.close();
            }
            //如果不为null,删除临时文件
            if (null != tempFile) {
                tempFile.delete();
            }
        }
    }

    /**
     * 通用部分
     *
     * @param getQrCodeReq
     * @param accessToken
     * @return
     */
    public byte[] getQrCodeStream(GetQrCodeReq getQrCodeReq, String accessToken) throws Exception {
        String realUrl = String.format(attendanceMiniProGetUnlimitedQrCodeUrl, accessToken);

        Map params = new HashMap<>();

        String encodeScene = getQrCodeReq.getScene();
        params.put("scene", encodeScene);
        params.put("page", getQrCodeReq.getPage());
        params.put("width", "120");

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String paramStr = gson.toJson(params);

        byte[] result = httpPostWithJSON(realUrl, paramStr);

        return result;
    }


    /**
     * 来自地址:http://blog.sina.com.cn/s/blog_56a68d550102ybos.html
     *
     * @param url
     * @param json
     * @return
     * @throws Exception
     */
    public byte[] httpPostWithJSON(String url, String json) throws Exception {
        byte[] result = null;
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");

        StringEntity se = new StringEntity(json);
        se.setContentType("application/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "UTF-8"));
        httpPost.setEntity(se);

        HttpResponse response = httpClient.execute(httpPost);

        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
//              instreams = resEntity.getContent();
                result = EntityUtils.toByteArray(resEntity);
            }
        }
        httpPost.abort();
        return result;
    }
}

你可能感兴趣的:(Java,weixin生态)