Skip to content

Commit

Permalink
Feature autocheck (#18)
Browse files Browse the repository at this point in the history
* Feat: 预约商品自动勾选商品
* Fix: 删除测试代码
  • Loading branch information
meooxx authored Mar 7, 2021
1 parent 341e8bc commit fe71c5b
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 4 deletions.
70 changes: 68 additions & 2 deletions jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,26 @@ async function submitOrderFromShoppingCart(
submitTimes,
maxWaitingMS
) {
try {
// 尝试取cookie
await helper.requestCartPage();
} catch (e) {
console.log('访问购物车页面出错');
}
const notInCartIds = await isSkuInCart(skuIds, area);
const skuIdsSet = new Set(skuIds);
for (let i = 0; i < skuIds.length; i++) {
const skuId = skuIds[i];
try {
const { stockInfo = {} } = await helper.getWareInfo({
const { stockInfo = {}, yuyueInfo = {} } = await helper.getWareInfo({
skuId,
...params[skuId],
num: 1,
});
if (yuyueInfo && yuyueInfo.yuyue) {
// eslint-disable-next-line no-param-reassign
params[skuId].yuyue = true;
}
console.log(
skuId,
'库存信息:',
Expand Down Expand Up @@ -208,7 +218,33 @@ async function submitOrderFromShoppingCart(
console.log('此流程一次最好抢购一个容易成功哦(也不可同一账号多开), 可以多账号多开');
}

let skuData = [];
const isYuyue = params.yuyue;
if (isYuyue) {
try {
const data = await getSkusData(skuIds, area);
skuData = [...skuIdsSet].map(s => data.get(s));
if (skuData.length === 0) {
throw Error('空的购物车数据');
}
} catch (e) {
console.log('获取购物车数据失败');
process.exit();
}
}

await timer(date, async () => {
if (isYuyue) {
const d = Date.now();
const validIds = [...skuIdsSet];
const [success] = await helper.checkSkus(skuData, validIds, area);
console.log(`使用${Date.now() - d}ms, 已勾选${validIds}`);
if (!success) {
console.log('勾选失败');
process.exit();
}
}

let isAvailable = false;
// !!不要更改大于 7, 超过七会触发京东频率限制!!
let i = 7;
Expand Down Expand Up @@ -316,6 +352,37 @@ async function isSkuInCart(skuId, areaId) {
return skuIds;
}

/**
*
* @param {*} skuId
* @param {*} areadId
* 购物车中sku的数据
*/
async function getSkusData(skuId, areaId) {
const skuIds = Array.isArray(skuId) ? skuId : [skuId];
// 获取购物车数据
const res = await helper.getCartData(areaId);
const data = new Map();

if (res.success) {
let allskus = [];
if (!res.resultData.cartInfo) return skuIds;
res.resultData.cartInfo.vendors.forEach(v => {
allskus = allskus.concat(v.sorted);
});
allskus.forEach(s => {
if (s.item && s.item.items) {
s.item.items.forEach(i => {
data.set(String(i.item.Id), i);
});
}
data.set(String(s.item.Id), s);
});
return data;
}
return data;
}

/**
*
* @param {*} skuId skuid
Expand Down Expand Up @@ -406,7 +473,6 @@ async function submitOrderProcess(
const now = Date.now();
printPoints = !printPoints;
process.stdout.write('\r\x1b[K');
process.stdout.write('当前时间:');
process.stdout.write(dayjs(now).format('YYYY-MM-DD HH:mm:ss.SSS'));
if (printPoints) {
process.stdout.write('...');
Expand Down
187 changes: 185 additions & 2 deletions tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ class Tools {
console.log(result, ',重试获取抢购链接');
// 直接重试
await new Promise(r => setTimeout(r, 100));
} catch (e) {}
} catch (e) {
//
}
}
console.log('没获取到抢购链接, 退出了');
return '';
Expand Down Expand Up @@ -645,7 +647,7 @@ class Tools {
};

// 获取购物车数据接口
getCartData = async area => {
getCartData = async (area) => {
const origin = 'https://api.m.jd.com';
const cookies = await this.reqTools.getCookies(origin);
const item = cookies.find(c => c.key === 'user-key') || {};
Expand Down Expand Up @@ -674,6 +676,187 @@ class Tools {
});
return await res.json();
};

// 选中sku
checkSkus = async (skus, ids = [], area) => {
const p = {
ThePacks: [],
TheSkus: [],
carttype: "5"
}
// 业务逻辑复制自官网
const suitParam = (e) => {
var t, a = e.itemType, n = e.item.Id, r = e.item.Num, c = e.item.SType, i = e.item.items[0], s = i.item.Num;
16 === c && 0 !== e.item.items.length && (c = 13);
var o = e.item.items.map((function (e) {
return {
Id: e.item.Id,
num: e.item.Num,
skuUuid: e.item.skuUuid,
useUuid: e.item.useUuid
}
}
));
switch (a) {
case 4:
return {
Id: n,
num: r,
sType: c,
TheSkus: o
};
case 9:
return 4 == i.itemType ? (t = 12 == a ? 29 : 24,
o = [{
Id: i.item.Id,
num: s,
sType: t
}]) : o = [{
Id: i.item.Id,
num: s
}],
{
Id: n,
num: r,
sType: c,
TheSkus: o
};
case 12:
return 4 == i.itemType ? (t = 12 == a ? 29 : 24,
o = [{
Id: i.item.Id,
num: s,
sType: t
}]) : o = [{
Id: i.item.Id,
num: s
}],
{
Id: n,
num: r,
sType: c,
TheSkus: o
}
}
}
// 业务逻辑复制自官网
const combineParam = (e, t) => {
var a = e.SType;
return 16 == a && (a = 13),
{
num: e.Num,
sType: a,
Id: e.Id,
TheSkus: [{
num: t.Num,
Id: t.Id,
skuUuid: t.skuUuid,
useUuid: t.useUuid
}]
}
}


skus.forEach(e => {
switch (e.itemType) {
case 1: {
const item = e.item
const param = {
Id: item.Id,
num: item.Num,
skuUuid: item.skuUuid,
useUuid: item.useUuid
}
p.TheSkus.push(param);
break
}
case 4: {
const c = suitParam(e);
p.ThePacks.push(c);
break;
}
case 9:
case 12: {
const i = e.item;
i.items && i.items.length > 0 && i.items.map((function (t) {
switch (t.itemType) {
case 1: {
var r = combineParam(e.item, t.item);
p.ThePacks.push(r);
break;
}
case 4: {
const i = suitParam(e);
p.ThePacks.push(i)
}
}
}
))
}

}
})

const remote = 'https://api.m.jd.com/api'
const cookies = await this.reqTools.getCookies(remote);
const item = cookies.find(c => c.key === 'user-key') || {};
const body = {
operations: [p],
serInfo: {
area,
'user-key': item['user-key'] || '',
},
};
const payload = {
functionId: 'pcCart_jc_cartCheckSingle',
appid: 'JDC_mall_cart',
body: JSON.stringify(body),
};
let i = 10
while (i--) {
try {
const res = await this.request(remote, {
headers: {
'User-Agent': this.userAgent,
origin: 'https://cart.jd.com',
referer: `https://cart.jd.com/`,
},
method: "POST",
body: qs.stringify(payload)
});

if (!ids) {
const result = await res.json()
return [true, result]
}
const result = await res.json()
if (!result.success) return [false]
const vendors = (result.resultData || {}).cartInfo ? result.resultData.cartInfo.vendors : []

let checkedNum = 0
for (let i = 0; i < vendors.length; i++) {
const v = vendors[i] || {}
for (let j = 0; j < v.sorted.length; j++) {
if (v.sorted[j].checkedNum > 0) {
checkedNum += v.sorted[j].checkedNum
}
if (checkedNum >= ids.length) {
return [true, result]
}
}
}
} catch (e) {
console.log('勾选商品失败:', index)
}

if (i - 1 !== 0) {
this.sleep(50)
}

}
return [false]

}
}

module.exports = Tools;

0 comments on commit fe71c5b

Please sign in to comment.