Java中生成微信小程序太陽碼的實(shí)現(xiàn)方案
背景
當(dāng)前小程序盛行的時代,無論走在那里都會看到各種各樣的小程序太陽碼,小程序項(xiàng)目中經(jīng)常也會用到小程序的太陽碼,那么我們?nèi)绾紊尚〕绦虻奶柎a呢?
實(shí)現(xiàn)方案
我們可以通過如下的方法實(shí)現(xiàn)小程序太陽碼生成。
生成有限制太陽碼
實(shí)現(xiàn)步驟
- 獲取小程序的access_token
- 設(shè)置path、with相關(guān)參數(shù)
- 調(diào)用getwxacodeunlimit接口,并將返回圖片存儲到本地
獲取小程序的access_token
public static String getAccessToken(String appid, String appsecret) { String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret+""; String accessToken = null; try { String response = HttpClientUtil.getInstance().sendHttpsGet( requestUrl, null); JSONObject json = JSONObject.parseObject(response); accessToken = String.valueOf(json.get("access_token")); } catch (Exception e) { logger.error("getAccessToken error", e); } return accessToken; }
說明:調(diào)用微信API接口傳入小程序的appid和appsecret參數(shù)即可。
調(diào)用微信api生成小程序太陽碼
public static String generatLimitSunCode(WxScanCodeParam param) throws Exception { String token =param.getAccessToken(); Map<String, String> params = new HashMap<>(); params.put("path", param.getPath()); params.put("width", "430"); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacode?access_token="+token); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); String body = JSON.toJSONString(params); StringEntity entity = new StringEntity(body); entity.setContentType("image/jpg"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity2 = response.getEntity(); if(!entity2.getContentType().getValue().equals("image/jpeg")) { String result = EntityUtils.toString(entity2, "UTF-8"); logger.error("generate sun code error,http execute result:" + result); return null; } } else { logger.error("generate sun code error,http execute result:" + statusCode); } InputStream inputStream = response.getEntity().getContent(); // 保存圖片到本地 int flag = saveImg(inputStream, param.getFilePath(), name); if (flag == 0) { throw new SysException("保存圖片[" + name + "]失敗"); } else { logger.info("太陽碼[{}]生成成功", name); } return param.getFilePath() + File.separatorChar + name; }
說明
參數(shù)說明
- path:掃碼進(jìn)入的小程序頁面路徑,最大長度 128 字節(jié),不能為空;例如:pages/index/index
- access_token:小程序授權(quán)token
注意事項(xiàng)
需要特殊注意,本方案生成的小程序太陽碼與二維碼的總數(shù)不能超過10萬個,微信沒有提供對應(yīng)的Api接口查詢的使用的數(shù)量,一旦超過了數(shù)量,將會導(dǎo)致小程序失效,且微信目前無法重置查詢次數(shù),適合于生成數(shù)量少的場景。
生成無限制太陽碼
獲取小程序的access_token
如同第一種的方案
調(diào)用微信api生成小程序太陽碼
/** * 生成無限制的小程序碼 * @param param * @return * @throws Exception */ public static String generatUnlimitSunCode(WxScanCodeParam param) throws Exception { String token =param.getAccessToken(); Map<String, String> params = new HashMap<>(); params.put("scene", param.getScene()); params.put("page", param.getPath()); params.put("width", "430"); CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); String body = JSON.toJSONString(params); StringEntity entity = new StringEntity(body); entity.setContentType("image/jpg"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { HttpEntity entity2 = response.getEntity(); if(!entity2.getContentType().getValue().equals("image/jpeg")) { String result = EntityUtils.toString(entity2, "UTF-8"); logger.error("generate sun code error,http execute result:" + result); return null; } } else { logger.error("generate sun code error,http execute result:" + statusCode); } InputStream inputStream = response.getEntity().getContent(); //太陽碼寫標(biāo)題 String content=param.getWriteContent(); if(StringUtil.isNotEmpty(content) && param.isWrite()) { inputStream = ImageUtil.addImageTitle(param.getWriteContent(), inputStream, 450, 450); } String name = param.getFileName()+".jpg";//文件名加后綴,跟上面對應(yīng) int flag = saveImg(inputStream, param.getFilePath(), name);// 保存圖片 if (flag == 0) { throw new SysException("保存圖片[" + name + "]失敗"); } else { logger.info("太陽碼[{}]生成成功", name); } return param.getFilePath() + File.separatorChar + name; }
說明
參數(shù)說明
- scene:最大32個可見字符,參數(shù)格式可以自行定義a&b或者a=1&b=2都行
- access_token:小程序授權(quán)token
參數(shù)過長問題
由于scene參數(shù)的長度只支持32位字符,如果參數(shù)超過了32位,我們將如何合處理?
解決方案
改問題的解決方案為:設(shè)計(jì)一張小程序參數(shù)表,將生成的參數(shù)存儲到表中,生成小程序是scene參數(shù)設(shè)置此表表的主鍵,小程序掃碼后,先請求后臺通過scene參數(shù)獲取小程序的具體參數(shù)。
如下示例:
擴(kuò)展功能
- 如何給生成的小程序添加標(biāo)題或者水印等
- 如何生成待小程序碼的海報(bào)
關(guān)于這些功能的實(shí)現(xiàn),如果有需要的可以隨時與我聯(lián)系。
總結(jié)
本文講解了如何生成微信小程序太陽碼,通過微信提供的兩種方案都可以實(shí)現(xiàn),在實(shí)際的項(xiàng)目中建議采用第二種方案。
到此這篇關(guān)于Java中生成微信小程序太陽碼的實(shí)現(xiàn)方案的文章就介紹到這了,更多相關(guān)小程序太陽碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java線程中的用戶態(tài)和內(nèi)核態(tài)解讀
這篇文章主要介紹了Java線程中的用戶態(tài)和內(nèi)核態(tài)解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06MyBatisPlus+SpringBoot實(shí)現(xiàn)樂觀鎖功能詳細(xì)流程
樂觀鎖是針對一些特定問題的解決方案,主要解決丟失更新問題,下面這篇文章主要給大家介紹了關(guān)于MyBatisPlus+SpringBoot實(shí)現(xiàn)樂觀鎖功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03Java關(guān)鍵字this(動力節(jié)點(diǎn)Java學(xué)院整理)
java中的this隨處可見,用法也多。通常情況下理解this關(guān)鍵字還是很容易的,但是在我初學(xué)的時候,有一個疑問卻一直不能很清晰的理解,現(xiàn)在慢慢的理解了,下面通過本文給大家記錄下,有需要的朋友參考下2017-03-03