分享项目---生成对应小程序二维码

微信二维码后端处理

1.获取微信token接口


    /**
     * 1、根据小程序 appId与密钥获取 accessToken
     *
     * @return
     */
    public String getAccessToken() {

        //根据key从redis中获取缓存
        String accessToken = String.valueOf(redisUtil.get(appId + ':' + appSecret));
        if (StrUtils.isNotEmpty(accessToken) && !accessToken.equals("null")) {
            log.info("accessToken从缓存中读取");
            return accessToken;
        } else {
            log.info("accessToken无缓存,请求接口读取");
            JSONObject json = JSON.parseObject(HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret));
            if (json != null && json.containsKey("access_token")) {
                accessToken = json.getString("access_token");
            }
            //将token 放入redis缓存
            Integer expires_in = json.getInteger("expires_in");
            redisUtil.set(appId + ':' + appSecret, accessToken);
            // 设置token缓存有效时间 7200秒-100秒
            redisUtil.expire(appId + ':' + appSecret, expires_in - 100);
            return accessToken;
        }
    }

2.生成微信小程序二维码

   /**
     * 生成微信小程序二维码
     *
     * @param scene 场景参数
     * @param page  小程序页面路径
     * @param width 二维码宽度
     * @return base64编码的二维码图片
     */
    public String createMiniQrCode(String scene, String page, int width) {
        try {
            // 获取小程序码URL
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + getAccessToken();
            // 构建请求参数
            JSONObject params = new JSONObject();
            params.put("scene", "allid=" + scene);  // scene会自动成为小程序页面的query参数allid的值,具体看小程序那边如何定义这个参数名字
            params.put("page", page);    // 指定跳转页面 - 去除/就正常了
            params.put("width", width);
            params.put("env_version", wechatEnv); // trial-体验版  develop-开发版   release-正式版
            log.info("生成小程序码请求参数: {}", params.toString());
            // 发送POST请求获取二维码图片字节数组
            byte[] response = HttpUtil.createPost(url)
                    .body(params.toString())
                    .execute()
                    .bodyBytes();
            // 检查返回是否为JSON错误信息
            String responseStr = new String(response);
            if (responseStr.contains("errcode")) {
                JSONObject errorJson = JSON.parseObject(responseStr);
                if (errorJson.containsKey("errcode") && errorJson.getIntValue("errcode") != 0) {
                    log.error("生成小程序码失败: errcode={}, errmsg={}, params={}",
                            errorJson.getInteger("errcode"),
                            errorJson.getString("errmsg"),
                            params.toString());
                    return null;
                }
            }
            // 将字节数组转为Base64字符串
            String base64QrCode = Base64.encode(response);
            return "data:image/png;base64," + base64QrCode;
        } catch (Exception e) {
            log.error("生成小程序码失败: {}", e.getMessage(), e);
            return null;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值