小程序码与小程序链接 /小程序码 /获取不限制的小程序码

获取不限制的小程序码


前言

调用微信相关接口生成二维码,获取不限制的小程序码,然后上传到阿里云oss服务器;


一、业务场景?

适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制

二、使用步骤

1.获取访问微信接口的token

代码如下(示例):


  //获取微信token
    public String getAppletAccessToken() {

        LoginUserVO loginUserVO = UserSession.getUser();
        String wxAppId = loginUserVO.getWxAppId();
        String wxAppSecret = wxConfig.getWxAppSecret();
        if(StringUtils.isEmpty(wxAppId)){
            wxAppId = wxConfig.getWxAppId();
            wxAppSecret = wxConfig.getWxAppSecret();
        }else{
            WechatAppInfo wechatAppInfo = wechatAppInfoService.getWechatAppInfoByWxAppId(wxAppId);
            if(wechatAppInfo != null ){
                wxAppSecret = wechatAppInfo.getWxAppSecret();
            }
        }

        String accessTokenKey = String.format(WechatRedisKey.WECHAT_APPLET_ACCESS_TOEKN, wxAppId);
        String accessToken = redisUtil.get(accessTokenKey);

        //判断如果为空
        if (StringUtils.isEmpty(accessToken)) {
            String lockKey = String.format(WechatRedisKey.LOCK_WECHAT_ACCESS_TOKEN);
            //加锁
            boolean isLock = redissonLocker.tryLock(lockKey, TimeUnit.SECONDS, 10, 10);
            if (isLock) {

                try {
                    accessToken = redisUtil.get(accessTokenKey);

                    if (StringUtils.isEmpty(accessToken)) {
                    
                        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
                        url = String.format(url, wxAppId, wxAppSecret);
                        RestTemplate restTemplate = new RestTemplate();
                        String response = restTemplate.getForObject(url, String.class);
                        log.info("获取微信accessToken:{}; {}", url, response);

                        accessToken = JSONObject.parseObject(response).getString("access_token");

                        if (StringUtils.isNotEmpty(accessToken)) {
                            Integer expiresIn = JSONObject.parseObject(response).getInteger("expires_in");
                            redisUtil.setEx(accessTokenKey, accessToken, expiresIn - 60, TimeUnit.SECONDS);
                        }

                    }
                } catch (Exception e) {
                    log.error("获取微信accessToken,获取锁失败", e);
                } finally {
                    redissonLocker.unlock(lockKey);
                }
            } else {
                log.error("取微信accessToken,获取锁失败");
            }

        }

        return accessToken;

    }

2.拿到token然后访问想要访问的接口这里要访问获取二维码接口

在这里插入图片描述

对应接口的入参访问接口
代码如下(示例):

 public FranchiseeCooperateQrCodeVo getFranchiseeCooperateQrCode() {


        try {

            FranchiseeCooperateQrCodeVo franchiseeCooperateQrCodeVo = new FranchiseeCooperateQrCodeVo();

            Long userId = UserSession.getUser().getUserId();
            //统计二维码邀请的人数
            Integer inviteCount =  userFranchiseeFormRecordService.getInviteCount(  userId );
            franchiseeCooperateQrCodeVo.setInviteCount( inviteCount );
       
            String phone = UserSession.getUser().getPhone();
            String storeCode = UserSession.getUser().getStoreCode();
            String scene = phone + "&" + storeCode + "&" + userId;
            String qrCodeGoodsKey = String.format(QrCodeRedisKey.WECHAT_FRANCHISEE_QR_CODE_KEY, scene);
            //判断缓存中是否存在
            String result = redisUtil.get(qrCodeGoodsKey);
            if (StringUtils.isNotEmpty(result)) {
                //缓存中的二维码路径
                franchiseeCooperateQrCodeVo.setWechatQrCode(result);
                return franchiseeCooperateQrCodeVo;
            }

            String accessToken = 获取微信访问token  在上面;
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";
            JSONObject obj = new JSONObject();
            //最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~
            obj.put("scene", scene);
            //默认是true,检查page 是否存在,为 true 时 page 必须是已经发布的小程序存在的页面(否则报错)
            obj.put("check_path", false);
            obj.put("env_version", wxConfig.getEnvironmentVersion());

            //默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面。
            obj.put("page", URLDecoder.decode("pages/这里替换为自己的页面路径/index", "UTF8"));
            //默认430,二维码的宽度,单位 px,最小 280px,最大 1280px
            obj.put("width", 1000);
            ResponseEntity<byte[]> response = restTemplate.postForEntity(String.format(url, accessToken), obj, byte[].class);
            if (response != null) {
                String res = new String(response.getBody());
                if (res.indexOf("errcode") > -1) {
                    log.info("二维码生成失败!");
                    log.info(res);
                    throw new GlobalException("获取小程序二维码失败!");
                }
                byte[] imageByte = response.getBody();
                InputStream inputStream = new ByteArrayInputStream(imageByte);

                String uploadFileName = aliOssConfig.getPrefixPath() + "qrcode/franchisee/" + storeCode +"_"+ phone + ".jpg";
              //上传到远程服务器
                OssClientUtil.uploadImages(uploadFileName, inputStream , aliOssConfig.getBucketName());
                log.info("二维码生成成功!");
                redisUtil.set(qrCodeGoodsKey, uploadFileName);
                //微信api的二维码
                franchiseeCooperateQrCodeVo.setWechatQrCode(uploadFileName);
                return franchiseeCooperateQrCodeVo;

            } else {
                log.info("二维码生成失败!");
                throw new GlobalException("获取小程序二维码失败!");
            }

        } catch (Exception e) {

            throw new GlobalException("获取小程序二维码失败!");
        }


    }

总结

注意入参限制。细心核对每一个请求入参;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值