Java中生成微信小程序太陽碼的實現(xiàn)方案
背景
當前小程序盛行的時代,無論走在那里都會看到各種各樣的小程序太陽碼,小程序項目中經常也會用到小程序的太陽碼,那么我們如何生成小程序的太陽碼呢?
實現(xiàn)方案
我們可以通過如下的方法實現(xiàn)小程序太陽碼生成。

生成有限制太陽碼
實現(xiàn)步驟
- 獲取小程序的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 字節(jié),不能為空;例如: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參數獲取小程序的具體參數。
如下示例:

擴展功能
- 如何給生成的小程序添加標題或者水印等
- 如何生成待小程序碼的海報
關于這些功能的實現(xiàn),如果有需要的可以隨時與我聯(lián)系。
總結
本文講解了如何生成微信小程序太陽碼,通過微信提供的兩種方案都可以實現(xiàn),在實際的項目中建議采用第二種方案。
到此這篇關于Java中生成微信小程序太陽碼的實現(xiàn)方案的文章就介紹到這了,更多相關小程序太陽碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot Test 多線程報錯的根本原因(dataSource already
在使用Springboot test進行相關測試的時候,發(fā)現(xiàn)開啟線程操作數據庫的時候異常,這篇文章主要介紹了SpringBoot Test 多線程報錯:dataSource already closed的根本原因及解決方法,需要的朋友可以參考下2022-06-06

