Java中生成微信小程序太陽(yáng)碼的實(shí)現(xiàn)方案
背景
當(dāng)前小程序盛行的時(shí)代,無(wú)論走在那里都會(huì)看到各種各樣的小程序太陽(yáng)碼,小程序項(xiàng)目中經(jīng)常也會(huì)用到小程序的太陽(yáng)碼,那么我們?nèi)绾紊尚〕绦虻奶?yáng)碼呢?
實(shí)現(xiàn)方案
我們可以通過(guò)如下的方法實(shí)現(xiàn)小程序太陽(yáng)碼生成。

生成有限制太陽(yáng)碼
實(shí)現(xiàn)步驟
- 獲取小程序的access_token
- 設(shè)置path、with相關(guān)參數(shù)
- 調(diào)用getwxacodeunlimit接口,并將返回圖片存儲(chǔ)到本地
獲取小程序的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;
}說(shuō)明:調(diào)用微信API接口傳入小程序的appid和appsecret參數(shù)即可。
調(diào)用微信api生成小程序太陽(yáng)碼
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("太陽(yáng)碼[{}]生成成功", name);
}
return param.getFilePath() + File.separatorChar + name;
}說(shuō)明
參數(shù)說(shuō)明
- path:掃碼進(jìn)入的小程序頁(yè)面路徑,最大長(zhǎng)度 128 字節(jié),不能為空;例如:pages/index/index
- access_token:小程序授權(quán)token
注意事項(xiàng)
需要特殊注意,本方案生成的小程序太陽(yáng)碼與二維碼的總數(shù)不能超過(guò)10萬(wàn)個(gè),微信沒(méi)有提供對(duì)應(yīng)的Api接口查詢的使用的數(shù)量,一旦超過(guò)了數(shù)量,將會(huì)導(dǎo)致小程序失效,且微信目前無(wú)法重置查詢次數(shù),適合于生成數(shù)量少的場(chǎng)景。
生成無(wú)限制太陽(yáng)碼
獲取小程序的access_token
如同第一種的方案
調(diào)用微信api生成小程序太陽(yáng)碼
/**
* 生成無(wú)限制的小程序碼
* @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();
//太陽(yáng)碼寫標(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";//文件名加后綴,跟上面對(duì)應(yīng)
int flag = saveImg(inputStream, param.getFilePath(), name);// 保存圖片
if (flag == 0)
{
throw new SysException("保存圖片[" + name + "]失敗");
}
else
{
logger.info("太陽(yáng)碼[{}]生成成功", name);
}
return param.getFilePath() + File.separatorChar + name;
}說(shuō)明
參數(shù)說(shuō)明
- scene:最大32個(gè)可見(jiàn)字符,參數(shù)格式可以自行定義a&b或者a=1&b=2都行
- access_token:小程序授權(quán)token
參數(shù)過(guò)長(zhǎng)問(wèn)題
由于scene參數(shù)的長(zhǎng)度只支持32位字符,如果參數(shù)超過(guò)了32位,我們將如何合處理?
解決方案
改問(wèn)題的解決方案為:設(shè)計(jì)一張小程序參數(shù)表,將生成的參數(shù)存儲(chǔ)到表中,生成小程序是scene參數(shù)設(shè)置此表表的主鍵,小程序掃碼后,先請(qǐng)求后臺(tái)通過(guò)scene參數(shù)獲取小程序的具體參數(shù)。
如下示例:

擴(kuò)展功能
- 如何給生成的小程序添加標(biāo)題或者水印等
- 如何生成待小程序碼的海報(bào)
關(guān)于這些功能的實(shí)現(xiàn),如果有需要的可以隨時(shí)與我聯(lián)系。
總結(jié)
本文講解了如何生成微信小程序太陽(yáng)碼,通過(guò)微信提供的兩種方案都可以實(shí)現(xiàn),在實(shí)際的項(xiàng)目中建議采用第二種方案。
到此這篇關(guān)于Java中生成微信小程序太陽(yáng)碼的實(shí)現(xiàn)方案的文章就介紹到這了,更多相關(guān)小程序太陽(yáng)碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java實(shí)現(xiàn)KFC點(diǎn)餐小程序
- Java點(diǎn)餐小程序之黑心商人
- 用SpringBoot+Vue+uniapp小程序?qū)崿F(xiàn)在線房屋裝修管理系統(tǒng)
- Java基于IDEA實(shí)現(xiàn)qq郵件發(fā)送小程序
- 微信小程序與Java后端接口交互
- 微信小程序微信登錄的實(shí)現(xiàn)方法詳解(JAVA后臺(tái))
- 微信小程序獲取手機(jī)號(hào)的完整實(shí)例(Java后臺(tái)實(shí)現(xiàn))
- java實(shí)現(xiàn)日歷窗口小程序
- java小程序火鍋店點(diǎn)餐系統(tǒng)
相關(guān)文章
Java線程中的用戶態(tài)和內(nèi)核態(tài)解讀
這篇文章主要介紹了Java線程中的用戶態(tài)和內(nèi)核態(tài)解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
MyBatisPlus+SpringBoot實(shí)現(xiàn)樂(lè)觀鎖功能詳細(xì)流程
樂(lè)觀鎖是針對(duì)一些特定問(wèn)題的解決方案,主要解決丟失更新問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于MyBatisPlus+SpringBoot實(shí)現(xiàn)樂(lè)觀鎖功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Java關(guān)鍵字this(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
java中的this隨處可見(jiàn),用法也多。通常情況下理解this關(guān)鍵字還是很容易的,但是在我初學(xué)的時(shí)候,有一個(gè)疑問(wèn)卻一直不能很清晰的理解,現(xiàn)在慢慢的理解了,下面通過(guò)本文給大家記錄下,有需要的朋友參考下2017-03-03
深入Java7的一些新特性以及對(duì)腳本語(yǔ)言支持API的介紹
本篇文章是對(duì)Java7的一些新特性以及對(duì)腳本語(yǔ)言支持API的概述,需要的朋友參考下2013-05-05
Java項(xiàng)目如何引入日志生成器及其日志分級(jí)
這篇文章主要介紹了Java項(xiàng)目引入日志生成器及其日志分級(jí),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

