微信小程序?qū)崿F(xiàn)獲取小程序碼和二維碼java接口開發(fā)
前言:目前小程序推出了自己的識(shí)別碼,小程序碼,這個(gè)圓形的碼看起來比二維碼好看。本文總結(jié)微信小程序的獲取小程序碼和二維碼并生成二維碼圖片的接口開發(fā)。主要內(nèi)容摘抄自微信小程序的API文檔,java接口開發(fā)是自己總結(jié)開發(fā)。
微信小程序API文檔:獲取二維碼
一、簡介
通過后臺(tái)接口可以獲取小程序任意頁面的二維碼,掃描該二維碼可以直接進(jìn)入小程序?qū)?yīng)的頁面。目前微信支持兩種二維碼,小程序碼(左),小程序二維碼(右),如下所示:
二、獲取小程序碼
目前有兩個(gè)接口可以生成小程序碼,開發(fā)者可以根據(jù)自己的需要選擇合適的接口。
1 不帶參數(shù)有限個(gè)數(shù)小程序碼接口
適用于需要的碼數(shù)量較少的業(yè)務(wù)場景
接口地址:https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
注:獲取accesstoken的方法跟微信公眾獲取accesstoken方法一致,不過小程序獲取accesstoken需要小程序的appid和appsercet。登錄 https://mp.weixin.qq.com ,就可以在網(wǎng)站的“設(shè)置”-“開發(fā)者設(shè)置”中,查看到微信小程序的 AppID 了,注意不可直接使用服務(wù)號(hào)或訂閱號(hào)的 AppID 。
獲取微信小程序的 AppID文章地址:小程序簡易教程
(1)POST 參數(shù)說明
參數(shù) | 類型 | 默認(rèn)值 | 說明 |
---|---|---|---|
path | String | 不能為空,最大長度 128 字節(jié) | |
width | Int | 430 | 二維碼的寬度 |
auto_color | Bool | false | 自動(dòng)配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調(diào) |
line_color | Object | {“r”:”0”,”g”:”0”,”b”:”0”} | auth_color 為 false 時(shí)生效,使用 rgb 設(shè)置顏色 例如 {“r”:”xxx”,”g”:”xxx”,”b”:”xxx”} |
注意:通過該接口生成的小程序碼,永久有效,但數(shù)量有效,請(qǐng)謹(jǐn)慎使用。用戶掃描該碼進(jìn)入小程序后,將直接進(jìn)入 path 對(duì)應(yīng)的頁面。
(2)請(qǐng)求接口測試
使用http請(qǐng)求插件postman或者RESTClient請(qǐng)求測試。
請(qǐng)求測試結(jié)果返回一個(gè)小程序碼圖片,與微信公眾平臺(tái)生成二維碼不同,小程序碼直接返回文件流,不是微信公眾平臺(tái)的url和ticket。
(3)java接口開發(fā)
注:此接口是基于Spring RestTemplate進(jìn)行http請(qǐng)求,進(jìn)行http請(qǐng)求有很多方法和工具類,可自行百度或參考下面的參考文章。接口只是提供一個(gè)解決方法的思路。
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("調(diào)用生成微信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("調(diào)用小程序生成微信永久小程序碼URL接口返回結(jié)果:" + 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("調(diào)用小程序生成微信永久小程序碼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的獲取方法就不多說,因?yàn)樾〕绦蚨S碼很坑爹的返回文件流,導(dǎo)致我們必須對(duì)流進(jìn)行處理轉(zhuǎn)換成圖片保存到本地,這樣還有一個(gè)嚴(yán)重的后果就是無法將二維碼保存到數(shù)據(jù)庫中,每次想獲取二維碼必須請(qǐng)求接口,此接口最多生成不超過100000個(gè),請(qǐng)大家謹(jǐn)慎使用。
2 帶參數(shù)無限個(gè)數(shù)小程序碼接口
適用于需要的碼數(shù)量極多,或僅臨時(shí)使用的業(yè)務(wù)場景
接口地址:https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
獲取accessToken的方法跟接口1一致。
(1)POST 參數(shù)說明
參數(shù) | 類型 | 默認(rèn)值 | 說明 |
---|---|---|---|
scene | String | 最大32個(gè)可見字符,只支持?jǐn)?shù)字,大小寫英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符請(qǐng)自行編碼為合法字符(因不支持%,中文無法使用 urlencode 處理,請(qǐng)使用其他編碼方式) | |
page | String | 必須是已經(jīng)發(fā)布的小程序頁面,例如 “pages/index/index” ,如果不填寫這個(gè)字段,默認(rèn)跳主頁面 | |
width | Int | 430 | 二維碼的寬度 |
auto_color | Bool | false | 自動(dòng)配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調(diào) |
line_color | Object | {“r”:”0”,”g”:”0”,”b”:”0”} | auto_color 為 false 時(shí)生效,使用 rgb 設(shè)置顏色 例如 {“r”:”xxx”,”g”:”xxx”,”b”:”xxx”} |
注意:通過該接口生成的小程序碼,永久有效,數(shù)量暫無限制。用戶掃描該碼進(jìn)入小程序后,開發(fā)者需在對(duì)應(yīng)頁面獲取的碼中 scene 字段的值,再做處理邏輯。使用如下代碼可以獲取到二維碼中的 scene 字段的值。調(diào)試階段可以使用開發(fā)工具的條件編譯自定義參數(shù) scene=xxxx 進(jìn)行模擬,開發(fā)工具模擬時(shí)的 scene 的參數(shù)值需要進(jìn)行 urlencode。同時(shí)需要注意,此接口的page參數(shù)中不能帶任何參數(shù),參數(shù)都在scene 參數(shù)中處理,切記?。。?/p>
// 這是首頁的 js Page({ onLoad: function(options) { // options 中的 scene 需要使用 decodeURIComponent 才能獲取到生成二維碼時(shí)傳入的 scene var scene = decodeURIComponent(options.scene) } })
(2)請(qǐng)求接口測試
(3)java接口開發(fā)
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("調(diào)用生成微信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("調(diào)用小程序生成微信永久小程序碼URL接口返回結(jié)果:" + 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("調(diào)用小程序生成微信永久小程序碼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 獲取小程序二維碼
適用于需要的碼數(shù)量較少的業(yè)務(wù)場景
接口地址:https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
(1)POST 參數(shù)說明
參數(shù) | 類型 | 默認(rèn)值 | 說明 |
---|---|---|---|
path | String | 不能為空,最大長度 128 字節(jié) | |
width | Int | 430 | 二維碼的寬度 |
注意:通過該接口生成的小程序二維碼,永久有效,數(shù)量限制見文末說明,請(qǐng)謹(jǐn)慎使用。用戶掃描該碼進(jìn)入小程序后,將直接進(jìn)入 path 對(duì)應(yīng)的頁面。
示例:
{"path": "pages/index?query=1", "width": 430}
注:pages/index 需要在 app.json 的 pages 中定義
(2)請(qǐng)求接口測試
(3)java接口開發(fā)
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("調(diào)用生成微信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("調(diào)用小程序生成微信永久二維碼URL接口返回結(jié)果:" + 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("調(diào)用小程序生成微信永久二維碼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:通過該接口,僅能生成已發(fā)布的小程序的二維碼。
2:可以在開發(fā)者工具預(yù)覽時(shí)生成開發(fā)版的帶參二維碼。
3:接口1加上接口2,總共生成的碼數(shù)量限制為100,000,請(qǐng)謹(jǐn)慎調(diào)用。
4 : POST 參數(shù)需要轉(zhuǎn)成 json 字符串,不支持 form 表單提交。
5 : auto_color line_color 參數(shù)僅對(duì)小程序碼生效。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js中設(shè)計(jì)一段程序,讓它能夠打印自己的方法收集藏
js中設(shè)計(jì)一段程序,讓它能夠打印自己的方法收集藏...2007-03-03JavaScript Drum Kit 指南(純 JS 模擬敲鼓效果)
這篇文章主要介紹了JavaScript Drum Kit 指南,也就是純 JS 模擬敲鼓效果實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-07-07原生js jquery ajax請(qǐng)求以及jsonp的調(diào)用方法
下面小編就為大家?guī)硪黄鷍s jquery ajax請(qǐng)求以及jsonp的調(diào)用方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08uniapp實(shí)現(xiàn)單選組件覆蓋選中樣式的方法
最近使用uniapp開發(fā),有些組件渲染之后會(huì)生成一些標(biāo)簽,需要修改生成標(biāo)簽的樣式,下面通過實(shí)例代碼講解uniapp實(shí)現(xiàn)單選組件覆蓋選中樣式的方法,感興趣的朋友一起看看吧2024-03-03JavaScript中apply與call的用法意義及區(qū)別說明
JavaScript中有一個(gè)call和apply方法,其作用基本相同,但也有略微的區(qū)別。2010-04-04使用uniapp打包微信小程序時(shí)主包和vendor.js過大解決(uniCloud的插件分包)
每個(gè)使用分包小程序必定含有一個(gè)主包,所謂的主包,即放置默認(rèn)啟動(dòng)頁面/TabBar頁面,以及一些所有分包都需用到公共資源/JS 腳本,下面這篇文章主要給大家介紹了關(guān)于使用uniapp打包微信小程序時(shí)主包和vendor.js過大解決的相關(guān)資料,,需要的朋友可以參考下2023-02-02JavaScript實(shí)現(xiàn)頁面無操作倒計(jì)時(shí)退出
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)頁面無操作倒計(jì)時(shí)退出,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10