官方文檔地址:https://mp.weixin.qq.com/wiki/18/28fc21e7ed87bec960651f0ce873ef8a.html
原文地址:http://jingyan.baidu.com/article/fdbd4277cfd0c6b89f3f485f.html
-
注意事項
我知道你們是沒有心情看注意事項的,但是很重要,所以我先擺上來。
用戶掃描帶參數的二維碼,可能推送以下兩種事件:
1.如果用戶還未關注公眾號,則用戶可以關注公眾號,關注后微信會將帶場景值關注事件推送給開發者。
2.如果用戶已經關注公眾號,在用戶掃描后會自動進入會話,微信也會將帶場景值掃描事件推送給開發者。
是的,是把參數返回給開發者,也就是你自己,不是給用戶推送特定信息或跳轉到頁面。
(我也想要那種用戶掃描關注后跳到指定頁面的高級功能,也有一些微信號實現了,但是窮人家沒有技術人員沒有第三方平台的,我暫時不知道怎么做。憋問,讓我研究了以后再分享給大家!)
-
找入口
好了現在正式來制作我們帶參數的二維碼吧。
首先登錄你的微信公眾號,點擊左下角【接口權限】,然后找到“生成帶參數的二維碼”,點擊進入,這樣我們就到了微信開發者文檔的相關地方了。
-
調試工具
建議你把此頁開發者文檔認真看一遍,當然不想看也可以跟着小編繼續。
看完或者沒看都好,在此頁中找到【使用網頁調試工具調試該接口】,點擊進去吧。
-
獲取access_token
appid和secret在你的微信公眾號左下角【基本配置】中找到,對應填入AppID—>appid,AppSecret—>secret,填好后點擊【檢查問題】,得到的就是access_token。獲得后的那段代碼千萬記得保存下來,每日獲取次數有限的,一會需用上。另外,access_token有兩小時時效,過期就不能用了。
-
創建ticket(上)
保存好上一步的信息以后,在調試工具接口重新選擇接口類型,選擇【推廣支持】,接口列表選擇【創建二維碼ticket接口】。
-
創建ticket(下)
在相應位置上復制你之前保存的access_token填入,就是復制“access_token”:后面雙引號內的那段代碼。
body那里復制微信開發者文檔中POST例子的那段代碼,見下圖。注意你是要創建臨時二維碼還是永久二維碼,復制相應的POST例子就行了。
這里臨時二維碼需要改動的是expire_seconds后面的數字,表示時間。最大值為30天,這里請換算成秒表示。
還需改動的是scene_id,自行輸入一個1-100000之間的整數(這個就是你的二維碼參數了,就是用這個數字給二維碼編號的意思,用戶掃描二維碼以后會返回這個數字給你,表明用戶是用通過此渠道關注你的,這樣就可以統計用戶信息與不同渠道宣傳效果。)
填好以后點擊【檢查問題】。
-
兌換二維碼
如果上面的步驟都沒有錯誤,這個時候以后就會返回一段ticket代碼。復制“ticket”:后面雙引號的那段代碼,重新回到微信開發者文檔,見下圖紅框內的鏈接點擊進去,然后就會打開一個空白網頁,然后把你復制的代碼替代打開的網址鏈接后面的TICKET就可以了,見下圖2的網址后面紅色遮擋部分。這時就獲得一個帶參數的二維碼圖片啦,可以自行下載。
(避免廣告嫌疑,小編把圖片中的二維碼圖片用紅色#字線條遮擋住了)
-
-
注意事項
我知道你們是沒有心情看注意事項的,但是很重要,所以我先擺上來。
用戶掃描帶參數的二維碼,可能推送以下兩種事件:
1.如果用戶還未關注公眾號,則用戶可以關注公眾號,關注后微信會將帶場景值關注事件推送給開發者。
2.如果用戶已經關注公眾號,在用戶掃描后會自動進入會話,微信也會將帶場景值掃描事件推送給開發者。
是的,是把參數返回給開發者,也就是你自己,不是給用戶推送特定信息或跳轉到頁面。
(我也想要那種用戶掃描關注后跳到指定頁面的高級功能,也有一些微信號實現了,但是窮人家沒有技術人員沒有第三方平台的,我暫時不知道怎么做。憋問,讓我研究了以后再分享給大家!)
-
找入口
好了現在正式來制作我們帶參數的二維碼吧。
首先登錄你的微信公眾號,點擊左下角【接口權限】,然后找到“生成帶參數的二維碼”,點擊進入,這樣我們就到了微信開發者文檔的相關地方了。
-
調試工具
建議你把此頁開發者文檔認真看一遍,當然不想看也可以跟着小編繼續。
看完或者沒看都好,在此頁中找到【使用網頁調試工具調試該接口】,點擊進去吧。
-
獲取access_token
appid和secret在你的微信公眾號左下角【基本配置】中找到,對應填入AppID—>appid,AppSecret—>secret,填好后點擊【檢查問題】,得到的就是access_token。獲得后的那段代碼千萬記得保存下來,每日獲取次數有限的,一會需用上。另外,access_token有兩小時時效,過期就不能用了。
-
創建ticket(上)
保存好上一步的信息以后,在調試工具接口重新選擇接口類型,選擇【推廣支持】,接口列表選擇【創建二維碼ticket接口】。
-
創建ticket(下)
在相應位置上復制你之前保存的access_token填入,就是復制“access_token”:后面雙引號內的那段代碼。
body那里復制微信開發者文檔中POST例子的那段代碼,見下圖。注意你是要創建臨時二維碼還是永久二維碼,復制相應的POST例子就行了。
這里臨時二維碼需要改動的是expire_seconds后面的數字,表示時間。最大值為30天,這里請換算成秒表示。
還需改動的是scene_id,自行輸入一個1-100000之間的整數(這個就是你的二維碼參數了,就是用這個數字給二維碼編號的意思,用戶掃描二維碼以后會返回這個數字給你,表明用戶是用通過此渠道關注你的,這樣就可以統計用戶信息與不同渠道宣傳效果。)
填好以后點擊【檢查問題】。
-
兌換二維碼
如果上面的步驟都沒有錯誤,這個時候以后就會返回一段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); } }