wifi-assistant/gen_qr_code.js
2024-12-27 22:44:32 +08:00

36 lines
1005 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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