import com.alibaba.fastjson.JSONObject;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class WxQrCode {
private static final String GETWXACODEUNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s";// 生成小程序码地址
private static final String URL_GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";//获取access_token地址
private static final String APP_ID = "";// 小程序appid
private static final String APP_SECRET = ""; // 小程序秘钥
public static final String PATH_HOME = "";
private static final String BASE_PREFIX = "data:image/png;base64,"; // base64图片固定前缀
/**
* 获取微信Access_Token
* @param appid
* @param secret
* @return
*/
public static String getWeixinAccessToken(String appid, String secret){
String url = URL_GET_TOKEN;
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("grant_type", "client_credential");
paramMap.put("appid", appid);
paramMap.put("secret", secret);
String content = HttpClientUtils.executeHttpGet(url, paramMap);
Map map = (Map) JsonMapper.fromJsonString(content, HashMap.class);
return (String)map.get("access_token");
}
/**
* 生成小程序码返回base64字符串
* @param sceneStr 要携带的参数
* @param accessToken 上面方法得到的token
* @param page 要跳转的小程序页面(必须是已发布的)
* @return
*/
public static String getminiqrQr(String sceneStr, String accessToken,String page) {
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(String.format(GETWXACODEUNLIMIT_URL,accessToken));
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", sceneStr);
paramJson.put("page", page);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
try (InputStream is = httpURLConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();){
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
return BASE_PREFIX+Base64.getEncoder().encodeToString(baos.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (httpURLConnection != null){
httpURLConnection.disconnect();
}
}
return null;
}
/**
* 模拟调用
* @param args
*/
public static void main(String[] args) {
String accessToken = getWeixinAccessToken(APP_ID,APP_SECRET);
String qrBase64 = getminiqrQr("1024",accessToken,PATH_HOME);
System.out.println(qrBase64);
}
写在后面:
1.以上代码中HttpClientUtils用你自己项目中的请求工具类就好,无特殊要求;
2.以上生成代码均模拟正常情况下请求,未对一些异常进行处理,该部分自行处理;
3.正常业务中不会每次都调用微信接口获取accessToken,一般是会放进缓存,定时更新,具体根据各自业务进行修改。
请发表评论