zoukankan      html  css  js  c++  java
  • 微信二维码带参数的二维码(转)

    官方文档地址:https://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html

    原文地址:http://jingyan.baidu.com/article/fdbd4277cfd0c6b89f3f485f.html

    1. 注意事项

      我知道你们是没有心情看注意事项的,但是很重要,所以我先摆上来。

       

      用户扫描带参数的二维码,可能推送以下两种事件:

      1.如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者

      2.如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者

       

      是的,是把参数返回给开发者,也就是你自己,不是给用户推送特定信息或跳转到页面。

      (我也想要那种用户扫描关注后跳到指定页面的高级功能,也有一些微信号实现了,但是穷人家没有技术人员没有第三方平台的,我暂时不知道怎么做。憋问,让我研究了以后再分享给大家!)

    2. 找入口

      好了现在正式来制作我们带参数的二维码吧。

      首先登录你的微信公众号,点击左下角【接口权限】,然后找到“生成带参数的二维码”,点击进入,这样我们就到了微信开发者文档的相关地方了。

      微信生成带参数的二维码(手把手教,通俗易懂)
    3. 调试工具

      建议你把此页开发者文档认真看一遍,当然不想看也可以跟着小编继续。

      看完或者没看都好,在此页中找到【使用网页调试工具调试该接口】,点击进去吧。

      微信生成带参数的二维码(手把手教,通俗易懂)
    4. 获取access_token

      appid和secret在你的微信公众号左下角【基本配置】中找到,对应填入AppID—>appid,AppSecret—>secret,填好后点击【检查问题】,得到的就是access_token。获得后的那段代码千万记得保存下来,每日获取次数有限的,一会需用上。另外,access_token有两小时时效,过期就不能用了。

      微信生成带参数的二维码(手把手教,通俗易懂)
      微信生成带参数的二维码(手把手教,通俗易懂)
    5. 创建ticket(上)

      保存好上一步的信息以后,在调试工具接口重新选择接口类型,选择【推广支持】,接口列表选择【创建二维码ticket接口】。

      微信生成带参数的二维码(手把手教,通俗易懂)
    6. 创建ticket(下)

      在相应位置上复制你之前保存的access_token填入,就是复制“access_token”:后面双引号内的那段代码。

      body那里复制微信开发者文档中POST例子的那段代码,见下图。注意你是要创建临时二维码还是永久二维码,复制相应的POST例子就行了。

      这里临时二维码需要改动的是expire_seconds后面的数字,表示时间。最大值为30天,这里请换算成秒表示。

      还需改动的是scene_id,自行输入一个1-100000之间的整数(这个就是你的二维码参数了,就是用这个数字给二维码编号的意思,用户扫描二维码以后会返回这个数字给你,表明用户是用通过此渠道关注你的,这样就可以统计用户信息与不同渠道宣传效果。)

      填好以后点击【检查问题】。

      微信生成带参数的二维码(手把手教,通俗易懂)
      微信生成带参数的二维码(手把手教,通俗易懂)
    7. 兑换二维码

      如果上面的步骤都没有错误,这个时候以后就会返回一段ticket代码。复制“ticket”:后面双引号的那段代码,重新回到微信开发者文档,见下图红框内的链接点击进去,然后就会打开一个空白网页,然后把你复制的代码替代打开的网址链接后面的TICKET就可以了,见下图2的网址后面红色遮挡部分。这时就获得一个带参数的二维码图片啦,可以自行下载。

      (避免广告嫌疑,小编把图片中的二维码图片用红色#字线条遮挡住了)

      微信生成带参数的二维码(手把手教,通俗易懂)
      微信生成带参数的二维码(手把手教,通俗易懂)
    1. 注意事项

      我知道你们是没有心情看注意事项的,但是很重要,所以我先摆上来。

       

      用户扫描带参数的二维码,可能推送以下两种事件:

      1.如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者

      2.如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者

       

      是的,是把参数返回给开发者,也就是你自己,不是给用户推送特定信息或跳转到页面。

      (我也想要那种用户扫描关注后跳到指定页面的高级功能,也有一些微信号实现了,但是穷人家没有技术人员没有第三方平台的,我暂时不知道怎么做。憋问,让我研究了以后再分享给大家!)

    2. 找入口

      好了现在正式来制作我们带参数的二维码吧。

      首先登录你的微信公众号,点击左下角【接口权限】,然后找到“生成带参数的二维码”,点击进入,这样我们就到了微信开发者文档的相关地方了。

      微信生成带参数的二维码(手把手教,通俗易懂)
    3. 调试工具

      建议你把此页开发者文档认真看一遍,当然不想看也可以跟着小编继续。

      看完或者没看都好,在此页中找到【使用网页调试工具调试该接口】,点击进去吧。

      微信生成带参数的二维码(手把手教,通俗易懂)
    4. 获取access_token

      appid和secret在你的微信公众号左下角【基本配置】中找到,对应填入AppID—>appid,AppSecret—>secret,填好后点击【检查问题】,得到的就是access_token。获得后的那段代码千万记得保存下来,每日获取次数有限的,一会需用上。另外,access_token有两小时时效,过期就不能用了。

      微信生成带参数的二维码(手把手教,通俗易懂)
      微信生成带参数的二维码(手把手教,通俗易懂)
    5. 创建ticket(上)

      保存好上一步的信息以后,在调试工具接口重新选择接口类型,选择【推广支持】,接口列表选择【创建二维码ticket接口】。

      微信生成带参数的二维码(手把手教,通俗易懂)
    6. 创建ticket(下)

      在相应位置上复制你之前保存的access_token填入,就是复制“access_token”:后面双引号内的那段代码。

      body那里复制微信开发者文档中POST例子的那段代码,见下图。注意你是要创建临时二维码还是永久二维码,复制相应的POST例子就行了。

      这里临时二维码需要改动的是expire_seconds后面的数字,表示时间。最大值为30天,这里请换算成秒表示。

      还需改动的是scene_id,自行输入一个1-100000之间的整数(这个就是你的二维码参数了,就是用这个数字给二维码编号的意思,用户扫描二维码以后会返回这个数字给你,表明用户是用通过此渠道关注你的,这样就可以统计用户信息与不同渠道宣传效果。)

      填好以后点击【检查问题】。

      微信生成带参数的二维码(手把手教,通俗易懂)
      微信生成带参数的二维码(手把手教,通俗易懂)
    7. 兑换二维码

      如果上面的步骤都没有错误,这个时候以后就会返回一段ticket代码。复制“ticket”:后面双引号的那段代码,重新回到微信开发者文档,见下图红框内的链接点击进去,然后就会打开一个空白网页,然后把你复制的代码替代打开的网址链接后面的TICKET就可以了,见下图2的网址后面红色遮挡部分。这时就获得一个带参数的二维码图片啦,可以自行下载。

      (避免广告嫌疑,小编把图片中的二维码图片用红色#字线条遮挡住了)

      微信生成带参数的二维码(手把手教,通俗易懂)
      微信生成带参数的二维码(手把手教,通俗易懂)

    二维码代码,攒未测试,请慎用

    maven引入包

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.21</version>
    </dependency>

    HttpClientUtils.java 工具类,用来发送请求
    package com.rainy.framework.twobar.utils;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.ssl.DefaultHostnameVerifier;
    import org.apache.http.conn.util.PublicSuffixMatcher;
    import org.apache.http.conn.util.PublicSuffixMatcherLoader;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    public class HttpClientUtils {
        private RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000).setConnectionRequestTimeout(15000).build();
    
        private static HttpClientUtils instance = null;
    
        private HttpClientUtils() {
        }
    
        public static HttpClientUtils getInstance() {
            if (instance == null) {
                instance = new HttpClientUtils();
            }
            return instance;
        }
    
        /**
         * 发送 post请求
         * 
         * @param httpUrl
         *            地址
         */
        public String sendHttpPost(String httpUrl) {
            HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
            return sendHttpPost(httpPost);
        }
    
        /**
         * 发送 post请求
         * 
         * @param httpUrl
         *            地址
         * @param params
         *            参数(格式:key1=value1&key2=value2)
         */
        public String sendHttpPost(String httpUrl, String params) {
            HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
            try {
                // 设置参数
                StringEntity stringEntity = new StringEntity(params, "UTF-8");
                stringEntity.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(stringEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sendHttpPost(httpPost);
        }
    
        /**
         * 发送 post请求
         * 
         * @param httpUrl
         *            地址
         * @param maps
         *            参数
         */
        public String sendHttpPost(String httpUrl, Map<String, String> maps) {
            HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
            // 创建参数队列
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            for (String key : maps.keySet()) {
                nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
            }
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sendHttpPost(httpPost);
        }
    
        /**
         * 发送 post请求(带文件)
         * 
         * @param httpUrl
         *            地址
         * @param maps
         *            参数
         * @param fileLists
         *            附件
         */
        public String sendHttpPost(String httpUrl, Map<String, String> maps, List<File> fileLists) {
            HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
            MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
            for (String key : maps.keySet()) {
                meBuilder.addPart(key, new StringBody(maps.get(key), ContentType.TEXT_PLAIN));
            }
            for (File file : fileLists) {
                FileBody fileBody = new FileBody(file);
                meBuilder.addPart("files", fileBody);
            }
            HttpEntity reqEntity = meBuilder.build();
            httpPost.setEntity(reqEntity);
            return sendHttpPost(httpPost);
        }
    
        /**
         * 发送Post请求
         * 
         * @param httpPost
         * @return
         */
        private String sendHttpPost(HttpPost httpPost) {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
            HttpEntity entity = null;
            String responseContent = null;
            try {
                // 创建默认的httpClient实例.
                httpClient = HttpClients.createDefault();
                httpPost.setConfig(requestConfig);
                // 执行请求
                response = httpClient.execute(httpPost);
                entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    // 关闭连接,释放资源
                    if (response != null) {
                        response.close();
                    }
                    if (httpClient != null) {
                        httpClient.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return responseContent;
        }
    
        /**
         * 发送 get请求
         * 
         * @param httpUrl
         */
        public String sendHttpGet(String httpUrl) {
            HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
            return sendHttpGet(httpGet);
        }
    
        /**
         * 发送 get请求Https
         * 
         * @param httpUrl
         */
        public String sendHttpsGet(String httpUrl) {
            HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
            return sendHttpsGet(httpGet);
        }
    
        /**
         * 发送Get请求
         * 
         * @param httpPost
         * @return
         */
        private String sendHttpGet(HttpGet httpGet) {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
            HttpEntity entity = null;
            String responseContent = null;
            try {
                // 创建默认的httpClient实例.
                httpClient = HttpClients.createDefault();
                httpGet.setConfig(requestConfig);
                // 执行请求
                response = httpClient.execute(httpGet);
                entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    // 关闭连接,释放资源
                    if (response != null) {
                        response.close();
                    }
                    if (httpClient != null) {
                        httpClient.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return responseContent;
        }
    
        /**
         * 发送Get请求Https
         * 
         * @param httpPost
         * @return
         */
        private String sendHttpsGet(HttpGet httpGet) {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
            HttpEntity entity = null;
            String responseContent = null;
            try {
                // 创建默认的httpClient实例.
                PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
                DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
                httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
                httpGet.setConfig(requestConfig);
                // 执行请求
                response = httpClient.execute(httpGet);
                entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    // 关闭连接,释放资源
                    if (response != null) {
                        response.close();
                    }
                    if (httpClient != null) {
                        httpClient.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return responseContent;
        }
    }
    TwobarParamDemo.java 通过Token、Create进行二维码创建,然后获取二维码图片代码
    package com.rainy.framework.twobar.demo;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.http.client.ClientProtocolException;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.rainy.framework.twobar.utils.HttpClientUtils;
    
    public class TwobarParamDemo {
        
        public static void main(String[] args) throws ClientProtocolException, IOException {
            
            //获取授权Token
            String appid = "xxx";
            String secret = "xxx";
            String getTokenUrlTemplate = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}";
            String getTokenUrl = getTokenUrlTemplate.replaceAll("\{appid\}", appid).replaceAll("\{secret\}", secret);
            String tokenStr = HttpClientUtils.getInstance().sendHttpGet(getTokenUrl);
            System.out.println(tokenStr);
            
            /**
             * action_name: 二维码类型,
             *         QR_SCENE为临时,
             *         QR_LIMIT_SCENE为永久,
             *         QR_LIMIT_STR_SCENE为永久的字符串参数值
             * 
             *     expire_seconds    该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
             *  action_name    二维码类型,QR_SCENE为临时,QR_LIMIT_SCENE为永久,QR_LIMIT_STR_SCENE为永久的字符串参数值
             *  action_info    二维码详细信息
             *  scene_id    场景值ID,临时二维码时为32位非0整型,永久二维码时最大值为100000(目前参数只支持1--100000)
             *  scene_str    场景值ID(字符串形式的ID),字符串类型,长度限制为1到64,仅永久二维码支持此字段
             *  
             */
            //获取授权创建带参数二维码
            JSONObject tokenObj = JSON.parseObject(tokenStr);
            String createUrlTemplate = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={token}";
            String createUrl = createUrlTemplate.replaceAll("\{token\}", tokenObj.getString("access_token"));
            Map<String, String> params = new HashMap<String, String>();
            params.put("expire_seconds", "2592000");
            params.put("action_name", "QR_LIMIT_STR_SCENE");
            params.put("action_info", "213456");
            params.put("scene_id", "123");
            params.put("scene_str", "456");
            String createStr = HttpClientUtils.getInstance().sendHttpPost(createUrl, params);
            System.out.println(createStr);
            
            //根据ticket生成二维码
            JSONObject ticketObj = JSON.parseObject(createStr);
            String ticketUrlTemplate = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={ticket}";
            String ticketUrl = ticketUrlTemplate.replaceAll("\{ticket\}", ticketObj.getString("ticket"));
            //正常情况,该URL就是正常可以获得二维码的地址
            System.out.println(ticketUrl);
        }
    
    }
  • 相关阅读:
    TOJ 2710: 过河 路径压缩
    树状数组模板
    TOJ 2017: N-Credible Mazes
    TOJ 4804: 树网的核
    Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
    TOJ 2541: Paper Cutting
    CODE FESTIVAL 2017 qual B
    hiho[Offer收割]编程练习赛30
    Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0
  • 原文地址:https://www.cnblogs.com/rainy-shurun/p/6172205.html
Copyright © 2011-2022 走看看