Skip to content

Commit

Permalink
feat: 下单逻辑优化
Browse files Browse the repository at this point in the history
  • Loading branch information
qiin committed Mar 2, 2021
1 parent 974b179 commit bc32292
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ switch (mode) {
const buyTime = prompt(
'输入抢购开始时间, 格式为 yyyy-MM-dd HH:mm:ss.SSS'
);
console.log('请确保购物车中待抢购商品已删除!!')
await ins.buyOnTime(buyTime);
break;

Expand Down
69 changes: 28 additions & 41 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export default class MonkeyMaster {
if (this.options.fp && this.options.eid) {
this.fp = this.options.fp;
this.eid = this.options.eid;
} else if (!isWindows) {
} else if (this.fpRequired) {
logger.info('获取必要信息中,大约需要30秒');

const browser = await initBrowser();
Expand Down Expand Up @@ -423,20 +423,9 @@ export default class MonkeyMaster {
this.submitOrder();
};

let jdTime = await this.timeSyncWithJD();
let timer = setTimeout(runOrder, setTimeStamp - jdTime);

await this.cancelSelectCartSkus();

while (setTimeStamp > jdTime) {
logger.info(`距离抢购还剩 ${(setTimeStamp - jdTime) / 1000} 秒`);

// 30秒同步一次时间
await sleep(random.real(5, 10));
clearTimeout(timer);
jdTime = await this.timeSyncWithJD();
timer = setTimeout(runOrder, setTimeStamp - jdTime);
}
await this.waiting4Start(setTimeStamp);
await runOrder();
}

async seckillOnTime(time, num = 1) {
Expand Down Expand Up @@ -481,20 +470,9 @@ export default class MonkeyMaster {
runOrder();
};

let jdTime = await this.timeSyncWithJD();
let timer = setTimeout(runOrder, setTimeStamp - jdTime);

await this.cancelSelectCartSkus();

while (setTimeStamp > jdTime) {
logger.info(`距离抢购还剩 ${(setTimeStamp - jdTime) / 1000} 秒`);

// 30秒同步一次时间
await sleep(random.real(5, 10));
clearTimeout(timer);
jdTime = await this.timeSyncWithJD();
timer = setTimeout(runOrder, setTimeStamp - jdTime);
}
await this.waiting4Start(setTimeStamp);
await runOrder();
}

async fqkillOnTime(time, num = 1) {
Expand Down Expand Up @@ -526,20 +504,9 @@ export default class MonkeyMaster {
runOrder();
};

let jdTime = await this.timeSyncWithJD();
let timer = setTimeout(runOrder, setTimeStamp - jdTime);

await this.cancelSelectCartSkus();

while (setTimeStamp > jdTime) {
logger.info(`距离抢购还剩 ${(setTimeStamp - jdTime) / 1000} 秒`);

// 30秒同步一次时间
await sleep(random.real(5, 10));
clearTimeout(timer);
jdTime = await this.timeSyncWithJD();
timer = setTimeout(runOrder, setTimeStamp - jdTime);
}
await this.waiting4Start(setTimeStamp);
await runOrder();
}

async timeSyncWithJD() {
Expand All @@ -557,6 +524,26 @@ export default class MonkeyMaster {
return serverTime + postConsume + this.avgTimeOffset;
}

async waiting4Start(setTimeStamp) {
let jdTime = Date.now();

while (setTimeStamp > jdTime) {
jdTime = await this.timeSyncWithJD();
const timeRemainMS = setTimeStamp - jdTime;

logger.info(`距离抢购还剩 ${timeRemainMS / 1000} 秒`);

// 30秒同步一次时间
if (timeRemainMS > 18 * 1000) {
await sleep(random.real(5, 15));
} else {
console.log('--------------闭眼祈祷---------------');
await sleep(timeRemainMS / 1000);
break;
}
}
}

/**
*
*
Expand Down Expand Up @@ -651,7 +638,7 @@ export default class MonkeyMaster {
if (item.items && item.items.length) {
return item.items.some(({ item }) => item.Id === skuid);
} else {
console.log(item.Id, skuid)
console.log(item.Id, skuid);
return item.Id === skuid;
}
});
Expand Down

1 comment on commit bc32292

@qiin2333
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#29

Please sign in to comment.