微信小程序生成携带参数的动态二维码,跳转小程序首页或者指定页

不想解释了,太累了,只是想说网上有很多错误的方法,腾讯也没有详细的介绍

1.用于获取access_token


1.用于获取access_token
*  @param APIKEY    小程序id
*  @param SECRETKEY 小程序密钥
public static String postToken() throws Exception {
        String APIKEY = FileUtil.findValue("AppID");//小程序id
        String SECRETKEY = FileUtil.findValue("AppSecret");//小程序密钥
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APIKEY + "&secret=" + SECRETKEY;
        URL url = new URL(requestUrl);
        // 打开和URL之间的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 设置通用的请求属性
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);

        // 得到请求的输出流对象
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.writeBytes("");
        out.flush();
        out.close();

        // 建立实际的连接
        connection.connect();
        // 定义 BufferedReader输入流来读取URL的响应
        BufferedReader in = null;
        if (requestUrl.contains("nlp"))
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
        else
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String result = "";
        String getLine;
        while ((getLine = in.readLine()) != null) {
            result += getLine;
        }
        in.close();
        JSONObject jsonObject = JSON.parseObject(result);
        String accesstoken = jsonObject.getString("access_token");
        return accesstoken;
    }

2.生成带参小程序二维码

2.生成带参小程序二维码
	createQRCodeUrl=https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=
     * @param sceneStr    参数
     * @param accessToken token
public static String getminiqrQr(Map<String, Object> map) {
        try {
            String token = postToken();
            String qrCodeUrl = FileUtil.findValue("createQRCodeUrl");
            URL url = new URL(qrCodeUrl + token);
            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();
            paramJson.put("scene", map.get("item_id") + "shareId" + map.get("shareId"));
            paramJson.put("page", "pages/item/detail/detail");
            paramJson.put("width", 258);
            paramJson.put("auto_color", true);
            /**
             * line_color生效
             * paramJson.put("auto_color", false);
             * JSONObject lineColor = new JSONObject();
             * lineColor.put("r", 0);
             * lineColor.put("g", 0);
             * lineColor.put("b", 0);
             * paramJson.put("line_color", lineColor);
             * */

            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            //scene 只有这一个用来传参数的字段 所以你可以自己拼接参数之类的
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            String filePath = FileUtil.findValue("imgPath")
                    + FileUtil.findValue("qrCode1")
                    + map.get("shareId") + map.get("item_id") + ".png";
//            String filePath = "C:/Users/Administrator/Desktop/"+map.get("shareId")+map.get("item_id")+".png";
            OutputStream os = new FileOutputStream(new File(filePath));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
            return filePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

工具类方法


	public static String findValue(String key) throws IOException {
		// 读取配置文件
		Properties properties = new Properties();
		InputStream inputFile = null;
		try {

			// 获取classes路径
			ClassPathResource cpr = new ClassPathResource("config.properties");
			//ClassPathResource cpr = new ClassPathResource("config.test.properties");
			//ClassPathResource cpr = new ClassPathResource("config.server.properties");
			// 打开文件
			inputFile = cpr.getInputStream();
			// 加载资源文件
			properties.load(new InputStreamReader(inputFile, "UTF-8"));
			String value = properties.getProperty(key);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputFile != null)
				// 关闭文件流
				inputFile.close();
		}
		return null;
	}

小程序代码提取字段值

onLoad: function (options) {
    // console.log(options,"options..........")
    var a = options.scene;  //这个a就是你后台scene的值  怎么分割看自己吧
    } 
点击微信开发工具,直接打开你生成的正确二维码,直接在你跳转的页面可以打印你输出的数据

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值