JAVA生成小程序码(太阳码)
工具类是获取token使用;
appId= 小程序appID
appSecret= 小程序秘钥
小程序中得配置分享项,不然图片是裂开的。
开发>开发管理>开发设置
讯享网
nginx 配置
location ~ ^/share {
#、share 你的访问路径
default_type text/html; alias /data/share/IQ8MzevUAz.txt; #你的文件地址 }
讯享网
生成小程序二维码官方文档
wxacode.createQRCode
获取小程序二维码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。
POST https://api.weixin..com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
wxacode.get
获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制,详见获取二维码。
POST https://api.weixin..com/wxa/getwxacode?access_token=ACCESS_TOKEN
wxacode.getUnlimited
获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。 更多用法详见 获取二维码。
POST https://api.weixin..com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene字段的值,再做处理逻辑。
使用如下代码可以获取到二维码中的 scene 字段的值。
调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
1.获取小程序appId 与appKey
2.生成小程序二维码页面参数传入的是page而不是path,其他的接口是path。
page后面不允许加参数,参数需要通过scene传入。而小程序也需要通过scene获取参数。
3.生成小程序二维码可将二维码写入本地,也可上传至服务器。自行选择
一个工具类搞定
讯享网 import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; @Component public class QrUtil {
private static String API_KEY; private static String SECRET; public static String getApiKey() {
return API_KEY; } @Value("${wx.appId}") public void setApiKey(String apiKey) {
API_KEY = apiKey; } public static String getSECRET() {
return SECRET; } @Value("${wx.appSecret}") public void setSECRET(String SECRET) {
QrUtil.SECRET = SECRET; } public static String postToken() throws Exception {
String requestUrl = "https://api.weixin..com/cgi-bin/token?grant_type=client_credential&appid="+ API_KEY +"&secret="+SECRET; URL url = new URL(requestUrl); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); // 设置通用的请求属性 connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setUseCaches(false); connection.setDoOutput(true); connection.setDoInput(true); // 得到请求的输出流对象 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(""); out.flush(); out.close(); // 建立实际的连接 connection.connect(); // 定义 BufferedReader输入流来读取URL的响应 BufferedReader in = null; if (requestUrl.contains("nlp")) in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK")); else in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String result = ""; String getLine; while ((getLine = in.readLine()) != null) {
result += getLine; } in.close(); JSONObject jsonObject = JSON.parseObject(result); String accesstoken=jsonObject.getString("access_token"); return accesstoken; } public static String getminiqrQr(String sceneStr,String page) {
try {
URL url = new URL("https://api.weixin..com/wxa/getwxacodeunlimit?access_token=" + postToken()); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 // 发送POST请求必须设置如下两行 httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); // 发送请求参数 JSONObject paramJson = new JSONObject(); paramJson.put("scene", "?userId="+sceneStr); paramJson.put("page", page); paramJson.put("width", 430); paramJson.put("is_hyaline", true); paramJson.put("auto_color", true); printWriter.write(paramJson.toString()); // flush输出流的缓冲 printWriter.flush(); //开始获取数据 // BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); // OutputStream os = new FileOutputStream(new File("C:/Users/c/Desktop/1.png")); // int len1; // byte[] arr = new byte[1024]; // while ((len1 = bis.read(arr)) != -1) {
// os.write(arr, 0, len1); // os.flush(); // } // os.close(); try (InputStream is = httpURLConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();){
byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len); } return "data:mediatype;base64," + Base64.getEncoder().encodeToString(baos.toByteArray()); } } catch (Exception e) {
e.printStackTrace(); } return null; } }



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/90968.html