JAVA は小さなプログラム コード (Sun コード) を生成します
ツール クラスはトークンを取得するために使用されます。
appId= ミニプログラム appID
appSecret= ミニプログラムの秘密鍵
共有アイテムはミニ プログラムで設定する必要があります。設定しないと画像が破損します。
開発 > 開発管理 > 開発設定
nginx 構成
location ~ ^/share {
#、share 你的访问路径
default_type text/html;
alias /data/share/IQ8MzevUAz.txt; #你的文件地址
}
ミニプログラムQRコードの公式文書を生成
リンク: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html< /span>
QR コードの生成方法は 3 つあり、利用シーンに合わせて選択できます。ここでは 3 番目の生成方法である wxacode.getUnlimited
wxacode.createQRCode
ミニ プログラムの QR コードを取得します。これは、コード数が少ないビジネス シナリオに適しています。このインターフェイスを通じて生成されたミニ プログラム コードは永続的に有効であり、数量制限があります。詳細については、「QR コードの取得」を参照してください。
投稿 https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
wxacode.get
ミニ プログラム コードを取得します。これは、少数のコードを必要とするビジネス シナリオに適しています。このインターフェイスを通じて生成されたミニ プログラム コードは永続的に有効であり、数量制限があります。詳細については、「QR コードの取得」を参照してください。
投稿 https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
wxacode.getUnlimited
多数のコードを必要とするビジネス シナリオに適したミニ プログラム コードを取得します。このインターフェイスを通じて生成された小さなプログラム コードは永続的に有効であり、数量は一時的に無制限です。詳しい使い方については「QRコードの取得」をご覧ください。
投稿 https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
このインターフェイスを通じて生成された小さなプログラム コードは永続的に有効であり、数量は一時的に無制限です。ユーザーがコードをスキャンしてミニ プログラムを入力した後、開発者は対応するページのコード内のシーン フィールドの値を取得し、処理ロジックを実行する必要があります。
次のコードを使用して、QR コードのシーン フィールドの値を取得します。
デバッグ段階では、シミュレーション用の開発ツールのカスタム パラメータ scene=xxxx の条件付きコンパイルを使用できます。開発ツールのシミュレーション中のシーンのパラメータ値は、 urlencode にする
1. ミニ プログラムの appId と appKey を取得します
2. ミニ プログラムの QR コードを生成します。渡されるページ パラメーターはパスではなくページであり、他のインターフェイスはパスです。
ページの後にパラメータを指定することはできません。パラメータはシーンを通じて渡す必要があります。アプレットはシーンを通じてパラメータを取得する必要もあります。
3. ミニ プログラムの QR コードを生成します。QR コードはローカルに書き込むことも、サーバーにアップロードすることもできます。自分で選択してください
1つのツールでそれができる
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.qq.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.qq.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;
}
}