目錄
????????調(diào)用方式
????????主要的請求參數(shù)
????????工具類
????????二維碼轉(zhuǎn)圖片
????????獲取accessToken
????????HTTPS 調(diào)用:
????????請求參數(shù)
????????代碼
????????實現(xiàn)類
調(diào)用方式
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
主要的請求參數(shù)
屬性 |
類型 |
必填 |
說明 |
access_token |
string |
是 |
接口調(diào)用憑證 |
scene |
string |
是 |
最大32個可見字符,只支持?jǐn)?shù)字,大小寫英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~ |
page |
string |
否 |
默認(rèn)是主頁,例如 pages/index/index,根路徑前不要填加 /,不能攜帶參數(shù) |
env_version |
string |
否 |
要打開的小程序版本。正式版為 "release",體驗版為 "trial",開發(fā)版為 "develop"。默認(rèn)是正式版。 |
is_hyaline |
bool |
否 |
默認(rèn)是false,是否需要透明底色,為 true 時,生成透明底色的小程序 |
工具類
二維碼轉(zhuǎn)圖片
import java.io.*;
public class WxaQrCodeUtil {
/**
* 二維碼保存到本地
* @param bytes
* @param path 保存到本地的路徑
*/
public static void saveQrCodeToLocal(byte[] bytes, String path) {
try {
InputStream inputStream = new ByteArrayInputStream(bytes);
//文件夾不存在則自動創(chuàng)建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
FileOutputStream out = new FileOutputStream(path);
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
inputStream.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
獲取accessToken
HTTPS 調(diào)用:
GET https://api.weixin.qq.com/cgi-bin/token
請求參數(shù)
屬性 |
類型 |
必填 |
說明 |
grant_type |
string |
是 |
填寫 client_credential |
appid |
string |
是 |
小程序唯一憑證,可在「微信公眾平臺 - 設(shè)置 - 開發(fā)設(shè)置」頁中獲得。 |
secret |
string |
是 |
小程序唯一憑證密鑰,即 AppSecret,獲取方式同 appid |
代碼
private static final Map<String, Object> objects = new HashMap<>();
public JSONObject getToken(){
JSONObject accessTokenRes = new JSONObject();
String accessToken = null;
try {
String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", APPID, SECRET);
HttpResponse execute = HttpRequest.get(token_url).execute();
String responseBody = execute.body();
if (responseBody == null) {
logger.info(messageUtil.getMessage("msg.get.token.fail"));
return null;
}
JSONObject token = JSON.parseObject(responseBody);
accessToken = token.getString("access_token");
if (StringUtils.isEmpty(accessToken)) {
logger.info(messageUtil.getMessage("msg.get.token.fail"));
return null;
}
accessTokenRes.put("access_token",accessToken);
accessTokenRes.put("date_time",new Date().getTime());
} catch (Exception e) {
logger.error(messageUtil.getMessage("msg.get.token.fail"));
logger.error(e.getMessage());
}
return accessTokenRes;
}
access_token 的有效期目前為 2 個小時,需定時刷新。文章來源:http://www.zghlxwxcb.cn/news/detail-761822.html
實現(xiàn)類
@ApiOperation(value = "生成二維碼", httpMethod = "GET")
@RequestMapping(value = "/api/qrcode", method = RequestMethod.GET)
@Transactional
public WxaQrCodeUrlResponse getQrCode(HttpServletRequest request) throws Exception {
WxaQrCodeUrlResponse res = new WxaQrCodeUrlResponse();
Date now = new Date();
// 獲取accessToken
String accessToken = null;
if(null != objects.get("date_time")){
accessToken = String.valueOf(objects.get("access_token"));
}
Long date = 0L;
if(null != objects.get("date_time")) {
date = Long.valueOf(String.valueOf(objects.get("date_time")));
}
if(null == accessToken || now.getTime()-date >= 2*60*60*1000){
JSONObject accessTokenRes = getToken();
objects.put("access_token",accessTokenRes.get("access_token"));
objects.put("date_time",now.getTime());
accessToken = String.valueOf(objects.get("access_token"));
}
if (StringUtils.isEmpty(accessToken)) {
res.setMessage("獲取token失敗");
res.setCode(Constants.SC_MSG);
return res;
}
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
// 請求參數(shù)
Map<String,Object> body = new HashMap<>();
body.put("scene", "自定義參數(shù)");
body.put("env_version", "release");
// 默認(rèn)是false,是否需要透明底色,為 true 時,生成透明底色的小程序
body.put("is_hyaline", false);
byte[] bytes = HttpClientUtils.getWechatQrcodeByHttpClient(url, body);
String fileName = "wxqrcode.png";
String path = "文件夾路徑" + File.separator + fileName;
WxaCodeUtil.saveQrCodeToLocal(bytes, path);
// TODO 圖片處理,返回圖片
// 刪除本地文件
new File(path).delete();
res.setCode(Constants.SC_OK);
return res;
}
實際遇到的問題:采用透明底色生成的二維碼,在微信中按壓圖片,不能識別小程序文章來源地址http://www.zghlxwxcb.cn/news/detail-761822.html
到了這里,關(guān)于【Java】微信小程序二維碼(后臺,附獲取accessToken)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!