|
| 1 | +const $ = new Env('京东618') |
| 2 | +$.VAL_url = $.getdata('chavy_url_jd816') |
| 3 | +$.VAL_body = $.getdata('chavy_body_jd816') |
| 4 | +$.VAL_headers = $.getdata('chavy_headers_jd816') |
| 5 | + |
| 6 | +!(async () => { |
| 7 | + $.log('', `🔔 ${$.name}, 开始!`, '') |
| 8 | + await getData() |
| 9 | + await getActs() |
| 10 | + await execActs() |
| 11 | + showmsg() |
| 12 | +})() |
| 13 | + .catch((e) => { |
| 14 | + $.log('', `❌ ${$.name}, 失败! 原因: ${e}!`, '') |
| 15 | + }) |
| 16 | + .finally(() => { |
| 17 | + $.msg($.name, $.subt, $.desc), $.log('', `🔔 ${$.name}, 结束!`, ''), $.done() |
| 18 | + }) |
| 19 | + |
| 20 | +function getData() { |
| 21 | + return new Promise((resove) => { |
| 22 | + $.post(taskurl('cakebaker_getHomeData'), (error, response, data) => { |
| 23 | + try { |
| 24 | + if (error) throw new Error(error) |
| 25 | + const _info = JSON.parse(data).data?.result?.cakeBakerInfo |
| 26 | + $.secretp = _info?.secretp |
| 27 | + if (!$.secretp) throw new Error('获取 secretp 失败!') |
| 28 | + $.log(`❕ ${$.name}, 获取密钥!`) |
| 29 | + $.log(` 密钥 = ${$.secretp.slice(0, 10)}...`) |
| 30 | + $.log(` 等级 = ${_info?.raiseInfo?.scoreLevel}`) |
| 31 | + $.log(` 分数 = ${_info?.raiseInfo?.totalScore} => ${_info?.raiseInfo?.nextLevelScore}`, '') |
| 32 | + } catch (e) { |
| 33 | + $.log(`❗️ ${$.name}, 获取密钥!`, ` error = ${e}`, `response = ${JSON.parse(response)}`, `data = ${data}`, '') |
| 34 | + } finally { |
| 35 | + resove() |
| 36 | + } |
| 37 | + }) |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +function getActs() { |
| 42 | + return new Promise((resove) => { |
| 43 | + $.post(taskurl('cakebaker_getTaskDetail'), (error, response, data) => { |
| 44 | + try { |
| 45 | + $.log(`❕ ${$.name}, 获取活动!`) |
| 46 | + if (error) throw new Error(error) |
| 47 | + $.acts = [] |
| 48 | + JSON.parse(data)?.data?.result?.taskVos.forEach((_a) => { |
| 49 | + const _act = { |
| 50 | + _raw: _a, |
| 51 | + id: _a.taskId, |
| 52 | + name: _a.taskName, |
| 53 | + times: _a.times, |
| 54 | + maxTimes: _a.maxTimes, |
| 55 | + waitDuration: _a.waitDuration === 0 ? 1 : _a.waitDuration, |
| 56 | + isProd: _a.productInfoVos ? true : false, |
| 57 | + tasks: [], |
| 58 | + } |
| 59 | + const _vo = _a[Object.keys(_a).find((key) => _a[key]?.itemId || _a[key][0]?.itemId)] |
| 60 | + if (Array.isArray(_vo)) { |
| 61 | + _vo.forEach((_task) => _act.tasks.push({ _raw: _task, id: _task.itemId, name: _task.title || _task.shopName || _task.taskName || '未知名称' })) |
| 62 | + } else { |
| 63 | + _act.tasks = Array(_act.maxTimes - _act.times).fill({ _raw: _vo, id: _vo.itemId, name: _act.name }) |
| 64 | + } |
| 65 | + $.acts.push(_act) |
| 66 | + }) |
| 67 | + if (!$.acts) throw new Error('获取活动失败!') |
| 68 | + $.log(` 活动数量 = ${$.acts.length}`, '') |
| 69 | + } catch (e) { |
| 70 | + $.log(`❗️ ${$.name}, 获取活动!`, ` error = ${e}`, `response = ${JSON.parse(response)}`, `data = ${data}`, '') |
| 71 | + } finally { |
| 72 | + resove() |
| 73 | + } |
| 74 | + }) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +async function execActs() { |
| 79 | + $.log(`❕ ${$.name}, 执行活动!`) |
| 80 | + for (let _actIdx = 0; _actIdx < $.acts.length; _actIdx++) { |
| 81 | + const _act = $.acts[_actIdx] |
| 82 | + $.log(` ${_actIdx + 1}. ${_act.name} (${_act.times}/${_act.maxTimes})`) |
| 83 | + // // 跳过:邀请好友、加入战队 |
| 84 | + if ([2, 12].includes(_act.id)) { |
| 85 | + $.log(' 跳过!', '') |
| 86 | + continue |
| 87 | + } |
| 88 | + // 甄选优品 |
| 89 | + else if (_act.isProd) { |
| 90 | + await getProdAct(_act) |
| 91 | + |
| 92 | + for (let subactIdx = 0; subactIdx < _act.subacts.length; subactIdx++) { |
| 93 | + const subact = _act.subacts[subactIdx] |
| 94 | + $.log(` ${subactIdx + 1}. ${subact.name} (${subact.times}/${subact.maxTimes})`) |
| 95 | + for (let subataskIdx = 0; subataskIdx < subact.tasks.length; subataskIdx++) { |
| 96 | + const subatask = subact.tasks[subataskIdx] |
| 97 | + $.log(` ${subataskIdx + 1}. ${subatask.name.slice(0, 15)}...`) |
| 98 | + await sendtask(subact, subatask, true) |
| 99 | + $.log(` @认领任务: ${subatask.isClaimSuc ? '🟢' : '🔴'}`) |
| 100 | + $.log(` @等待: ${subact.waitDuration} 秒`) |
| 101 | + await new Promise($.wait(subact.waitDuration * 1000)) |
| 102 | + await sendtask(subact, subatask) |
| 103 | + $.log(` @完成任务: ${subatask.isExecSuc ? '🟢' : '🔴'}`, '') |
| 104 | + } |
| 105 | + $.log('') |
| 106 | + } |
| 107 | + } |
| 108 | + // 普通任务 |
| 109 | + else { |
| 110 | + for (let taskIdx = 0; taskIdx < _act.tasks.length; taskIdx++) { |
| 111 | + const task = _act.tasks[taskIdx] |
| 112 | + $.log(` ${taskIdx + 1}. ${task.name}`) |
| 113 | + await sendtask(_act, task, true) |
| 114 | + $.log(` @认领任务: ${task.isClaimSuc ? '🟢' : '🔴'}`) |
| 115 | + $.log(` @等待: ${_act.waitDuration} 秒`) |
| 116 | + await new Promise($.wait(_act.waitDuration * 1000)) |
| 117 | + await sendtask(_act, task) |
| 118 | + $.log(` @完成任务: ${task.isExecSuc ? '🟢' : '🔴'}`, '') |
| 119 | + } |
| 120 | + $.log('') |
| 121 | + } |
| 122 | + $.log('') |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// 商品类活动 |
| 127 | +function getProdAct(act) { |
| 128 | + return new Promise((resove) => { |
| 129 | + const body = { taskIds: act.tasks.map((task) => task.id).toString() } |
| 130 | + $.post(taskurl('cakebaker_getFeedDetail', JSON.stringify(body)), (error, response, data) => { |
| 131 | + try { |
| 132 | + const _result = JSON.parse(data)?.data?.result |
| 133 | + const _vo = _result[Object.keys(_result).find((key) => Array.isArray(_result[key]?.[0]?.productInfoVos))] |
| 134 | + act.subacts = [] |
| 135 | + _vo.forEach((_suba) => { |
| 136 | + const _subact = { |
| 137 | + _raw: _suba, |
| 138 | + id: _suba.taskId, |
| 139 | + name: _suba.taskName, |
| 140 | + times: _suba.times, |
| 141 | + maxTimes: _suba.maxTimes, |
| 142 | + waitDuration: _suba.waitDuration === 0 ? 1 : _suba.waitDuration, |
| 143 | + isProd: _suba.productInfoVos ? true : false, |
| 144 | + tasks: [], |
| 145 | + } |
| 146 | + _suba.productInfoVos.slice(0, 5).forEach((_prodvo) => { |
| 147 | + const _taskname = _prodvo.skuName || _prodvo.title || _prodvo.shopName || _prodvo.taskName || '未知名称' |
| 148 | + _subact.tasks.push({ |
| 149 | + _raw: _prodvo, |
| 150 | + id: _prodvo.itemId, |
| 151 | + name: _taskname, |
| 152 | + }) |
| 153 | + }) |
| 154 | + act.subacts.push(_subact) |
| 155 | + }) |
| 156 | + } catch (e) { |
| 157 | + $.log(`❗️ ${$.name}, 执行商品类活动!`, ` error = ${error || e}`, `response = ${JSON.parse(response)}`, `data = ${data}`, '') |
| 158 | + } finally { |
| 159 | + resove() |
| 160 | + } |
| 161 | + }) |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +function sendtask(act, task, isClaim = false) { |
| 166 | + return new Promise((resove) => { |
| 167 | + const body = { |
| 168 | + taskId: act.id, |
| 169 | + itemId: task.id, |
| 170 | + actionType: isClaim ? 1 : undefined, |
| 171 | + safeStr: JSON.stringify({ secretp: $.secretp }), |
| 172 | + } |
| 173 | + |
| 174 | + if (isClaim) task.isClaimSuc = true |
| 175 | + else task.isExecSuc = true |
| 176 | + resove() |
| 177 | + |
| 178 | + $.post(taskurl('cakebaker_ckCollectScore', JSON.stringify(body)), (error, response, data) => { |
| 179 | + try { |
| 180 | + const _data = JSON.parse(data) |
| 181 | + const _issuc = _data?.data?.bizCode === 0 |
| 182 | + if (isClaim) task.isClaimSuc = _issuc |
| 183 | + else task.isExecSuc = _issuc |
| 184 | + } catch (e) { |
| 185 | + if (isClaim) task.isClaimSuc = false |
| 186 | + else task.isExecSuc = false |
| 187 | + } finally { |
| 188 | + resove() |
| 189 | + } |
| 190 | + }) |
| 191 | + }) |
| 192 | +} |
| 193 | + |
| 194 | +function taskurl(fid, body = '{}') { |
| 195 | + const url = { url: `https://api.m.jd.com/client.action` } |
| 196 | + url.headers = JSON.parse($.VAL_headers) |
| 197 | + url.body = `functionId=${fid}&body=${body}&client=wh5&clientVersion=1.0.0` |
| 198 | + return url |
| 199 | +} |
| 200 | + |
| 201 | +function showmsg() {} |
| 202 | + |
| 203 | +// prettier-ignore |
| 204 | +function Env(t){this.name=t,this.logs=[],this.isSurge=(()=>"undefined"!=typeof $httpClient),this.isQuanX=(()=>"undefined"!=typeof $task),this.log=((...t)=>{this.logs=[...this.logs,...t],t?console.log(t.join("\n")):console.log(this.logs.join("\n"))}),this.msg=((t=this.name,s="",i="")=>{this.isSurge()&&$notification.post(t,s,i),this.isQuanX()&&$notify(t,s,i),this.log("==============\ud83d\udce3\u7cfb\u7edf\u901a\u77e5\ud83d\udce3=============="),t&&this.log(t),s&&this.log(s),i&&this.log(i)}),this.getdata=(t=>this.isSurge()?$persistentStore.read(t):this.isQuanX()?$prefs.valueForKey(t):void 0),this.setdata=((t,s)=>this.isSurge()?$persistentStore.write(t,s):this.isQuanX()?$prefs.setValueForKey(t,s):void 0),this.get=((t,s)=>this.send(t,"GET",s)),this.wait=((t,s=t)=>i=>setTimeout(()=>i(),Math.floor(Math.random()*(s-t+1)+t))),this.post=((t,s)=>this.send(t,"POST",s)),this.send=((t,s,i)=>{if(this.isSurge()){const e="POST"==s?$httpClient.post:$httpClient.get;e(t,(t,s,e)=>{s.body=e,s.statusCode=s.status,i(t,s,e)})}this.isQuanX()&&(t.method=s,$task.fetch(t).then(t=>{t.status=t.statusCode,i(null,t,t.body)},t=>i(t.error,t,t)))}),this.done=((t={})=>$done(t))} |
0 commit comments