2025-02-12 05:55:57 +00:00
|
|
|
|
import axios from "axios";
|
|
|
|
|
import { writeFileSync } from "fs";
|
|
|
|
|
import config from "./create_qr_code.json";
|
|
|
|
|
|
|
|
|
|
const generateQRCode = async (path: string, scene: string) => {
|
|
|
|
|
const url = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${config.access_token}`;
|
|
|
|
|
const params = {
|
|
|
|
|
scene: scene, // 参数,格式为json字符串
|
|
|
|
|
page: path, // 扫码后跳转的小程序页面路径
|
|
|
|
|
width: 430, // 二维码的宽度
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: url,
|
|
|
|
|
data: params,
|
2025-02-12 06:09:36 +00:00
|
|
|
|
responseType: "arraybuffer", // 重要: 需要设置响应类型为arraybuffer
|
2025-02-12 05:55:57 +00:00
|
|
|
|
});
|
|
|
|
|
|
2025-02-12 06:09:36 +00:00
|
|
|
|
// 将response.data(一个Buffer)写入文件
|
2025-02-12 05:55:57 +00:00
|
|
|
|
writeFileSync("qrcode.png", response.data);
|
|
|
|
|
console.log("小程序码已生成");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("生成小程序码失败", error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
generateQRCode(
|
|
|
|
|
"pages/connect-wifi/connect-wifi",
|
|
|
|
|
`{"SSID":"${config.ssid}","password":"${config.password}"}`
|
|
|
|
|
);
|