Skip to content

Commit

Permalink
feat: 修正部分特殊情况下会异常的问题
Browse files Browse the repository at this point in the history
1. 开红包和查询红包增加通用重试机制
2. 修正requestApi出错时,打印resp会报错 Converting circular structure to JSON 的问题
  • Loading branch information
fzls committed Dec 7, 2021
1 parent 67f6e03 commit eea2745
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions jd_angryKoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ let notify, allMessage = '';
// 未开启公平模式,则按照顺序互助,前面的先互助满
for (let idx = 0; idx < cookiesArr.length; idx++) {
var cookie = cookiesArr[idx];

if (kois.indexOf(cookie.match(/pt_pin=([^; ]+)(?=;?)/) && cookie.match(/pt_pin=([^; ]+)(?=;?)/)[1]) != -1) {
otherIndexes.push(idx)
}else{
} else {
cookieIndexOrder.push(idx)
}
}
Expand Down Expand Up @@ -170,28 +170,20 @@ async function getHelpInfoForCk(cookieIndex, cookie) {
console.log(`开始请求第 ${cookieIndex} 个账号的信息`)

let data;
let MAX_TRY = 3

// 尝试开启今日的红包活动
for (let tryIdex = 1; tryIdex <= MAX_TRY; tryIdex++) {
// 开启红包
data = await with_retry("开启红包活动", async () => {
var num = "";
for (var g = 0; g < 6; g++) {
num += Math.floor(Math.random() * 10);
}
data = await requestApi('h5launch', cookie, {
return await requestApi('h5launch', cookie, {
"followShop": 0,
"random": num,
"log": "42588613~8,~0iuxyee",
"sceneid": "JLHBhPageh5"
});

if (data) {
break
}

console.error(`[${tryIdex}/${MAX_TRY}] h5launch 请求时似乎出错了,有可能是网络波动,将最多试三次`)
await $.wait(5000)
}
})

switch (data?.data?.result?.status) {
case 1://火爆
Expand All @@ -213,19 +205,11 @@ async function getHelpInfoForCk(cookieIndex, cookie) {
}

// 已开启活动,尝试查询具体信息
for (let tryIdex = 1; tryIdex <= MAX_TRY; tryIdex++) {
data = await requestApi('h5activityIndex', cookie, {
data = await with_retry("查询红包信息", async () => {
return await requestApi('h5activityIndex', cookie, {
"isjdapp": 1
});

if (data) {
break
}

console.error(`[${tryIdex}/${MAX_TRY}] h5activityIndex 请求时似乎出错了,有可能是网络波动,将最多试三次`)
await $.wait(5000)
}

})

if (data?.data?.result?.redpacketConfigFillRewardInfo) {
// 打印今日红包概览
Expand All @@ -251,7 +235,7 @@ async function getHelpInfoForCk(cookieIndex, cookie) {

switch (data?.data?.code) {
case 20002://已达拆红包数量限制
console.debug("已领取今天全部红包")
console.debug("已领取今天全部红包,将跳过")
break;
case 10002://活动正在进行,火爆号
console.debug(`h5activityIndex 被风控,变成黑号了, data=${JSON.stringify(data)}`)
Expand All @@ -271,9 +255,11 @@ async function getHelpInfoForCk(cookieIndex, cookie) {
}

async function appendRewardInfoToNotify(cookieIndex, cookie) {
let data = await requestApi('h5activityIndex', cookie, {
"isjdapp": 1
});
let data = await with_retry("查询红包信息", async () => {
return await requestApi('h5activityIndex', cookie, {
"isjdapp": 1
});
})

// 判断是否有红包可以领
if (calcCanTakeRedpacketCount(data?.data?.result) > 0) {
Expand All @@ -294,9 +280,11 @@ async function appendRewardInfoToNotify(cookieIndex, cookie) {
}

console.info(`领取完毕,重新查询最新锦鲤红包信息`)
data = await requestApi('h5activityIndex', cookie, {
"isjdapp": 1
});
data = await with_retry("查询红包信息", async () => {
return await requestApi('h5activityIndex', cookie, {
"isjdapp": 1
});
})
}

// 打印今日红包概览
Expand Down Expand Up @@ -349,6 +337,27 @@ function calcCanTakeRedpacketCount(info) {
return count
}

async function with_retry(ctx = "", callback_func, max_retry_times = 3, retry_interval = 5000) {
let data;

// 尝试开启今日的红包活动
for (let tryIdex = 1; tryIdex <= max_retry_times; tryIdex++) {
if (tryIdex > 1) {
console.debug(`[${tryIdex}/${max_retry_times}] 重新尝试 ${ctx}`)
}

data = await callback_func()
if (data) {
break
}

console.error(`[${tryIdex}/${max_retry_times}] ${ctx} 请求时似乎出错了,有可能是网络波动,将等待 ${retry_interval / 1000} 秒,最多试 ${max_retry_times} 次\n`)
await wait(retry_interval)
}

return data
}

async function openRedPacket(cookie) {
var num = "";
for (var g = 0; g < 6; g++) {
Expand Down Expand Up @@ -421,7 +430,7 @@ async function requestApi(functionId, cookie, body = {}) {
data = JSON.parse(data)
} catch (e) {
$.logErr('Error: ', e, resp)
console.warn(`请求${functionId}失败,resp=${JSON.stringify(resp)}data=${JSON.stringify(data)}, e=${JSON.stringify(e)}`)
console.warn(`请求${functionId}失败,data=${JSON.stringify(data)}, e=${JSON.stringify(e)}`)
} finally {
resolve(data)
}
Expand Down Expand Up @@ -837,4 +846,4 @@ function Env(t, e) {
this.log("", `🔔${this.name}, 结束! 🕛 ${s} 秒`), this.log(), (this.isSurge() || this.isQuanX() || this.isLoon()) && $done(t)
}
}(t, e)
}
}

0 comments on commit eea2745

Please sign in to comment.