如何生成微信小程序碼(獲取微信小程序碼) - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 小程序相關 >

如何生成微信小程序碼(獲取微信小程序碼)

發表時(shí)間:2020-10-23

發布人(rén):融晨科技

浏覽次數:67

前言

在(zài)微信小程序的(de)某些業務場景中,需要(yào / yāo)用戶微信掃碼後直接進入到(dào)小程序的(de)某個(gè)頁面(有時(shí)小程序碼還需攜帶一些參數),在(zài)這(zhè)種場景下,就(jiù)需要(yào / yāo)生成小程序碼。

流程

1、獲取小程序接口調用憑證(accesstoken)

2、獲取小程序二維碼(獲取成功後,微信服務端将返回二進制字節流,因此需要(yào / yāo)創建一個(gè)文件保存圖片)

代碼案例

1、引入fastjson依賴

        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>

2、Java代碼示例

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * @description 獲取小程序二維碼
 * @author: liyinlong
 * @date 2020/10/23 10:11
 */

public class GetMiniQrcode {

    public static void main(String[] args) {
        String appid = "";
        String secret = "";

        String token = getAccessToken(appid, secret);

        //發送json請求,對象必須封裝成json格式
        JSONObject params = new JSONObject();
        params.put("scene","xiaozhugedeboke");//參數
        params.put("page","pages/verifycode/verifycode");

        //注意!一定要(yào / yāo)将對象轉成字符串
        String param = params.toJSONString();

        String file = "e://a.png";
        getQrCode("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token, param, file);

    }


    /**
     * @param url   發送請求的(de)URL
     * @param param 請求參數
     * @return 所代表遠程資源的(de)響應結果
     * @description 向指定 URL 發送POST方法的(de)請求
     * @author: liyinlong
     * @date 2020-01-05 21:00
     */
    public static String getQrCode(String url, String param,String file) {
        System.out.println("\n==============================POST請求開始==============================");
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打開和(hé / huò)URL之(zhī)間的(de)連接
            URLConnection conn = realUrl.openConnection();
            // 設置通用的(de)請求屬性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //設置Content-type 爲(wéi / wèi) application/json
            conn.addRequestProperty("Content-type", "application/json");
            // 發送POST請求必須設置如下兩行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 獲取URLConnection對象對應的(de)輸出(chū)流
            out = new PrintWriter(conn.getOutputStream());
            // 發送請求參數
            out.print(param);
            // flush輸出(chū)流的(de)緩沖
            out.flush();
            // 定義BufferedReader輸入流來(lái)讀取URL的(de)響應

            InputStream inputStream = conn.getInputStream();

            FileOutputStream outputStream = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = inputStream.read(buf, 0, 1024)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.flush();

            outputStream.close();
        } catch (Exception e) {
            System.out.println("發送 POST 請求出(chū)現異常!" + e);
            e.printStackTrace();
        }
        //使用finally塊來(lái)關閉輸出(chū)流、輸入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("url:" + url);
        System.out.println("POST請求結果:" + result);
        System.out.println("==============================POST請求結束==============================\n");
        return result;
    }

    /**
     * @author: liyinlong
     * @description 獲取小程序接口調用憑證
     * 官方文檔: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
     * 接口調用url https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
     * @date 2020/10/23 10:31
     * @param appid 小程序appid
     * @param appsecert 小程序密鑰
     * @return 小程序接口調用憑證accesstoken
     */
    public static String getAccessToken(String appid,String appsecert) {
        System.out.println("\n==============================GET請求開始==============================");

        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid
                + "&secret=" + appsecert;

        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打開和(hé / huò)URL之(zhī)間的(de)連接
            URLConnection conn = realUrl.openConnection();
            // 設置通用的(de)請求屬性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //設置Content-type 爲(wéi / wèi) application/json
            conn.addRequestProperty("Content-type", "application/json");
            // 發送POST請求必須設置如下兩行
            conn.setDoOutput(true);
            conn.setDoInput(true);

            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("發送 GET 請求出(chū)現異常!" + e);
            e.printStackTrace();
        }
        //使用finally塊來(lái)關閉輸出(chū)流、輸入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("url:" + url);
        System.out.println("GET請求結果:" + result);
        JSONObject res = JSONObject.parseObject(result);

        System.out.println("==============================GET請求結束==============================\n");
        return res.getString("access_token");
    }
}

相關案例查看更多