const readline = require('readline');
const https = require('https');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 获取图形验证码链接
function getImageCode(phoneNumber) {
return new Promise((resolve, reject) => {
const imageCodeUrl = `https://ssologin.bestwehotel.com/plateno_mall/unionLogin/gainIdentificationCode/${phoneNumber}`;
https.get(imageCodeUrl, (res) => {
let imageData = '';
res.on('data', (chunk) => {
imageData += chunk;
});
res.on('end', () => {
const imageDataJson = JSON.parse(imageData);
resolve(imageDataJson);
});
}).on('error', (error) => {
reject(error);
});
});
}
// 获取手机验证码
function getPhoneCode(phoneNumber, captchaCode) {
return new Promise((resolve, reject) => {
const phoneCodeUrl = `https://ssologin.bestwehotel.com/plateno_mall/unionLogin/gainIdentificationCode/${phoneNumber}?authCode=${captchaCode}`;
https.get(phoneCodeUrl, (res) => {
let phoneData = '';
res.on('data', (chunk) => {
phoneData += chunk;
});
res.on('end', () => {
const phoneDataJson = JSON.parse(phoneData);
resolve(phoneDataJson);
});
}).on('error', (error) => {
reject(error);
});
});
}
// 登录
function login(phoneNumber, phoneCode) {
return new Promise((resolve, reject) => {
const loginUrl = 'https://ssologin.bestwehotel.com/plateno_mall/unionLogin/login';
const loginData = JSON.stringify({
"mobile": phoneNumber,
"validateCode": phoneCode
});
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': loginData.length
}
};
const req = https.request(loginUrl, options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
const data = JSON.parse(responseData);
resolve(data);
});
});
req.on('error', (error) => {
reject(error);
});
req.write(loginData);
req.end();
});
}
// 主流程
rl.question('请输入登录手机号:', async (phoneNumber) => {
try {
const imageCodeResponse = await getImageCode(phoneNumber);
if (imageCodeResponse.code !== 100) {
console.log("获取图形验证码接口返回错误:" + imageCodeResponse.msg);
rl.close();
return;
}
console.log("浏览器打开链接查看验证码: " + imageCodeResponse.data)
const captchaCode = await askQuestion('请输入图形验证码:');
const phoneCodeResponse = await getPhoneCode(phoneNumber, captchaCode);
if (phoneCodeResponse.code !== 200) {
console.log("获取手机验证码失败: "+ phoneCodeResponse.msg);
rl.close();
return;
}
const phoneCode = await askQuestion('请输入手机验证码:');
const loginResponse = await login(phoneNumber, phoneCode);
if (loginResponse.code !== 200) {
console.log("登录失败: "+ loginResponse.msg);
rl.close();
return;
}
console.log("登录成功!");
const token = loginResponse.data
// {"code":200,"msg":"请求成功","data":"U7PlPHRy3TcckwnZNy+SRXg7AHp0pU+wRjYNDW92Qh+pJpW3Rbj+lZIfyHVTEPLK","systemTime":1711596916392}
// 开始锁单
const data = JSON.stringify({
"clientInfo": {
"lbs": ",",
"phoneType": "",
"appVersion": "",
"deviceId": "",
"sysCode": "",
"browserType": "Netscape",
"sysType": "Win32",
"channelId": "",
"platformId": "",
"sid": 574582,
"activityId": "",
"channel": 2001
},
"orderList": [
{
"skuId": "131762",
"skuCount": "1",
"priceType": "3",
"usePointNums": "9500",
"currentPirce": "0",
"priceToPoint": "0",
"activityId": "",
"activityType": ""
}
],
"resource": 2001,
"couponId": null,
"userPoint": "15"
});
const options = {
hostname: 'malls.bestwehotel.com',
path: '/plateno_mall/common/proxy/booking-bookingDetail',
method: 'POST',
headers: {
'Authtype': '5',
'Blackbox': 'kWPH91711592677c6Z8zt4hpP4',
'Content-Length': data.length,
'Content-Type': 'application/json;charset=UTF-8',
'Token': token,
'Unitype': '10000011',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
}
};
const req = https.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
console.log(JSON.parse(responseData).msg);
});
});
req.on('error', (error) => {
console.error(error);
});
req.write(data);
req.end();
} catch (error) {
console.error(error);
} finally {
rl.close();
}
});
function askQuestion(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
rl.on('close', () => {
console.log('程序结束');
});
Nodejs实现登录锦江会员抢单(命令行交互)
最新推荐文章于 2024-08-25 10:16:22 发布