溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中如何生成微信小程序太陽(yáng)碼

發(fā)布時(shí)間:2022-06-01 09:07:37 來(lái)源:億速云 閱讀:1413 作者:zzz 欄目:開(kāi)發(fā)技術(shù)

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

    實(shí)現(xiàn)方案

    我們可以通過(guò)如下的方法實(shí)現(xiàn)小程序太陽(yáng)碼生成。

    Java中如何生成微信小程序太陽(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接口查詢(xún)的使用的數(shù)量,一旦超過(guò)了數(shù)量,將會(huì)導(dǎo)致小程序失效,且微信目前無(wú)法重置查詢(xún)次數(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)碼寫(xiě)標(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ù)。

    如下示例:

    Java中如何生成微信小程序太陽(yáng)碼

    到此,關(guān)于“Java中如何生成微信小程序太陽(yáng)碼”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI