声明此代码仅供技术交流学习,擅自用于其他,一切后果与本人无关
由于公司业务需要,需要管理几百个小程序,刚开始不多的时候还可以通过手动扫码登录,但随着时间积累,小程序越来越多,管理起来就很痛苦了,经过大概1周的开发,利用node的爬虫,解决了这个问题,
整体流程
- 先建立一个可视化界面,来管理几百个小程序的账号和密码
- 一次性登录50个小程序(因为一部手机只能绑定50个小程序管理员),并获取每个小程序的token和cookie,存在redis
- 每次操作相应api(需要爬微信接口),都可以直接通过token和cookie来操作
登录逻辑
- 通过账号密码来获取登录cookie
- 通过cookie去下载二维码
- 每次一个账号密码新建立一个轮询来判断是否扫码成功
- 登录成功后拿到token,把token和cookie存在redis里面
//账号密码登录获取token并且下载二维码,成功一个就新建一个轮询来监听扫码是否成功
let config = {
returnfn: null, //回调方法
loginindex: 0, //当前需要执行多少个
imglist: [], //图片列表
xcxKey: [] //小程序的账号密码列表
}
for (let item of config.xcxKey) {
config.loginindex += 1;
getqrcode(item)
}
function getqrcode(item) {
request({
'method': 'POST',
'url': 'https://mp.weixin.qq.com/cgi-bin/bizlogin?action=startlogin',
'form': {
"username": item.miniAccount,
"pwd": md5(item.miniPassword),
"imgcode": "",
'f': 'json',
'userlang': 'zh_CN',
'redirect_url': "",
'token': "",
'lang': 'zh_CN',
'ajax': 1,
},
'headers': {
"Origin": "https://mp.weixin.qq.com",
'referer': `https://mp.weixin.qq.com/`,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36 Edg/91.0.864.48",
}
}, async function (error, response) {
try {
let responseCookies = response.headers['set-cookie'];
let requestCookies = '';
for (let i = 0; i < responseCookies.length; i++) {
let oneCookie = responseCookies[i];
oneCookie = oneCookie.split(';');
requestCookies = requestCookies + oneCookie[0] + ';';
}
let imgurl = Math.random().toString(36).substr(2) + tool.getTime()
await request({
'url': `https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=getqrcode¶m=4300&rd=123`,
'headers': {
Cookie: requestCookies,
"Referer": `https://mp.weixin.qq.com/cgi-bin/bizlogin?action=validate&lang=zh_CN&account=${item.miniAccount}&token=`,
}
}).pipe(fs.createWriteStream(`./img/${imgurl}.png`));
config.imglist.push({
name: item.name,
QrCode: `图片文件夹/${imgurl}.png`
})
new getLoginState(item, requestCookies)
} catch (e) {
console.log(e)
}
config.loginindex -= 1;
if (config.loginindex == 0) {
config.returnfn(config.imglist)
}
});
}
// 循环调用二维码
function getLoginState(item, cookie) {
function forGetLoginState() {
console.log(`${item.name}未扫码,三分钟内扫码`)
$ajax.get(`https://mp.weixin.qq.com/cgi-bin/loginqrcode?action=ask&token=&lang=zh_CN&f=json&ajax=1`, {
referer: `https://mp.weixin.qq.com/cgi-bin/bizlogin?action=validate&lang=zh_CN&account=${item.miniAccount}&token=`,
Cookie: cookie
}).then((res) => {
if (res.base_resp.ret == 1 || res.status == 0 || res.status == 4) {
setTimeout(() => {
forGetLoginState()
}, 3000)
} else {
new saveToken(item, cookie)
}
})
}
forGetLoginState()
}
//扫码成功后保存token
function saveToken(item, cookie) {
request({
'method': 'POST',
'url': 'https://mp.weixin.qq.com/cgi-bin/bizlogin?action=login',
'form': {
userlang: 'zh_CN',
redirect_url: '',
token: '',
lang: 'zh_CN',
f: 'json',
ajax: '1'
},
'headers': {
"Origin": "https://mp.weixin.qq.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36 Edg/91.0.864.48",
Cookie: cookie,
referer: `https://mp.weixin.qq.com/cgi-bin/bizlogin?action=validate&lang=zh_CN&account=${item.miniAccount}&token=`
}
}, function (error, response) {
console.log(`${item.name}token获取成功`)
try{
let responseCookies = response.headers['set-cookie'];
let requestCookies = '';
for (let i = 0; i < responseCookies.length; i++) {
let oneCookie = responseCookies[i];
oneCookie = oneCookie.split(';');
requestCookies = requestCookies + oneCookie[0] + ';';
}
let body = JSON.parse(response.body || {});
let token = "";
if (body.redirect_url) {
token = body.redirect_url.split('token=')[1]
}
redis.setRedisObj(`${item.miniAccount}_cookie`, requestCookies, false)
redis.setRedisObj(`${item.miniAccount}_token`, token, false)
redis.client.expire(`${item.miniAccount}_cookie`, 36000)
redis.client.expire(`${item.miniAccount}_token`, 36000)
}catch(e){
console.log(e)
}
});
}
调用微信公众平台相关接口,提审,发布审核,查看广告审核状态等
这个就可自己通过查看接口,获取相关接口地址,只需要每次调用的时候都要传入token和cookie,如果需要扫码,就和登录流程一样,下载二维码到本地返回到前端,每次新建一个轮询来监听扫码是否成功