小程序模板網(wǎng)

微信小程序--java接口開(kāi)發(fā)獲取小程序碼和二維碼

發(fā)布時(shí)間:2017-12-29 18:08 所屬欄目:小程序開(kāi)發(fā)教程

微信小程序API文檔:獲取二維碼

一、簡(jiǎn)介

通過(guò)后臺(tái)接口可以獲取小程序任意頁(yè)面的二維碼,掃描該二維碼可以直接進(jìn)入小程序?qū)?yīng)的頁(yè)面。目前微信支持兩種二維碼,小程序碼(左),小程序二維碼(右),如下所示: 
這里寫圖片描述

二、獲取小程序碼

目前有兩個(gè)接口可以生成小程序碼,開(kāi)發(fā)者可以根據(jù)自己的需要選擇合適的接口。

1 不帶參數(shù)有限個(gè)數(shù)小程序碼接口

適用于需要的碼數(shù)量較少的業(yè)務(wù)場(chǎng)景

接口地址:

https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

注:獲取accesstoken的方法跟微信公眾獲取accesstoken方法一致,不過(guò)小程序獲取accesstoken需要小程序的appid和appsercet。登錄 https://mp.weixin.qq.com ,就可以在網(wǎng)站的“設(shè)置”-“開(kāi)發(fā)者設(shè)置”中,查看到微信小程序的 AppID 了,注意不可直接使用服務(wù)號(hào)或訂閱號(hào)的 AppID 。  這里寫圖片描述  獲取微信小程序的 AppID文章地址:小程序簡(jiǎn)易教程

(1)POST 參數(shù)說(shuō)明

參數(shù) 類型 默認(rèn)值 說(shuō)明
path String   不能為空,最大長(zhǎng)度 128 字節(jié)
width Int 430 二維碼的寬度
auto_color Bool false 自動(dòng)配置線條顏色,如果顏色依然是黑色,則說(shuō)明不建議配置主色調(diào)
line_color Object {“r”:”0”,”g”:”0”,”b”:”0”} auth_color 為 false 時(shí)生效,使用 rgb 設(shè)置顏色 例如 {“r”:”xxx”,”g”:”xxx”,”b”:”xxx”}

注意:通過(guò)該接口生成的小程序碼,永久有效,但數(shù)量有效,請(qǐng)謹(jǐn)慎使用。用戶掃描該碼進(jìn)入小程序后,將直接進(jìn)入 path 對(duì)應(yīng)的頁(yè)面。

(2)請(qǐng)求接口測(cè)試

使用http請(qǐng)求插件postman或者RESTClient請(qǐng)求測(cè)試。  這里寫圖片描述  請(qǐng)求測(cè)試結(jié)果返回一個(gè)小程序碼圖片,與微信公眾平臺(tái)生成二維碼不同,小程序碼直接返回文件流,不是微信公眾平臺(tái)的url和ticket。

(3)java接口開(kāi)發(fā)

注:此接口是基于Spring RestTemplate進(jìn)行http請(qǐng)求,進(jìn)行http請(qǐng)求有很多方法和工具類,可自行百度或參考下面的參考文章。接口只是提供一個(gè)解決方法的思路。

    public Map getminiqrQr(String accessToken) {
        RestTemplate rest = new RestTemplate();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken;
            Map<String,Object> param = new HashMap<>();
            param.put("page", "pages/index/index");
            param.put("width", 430);
            param.put("auto_color", false);
            Map<String,Object> line_color = new HashMap<>();
            line_color.put("r", 0);
            line_color.put("g", 0);
            line_color.put("b", 0);
            param.put("line_color", line_color);
            LOG.info("調(diào)用生成微信URL接口傳參:" + param);
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            HttpEntity requestEntity = new HttpEntity(param, headers);
            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
            LOG.info("調(diào)用小程序生成微信永久小程序碼URL接口返回結(jié)果:" + entity.getBody());
            byte[] result = entity.getBody();
            LOG.info(Base64.encodeBase64String(result));
            inputStream = new ByteArrayInputStream(result);

            File file = new File("C:/Users/wangqiulin/Desktop/1.png");
            if (!file.exists()){
                file.createNewFile();
            }
            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();
        } catch (Exception e) {
            LOG.error("調(diào)用小程序生成微信永久小程序碼URL接口異常",e);
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;    
    }

注:pages/index 需要在 app.json 的 pages 中定義

(2)請(qǐng)求接口測(cè)試

這里寫圖片描述

(3)java接口開(kāi)發(fā)

    public Map getminiqrQr(String accessToken) {
        RestTemplate rest = new RestTemplate();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            String url = "https://api.weixin.qq.com/wxaapp/createwxaqrcode?access_token="+accessToken;
            Map<String,Object> param = new HashMap<>();
            param.put("page", "pages/index/index");
            param.put("width", 430);
            LOG.info("調(diào)用生成微信URL接口傳參:" + param);
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            HttpEntity requestEntity = new HttpEntity(param, headers);
            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
            LOG.info("調(diào)用小程序生成微信永久二維碼URL接口返回結(jié)果:" + entity.getBody());
            byte[] result = entity.getBody();
            LOG.info(Base64.encodeBase64String(result));
            inputStream = new ByteArrayInputStream(result);

            File file = new File("C:/Users/wangqiulin/Desktop/1.png");
            if (!file.exists()){
                file.createNewFile();
            }
            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();
        } catch (Exception e) {
            LOG.error("調(diào)用小程序生成微信永久二維碼URL接口異常",e);
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;    
    }

三、說(shuō)明

1:通過(guò)該接口,僅能生成已發(fā)布的小程序的二維碼。  2:可以在開(kāi)發(fā)者工具預(yù)覽時(shí)生成開(kāi)發(fā)版的帶參二維碼。  3:接口1加上接口2,總共生成的碼數(shù)量限制為100,000,請(qǐng)謹(jǐn)慎調(diào)用。  4 : POST 參數(shù)需要轉(zhuǎn)成 json 字符串,不支持 form 表單提交。  5 : auto_color line_color 參數(shù)僅對(duì)小程序碼生效。


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://m.u-renovate.com/wxmini/doc/course/18310.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×