获取生成不限个数的小程序码(建议使用,永久有效)

一、controller

    @GetMapping(value="/getUnlimited")
    @ApiOperation(value = "获取小程序码")
    public String getUnlimited() {
        try {
            String filePath = "D:\\code\\";
            //获取accessToken
            String accessToken = miniProgramService.getAccessToken();
            //生成图片
            WxQrCode.getminiqrQr(accessToken,filePath);
            return  "hello world" ;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

二、service (获取token并生成图片)  postHeadersT方法参考resttemplate那篇文章


try {
            //获取token
            JSONObject accessToken = wxServiceExt.getAccessToken();
            String token = accessToken.get(CommonConstant.ACCESS_TOKEN).toString();
            if(StringUtils.isBlank(token)){
                throw new UnsupportedOperationException("获取accessToken失败!");
            }
            return token;
        } catch (Exception e) {
            log.info("获取accessToken失败,原因是"+e.getMessage());
           return "";
        }





 /**
     *
     * 获取accessToken
     * @return
     */
    public JSONObject getAccessToken() {
        String AccessToken_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;//小程序id
        String resultData = postHeadersT(AccessToken_URL, getHeaders(CommonConstant.DATA_INT_1), null, HttpMethod.GET);
        JSONObject parseObject = JSONObject.parseObject(resultData);
        return parseObject;
    }





三、生成小程序二维码(主要代码)


    @Autowired
    WXServiceExt wxServiceExt;

    /**
     * 生成不限个数的小程序码(建议使用)
     *
     * @param accessToken
     * @param filePath
     * @return
     */
    public static void getminiqrQr(String accessToken, String filePath) {
        String WxCode_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";//小程序密钥
        String fileName = "code1.png";
        try {
            String wxCodeURL = WxCode_URL.replace("ACCESS_TOKEN", accessToken);
            URL url = new URL(wxCodeURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            // 注意小程序页面如果不存在,则注释掉page参数
            //paramJson.put("page", "pages/index/index");
            paramJson.put("scene", "1234567890");
            paramJson.put("width", 430);
            paramJson.put("is_hyaline", true);
            paramJson.put("auto_color", true);
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File(filePath + fileName));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
        } catch (Exception e) {
            log.error("===> error : ", e);
        }
    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用PHP生成带参数的小程序,并且带参数进入,可以使用微信官方提供的小程序API。 首先,您需要在小程序后台创建一个带参数的二维。具体步骤如下: 1. 进入小程序后台,在“开发”->“开发设置”->“开发者ID”中获取AppID和AppSecret。 2. 在“开发”->“开发设置”->“接口设置”中,启用“小程序”功能。 3. 在“小程序”页面中,选择“参数设置”,设置好需要携带的参数。 4. 点击“生成小程序”按钮,即可生成带参数的小程序。 然后,您可以使用PHP代调用小程序API,在生成小程序的同时携带参数。具体代如下: ```php <?php $appid = "您的AppID"; $secret = "您的AppSecret"; $scene = "参数值"; // 需要携带的参数 $page = "pages/index/index"; // 进入小程序后跳转的页面 $width = 430; // 小程序的宽度 $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="; $access_token = getAccessToken($appid, $secret); // 获取access_token // 调用小程序API生成小程序 $result = httpRequest($url . $access_token, json_encode(array( "scene" => $scene, "page" => $page, "width" => $width ))); // 将生成小程序保存到本地 file_put_contents("qrcode.jpg", $result); // 获取access_token function getAccessToken($appid, $secret) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}"; $result = httpRequest($url); $json = json_decode($result, true); return $json["access_token"]; } // 发起HTTP请求 function httpRequest($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); return $result; } ?> ``` 上述代生成一个带参数的小程序,并保存到本地。在小程序中,您可以在相应的页面中获取携带的参数,从而进行相应的业务处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值