Skip to content

Commit

Permalink
V1.2.4 (#21)
Browse files Browse the repository at this point in the history
* Fix: 过滤无效的sku数据
* FIx: 抢购后取消选中
* Fix: 可能导致错误的无货的状态
  • Loading branch information
meooxx authored Apr 17, 2021
1 parent f501a9e commit 4c49148
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
20 changes: 15 additions & 5 deletions jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,19 @@ async function submitOrderFromShoppingCart(
}

let skuData = [];
if (yuyueSkuSet.size > 0) {
if (skuIdsSet.size > 0) {
try {
const data = await getSkusData(area);
skuData = [...yuyueSkuSet].map(s => data.get(s));
skuData = [...skuIdsSet].map(s => data.get(s)).filter(Boolean).map(item => {
if (item.item.items) {
// eslint-disable-next-line no-param-reassign
item.item.items = item.item.items.filter(i => skuIdsSet.has(i.item.Id));
return item;
}
});
if (skuData.length === 0) {
throw Error('空的购物车数据');
console.log('空的购物车数据');
throw Error();
}
} catch (e) {
console.log('获取购物车数据失败');
Expand Down Expand Up @@ -285,6 +292,7 @@ async function submitOrderFromShoppingCart(
console.log(
`访问订单结算时间:${dayjs(checkoutPageTime).format('YYYY-MM-DD HH:mm:ss.SSS')}`
);
await helper.checkSkus(skuData, [], area, true);
process.exit();
}
i = submitTimes || 10;
Expand All @@ -295,7 +303,7 @@ async function submitOrderFromShoppingCart(
if (res.success) {
const now = new Date();
const text = `订单提交成功!订单号:${res.orderId
},时间:${dayjs(now).format('YYYY-MM-DD HH:mm:ss.SSS')}`;
},时间:${dayjs(now).format('YYYY-MM-DD HH:mm:ss.SSS')}`;
console.log(text);
await helper.sendToWechat(text);
process.exit();
Expand All @@ -321,6 +329,8 @@ async function submitOrderFromShoppingCart(
console.log(
`提交订单开始时间:${dayjs(submitOrderTime).format('YYYY-MM-DD HH:mm:ss.SSS')}`
);
await helper.checkSkus(skuData, [], area, true);
process.exit();
});
}
/**
Expand Down Expand Up @@ -372,7 +382,7 @@ async function getSkusData(areaId) {
allskus.forEach(s => {
if (s.item && s.item.items) {
s.item.items.forEach(i => {
data.set(String(i.item.Id), i);
data.set(String(i.item.Id), s);
});
}
data.set(String(s.item.Id), s);
Expand Down
26 changes: 22 additions & 4 deletions tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,16 @@ class Tools {
return await res.json();
};

// 选中sku
checkSkus = async (skus, ids = [], area) => {
// 选中/取消选中sku
/**
*
* @param {*} skus
* @param {*} ids
* @param {*} area
* @param {*} uncheck 取消选中
* @returns
*/
checkSkus = async (skus, ids = [], area, uncheck) => {
const p = {
ThePacks: [],
TheSkus: [],
Expand Down Expand Up @@ -800,6 +808,12 @@ class Tools {
const remote = 'https://api.m.jd.com/api'
const cookies = await this.reqTools.getCookies(remote);
const item = cookies.find(c => c.key === 'user-key') || {};
let functionId = 'pcCart_jc_cartCheckSingle'
if (uncheck) {
p.carttype = '6'
functionId = 'pcCart_jc_cartUnCheckSingle'
}

const body = {
operations: [p],
serInfo: {
Expand All @@ -808,10 +822,11 @@ class Tools {
},
};
const payload = {
functionId: 'pcCart_jc_cartCheckSingle',
functionId,
appid: 'JDC_mall_cart',
body: JSON.stringify(body),
};

let i = 10
while (i--) {
try {
Expand All @@ -824,7 +839,10 @@ class Tools {
method: "POST",
body: qs.stringify(payload)
});

if (uncheck) {
console.log("已经取消勾选")
break
}
if (!ids) {
const result = await res.json()
return [true, result]
Expand Down

0 comments on commit 4c49148

Please sign in to comment.