采用的是wxacode.createQRCode擷取二維碼,此二維碼有調用次數限制(100000次)
微信小程式官方文檔位址:createQRCode
1.第一步擷取accessToken
/**
* @return access_token
* @throws Exception
*/
public static String getAccessToken() throws Exception {
String apiKey = "你的小程式apiKey";//小程式apiKey
String secretKey = "你的小程式secretKey";//小程式密鑰
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + apiKey + "&secret=" + secretKey;
URL url = new URL(requestUrl);
// 打開和URL之間的連接配接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 設定通用的請求屬性
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到請求的輸出流對象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes("");
out.flush();
out.close();
// 建立實際的連接配接
connection.connect();
// 定義 BufferedReader輸入流來讀取URL的響應
BufferedReader in = null;
if (requestUrl.contains("nlp")) {
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
} else {
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
}
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
JSONObject jsonObject = JSON.parseObject(result);
String accesstoken = jsonObject.getString("access_token");
return accesstoken;
}
2.擷取小程式二維碼到本地
/**
* 生成帶參小程式二維碼(輸出到本地)
*
* @param machineNo 機器編号
* @param accessToken token
*/
public static Map<String, Object> getMiniqrQrToLocal(String machineNo, String accessToken) {
Map<String, Object> retMap = null;
try {
URL url = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + accessToken);
// URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// 發送POST請求必須設定如下兩行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");// 送出模式
httpURLConnection.setRequestProperty("Content-Type", "application/x-javascript; charset=UTF-8");
// 擷取URLConnection對象對應的輸出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 發送請求參數
JSONObject paramJson = new JSONObject();
// 掃碼進入的小程式頁面路徑,最大長度 128 位元組,不能為空;對于小遊戲,可以隻傳入 query 部分,來實作傳參效果,如:傳入 "?foo=bar",即可在 wx.getLaunchOptionsSync 接口中的 query 參數擷取到 {foo:"bar"}
paramJson.put("path", "pages/index/index?machineNo="+machineNo);//傳入機器編碼
paramJson.put("width", 430);
printWriter.write(paramJson.toString());
// flush輸出流的緩沖
printWriter.flush();
//開始擷取資料
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
/*ByteArrayOutputStream os = new ByteArrayOutputStream();*/
OutputStream os = new FileOutputStream(new File("/Desktop/image/wechatImage.jpg"));//本機位置
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return retMap;
}
3.測試使用
public static void main(String[] args) throws Exception {
String accessToken = accessToken();
System.out.println("擷取的accessToken:" + accessToken);
//擷取二維碼
getMiniqrQrToLocal("123",accessToken);
}
4.小程式端接收參數
在page/index/index.js頁面中的onload函數接收即可
onLoad: function(e) {
console.log("接收的機器參數為:"+e.machineNo);
}