微信小程式 獲取小程式碼和二維碼java介面開發
前言:目前小程式推出了自己的識別碼,小程式碼,這個圓形的碼看起來比二維碼好看。本文總結微信小程式的獲取小程式碼和二維碼並生成二維碼圖片的介面開發。主要內容摘抄自微信小程式的API文件,java介面開發是自己總結開發。
微信小程式API文件:獲取二維碼
一、簡介
通過後臺介面可以獲取小程式任意頁面的二維碼,掃描該二維碼可以直接進入小程式對應的頁面。目前微信支援兩種二維碼,小程式碼(左),小程式二維碼(右),如下所示:
二、獲取小程式碼
目前有兩個介面可以生成小程式碼,開發者可以根據自己的需要選擇合適的介面。
1 不帶引數有限個數小程式碼介面
適用於需要的碼數量較少的業務場景
介面地址:
https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
注:獲取accesstoken的方法跟微信公眾獲取accesstoken方法一致,不過小程式獲取accesstoken需要小程式的appid和appsercet。登入 https://mp.weixin.qq.com ,就可以在網站的“設定”-“開發者設定”中,檢視到微信小程式的 AppID 了,注意不可直接使用服務號或訂閱號的 AppID 。
獲取微信小程式的 AppID文章地址:小程式簡易教程
(1)POST 引數說明
引數 | 型別 | 預設值 | 說明 |
---|---|---|---|
path | String | 不能為空,最大長度 128 位元組 | |
width | Int | 430 | 二維碼的寬度 |
auto_color | Bool | false | 自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調 |
line_color | Object | {“r”:”0”,”g”:”0”,”b”:”0”} | auth_color 為 false 時生效,使用 rgb 設定顏色 例如 {“r”:”xxx”,”g”:”xxx”,”b”:”xxx”} |
注意:通過該介面生成的小程式碼,永久有效,但數量有效,請謹慎使用。使用者掃描該碼進入小程式後,將直接進入 path 對應的頁面。
(2)請求介面測試
使用http請求外掛postman或者RESTClient請求測試。
請求測試結果返回一個小程式碼圖片,與微信公眾平臺生成二維碼不同,小程式碼直接返回檔案流,不是微信公眾平臺的url和ticket。
(3)java介面開發
注:此介面是基於Spring RestTemplate進行http請求,進行http請求有很多方法和工具類,可自行百度或參考下面的參考文章。介面只是提供一個解決方法的思路。
public Map getminiqrQr(String accessToken) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken;
Map<String,Object> param = new HashMap<>();
param.put("page", "pages/index/index");
param.put("width", 430);
param.put("auto_color", false);
Map<String,Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
LOG.info("呼叫生成微信URL介面傳參:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
LOG.info("呼叫小程式生成微信永久小程式碼URL介面返回結果:" + entity.getBody());
byte[] result = entity.getBody();
LOG.info(Base64.encodeBase64String(result));
inputStream = new ByteArrayInputStream(result);
File file = new File("C:/Users/wangqiulin/Desktop/1.png");
if (!file.exists()){
file.createNewFile();
}
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
LOG.error("呼叫小程式生成微信永久小程式碼URL介面異常",e);
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
說明:accessToken的獲取方法就不多說,因為小程式二維碼很坑爹的返回檔案流,導致我們必須對流進行處理轉換成圖片儲存到本地,這樣還有一個嚴重的後果就是無法將二維碼儲存到資料庫中,每次想獲取二維碼必須請求介面,此介面最多生成不超過100000個,請大家謹慎使用。
2 帶引數無限個數小程式碼介面
適用於需要的碼數量極多,或僅臨時使用的業務場景
介面地址:
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
獲取accessToken的方法跟介面1一致。
(1)POST 引數說明
引數 | 型別 | 預設值 | 說明 |
---|---|---|---|
scene | String | 最大32個可見字元,只支援數字,大小寫英文以及部分特殊字元:!#$&’()*+,/:;[email protected]_~,其它字元請自行編碼為合法字元(因不支援%,中文無法使用 urlencode 處理,請使用其他編碼方式) | |
page | String | 必須是已經發布的小程式頁面,例如 “pages/index/index” ,如果不填寫這個欄位,預設跳主頁面 | |
width | Int | 430 | 二維碼的寬度 |
auto_color | Bool | false | 自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調 |
line_color | Object | {“r”:”0”,”g”:”0”,”b”:”0”} | auto_color 為 false 時生效,使用 rgb 設定顏色 例如 {“r”:”xxx”,”g”:”xxx”,”b”:”xxx”} |
注意:通過該介面生成的小程式碼,永久有效,數量暫無限制。使用者掃描該碼進入小程式後,開發者需在對應頁面獲取的碼中 scene 欄位的值,再做處理邏輯。使用如下程式碼可以獲取到二維碼中的 scene 欄位的值。除錯階段可以使用開發工具的條件編譯自定義引數 scene=xxxx 進行模擬,開發工具模擬時的 scene 的引數值需要進行 urlencode。同時需要注意,此介面的page引數中不能帶任何引數,引數都在scene 引數中處理,切記!!!
// 這是首頁的 js
Page({
onLoad: function(options) {
// options 中的 scene 需要使用 decodeURIComponent 才能獲取到生成二維碼時傳入的 scene
var scene = decodeURIComponent(options.scene)
}
})
(2)請求介面測試
(3)java介面開發
public Map getminiqrQr(String sceneStr, String accessToken) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
Map<String,Object> param = new HashMap<>();
param.put("scene", sceneStr);
param.put("page", "pages/index/index");
param.put("width", 430);
param.put("auto_color", false);
Map<String,Object> line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
param.put("line_color", line_color);
LOG.info("呼叫生成微信URL介面傳參:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
LOG.info("呼叫小程式生成微信永久小程式碼URL介面返回結果:" + entity.getBody());
byte[] result = entity.getBody();
LOG.info(Base64.encodeBase64String(result));
inputStream = new ByteArrayInputStream(result);
File file = new File("C:/Users/wangqiulin/Desktop/1.png");
if (!file.exists()){
file.createNewFile();
}
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
LOG.error("呼叫小程式生成微信永久小程式碼URL介面異常",e);
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
3 獲取小程式二維碼
適用於需要的碼數量較少的業務場景
介面地址:
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
(1)POST 引數說明
引數 | 型別 | 預設值 | 說明 |
---|---|---|---|
path | String | 不能為空,最大長度 128 位元組 | |
width | Int | 430 | 二維碼的寬度 |
注意:通過該介面生成的小程式二維碼,永久有效,數量限制見文末說明,請謹慎使用。使用者掃描該碼進入小程式後,將直接進入 path 對應的頁面。
示例:
{"path": "pages/index?query=1", "width": 430}
注:pages/index 需要在 app.json 的 pages 中定義
(2)請求介面測試
(3)java介面開發
public Map getminiqrQr(String accessToken) {
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String url = "https://api.weixin.qq.com/wxaapp/createwxaqrcode?access_token="+accessToken;
Map<String,Object> param = new HashMap<>();
param.put("page", "pages/index/index");
param.put("width", 430);
LOG.info("呼叫生成微信URL介面傳參:" + param);
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
LOG.info("呼叫小程式生成微信永久二維碼URL介面返回結果:" + entity.getBody());
byte[] result = entity.getBody();
LOG.info(Base64.encodeBase64String(result));
inputStream = new ByteArrayInputStream(result);
File file = new File("C:/Users/wangqiulin/Desktop/1.png");
if (!file.exists()){
file.createNewFile();
}
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
LOG.error("呼叫小程式生成微信永久二維碼URL介面異常",e);
} finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
三、說明
1:通過該介面,僅能生成已釋出的小程式的二維碼。
2:可以在開發者工具預覽時生成開發版的帶參二維碼。
3:介面1加上介面2,總共生成的碼數量限制為100,000,請謹慎呼叫。
4 : POST 引數需要轉成 json 字串,不支援 form 表單提交。
5 : auto_color line_color 引數僅對小程式碼生效。