小程序指定頁面太陽碼生成

小程序指定頁面太陽碼生成

小程序指定頁面(門店)生成太陽碼,打印在門店內展示或者分享,擴展線上門店曝光率。也有客戶明確提出需要“太陽碼”(原因是“比較好看”),如需生成四四方方的二維碼請參閱指定頁面二維碼生成。故此小程序生成太陽碼總結如下:

生成太陽碼java後臺實現

二維碼獲取詳細接口信息可參照官方文檔
接口調用憑證(access_token)可參考官方文檔

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.io.InputStream;

/**
 * @program: jk
 * @description: test
 * @author: Mr.jkx
 * @create: 2019-07-01 10:34
 */
public class TestJkx {
    public static void main(String[] args) throws IOException {
        // 接口調用憑證(access_token)
        String token = "";
        JSONObject paramJson = new JSONObject();
        // scene,小程序頁面後邊跟的參數(pages/index/shopDetailed/shopDetailed?tId=503174af933),此字段長度最大32位,詳情可參閱“官方文檔”
        paramJson.put("scene", "tId=503174af933");
        // scene,小程序頁面生成出太陽碼,掃描跳轉頁面路徑
        paramJson.put("page", "pages/index/shopDetailed/shopDetailed");
        paramJson.put("width", 430);
        paramJson.put("auto_color", true);
        // 生成太陽碼微信請求路徑
        String requstPath = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";
        String url = requstPath.replace("ACCESS_TOKEN", token);
        // 請求,獲取回執數據
        HttpResponse response = WinxinUtil.doPostOfQRCode(url, paramJson.toString());
        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                InputStream instreams = resEntity.getContent();
                // 將微信回執數據生成太陽碼圖片,保存至服務器,並將地址反回前端展示
                String QRCodePath = QRFileUtil.saveToImgByInputStream(instreams);
                System.out.println("太陽碼圖片服務器地址:" + QRCodePath);
            }
        }
    }
}

請求微信獲取回執數據

post請求微信獲取太陽碼回執信息

	public static HttpResponse doPostOfQRCode(String url, String outStr) throws IOException {
        DefaultHttpClient client = new DefaultHttpClient();//獲取DefaultHttpClient請求
        HttpPost httpost = new HttpPost(url);//HttpPost將使用Get方式發送請求URL
        JSONObject jsonObject = null;
        httpost.setEntity(new StringEntity(outStr, "UTF-8"));//使用setEntity方法,將我們傳進來的參數放入請求中
        HttpResponse response = client.execute(httpost);//使用HttpResponse接收client執行httpost的結果
        return response;
    }

微信回執信息生成圖片太陽碼

根據微信回執的信息生成太陽碼並保存到服務器

	public static String saveToImgByInputStream(InputStream instreams) {
        JSONObject result = new JSONObject();
        String filePath = "C:\\uploadfiles";// 文件保存目錄
        String fileName = UUIDUtil.getUUID() + ".jpg";
        if (instreams != null) {
            try {
                File file = new File(filePath, fileName);//可以是任何圖片格式.jpg,.png等
                FileOutputStream fos = new FileOutputStream(file);
                byte[] b = new byte[1024];
                int nRead = 0;
                while ((nRead = instreams.read(b)) != -1) {
                    fos.write(b, 0, nRead);
                }
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
        result.put("qRCodeFilePath", "https://xxx/file/download?fileName="+fileName);
        return result.toJSONString();
    }

main方法測試

此太陽碼僅測試
在這裏插入圖片描述

溫馨提示

1.生成太陽碼頁面必須是已發佈小程序中存在的頁面
2.請求參數中“scene”字段長度最大爲32位,拼裝參數時請注意,UUID是不可以的了(作者項目主鍵id全部爲UUID,此模塊只能後期縮短UUID長度)
3.測試中,沒有發佈的小程序頁面或者參數長度不正確也能生成出圖片,只是圖片打不開,顯示“似乎不支持此文件格式”;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章