java 生成支付宝小程序和微信小程序推广码,扫码打开指定页面

java 生成微信小程序和支付宝小程序推广码,扫码打开指定页面
1.生成微信小程序码

需要调用微信官方提供的接口 获取小程序码)

POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

在调用getwxacode接口之前我们需要先获取AccessToken

GET https://api.weixin.qq.com/cgi-bin/token

我这边是先获取到了ACCESS_TOKEN,将它放到了Redis 缓存当中,ACCESS_TOKEN后台接口调用凭据,token有效期为7200s

   private byte[] createMiniQRCode(String pageUrl, String miniType) {
       String   accessToken = RedisUtil.get(RedisKeyConstants.ACCESS_TOKEN);
        log.debug("accessToken  :{}", accessToken);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("path", pageUrl);
        jsonObject.put("width", 1280);
        HttpResponse httpResponse = HttpRequest.post("https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken).body(jsonObject.toJSONString()).execute();
        if (httpResponse.getStatus() != 200) {
            throw new CommonException(WSEnum.ERROR.resultCode, "调用微信创建二维接口异常");
        }
        log.info("创建小程序二维码结果:{}", httpResponse.getStatus());
        return httpResponse.bodyBytes();
    }
2.生成支付宝小程序推广码

alipay.open.app.qrcode.create接口文档需要先导入依赖

   <dependency>
            <groupId>com.alipay</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>alipay-sdk-java-4.33.31.ALL</version>
            <scope>compile</scope>
   </dependency>
具体代码
  public AlipayOpenAppQrcodeCreateResponse createAliQRCode(String url_param, String query_param, String describe) {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(
                    alipayConfig.getUrl(),
                    alipayConfig.getApp_id(),
                    alipayConfig.getPrivate_key(),
                    "json",
                    "utf-8",
                    alipayConfig.getAlipay_public_key(),
                    "RSA2");

            //设置请求参数
            Map<String, String> alipayMap = new HashMap<>();
            alipayMap.put("url_param", url_param);//小程序中能访问到的页面路径
            alipayMap.put("query_param", query_param);//小程序的启动参数,打开小程序的query,在小程序onLaunch的方法中获取
            alipayMap.put("describe", describe);//对应的二维码描述
            alipayMap.put("size", "l");//对应的二维码的大小
            //参数转化位json格式
            String json = new Gson().toJson(alipayMap);
            AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
            request.setBizContent(json);

            AlipayOpenAppQrcodeCreateResponse response = alipayClient.execute(request);
            return response;
        } catch (AlipayApiException alipayApiException) {
            log.info("调用失败");
            return null;
        }

    }

3.将微信小程序与支付宝小程序,生成二维码代码合并都转换为base64数据流

代码:

public static void main(String[] args) {
        String encode = "";//base64字符串
        if () {
           String miniPageUrl = defaultAddres+ URLEncoder.encode(pageUrl, "utf-8");
            //生成支付宝小程序
            AlipayOpenAppQrcodeCreateResponse response = createAliQRCode(miniPageUrl, "sid=" + sid, "支付宝sid=" + sid);
            if (response.isSuccess()) {
                encode = "data:image/jpeg;base64," + Base64Util.imageUrlToBase64(response.getQrCodeUrlCircleBlue());
                log.info("调用成功! 返回值 {}", JSONObject.toJSONString(response));
            } else {
                throw new CommonException(WSEnum.ERROR.resultCode, "生成支付宝小程序推广码失败!");
            }

        } else  {
            //分享地址
            String miniPageUrl = defaultAddresUrl() +sceneId + "&directTo=" + pageUrl;
            //生成微信小程序二维码
            byte[] resultBytes = createMiniQRCode(miniPageUrl);
            encode = getEncode(resultBytes);
        }

    }
    public String getEncode(byte[] resultBytes) {
        if (resultBytes == null) {
            throw new CommonException(WSEnum.ERROR.resultCode, "微信接口访问异常!");
        }
        if (resultBytes.length < 40000) {
            String resultString = new String(resultBytes);
            JSONObject object = JSONObject.parseObject(resultString);
            throw new CommonException(WSEnum.ERROR.resultCode, (String) object.get("errmsg"));
        }
        //获取二维码数据流
        return "data:image/jpeg;base64," + Base64Encoder.encode(resultBytes);
    }

将base64字符串转换为二维图片就欧克了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值