一、准備工作
- 微信公眾平台接口調試工具
- 小程序的唯一標識(appid)
- 小程序的密鑰(secret)
二、獲取access_token
打開微信公眾平台接口調試工具,在參數列表中輸入小程序的appid和secret,點擊檢查問題,如果appid和secret正確,則可以返回正確的access_token結果(圖中下方的紅框)
三、生成微信小程序二維碼
生成小程序二維碼官方文檔
一共有三種生成二維碼的方式,可以根據使用場景去選擇,這里我使用的是第三種生成方式 wxacode.getUnlimited
wxacode.createQRCode 獲取小程序二維碼,適用於需要的碼數量較少的業務場景。通過該接口生成的小程序碼,永久有效,有數量限制,詳見獲取二維碼。 POST https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN wxacode.get 獲取小程序碼,適用於需要的碼數量較少的業務場景。通過該接口生成的小程序碼,永久有效,有數量限制,詳見獲取二維碼。 POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN wxacode.getUnlimited 獲取小程序碼,適用於需要的碼數量極多的業務場景。通過該接口生成的小程序碼,永久有效,數量暫無限制。 更多用法詳見 獲取二維碼。 POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
使用wxacode.getUnlimited生成小程序二維碼
獲取小程序碼,適用於需要的碼數量極多的業務場景。通過該接口生成的小程序碼,永久有效,數量暫無限制。 更多用法詳見 獲取二維碼。
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
說明
通過該接口生成的小程序碼,永久有效,數量暫無限制。用戶掃描該碼進入小程序后,開發者需在對應頁面獲取的碼中 scene字段的值,再做處理邏輯。
使用如下代碼可以獲取到二維碼中的 scene 字段的值。
調試階段可以使用開發工具的條件編譯自定義參數 scene=xxxx 進行模擬,開發工具模擬時的 scene 的參數值需要進行 urlencode
// 這是首頁的 js Page({ onLoad: function(options) { // options 中的 scene 需要使用 decodeURIComponent 才能獲取到生成二維碼時傳入的 scene var scene = decodeURIComponent(options.scene) } })
獲取接口調用憑證
1 /** 2 * 接口調用憑證 access_token 3 */ 4 public static String postToken(String appId, String appKey) throws Exception { 5 6 String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appKey; 7 URL url = new URL(requestUrl); 8 // 打開和URL之間的連接 9 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 10 connection.setRequestMethod("POST"); 11 // 設置通用的請求屬性 12 connection.setRequestProperty("Content-Type", "application/json"); 13 connection.setRequestProperty("Connection", "Keep-Alive"); 14 connection.setUseCaches(false); 15 connection.setDoOutput(true); 16 connection.setDoInput(true); 17 18 // 得到請求的輸出流對象 19 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); 20 out.writeBytes(""); 21 out.flush(); 22 out.close(); 23 24 // 建立實際的連接 25 connection.connect(); 26 // 定義 BufferedReader輸入流來讀取URL的響應 27 BufferedReader in; 28 if (requestUrl.contains("nlp")) 29 in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); 30 else 31 in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); 32 StringBuilder result = new StringBuilder(); 33 String getLine; 34 while ((getLine = in.readLine()) != null) { 35 result.append(getLine); 36 } 37 in.close(); 38 JSONObject jsonObject = JSONObject.parseObject(result.toString()); 39 return jsonObject.getString("access_token"); 40 }
調用微信接口生成微信小程序二維碼
1 /** 2 * 生成微信小程序二維碼 3 * 4 * @param filePath 5 * 本地生成二維碼路徑 6 * @param page 7 * 當前小程序相對頁面 必須是已經發布的小程序存在的頁面(否則報錯),例如 pages/index/index, 根路徑前不要填加 /,不能攜帶參數(參數請放在scene字段里),如果不填寫這個字段,默認跳主頁面 8 * @param scene 9 * 最大32個可見字符,只支持數字,大小寫英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符請自行編碼為合法字符(因不支持%,中文無法使用 urlencode 處理,請使用其他編碼方式) 10 * @param accessToken 11 * 接口調用憑證 12 */ 13 public static void generateQrCode(String filePath, String page, String scene, String accessToken) { 14 15 try { 16 17 //調用微信接口生成二維碼 18 URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken); 19 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 20 httpURLConnection.setRequestMethod("POST");// 提交模式 21 // conn.setConnectTimeout(10000);//連接超時 單位毫秒 22 // conn.setReadTimeout(2000);//讀取超時 單位毫秒 23 // 發送POST請求必須設置如下兩行 24 httpURLConnection.setDoOutput(true); 25 httpURLConnection.setDoInput(true); 26 // 獲取URLConnection對象對應的輸出流 27 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); 28 // 發送請求參數 29 JSONObject paramJson = new JSONObject(); 30 //這就是你二維碼里攜帶的參數 String型 名稱不可變 31 paramJson.put("scene", scene); 32 //注意該接口傳入的是page而不是path 33 paramJson.put("page", page); 34 //這是設置掃描二維碼后跳轉的頁面 35 paramJson.put("width", 200); 36 paramJson.put("is_hyaline", true); 37 paramJson.put("auto_color", true); 38 printWriter.write(paramJson.toString()); 39 // flush輸出流的緩沖 40 printWriter.flush(); 41 42 //開始獲取數據 43 BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); 44 OutputStream os = new FileOutputStream(new File(filePath)); 45 int len; 46 byte[] arr = new byte[1024]; 47 while ((len = bis.read(arr)) != -1) { 48 os.write(arr, 0, len); 49 os.flush(); 50 } 51 os.close(); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 56 System.out.println("打開地址查看生成的二維碼:" + filePath); 57 58 }
測試類
1 public static void main(String[] args) throws Exception { 2 3 //獲取接口調用憑證access_token 4 String appId = "小程序id";//小程序id 5 String appKey = "小程序密鑰";//小程序密鑰 6 String token = postToken(appId, appKey); 7 8 //生成二維碼 9 generateQrCode("E:\\tools\\qrCode\\test.png", "pages/index/index", "aa=108&bb=2&cc=3", token); 10 11 }
注意
1 1.獲取小程序appId 與appKey 2 2.生成小程序二維碼頁面參數傳入的是page而不是path,其他的接口是path。 3 page后面不允許加參數,參數需要通過scene傳入。而小程序也需要通過scene獲取參數。 4 3.生成小程序二維碼可將二維碼寫入本地,也可上傳至服務器。自行選擇 5
參考地址:
https://www.cnblogs.com/daipianpian/p/9239452.html
http://www.what21.com/u/10004/6756200547748968305.htm
生成小程序二維碼官方文檔
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html