微信小程序带参二维码

需求:生成小程序中的海报,需要小程序二维码可以使用户保存到本地在朋友圈分享

生成二维码工具类代码如下:

  1 package com.aone.foottalk.action.wx.util;
  2 
  3 import java.io.File;
  4 import java.io.FileOutputStream;
  5 //import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.util.HashMap;
  8 import java.util.Map;
  9 
 10 //import org.apache.commons.io.IOUtils;
 11 import org.apache.http.HttpResponse;
 12 import org.apache.http.client.methods.HttpPost;
 13 import org.apache.http.entity.StringEntity;
 14 import org.apache.http.impl.client.CloseableHttpClient;
 15 import org.apache.http.impl.client.HttpClientBuilder;
 16 import org.apache.http.protocol.HTTP;
 17  
 18 import com.alibaba.fastjson.JSON;
 19 //import com.aone.foottalk.action.back.image.ImgTAction;
 20 //import com.aone.foottalk.common.QiniuUtil;
 21 
 22 import net.sf.json.JSONObject;
 23 /**
 24  * 二维码工具
 25  * @author 开发
 26  *
 27  */
 28 public class AUtil {
 29     
 30     public static String getminiqrQr(String neirong,String tzdz) throws Exception{
 31         String dizhi="";//返回给前端的地址
 32         /*************获取小程序token值*******************/
 33         String access_token ="";
 34         // 小程序(商城)
 35         String appId = "***************";
 36         String secret = "***************";
 37         String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
 38         JSONObject json = CommonUtil.httpsRequest(url, "GET", null);
 39         if (null != json) {
 40             access_token=json.getString("access_token");
 41         }
 42         System.out.println("token"+access_token);
 43         /********************************/
 44         Map<String, Object> params = new HashMap<>();
 45         params.put("scene", neirong);
 46         params.put("page", "pages"+"/"+tzdz+"/"+tzdz);
 47         params.put("width", 430);
 48         
 49         CloseableHttpClient  httpClient = HttpClientBuilder.create().build();
 50         HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
 51         httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
 52         String body = JSON.toJSONString(params);
 53         StringEntity entity;
 54         entity = new StringEntity(body);
 55         entity.setContentType("image/png");
 56         httpPost.setEntity(entity);
 57         
 58         HttpResponse response;
 59         response = httpClient.execute(httpPost);
 60         InputStream inputStream = response.getEntity().getContent();
 61         /**===========================上传七牛==================================**/
 62         /*try {
 63             byte[] data = IOUtils.toByteArray(inputStream);
 64             Map<String, String> map = QiniuUtil.upload(data, neirong);
 65             String code = map.get("code");
 66             if ("200".equals(code)) {
 67                 System.out.println(map.get("path"));
 68                 dizhi=map.get("path");
 69                 System.out.println("地址:"+dizhi);
 70             }
 71         } catch (IOException ex) {
 72             System.out.println(ImgTAction.class.getName() + "has thrown an exception: " + ex.getMessage());
 73         } finally {
 74             try {
 75                 inputStream.close();
 76             } catch (IOException ignored) {
 77 
 78             }
 79         }*/
 80         /**============================保存到本地=================================**/  
 81         File targetFile = new File("/photo");  
 82         if(!targetFile.exists()){    
 83             targetFile.mkdirs();    
 84         }       
 85         FileOutputStream out = new FileOutputStream("/888.png");
 86 
 87         byte[] buffer = new byte[8192];
 88         int bytesRead = 0;
 89         while((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
 90             out.write(buffer, 0, bytesRead);
 91         }
 92         out.flush();
 93         out.close();
 94         return dizhi;
 95     }
 96     
 97     public static void main(String args[]){
 98         try {
 99             getminiqrQr("333","index");
100         } catch (Exception e) {
101             // TODO Auto-generated catch block
102             e.printStackTrace();
103         }
104     }
105 }
View Code

待完善

(目前根据已发布小程序的appId与secret所获取的token可以下载到带参的二维码图片,但是没发布的没法得到小程序二维码图片)

(小程序下载二维码需要https的图片地址,但是七牛目前是http的,图片保存地址待处理)

猜你喜欢

转载自www.cnblogs.com/LJing21/p/10191984.html