利用微信官方提供的api,获取accessToken,然后根据accessToken获取小程序的二维码或小程序码。
微信官方提供了获取acessToke的接口.接口地址说明.
代码:
public static String getAccessTokenByAppId(String appId) throws Exception {
LOG.info("getAccessToken, appId="+appId);
if(StringUtils.isEmpty(appId)){
throw new Exception("appId is null");
}
Map parm=new HashMap<>();
parm.put("appId",appId);
String getTokenUrl="http://api.znovpc.com/authorization/oauth2/miniprogram/getAccessToken";
String resultJson= HttpClientUtils.doHttpsGet(getTokenUrl,parm);
LOG.info("getaccesstoken result:{}",new Object[]{resultJson});
WxJsonUtil wxJsonUtil= JSONObject.parseObject(resultJson,WxJsonUtil.class);
if(!wxJsonUtil.isSuccess()){
throw new Exception("get token error");
}
String access_token=wxJsonUtil.getData();
return access_token;
}
通过accessToken获取二维码流,微信官方提供了三个二维码的接口,各有不同,可根据需要选择。接口说明。
这里选择接口A。
代码:
public static InputStream getQRCodeStreamByAccessToken(String accessToken, String path, String width) {
LOG.info("getQRCodeStreamByAccessToken, accessToken=" + accessToken + ", path="+path);
if(StringUtils.isEmpty(accessToken)){
return null;
}
if(accessToken==null){
LOG.info("getQRcode.ep, accessToken==null");
return null;
}
String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken;
Map param = new HashMap<>();
param.put("path", path);
if(StringUtils.isEmpty(width)){
//默认大小的430
width= "430";
}
param.put("width", Integer.parseInt(width));
param.put("auto_color", false);
Map 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);
String params = JSONObject.toJSONString(param);
LOG.info("调用生成微信URL接口传参:" + params);
InputStream is=getQRCodeInputStream(url,params);
return is;
}
public static InputStream getQRCodeInputStream(String strURL, String params) {
LOG.info("getQRCodeInputStream, strURL="+strURL+", params="+ params);
try {
URL url = new URL(strURL);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
connection.connect();
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
InputStream is = connection.getInputStream();
return is;
} catch (IOException e) {
LOG.error("getQRCodeInputStream() failed",e);
}
return null;
}
得到二维码的流数据后,将其转化为图片文件。
代码:
public File getFileByInputStream(InputStream is, String filePath, String fileName) {
LOG.info("getFileByInputStream, filePath="+filePath+", fileName="+ fileName);
File file=null;
if(is!= null&& !StringUtils.isEmpty(filePath)&&!StringUtils.isEmpty(fileName)){
try {
file=new File(filePath,fileName);//可以是任何图片格式.jpg,.png等
FileOutputStream fos=new FileOutputStream(file);
byte[] b = new byte[1024];
int nRead = 0;
while ((nRead = is.read(b)) != -1) {
fos.write(b, 0, nRead);
}
fos.flush();
fos.close();
} catch (IOException e) {
LOG.error("getFileByInputStream failed",e);
}
}
return file;
}