1. 程式人生 > >微信小程式帶參二維碼

微信小程式帶參二維碼

需求:生成小程式中的海報,需要小程式二維碼可以使使用者儲存到本地在朋友圈分享

生成二維碼工具類程式碼如下:

  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的,圖片儲存地址待處理)