1. 程式人生 > >微信小程式建立二維碼

微信小程式建立二維碼

1.獲取小程式的accessToken 資訊

2.呼叫介面獲取小程式碼或小程式二維碼

介面A: 適用於需要的碼數量較少的業務場景介面地址:

https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

獲取 access_token 詳見官方文件

POST 引數說明

引數型別預設值說明
pathString不能為空,最大長度 128 位元組
widthInt430二維碼的寬度
auto_colorBoolfalse自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調
line_colorObject{"r":"0","g":"0","b":"0"}auth_color 為 false 時生效,使用 rgb 設定顏色 例如 {"r":"xxx","g":"xxx","b":"xxx"},十進位制表示
is_hyalineBoolfalse是否需要透明底色, is_hyaline 為true時,生成透明底色的小程式碼

注意:通過該介面生成的小程式碼,永久有效,數量限制見文末說明,請謹慎使用。使用者掃描該碼進入小程式後,將直接進入 path 對應的頁面。

public void getwxacode(String authorizer_appid,String access_token) {
    InputStream inputStream = null;
OutputStream outputStream = null; File file = null; try { String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+access_token; JSONObject param = new JSONObject(); param.put("path","pages/index/index"); param.put("width",430); //二維碼的寬度 param.put("auto_color",false); //自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調
param.put("is_hyaline",false); //是否需要透明底色, is_hyaline 為true時,生成透明底色的小程式碼 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); logger.info(">>>>>> 呼叫生成微信小程式碼URL介面入參:"+param.toString()+" <<<<<< "); MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); HttpEntity requestEntity = new HttpEntity(param, headers); ResponseEntity<byte[]> entity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,byte[].class); logger.info(">>>>>> 呼叫小程式生成微信永久小程式碼URL介面回參: " + entity); byte[] result = entity.getBody(); inputStream = new ByteArrayInputStream(result); File filePath = new File("/soft/TEMP"); if (!filePath.mkdir()) filePath.mkdirs(); file = new File(filePath+File.separator+authorizer_appid+".jpeg"); if (!file.exists()) { file.createNewFile(); } outputStream = new FileOutputStream(file); inputStream = new ByteArrayInputStream(result); int content = 0; byte[] buffer = new byte[1024 * 8]; while ((content = inputStream.read(buffer,0,1024)) != -1) { outputStream.write(buffer, 0, content); } outputStream.flush(); }catch (Exception e){ logger.error("呼叫小程式生成微信永久小程式碼URL介面異常: "+e.getMessage()); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }

介面B:適用於需要的碼數量極多的業務場景

介面地址:

https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

獲取 access_token 詳見官方文件

POST 引數說明

引數型別預設值說明
sceneString最大32個可見字元,只支援數字,大小寫英文以及部分特殊字元:!#$&'()*+,/:;[email protected]_~,其它字元請自行編碼為合法字元(因不支援%,中文無法使用 urlencode 處理,請使用其他編碼方式)
pageString必須是已經發布的小程式存在的頁面(否則報錯),例如 "pages/index/index" ,根路徑前不要填加'/',不能攜帶引數(引數請放在scene欄位裡),如果不填寫這個欄位,預設跳主頁面
widthInt430二維碼的寬度
auto_colorBoolfalse自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調
line_colorObject{"r":"0","g":"0","b":"0"}auto_color 為 false 時生效,使用 rgb 設定顏色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十進位制表示
is_hyalineBoolfalse是否需要透明底色, is_hyaline 為true時,生成透明底色的小程式碼

注意:通過該介面生成的小程式碼,永久有效,數量暫無限制。使用者掃描該碼進入小程式後,開發者需在對應頁面獲取的碼中 scene 欄位的值,再做處理邏輯。使用如下程式碼可以獲取到二維碼中的 scene 欄位的值。除錯階段可以使用開發工具的條件編譯自定義引數 scene=xxxx 進行模擬,開發工具模擬時的 scene 的引數值需要進行 urlencode

public void getwxacodeunlimit(String authorizer_appid,String scene,String access_token) {
    InputStream inputStream = null;
OutputStream outputStream = null;
File file = null;
    try {String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token;
JSONObject param = new JSONObject();
param.put("scene",scene);   //頁面可接受引數
param.put("page","pages/index/index");  //必須是已經發布的小程式存在的頁面(否則報錯),如果不填寫這個欄位,預設跳主頁面
param.put("width",430); //二維碼的寬度
param.put("auto_color",false);  //自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調
param.put("is_hyaline",false);  //是否需要透明底色, is_hyaline 為true時,生成透明底色的小程式碼
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);
logger.info(">>>>>> 呼叫生成微信小程式碼URL介面入參:"+param.toString()+"  <<<<<< ");
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
JSONObject return_json = restTemplate.postForEntity(url,requestEntity,JSONObject.class).getBody();
logger.info(">>>>>> 返回JSON資料: " + return_json);
ResponseEntity<byte[]> entity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,byte[].class);
logger.info(">>>>>> 呼叫小程式生成微信永久小程式碼URL介面回參: " + entity.getBody());
        byte[] result = entity.getBody();
inputStream = new ByteArrayInputStream(result);
File filePath = new File("/soft/TEMP");
        if (!filePath.mkdir())
            filePath.mkdirs();
file = new File(filePath+File.separator+authorizer_appid+".jpeg");
        if (!file.exists()) {
            file.createNewFile();
}
        outputStream = new FileOutputStream(file);
inputStream = new ByteArrayInputStream(result);
        int content = 0;
        byte[] buffer = new byte[1024 * 8];
        while ((content = inputStream.read(buffer,0,1024)) != -1) {
            outputStream.write(buffer, 0, content);
}
        outputStream.flush();
}catch (Exception e){
        logger.error("呼叫小程式生成微信永久小程式碼URL介面異常: "+e.getMessage());
} finally {
        if (inputStream != null) {
            try {
                inputStream.close();
} catch (IOException e) {
                e.printStackTrace();
}
        }
        if (outputStream != null) {
            try {
                outputStream.close();
} catch (IOException e) {
                e.printStackTrace();
}
        }
    }
}

獲取小程式二維碼

介面C:適用於需要的碼數量較少的業務場景

介面地址:

https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

獲取 access_token 詳見官方文件

POST 引數說明

引數型別預設值說明
pathString不能為空,最大長度 128 位元組
widthInt430二維碼的寬度

注意:通過該介面生成的小程式二維碼,永久有效,數量限制見文末說明,請謹慎使用。使用者掃描該碼進入小程式後,將直接進入 path 對應的頁面。

public void createwxaqrcode(String authorizer_appid,Sring access_token) {
    InputStream inputStream = null;
OutputStream outputStream = null;
File file = null;
    try {
       String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+authorizer_access_token;
JSONObject param = new JSONObject();
param.put("path","pages/index/index");  //必須是已經發布的小程式存在的頁面(否則報錯),如果不填寫這個欄位,預設跳主頁面
logger.info(">>>>>> 呼叫生成微信小程式碼URL介面入參:"+param.toString()+"  <<<<<< ");
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,byte[].class);
logger.info(">>>>>> 呼叫小程式生成微信永久小程式碼URL介面回參: " + entity);
        byte[] result = entity.getBody();
inputStream = new ByteArrayInputStream(result);
File filePath = new File("/soft/TEMP");
        if (!filePath.mkdir())
            filePath.mkdirs();
file = new File(filePath+File.separator+authorizer_appid+".jpeg");
        if (!file.exists()) {
            file.createNewFile();
}
        outputStream = new FileOutputStream(file);
inputStream = new ByteArrayInputStream(result);
        int content = 0;
        byte[] buffer = new byte[1024 * 8];
        while ((content = inputStream.read(buffer,0,1024)) != -1) {
            outputStream.write(buffer, 0, content);
}
        outputStream.flush();
}catch (Exception e){
        logger.error("呼叫小程式生成微信永久小程式碼URL介面異常: "+e.getMessage());
} finally {
        if (inputStream != null) {
            try {
                inputStream.close();
} catch (IOException e) {
                e.printStackTrace();
}
        }
        if (outputStream != null) {
            try {
                outputStream.close();
} catch (IOException e) {
                e.printStackTrace();
}
        }
    }
}

Bug & Tip

  1. tip:通過該介面,僅能生成已釋出的小程式的二維碼。
  2. tip:可以在開發者工具預覽時生成開發版的帶參二維碼。
  3. tip:介面A加上介面C,總共生成的碼數量限制為100,000,請謹慎呼叫。
  4. tip: POST 引數需要轉成 json 字串,不支援 form 表單提交。
  5. tip: auto_color line_color 引數僅對小程式碼生效。

錯誤碼

45009:B介面呼叫分鐘頻率受限(目前5000次/分鐘,會調整),如需大量小程式碼,建議預生成。45029:A介面和C介面生成碼個數總和到達最大個數限制。41030:B介面所傳page頁面不存在,或者小程式沒有釋出,請注意B介面沒有path引數,傳path引數雖然可以生成小程式碼,但是隻能跳主頁面。