為滿足不同需求和場景,這里提供了兩個接口,開發者可挑選適合自己的接口。
- 接口 A: 適用於需要的碼數量較少的業務場景
- 生成小程序碼,可接受 path 參數較長,生成個數受限,數量限制見 注意事項,請謹慎使用。
- 接口 B:適用於需要的碼數量極多的業務場景
- 生成小程序碼,可接受頁面參數較短,生成個數不受限。
以接口B為例:
通過該接口生成的小程序碼,永久有效,數量不限制,用戶掃描該碼進入小程序后,將直接進入 path 對應的頁面。
接口地址:https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

效果圖



QRcodeController.java
import com.alibaba.druid.util.HttpClientUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pinpinhuo.web.utils.WxUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/weixinCode")
public class QRCodeController {
/**
* 詳情看官方文檔 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
* 此處以官方接口B為例
* 生成小程序碼,可接受頁面參數較短,生成個數不受限
* param :參數 例如:123 page:需要跳轉的頁面地址 例如:pages/index
*/
@RequestMapping("/getCode")
public void smallProgramCode(String param, String page, HttpServletResponse response) {
OutputStream stream = null;
try {
//獲取AccessToken
String accessToken = getAccessToken();
//設置響應類型
response.setContentType("image/png");
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
//組裝參數
Map<String, Object> paraMap = new HashMap<>();
//二維碼攜帶參數 不超過32位 參數類型必須是字符串
paraMap.put("scene", param);
//二維碼跳轉頁面
paraMap.put("page", page);
//二維碼的寬度
paraMap.put("width", 450);
//自動配置線條顏色,如果顏色依然是黑色,則說明不建議配置主色調
paraMap.put("auto_color", false);
//是否需要透明底色, is_hyaline 為true時,生成透明底色的小程序碼
paraMap.put("is_hyaline", false);
//執行post 獲取數據流
byte[] result = WxUtil.doImgPost(url, paraMap);
//輸出圖片到頁面
response.setContentType("image/png");
stream = response.getOutputStream();
stream.write(result);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 獲取ACCESS_TOKEN
* 官方文檔地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
* <p>
* 需要正確的 appid 和 secret 查看自己的微信開放平台
*/
public String getAccessToken() {
//這里需要換成你d的小程序appid
String appid = "wx8feahukgeaer4fe9a";
//這里需要換成你d的小程序secret
String appSecret = "f02185k9zafbwrtbjyr33h7318c1tj";
//獲取微信ACCESS_TOKEN 的Url
String accent_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
String url = accent_token_url.replace("APPID", appid).replace("APPSECRET", appSecret);
//發送請求
String s = WxUtil.get(url);
Map<String, Object> resultMap = JSON.parseObject(s,new HashMap<>().getClass());
System.out.println("access_token------>" + resultMap.get("access_token").toString());
return resultMap.get("access_token").toString();
}
}
WxUtil.java
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.ConnectionPoolTimeoutException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.net.SocketTimeoutException;
import java.util.Map;
public class WxUtil {
/**
* 獲取數據流
* @param url
* @param paraMap
* @return
*/
public static byte[] doImgPost(String url, Map<String, Object> paraMap) {
byte[] result = null;
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
try {
// 設置請求的參數
JSONObject postData = new JSONObject();
for (Map.Entry<String, Object> entry : paraMap.entrySet()) {
postData.put(entry.getKey(), entry.getValue());
}
httpPost.setEntity(new StringEntity(postData.toString(), "UTF-8"));
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toByteArray(entity);
} catch (ConnectionPoolTimeoutException e) {
e.printStackTrace();
} catch (ConnectTimeoutException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return result;
}
}
原文鏈接:https://my.oschina.net/u/4606165/blog/4484782
