實作方案
我們可以透過以下的方法實作小程式太陽碼生成。
產生有限制太陽碼
實作步驟
取得小程式的access_token
設定path、with相關參數
呼叫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; }
說明:呼叫微信API介面傳入小程式的appid和appsecret參數即可。
呼叫微信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; }
說明
#參數說明
path:掃碼進入的小程序頁面路徑,最大長度128 位元組,不能為空;例如:pages/index/index
#access_token:小程式授權token
注意事項
需要特殊注意,本方案產生的小程式太陽碼與二維碼的總數不能超過10萬個,微信沒有提供對應的Api介面查詢的使用的數量,一旦超過了數量,將會導緻小程式失效,且微信目前無法重置查詢次數,適合於產生數量少的場景。
產生無限制太陽碼
取得小程式的access_token
#如同第一種的方案
呼叫微信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(); //太阳码写标题 String content=param.getWriteContent(); if(StringUtil.isNotEmpty(content) && param.isWrite()) { inputStream = ImageUtil.addImageTitle(param.getWriteContent(), inputStream, 450, 450); } String name = param.getFileName()+".jpg";//文件名加后缀,跟上面对应 int flag = saveImg(inputStream, param.getFilePath(), name);// 保存图片 if (flag == 0) { throw new SysException("保存图片[" + name + "]失败"); } else { logger.info("太阳码[{}]生成成功", name); } return param.getFilePath() + File.separatorChar + name; }
說明
參數說明
scene:最大32個可見字元,參數格式可以自行定義a&b或a=1&b=2都行
access_token:小程式授權token
#參數過長問題
由於scene參數的長度只支援32位元字符,如果參數超過了32位,我們將如何合處理?
解決方案
改變問題的解決方案為:設計一張小程式參數表,將產生的參數儲存到表中,產生小程式是scene參數設定此表表的主鍵,小程式掃碼後,先請求後台透過scene參數取得小程式的具體參數。
如下範例:
以上是Java中如何產生微信小程式太陽碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在IDEA中連接Oracle數據庫時出現數字溢出錯誤的處理方法當我們在使用IntelliJ...

在研究MyBatis框架時,開發者們常常會遇到關於註解的各種問題,其中一個常見的問題是如何正確使用@ResultType注...

Springboot項目多數據源配置下的數據庫訪問性能問題排查本文針對一個Springboot項目中使用Atomikos進行多數據源配�...

Java項目打包成可執行JAR文件時遭遇NoClassDefFoundError難題很多Java開發者在將項目打包成可執行JAR文件時,可能會�...

關於IntelliJIDEA破解的分析方法在編程界,IntelliJ...

問題介紹:視頻質量提升是視頻處理中的一個重要環節,尤其是在處理低清晰度的視頻時,如何利用Java語言和�...

在處理SpringBoot應用中,我們經常會遇到如何正確接收請求參數的問題。特別是當參數格式不是常見的JSON時,更�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具