赞
踩
生成二维码图片,拿到图片路径
@Value("${xcx.AppletAppId}") private String appId; @Value("${xcx.AppletAppSecret}") private String appSecret; @Value("${file.uploadFolder}") private String uploadFolder; @Value("${file.url}") private String url; @GetMapping("/shareQr") @ApiOperation("自评分享码") public AjaxResult shareQr( @RequestParam("id") String id ) throws Exception { if (id == null ){ return AjaxResult.Error("数据异常。"); } String scene = "id=" + id ; // 替换成自己的参数值 String name = id; // 替换成自己的页面路径 String page = "pages/choose/detail/index?"+scene; // 替换成自己的页面路径 String now = DateUtils.format(new Date(), "yyyy-MM-dd"); String filePath = uploadFolder +"pcQr" + File.separator + now + "-" + name+".png"; //正式版为 "release",体验版为 "trial",开发版为 "develop"。 String envVersion = "trial"; Boolean checkPath = true ; MiniProgramQRCode.getQRCode( appId , appSecret , scene , page , filePath , envVersion , checkPath); return AjaxResult.Success("success", filePath); }
package com.sipsd.util; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class MiniProgramQRCode { private static final String API_GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; private static final String API_GET_QR_CODE = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s"; // private static final String API_GET_QR_CODE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s"; // private static final String API_GET_QR_CODE = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s"; // wxacode.get -----A path // 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。 // POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN // wxacode.getUnlimited --B page // 获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 更多用法详见 获取二维码。 // POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN // // wxacode.createQRCode -- C path // 获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。 // POST https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN public static void getQRCode( String appId , String appSecret , String scenes , String pages , String filePaths ,String envVersion , Boolean checkPath) throws Exception { String accessToken = getToken(appId, appSecret); String scene = scenes; // 替换成自己的参数值 String page = pages; // 替换成自己的页面路径 int width = 200; // 替换成自己的二维码宽度 String filePath = filePaths; // 替换成自己的保存路径 byte[] qrCodeBytes = getQRCode(accessToken, scene, page, width , envVersion , checkPath); FileOutputStream fos = new FileOutputStream(filePath); IOUtils.write(qrCodeBytes, fos); fos.flush(); fos.close(); } public static String getToken(String appId, String appSecret) throws Exception { String url = String.format(API_GET_TOKEN, URLEncoder.encode(appId, "UTF-8"), URLEncoder.encode(appSecret, "UTF-8")); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); InputStream is = conn.getInputStream(); String json = IOUtils.toString(is); is.close(); conn.disconnect(); return json.substring(json.indexOf("access_token") + "access_token".length() + 3, json.indexOf("expires_in") - 3); } public static byte[] getQRCode(String accessToken, String scene, String page, int width , String envVersion ,Boolean checkPath) throws Exception { String url = String.format(API_GET_QR_CODE, accessToken); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); if (StringUtils.isNotBlank(scene)) { page = page + "?" + scene; } if (StringUtils.isBlank(envVersion)) { envVersion = "release"; } // 正式版为 "release",体验版为 "trial",开发版为 "develop"。 // String json = "{\"page\":\"" + page + "\",\"width\":" + width + ", \"scene\":\"" + scene + "\" , \"env_version\":\" "+ envVersion + "\" , \"check_path\":\""+ checkPath + "\" }"; String json = "{\"path\":\"" + page + "\",\"width\":" + width + ", \"env_version\":\" "+ envVersion + "\" , \"check_path\":\""+ checkPath + "\" }"; conn.getOutputStream().write(json.getBytes("UTF-8")); InputStream is = conn.getInputStream(); byte[] qrCodeBytes = IOUtils.toByteArray(is); is.close(); conn.disconnect(); return qrCodeBytes; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。