获取小程序码 数量有限制。
获取小程序二维码 数量有限制。
生成小程序码 可以使用扫一扫直接跳转小程序 打开指定页面 本且携带参数。
@Transactional public void add(Sales sales, String webPath) { salesDao.save(sales); //生成小程序 二维码和pdf+qr String scene = sales.getId() + ""; JSONObject token = (JSONObject) servletContext.getAttribute("token"); String base64String = WeiXinBufferToImage.bufferToBase64(token.getString("access_token"), PropertiesUtil.xcx_qr_path, PropertiesUtil.xcx_qr_width, scene); String uploadFile = "/static/xcx_qr/" + DateUtil.formatDate(new Date(), "yyyyMMdd") + "/"; String fileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS") + ".jpg"; //调用产生文件夹的方法 FileUtil.makeDirs(webPath + uploadFile); boolean flag = Base64Util.GenerateImage(base64String, webPath + uploadFile + fileName); sales.setXcxQr(uploadFile + fileName); System.out.println(DateUtil.formatDate(new Date(), "yyyyMMdd HHmmssSSS") + "获取的小程序 二维码是:" + sales.getXcxQr()); //生成小程序 二维码
xcx_qr_path = pages/sales_view/sales_view xcx_qr_width = 50
package com.java456.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import org.apache.commons.codec.binary.Base64; import net.sf.json.JSONObject; public class WeiXinBufferToImage { /** * path : pages/profile/profile * width 300 * scene 填55(这个55是订单的id )String scene = sales.getId() + ""; * token 微信的token **/ public static String bufferToBase64(String token,String page,Integer width ,String scene) { try { PrintWriter out = null; //BufferedReader in = null; InputStream in = null; String result = ""; try { URL realUrl = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 JSONObject jsonObject = new JSONObject(); jsonObject.put("scene", scene); //参数自定义 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符: jsonObject.put("page",page);//要生成小程序码的链接 jsonObject.put("width",width); out.print(jsonObject); // flush输出流的缓冲 out.flush(); in = conn.getInputStream(); //获取流返回的错误信息 /* String s; StringBuffer sb = new StringBuffer(); BufferedReader bf_in = new BufferedReader(new InputStreamReader(in, "UTF-8")); while ((s = bf_in.readLine()) != null) { sb.append(s); } System.out.println(sb.toString()); /* int len=0; byte[] buffer=new byte[1024]; ByteArrayOutputStream bos=new ByteArrayOutputStream(); while((len=in.read(buffer))>0){ bos.write(buffer, 0, len); } System.out.println(new String(bos.toByteArray())); */ //获取流返回的错误信息 byte[] data = null; // 读取图片字节数组 try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = in.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } data = swapStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return new String(Base64.encodeBase64(data)); } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } // resultObj.put("result",result); return result; } catch (Exception e) { e.printStackTrace(); return null; } } }
package java456.com.utils; import org.apache.tomcat.util.codec.binary.Base64; import org.thymeleaf.util.StringUtils; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; public class Base64Util { // 图片转化成base64字符串 public static String encodeToString(String imagePath) throws IOException { String type = StringUtils.substring(imagePath, imagePath.lastIndexOf(".") + 1); BufferedImage image = ImageIO.read(new File(imagePath)); String imageString = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray(); //BASE64Encoder encoder = new BASE64Encoder(); // imageString = encoder.encode(imageBytes); imageString = Base64.encodeBase64String(imageBytes); bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; } // 图片转化成base64字符串 这个方法有误 。不能使用 public static String GetImageStr(String imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 InputStream in = null; byte[] data = null; // 读取图片字节数组 try { in = new FileInputStream(imageUrl); 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编码过的字节数组字符串 return Base64.encodeBase64String(data); } // base64字符串转化成图片 /** * * @param base64 这个base64 * webPath + 图片路径。 * @param imgSrc 保存图片的位置 D:/df123/static/upload_image/lunbo_cover/123.jpg * @return */ public static boolean GenerateImage(String base64,String imgSrc) { // 对字节数组字符串进行Base64解码并生成图片 if (base64 == null) // 图像数据为空 return false; //BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 //byte[] b = decoder.decodeBuffer(base64); byte[] b = Base64.decodeBase64(base64); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 调整异常数据 b[i] += 256; } } // 生成jpeg图片 String imgFilePath = imgSrc;// 新生成的图片d://222.jpg OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }
onLoad: function (options) { var scene = options.scene; if (scene == null) { console.log("scene没有获取到,目前是,null"); /** 这是开发模式这样弄,正式模式这个将删除。 */ scene = 1713; this.load_sales_data(scene); /** 这是开发模式这样弄,正式模式这个将删除。 */ } else { this.load_sales_data(scene); } },
这个暂时不知道。 唯一可以做的就是, scene 拼接2个参数。
关于微信小程序getwxacodeunlimit接口生成二维码的限制问题,根据历史附件信息和微信官方文档,整理如下关键信息: 一、接口基本限制 调用频率限制: 单个小程序每日调用上限为100,000次 高频调用需提前申请扩容 参数限制: scene参数最大32个字符 page路径需已发布且存在 width限制为280px-1280px
且这个数量还有限制。
@RequestMapping({"/erweima" }) @ResponseBody public Map<String, String> get1( HttpServletRequest request) { Map<String, String> versions = new HashMap<>(); String webPath = request.getServletContext().getRealPath(""); //生成一个小程序二维码 String xcx_qr_path = "pages/blog_view/blog_view?id=6"; Integer xcx_qr_width = 300; JSONObject token = (JSONObject) servletContext.getAttribute("token"); String base64String = WeiXinBufferToImage.bufferToBase642(token.getString("access_token"), xcx_qr_path, xcx_qr_width); String uploadFile = "/static/xcx_erweima/" + DateUtil.formatDate(new Date(), "yyyyMMdd") + "/"; String fileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS") + ".jpg"; //调用产生文件夹的方法 FileUtil.makeDirs(webPath + uploadFile); Base64Util.GenerateImage(base64String, webPath + uploadFile + fileName); versions.put("page",uploadFile + fileName); return versions; }
/** * * 获取 小程序 方形二维码 **/ public static String bufferToBase642(String token,String page,Integer width ) { try { PrintWriter out = null; //BufferedReader in = null; InputStream in = null; String result = ""; try { URL realUrl = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+token); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 JSONObject jsonObject = new JSONObject(); jsonObject.put("path",page);//要生成小程序码的链接 jsonObject.put("width",width); out.print(jsonObject); // flush输出流的缓冲 out.flush(); in = conn.getInputStream(); //获取流返回的错误信息 // String s; // StringBuffer sb = new StringBuffer(); // BufferedReader bf_in = new BufferedReader(new InputStreamReader(in, "UTF-8")); // while ((s = bf_in.readLine()) != null) { // sb.append(s); // } // System.out.println(sb.toString()); // int len=0; // byte[] buffer=new byte[1024]; // ByteArrayOutputStream bos=new ByteArrayOutputStream(); // while((len=in.read(buffer))>0){ // bos.write(buffer, 0, len); // } // System.out.println(new String(bos.toByteArray())); //获取流返回的错误信息 byte[] data = null; // 读取图片字节数组 try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = in.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } data = swapStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return new String(Base64.encodeBase64(data)); } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } // resultObj.put("result",result); return result; } catch (Exception e) { e.printStackTrace(); return null; } }
pages/blog_view/blog_view?id=6 options.id
站长微信:xiaomao0055
站长QQ:14496453