From 4c491485c70fba32b198a33f04c374ec7b5c0d09 Mon Sep 17 00:00:00 2001 From: superq <13815865+meooxx@users.noreply.github.com> Date: Sat, 17 Apr 2021 20:34:14 +0800 Subject: [PATCH] V1.2.4 (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: 过滤无效的sku数据 * FIx: 抢购后取消选中 * Fix: 可能导致错误的无货的状态 --- jobs.js | 20 +++++++++++++++----- tools.js | 26 ++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/jobs.js b/jobs.js index 34a477f..ab9813a 100644 --- a/jobs.js +++ b/jobs.js @@ -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('获取购物车数据失败'); @@ -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; @@ -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(); @@ -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(); }); } /** @@ -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); diff --git a/tools.js b/tools.js index 6f4468f..e503569 100644 --- a/tools.js +++ b/tools.js @@ -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: [], @@ -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: { @@ -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 { @@ -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]