日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Java中如何生成微信小程序太阳码

發布時間:2023/12/15 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 Java中如何生成微信小程序太阳码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章主要介紹“Java中如何生成微信小程序太陽碼”,在日常操作中,相信很多人在Java中如何生成微信小程序太陽碼問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java中如何生成微信小程序太陽碼”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

    實現方案

    我們可以通過如下的方法實現小程序太陽碼生成。

    生成有限制太陽碼

    實現步驟

    • 獲取小程序的access_token

    • 設置path、with相關參數

    • 調用getwxacodeunlimit接口,并將返回圖片存儲到本地

    獲取小程序的access_token
    publicstaticStringgetAccessToken(Stringappid,Stringappsecret)
    {
    StringrequestUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret+"";
    StringaccessToken=null;
    try
    {
    Stringresponse=HttpClientUtil.getInstance().sendHttpsGet(
    requestUrl,null);
    JSONObjectjson=JSONObject.parseObject(response);
    accessToken=String.valueOf(json.get("access_token"));
    }
    catch(Exceptione)
    {
    logger.error("getAccessTokenerror",e);
    }
    
    returnaccessToken;
    }

    說明:調用微信API接口傳入小程序的appid和appsecret參數即可。

    調用微信api生成小程序太陽碼
    publicstaticStringgeneratLimitSunCode(WxScanCodeParamparam)throwsException
    {
    Stringtoken=param.getAccessToken();
    Map<String,String>params=newHashMap<>();
    params.put("path",param.getPath());
    params.put("width","430");
    CloseableHttpClienthttpClient=HttpClientBuilder.create().build();
    HttpPosthttpPost=newHttpPost("https://api.weixin.qq.com/wxa/getwxacode?access_token="+token);
    httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");
    Stringbody=JSON.toJSONString(params);
    StringEntityentity=newStringEntity(body);
    entity.setContentType("image/jpg");
    httpPost.setEntity(entity);
    HttpResponseresponse=httpClient.execute(httpPost);
    intstatusCode=response.getStatusLine().getStatusCode();
    if(statusCode==HttpStatus.SC_OK)
    {
    HttpEntityentity2=response.getEntity();
    if(!entity2.getContentType().getValue().equals("image/jpeg"))
    {
    Stringresult=EntityUtils.toString(entity2,"UTF-8");
    logger.error("generatesuncodeerror,httpexecuteresult:"+result);
    returnnull;
    }
    }
    else
    {
    logger.error("generatesuncodeerror,httpexecuteresult:"+statusCode);
    }
    
    InputStreaminputStream=response.getEntity().getContent();
    //保存圖片到本地
    intflag=saveImg(inputStream,param.getFilePath(),name);
    if(flag==0)
    {
    thrownewSysException("保存圖片["+name+"]失敗");
    }
    else
    {
    logger.info("太陽碼[{}]生成成功",name);
    }
    returnparam.getFilePath()+File.separatorChar+name;
    }
    說明
    參數說明
    • path:掃碼進入的小程序頁面路徑,最大長度 128 字節,不能為空;例如:pages/index/index

    • access_token:小程序授權token

    注意事項

    需要特殊注意,本方案生成的小程序太陽碼與二維碼的總數不能超過10萬個,微信沒有提供對應的Api接口查詢的使用的數量,一旦超過了數量,將會導致小程序失效,且微信目前無法重置查詢次數,適合于生成數量少的場景。

    生成無限制太陽碼

    獲取小程序的access_token

    如同第一種的方案

    調用微信api生成小程序太陽碼
    /**
    *生成無限制的小程序碼
    *@paramparam
    *@return
    *@throwsException
    */
    publicstaticStringgeneratUnlimitSunCode(WxScanCodeParamparam)throwsException
    {
    Stringtoken=param.getAccessToken();
    Map<String,String>params=newHashMap<>();
    params.put("scene",param.getScene());
    params.put("page",param.getPath());
    params.put("width","430");
    CloseableHttpClienthttpClient=HttpClientBuilder.create().build();
    HttpPosthttpPost=newHttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token);
    httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");
    Stringbody=JSON.toJSONString(params);
    StringEntityentity=newStringEntity(body);
    entity.setContentType("image/jpg");
    httpPost.setEntity(entity);
    HttpResponseresponse=httpClient.execute(httpPost);
    intstatusCode=response.getStatusLine().getStatusCode();
    if(statusCode==HttpStatus.SC_OK)
    {
    HttpEntityentity2=response.getEntity();
    if(!entity2.getContentType().getValue().equals("image/jpeg"))
    {
    Stringresult=EntityUtils.toString(entity2,"UTF-8");
    logger.error("generatesuncodeerror,httpexecuteresult:"+result);
    returnnull;
    }
    }
    else
    {
    logger.error("generatesuncodeerror,httpexecuteresult:"+statusCode);
    }
    
    InputStreaminputStream=response.getEntity().getContent();
    
    //太陽碼寫標題
    Stringcontent=param.getWriteContent();
    if(StringUtil.isNotEmpty(content)&&param.isWrite())
    {
    inputStream=ImageUtil.addImageTitle(param.getWriteContent(),inputStream,450,450);
    }
    
    Stringname=param.getFileName()+".jpg";//文件名加后綴,跟上面對應
    
    
    intflag=saveImg(inputStream,param.getFilePath(),name);//保存圖片
    if(flag==0)
    {
    thrownewSysException("保存圖片["+name+"]失敗");
    }
    else
    {
    logger.info("太陽碼[{}]生成成功",name);
    }
    returnparam.getFilePath()+File.separatorChar+name;
    }
    說明
    參數說明
    • scene:最大32個可見字符,參數格式可以自行定義a&b或者a=1&b=2都行

    • access_token:小程序授權token

    參數過長問題

    由于scene參數的長度只支持32位字符,如果參數超過了32位,我們將如何合處理?

    解決方案

    改問題的解決方案為:設計一張小程序參數表,將生成的參數存儲到表中,生成小程序是scene參數設置此表表的主鍵,小程序掃碼后,先請求后臺通過scene參數獲取小程序的具體參數。

    如下示例:

    總結

    以上是生活随笔為你收集整理的Java中如何生成微信小程序太阳码的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。