62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
|
|
/**
|
|||
|
|
* 将最新上传的开发版设为体验版
|
|||
|
|
* 用法:WX_APPSECRET=你的AppSecret node scripts/set-experience.js
|
|||
|
|
* 或在 package.json 里配置后:npm run set-experience
|
|||
|
|
*/
|
|||
|
|
const https = require('https');
|
|||
|
|
|
|||
|
|
const APPID = 'wx349f46ac71770777';
|
|||
|
|
const APPSECRET = process.env.WX_APPSECRET;
|
|||
|
|
|
|||
|
|
if (!APPSECRET) {
|
|||
|
|
console.error('错误:请设置环境变量 WX_APPSECRET(微信公众平台 -> 开发 -> 开发管理 -> 开发设置 -> AppSecret)');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function request(method, path, body) {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
const data = body ? JSON.stringify(body) : null;
|
|||
|
|
const req = https.request({
|
|||
|
|
hostname: 'api.weixin.qq.com',
|
|||
|
|
path,
|
|||
|
|
method,
|
|||
|
|
headers: data ? { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) } : {},
|
|||
|
|
}, (res) => {
|
|||
|
|
let raw = '';
|
|||
|
|
res.on('data', (c) => (raw += c));
|
|||
|
|
res.on('end', () => {
|
|||
|
|
try { resolve(JSON.parse(raw)); } catch { resolve(raw); }
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
req.on('error', reject);
|
|||
|
|
if (data) req.write(data);
|
|||
|
|
req.end();
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
(async () => {
|
|||
|
|
try {
|
|||
|
|
// 1. 获取 access_token
|
|||
|
|
const tokenRes = await request('GET', `/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`);
|
|||
|
|
if (!tokenRes.access_token) {
|
|||
|
|
console.error('获取 access_token 失败:', JSON.stringify(tokenRes));
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
console.log('access_token 获取成功');
|
|||
|
|
|
|||
|
|
// 2. 设为体验版
|
|||
|
|
const result = await request('POST', `/wxa/version_control/release?access_token=${tokenRes.access_token}`, {
|
|||
|
|
action: 'set_experience',
|
|||
|
|
});
|
|||
|
|
console.log('设置体验版结果:', JSON.stringify(result));
|
|||
|
|
if (result.errcode === 0) {
|
|||
|
|
console.log('✓ 已成功设为体验版');
|
|||
|
|
} else {
|
|||
|
|
console.log('提示:若 errcode 表示接口不支持,请在微信公众平台 -> 管理 -> 版本管理 -> 开发版本 中手动点击「选为体验版」');
|
|||
|
|
}
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error('执行失败:', e.message);
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
})();
|