获取小程序二维码

一、获取token值
public static Map<String, Object> getToken() {
Map<String, Object> data = new HashMap<String, Object>();
try {
StringBuilder urlSb = new StringBuilder();
urlSb.append(“https://api.weixin.qq.com/cgi-bin/token”);
urlSb.append("?");
urlSb.append(“grant_type=%s&appid=%s&secret=%s”);
String tokenUrl = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序值&secret=小程序的值”;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(tokenUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String resultText = EntityUtils.toString(httpEntity, “utf-8”);
@SuppressWarnings(“unchecked”)
Map<Object, Object> resultMap = JSON.parseObject(resultText, Map.class);
if (resultMap != null) {
if (resultMap != null && resultMap.size() > 0) {
// 合并2次调用结果
Set keySet = resultMap.keySet();
for (Object key : keySet) {
if (!data.containsKey(key)) {
data.put((String) key, resultMap.get(key));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}

二、调用token并绑定token同时设置基础参数
public static String StringUtil(Long id){
Map<String, Object> date= getToken();
String url=“https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+date.get("access_token”);
HashMap<String, String> params = new HashMap<>();
params.put(“scene”, “id=”+id+"");
params.put(“page”, “pages/goods/detail/index”);
JSONObject json=JSONObject.fromObject(params);
String val = “”;
Random random = new Random();
for (int i = 0; i < 5; i++) {
val += String.valueOf(random.nextInt(10));
}
wxPost(url,json,val);
return GetImageStr(“d:/”+ val + “.png”);
}
参数scene为添加,page为小程序路径
三、写入图片

//第二步写入图片
public static void wxPost(String uri, JSONObject paramJson, String fileName) {
try {
URL url = new URL(uri);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(“POST”);// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
// 开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
File file=new File(“d:/”+ fileName + “.png”);// /Users/shenzhuan/logs/
OutputStream os = new FileOutputStream(file);
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
bis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

四、将图片转换为base64
public static String GetImageStr(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}

注意:生成的图片为1kb是,是小程序返回的错误
查看方式:删除后缀.png然后用开发工具打开会显示错误状态码,根据状态码查询具体错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值